Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cbon
    Junior Member
    • Jun 2010
    • 2

    #1

    BWA - number of sequences aligned

    Hello everyone,

    I am quite a newbie in bioinformatics, and I have some difficulties using bwa software.

    I successfully aligned my dataset to my reference genome. Now, I want to see the impact of the settings on my results, but I don't know how to obtain the number of reads aligned in the .sam file.

    Is there a useful trick to obtain the number of reads aligned ?

    I have found no clue on the bwa manual, neither in the samtool package and I begin to feel quite helpless ...

    Thank you for your answers !
  • dawe
    Senior Member
    • Apr 2009
    • 258

    #2
    Originally posted by Cbon View Post
    Hello everyone,

    Is there a useful trick to obtain the number of reads aligned ?
    Try

    Code:
    samtools flagstat [I]file.bam[/I]
    Although I don't know if it works on sam files (in case you should convert in BAM, which I believe should be the default...).

    d

    Comment

    • racetrout
      Junior Member
      • Jun 2010
      • 2

      #3
      If you are using UNIX and GNU awk,
      Code:
      gawk '!/^[@#]/ && (!and($2,4){print}' bwa.sam| wc -l
      should give you the number of reads aligned - providing BWA is setting the flag for unaligned reads. If you have many reads and BWA writes out only the aligned reads,
      Code:
      wc -l bwa.sam
      is a very good simple approximate of aligned reads (it counts the header of course, but the ~5 lines of the header compared to the millions of reads are negligible).

      Comment

      • Cbon
        Junior Member
        • Jun 2010
        • 2

        #4
        Thank you both for your answers !

        My files are already converted in .bam, so I hope I can use the flagstat command. If it doesn't work, I will try the code.

        thank you again !

        Comment

        • saml
          Junior Member
          • Oct 2010
          • 4

          #5
          Originally posted by racetrout View Post
          If you are using UNIX and GNU awk,
          Code:
          gawk '!/^[@#]/ && (!and($2,4){print}' bwa.sam| wc -l
          should give you the number of reads aligned - providing BWA is setting the flag for unaligned reads. If you have many reads and BWA writes out only the aligned reads,
          Code:
          wc -l bwa.sam
          is a very good simple approximate of aligned reads (it counts the header of course, but the ~5 lines of the header compared to the millions of reads are negligible).
          Yes, but removing the lines is easy too. Skipping 2 header lines (think I had that), and starting on the 3rd line, can be done with the tail command and adding a '+' before the number of lines, to denote which line to start from:

          Code:
          tail -n +3 bwa.sam | gawk '!/^[@#]/ && (!and($2,4){print}' | wc -l
          ... or for the case with only aligned reads coming from bwa:
          Code:
          tail -n +3 bwa.sam | wc -l

          Comment

          • swbarnes2
            Senior Member
            • May 2008
            • 910

            #6
            bwa by default includes all reads, even unaligned ones. You can tell that they are unmapped, from the binary tag.

            So the flagstat command will distinguish between the mapped an unmapped reads.

            You can also use samtools view -bF0x4 all.bam > mapped .bam to get a bam which has all the unmapped reads filtered out.

            Comment

            • nilshomer
              Nils Homer
              • Nov 2008
              • 1283

              #7
              Use "samtools view -c -S <in.sam>" and add the "-f/-F" options as desired.

              Comment

              • solonas13
                Junior Member
                • May 2012
                • 1

                #8
                I personally find it---at the best case---unacceptable the fact that a short-read aligner does not have an extra line of code to report the total number of aligned reads... Especially when it outputs (on the standard error) things like:

                `[infer_isize] (25, 50, 75) percentile: (137, 179, 227)'

                Anyhow, isn't there a closing parenthesis missing from this one?

                Originally posted by saml View Post
                Code:
                tail -n +3 bwa.sam | gawk '!/^[@#]/ && (!and($2,4){print}' | wc -l
                Thanks
                Last edited by solonas13; 05-13-2012, 03:47 PM.

                Comment

                Latest Articles

                Collapse

                • SEQadmin2
                  Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                  by SEQadmin2



                  CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                  Despite this, “CRISPR helped turn genome editing from a specialized technique into
                  ...
                  Yesterday, 11:01 AM
                • 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

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by SEQadmin2, Yesterday, 02:55 AM
                0 responses
                9 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-24-2026, 12:17 PM
                0 responses
                12 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-23-2026, 11:41 AM
                0 responses
                12 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-20-2026, 11:10 AM
                0 responses
                24 views
                0 reactions
                Last Post SEQadmin2  
                Working...