Unconfigured Ad

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

    #1

    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

              • kinpict.com
                Reply to 16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
                by kinpict.com
                The part about this topic feels surprisingly grounded, and the details on the page is the bit that made me slow down. I’d keep <a href="https://kinpict.com/">ai family photo generator</a> bookmarked for later.
                Today, 06:37 PM
              • SEQadmin2
                16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
                by SEQadmin2





                NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification,...
                10-30-2026, 06:46 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 11-12-2026, 07:59 AM
              0 responses
              25 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 11-05-2026, 09:25 AM
              0 responses
              35 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 11-03-2026, 11:54 AM
              2 responses
              53 views
              0 reactions
              Last Post kinpict.com  
              Started by SEQadmin2, 10-28-2026, 09:57 AM
              0 responses
              175 views
              0 reactions
              Last Post SEQadmin2  
              Working...