I'm a bioinformatics student and new to bash and programming stuff. I want to calculate genome coverage.
This is my script. I switch the real parameter with xx
but I'm sure xx
s are not problematic. Other students already execute this script with no error.
filename=$1
reference=/xx
filebase=$(basename $filename .bam)
samtools view ${filename} -F 4 -q 30 -b > ${filebase}.f.bam
genomeCoverageBed -ibam -g ${filebase}.f.bam ${reference} > /mnt/ABC/projects/abc/def/${filebase}.cov
coverage=$(grep genome /mnt/ABC/projects/abc/def/${filebase}.cov | awk '{NUM+=$2*$3; DEN+=$3} END {print NUM/DEN}')
echo -e "${filename},${coverage}" >> coverages.txt
When I execute this script with
sh ./coverage.sh /mnt/XYZ/share/sdf_rawdata/hsa/mergedbams/ghj_merged_200203.hs37d5.cons.90perc.bam
it doesn't work and gives me: awk: cmd. line:1: fatal: division by zero attempted and unrecognized parameter:/mnt/XYZ/share/sdf_rawdata/hsa/mergedbams/ghj_merged_200203.hs37d5.cons.90perc.bam
error
and in the coverages.txt file it only has this line:
-e /mnt/NEOGENE2/share/compevo_rawdata/hsa/mergedbams/Ash128_all.merged.hs37d5.fa.cons.90perc.bam
, nothing more.
Thanks for helping.
You need to put a condition to check if DEN
variable is NOT NULL then only do the division in END
block of awk
code(trying to fix OP's attempt here).
coverage=$(grep genome /mnt/ABC/projects/abc/def/${filebase}.cov | awk '{NUM+=$2*$3; DEN+=$3} END {if(DEN){print NUM/DEN}}')
You need not to use grep
command along with awk
, we could search string in awk
itself, may be something like:
coverage=$(awk '/genome/{NUM+=$2*$3; DEN+=$3} END {if(DEN){print NUM/DEN}}' "/mnt/ABC/projects/abc/def/${filebase}.cov")
Why error is coming: Because its sometimes your variable DEN
is having zero values in it. Let's take an example here in shorter way(just some examples to understand the error here):
When variable a
is NULL then also we get same error.
awk 'BEGIN{a="";b=1;print b/a}'
awk: cmd. line:1: fatal: division by zero attempted
When variable a
is zero then also we get same error:
awk 'BEGIN{a=0;b=1;print b/a}'
awk: cmd. line:1: fatal: division by zero attempted