Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagebash
samtools view -F 0x4 -c yeast_pe.sort.bam

x

x

xThere should be 547664 mapped alignments.

Knowing these two numbers we can just divide them, using awk (remember, bash only does integer arithmetic). Because we're not piping anything in to awk, any body we specify won't be executed. So we do the math in the BEGIN section:

Code Block
languagebash
awk 'BEGIN{ print 100*6697/547664,"%" }'

The result should be 1.22283 %.

In addition to the rate, converted to a percentage, we also output the literal percent sign ( % ). The double quotes ( " ) denote a literal string in awk, and the comma between the number and the string says to separate the two fields with whatever the default Output Field Separator (OFS) is. By default, both awk's Input Field Separator (IFS) and Output Field Separator are whitespace (any number of space characters).

So what if we want to get fancy and do all this in a one-liner command? We can use echo and backtick evaluation to put both numbers in a string, then pipe that string to awk. This time we use awk body code, and refer to the two fields being passed in: total count ($1) and indel count ($2).

Code Block
languagebash
echo "`samtools view -F 0x4 -c yeast_pe.sort.bam` `samtools view -F 0x4 yeast_pe.sort.bam | cut -f 6 | grep -P '[ID]' | wc -l`" | awk '{ print 100 * $2/$1,"%" }'

Filtering for only mapped reads (samtools view -F 0x4)

...