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
                Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                by SEQadmin2



                Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                ...
                07-09-2026, 11:10 AM
              • SEQadmin2
                Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                by SEQadmin2



                Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                07-08-2026, 05:17 AM
              • GATTACAT
                Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                by GATTACAT
                Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                07-01-2026, 11:43 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 07-13-2026, 10:26 AM
              0 responses
              20 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-09-2026, 10:04 AM
              0 responses
              31 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-08-2026, 10:08 AM
              0 responses
              20 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-07-2026, 11:05 AM
              0 responses
              34 views
              0 reactions
              Last Post SEQadmin2  
              Working...