Welcome to the Genome Toolbox! I am glad you navigated to the blog and hope you find the contents useful and insightful for your genomic needs. If you find any of the entries particularly helpful, be sure to click the +1 button on the bottom of the post and share with your colleagues. Your input is encouraged, so if you have comments or are aware of more efficient tools not included in a post, I would love to hear from you. Enjoy your time browsing through the Toolbox.
Showing posts with label remove. Show all posts
Showing posts with label remove. Show all posts

Thursday, January 16, 2014

Remove Outliers from R Variable

R boxplot is an easy function to visualize a variable and get a sense of the distribution of values as well as potential outlier data points that may exist.  By saving the output to a variable (ex: bxplt <- boxplot(data$expression)), you can see a list of outlier points (ex: bxplt$out) that you may wish to exclude from an analysis.  The method R uses to identify extreme values is to calculate 1.5 times the interquartile range (ie: third quartile minus first quartile) and create limits by subtracting this value from the first quartile and adding it to the third quartile.  Any point that is less than the smaller limit or greater than the larger limit is considered an outlier by this method.  Here is a simple function I created to remove outliers from an R variable, the script essentially removes outliers identified by the boxplot function by replacing outlier values with NA and returning this modified variable for analysis.  So, for example, if you wanted to find the mean of data$expression with outliers removed all you would need to do is first run the below function and then use the command mean(ro(data$expression)).  Overall, a pretty simple way to remove out outliers if you do indeed choose to do so.

Tuesday, January 14, 2014

Remove Rows with NA Values From R Data Frame

Rows with NA values can be a pesky nuisance when trying to analyze data in R. Here is a short primer on how to remove them.

There are two primary options when getting rid of NA values in R, the na.omit/is.na commands and the complete.cases command.  Both are part of the base stats package and require no additional library or package to be loaded.  Below are examples of how the two work with a data frame called data and a variable called var.


The na.omit/is.na commands work as follows:
na.omit(data) - will only select rows with complete data in all columns
data[rowSums(is.na(data[,c(2,3,5)]))==0,] - will only select rows with complete data in columns 2, 3, and 5
var[!is.na(var)] - will only select values of a variable not equal to NA


The complete.cases command works as follows:
data[complete.cases(data),] - will only select rows with complete data in all columns
data[complete.cases(data[,c(2,3,5)]),] - will only select rows with complete data in columns 2, 3, and 5
var[complete.cases(var)] - will only select values of a variable not equal to NA


I use both commands at times, but ultimately prefer the complete.cases command for the cleaner syntax and generalizability.  Hope this helps you remove those NA's from your data.  If you have additional tips or questions please leave a comment below.

Friday, November 22, 2013

One Line Command to Remove Header from File in UNIX

Sometimes you need to remove a header, footer, or range of lines from a file to better manipulate it in UNIX.  Here are some quick one liners to do so:



Wednesday, June 26, 2013

Remove a List of Reads from a BAM File

Sometimes it is necessary to remove a subset of reads from a .bam file.  In my case, I wanted to remove a few chimeric reads where it appeared reads from different amplicons were fusing together before entering the sequencer.  Here is a line of code where I use Samtools and grep to remove a list of read ID's from the original .bam file and create a new filtered .bam file.  Hope it is useful for other applications as well.


Note the trailing hyphen at the end.