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 zero. Show all posts
Showing posts with label zero. Show all posts

Tuesday, November 4, 2014

Fix Origin of R Plot Axes at Zero

Standard R plots does not set the origin of the x and y axis at zero. To reset this parameter so that the origin of the plot is fixed at 0,0 simply use the xaxs and yaxs parameters. Here is a description from the R graphical parameters help page.

The style of axis interval calculation to be used for the x-axis. Possible values are "r", "i", "e", "s", "d". The styles are generally controlled by the range of data or xlim, if given.
Style "r" (regular) first extends the data range by 4 percent at each end and then finds an axis with pretty labels that fits within the extended range.
Style "i" (internal) just finds an axis with pretty labels that fits within the original data range.
Style "s" (standard) finds an axis with pretty labels within which the original data range fits.
Style "e" (extended) is like style "s", except that it is also ensures that there is room for plotting symbols within the bounding box.
Style "d" (direct) specifies that the current axis should be used on subsequent plots.
(Only "r" and "i" styles have been implemented in R.)

Finally, here is a brief example code snippet to demonstrate how the syntax works.

Monday, August 18, 2014

Rounding in R: How to Keep Trailing Zeros

The round function in R is an easy way to round a number to a desired number of digits after the decimal point.  When the final digit of the number happens to be a zero, R gets a little aggressive and chooses to drop the last zero as well.  If you want to neatly print output, this behavior can be a bit frustrating. I wish there was an option for the round function to fix this behavior for values ending in zero, but I couldn't find anything. Luckily, there is an easy work around to fix this: using the base R sprintf function.  Here is a quick example script of how to round a number and keep trailing zeros (Note: this results in converting the number to a character format).

Thursday, February 20, 2014

Zero Fill Coverage Gaps in Samtools Depth Output

Merging depth output from multiple .bam files can be difficult since Samtools only outputs depth counts for coordinates with non-zero coverage.  If you want to merge depth output from these .bam files you first need to fill in the base pair positions of no coverage with zero values so the depth output for all .bam files is the same length.  Then using a simple UNIX cat you can merge multiple .bam file depth output into one file for comparison and analysis.

Here is a simple Python script to zero fill Samtools depth output:



And below is an example of how to use it:

Thursday, January 23, 2014

Formatting Excel Cells with Zero Filling

While Microsoft Excel could use some improvements for data management and analysis, it remains my program of choice for putting together summary tables, particularly the descriptive statistics of most Table 1's.  I'm pretty particular with formatting and wanted to create a Table 1 with column percentages that all lined up nicely.  To do this I needed to zero fill numbers both before and after the decimal point so that each number, when formatted, took up the same amount of space in the column.  I the past I would do this by pasting the table as text (without formulas) and then manually filling in zeros.  This was tedious, especially when having to redo tables after sample numbers changed.

Today I found out there is a way to have Excel automatically include these zeros.  You can do this by creating a custom number format.  Here's how to do so.

1). Right click on the cell you want to format and choose Format Cells...
2). Click the Number tab and select Custom in the Category: list.
3). Put in your desired formatting.  You can do this by building off other format types.  In my case, I wanted to have a format so that the numbers 3.5562 and 55 appeared as (03.6) and (55.0), respectively.  To do this the Type: box needed to have the format (00.0).  This will zero fill both before and after the decimal point as well as round all numbers by one decimal place.

As you can imagine, you can customize this to zero fill based on your particular needs or desired format type.  Below is an example Table 1 excerpt to show how the formatting looks.