Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • cmccabe
    Senior Member
    • Jul 2012
    • 355

    samtools and awk depth calculation

    Trying to calculate the average depth of a .bam file: Any ideas?

    Essentialy I just want to get the ....X coverage.

    Code:
     
    samtools depth IonXpress_001.bam | awk '{sum+=$3} END { print "Average = ",sum/NR}' > output.txt
    -bash: samtools: command not found
    awk: cmd. line:1: fatal: division by zero attempted
    Thank you .

    EDIT: I also tried the below to calculate the genome of the .bam for 'NR', but it says samtools command not found.

    Code:
     ./samtools view -H IonXpress_001.bam | grep -P '^@SQ' | cut -f 3 -d ':' | awk '{sum+=$1} END {print sum}'
    = 3095693981
    Last edited by cmccabe; 06-11-2015, 01:07 PM.
  • cmccabe
    Senior Member
    • Jul 2012
    • 355

    #2
    Figured it out, just forgot to add samtools to PATH.... oops, but the average is calculated to 0, should it not be $3 or is there somehing else? Thank you

    Code:
     ./samtools depth IonXpress_001.bam | awk '{sum+=$1} END { print "Average = ",sum/3095693981}' > output.txt
    Last edited by cmccabe; 06-11-2015, 01:06 PM.

    Comment

    • GenoMax
      Senior Member
      • Feb 2008
      • 7142

      #3
      Here is a thread with multiple options (including an awk solution): https://www.biostars.org/p/5165/

      Comment

      • cmccabe
        Senior Member
        • Jul 2012
        • 355

        #4
        Thank you, I will try some of the other solutions in the post. Do you see why theawk is not working? I am trying to figure it out as our lab is doing more ngs and different data is often desired. Thank you .

        Comment

        • GenoMax
          Senior Member
          • Feb 2008
          • 7142

          #5
          Is that BAM directly from Ion software or did you make it yourself? Is the samtools depth part producing an output that looks reasonable?

          Comment

          • cmccabe
            Senior Member
            • Jul 2012
            • 355

            #6
            The bam is from Ion software and I will post the output of samtools depth tomorrow. Thank you very much .

            Comment

            • cmccabe
              Senior Member
              • Jul 2012
              • 355

              #7
              I found the perl script on that page incredibly useful.

              Code:
              ./samtools mpileup in.bam | awk '{print $4}' | perl ~/coverage.pl
              I am trying to modify the below to calculate the average coverage of a bed file, is that possible?

              Code:
              ./samtools view -b in.bam <genomic region> | ./samtools mpileup - | awk '{print $4}' | perl ~/coverage.pl
              Thank you .

              Comment

              Latest Articles

              Collapse

              • SEQadmin2
                Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                by SEQadmin2


                I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

                Here are nine questions we think about, in roughly the order they matter, before...
                06-18-2026, 07:11 AM
              • SEQadmin2
                From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
                by SEQadmin2


                Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


                The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
                ...
                06-02-2026, 10:05 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 06-26-2026, 11:10 AM
              0 responses
              14 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 06-17-2026, 06:09 AM
              0 responses
              48 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 06-09-2026, 11:58 AM
              0 responses
              107 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 06-05-2026, 10:09 AM
              0 responses
              125 views
              0 reactions
              Last Post SEQadmin2  
              Working...