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
                Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                by SEQadmin2


                Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                The systematic characterization of the human proteome has
                ...
                Today, 11:48 AM
              • 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

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, Today, 11:10 AM
              0 responses
              7 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-13-2026, 10:26 AM
              0 responses
              29 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-09-2026, 10:04 AM
              0 responses
              38 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-08-2026, 10:08 AM
              0 responses
              25 views
              0 reactions
              Last Post SEQadmin2  
              Working...