Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Azazel
    Member
    • Oct 2010
    • 52

    Counting entries in bam-files: difficulties with samtools and bamToBed

    I have a bam-file 'stuff.bam' and want to know how many sequences are described in it. I am not getting the same results when using samtools and bamToBed:
    Code:
    [prompt]$ ~/samtools-0.1.12a/samtools view -c stuff.bam 
    21054492
    ...ok, now I count the entries in a conversion of stuff in bed-format:
    Code:
    [prompt]$ bamToBed -i stuff.bam | wc -l
    20567680
    Why does this give different results, and how can I get the correct number?

    --
    I am using
    samtools-0.1.12a
    BEDTools-Version-2.9.0
  • droog_22
    Member
    • Nov 2010
    • 10

    #2
    BAM files can contain mapped and unmapped reads

    Dear Azazel,

    Using samtools view you are counting the complete number of reads in your BAM file - be it mapped or unmapped reads.

    Code:
    $ samtools view data.bam | wc -l
    Information about which read was mapped and how can be seen in the flag field of the BAM file. See more here http://samtools.sourceforge.net/samtools.shtml and here http://picard.sourceforge.net/explain-flags.html

    You could use this to count the different flag tags in your file:

    Code:
    $ samtools view data.bam | awk '{print $2}' | sort | uniq -c
    10976575 0
    10968005 16
    5483180 4
    So 5.5M reads do not map the genome at all, and roughly 11M map to the forward respectively the reverse strand of the genome you used to map the reads.

    Using BamToBed you just count the number of mapped reads. A which hasn't been mapped obviously does not have any chromosome, start, and end information and is thus not taken into account.

    So the following command will give you the total 22M reads which map to the genome.

    Code:
    $ bamToBed -i data/bowtie_remapped/input_0.5-2h_1_o.bam | wc -l
    21944580
    Hope this helps,

    Cheers, droog_22

    Comment

    • frozenlyse
      Senior Member
      • Sep 2008
      • 135

      #3
      "samtools flagstat" is probably a better tool for the job, it will give a summary of all the reads in your bam file eg
      Code:
      $ samtools flagstat data_1.bam 
      25038903 in total
      0 QC failure
      0 duplicates
      16921771 mapped (67.58%)
      0 paired in sequencing
      0 read1
      0 read2
      0 properly paired (-nan%)
      0 with itself and mate mapped
      0 singletons (-nan%)
      0 with mate mapped to a different chr
      0 with mate mapped to a different chr (mapQ>=5)

      Comment

      • DerSeb
        Member
        • Oct 2009
        • 44

        #4
        What I like to di is to count the flags that are present:

        samtools view *.bam | awk ' { per[$2] += 1 }END { for (i in per)print i, per[i] }

        This command will show you which flags are present and how often they occur.

        Comment

        • Azazel
          Member
          • Oct 2010
          • 52

          #5
          OK! I understand. Just wanted to say thanks for the answers.

          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
            ...
            07-20-2026, 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, Yesterday, 12:17 PM
          0 responses
          13 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-23-2026, 11:41 AM
          0 responses
          14 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-20-2026, 11:10 AM
          0 responses
          23 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-13-2026, 10:26 AM
          0 responses
          37 views
          0 reactions
          Last Post SEQadmin2  
          Working...