This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Gc Content From Bam

Dear all, I have one problem, I would like to count the content of GC nucleotids from bam file for each read. I think I have to count it from $10, but I do not know how? Could you help me please? Thank you

gc bam awk perl

2 answers

awk '{ n=length($10); print $10, gsub(/[GCCgcs]/,"",$10)/n;}' your.sam

I am not sure if it is good, because every time I receive the error awk: (FILENAME=filip.sam FNR=1) fatal: division by zero attempted, than there is the problem with "n" or I do not know.

I tested my awk script without the sam header.

Oh ok, because I use to convert samtools view -h (with header). Without header is working, than thank you so much for your help.

samtools view [bam] | perl -lane '$N =()= $F[9] =~ /GC/gi; print $N;'

The odd looking =()= operator instantly converts the match from array to scalar context.

Perl one liner 'a' splits by tabs, 'l' prints a new line, 'n' operates on all lines and 'e' indicates expression.

An alternate Perl part:

perl -p -lae '$_ =()= $F[9] =~ /GC/gi'

I am not sure, but I try the perl code and I recieve different number from the awk code and awk code has a right numbers of GC count (I did not use /n)..

Log in to answer this question.