Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oiiio
    Senior Member
    • Jan 2011
    • 105

    #1

    sorting a BAM produces a smaller file than the original

    I recently sorted a bunch of BAMs that are about 400 MB in size each. After sorting with samtools the output sorted BAMs are about 3-4 MB smaller than the unsorted originals. Does anyone know why this is? My understanding of a sort is that it simply rearranges the available reads inside a BAM, and I don't see why this would create a smaller file.
  • ShaunMahony
    Member
    • Apr 2008
    • 27

    #2
    Is there any chance that the unsorted BAM included unmapped reads and the sorted one doesn't? What did you use for alignment?

    Comment

    • gprakhar
      Member
      • Aug 2010
      • 78

      #3
      ignore this
      Last edited by gprakhar; 08-24-2011, 05:19 AM. Reason: Incorrect information.

      Comment

      • kmcarr
        Senior Member
        • May 2008
        • 1181

        #4
        Sorting does not remove duplicate reads.

        I seem to recall this same question asked previously on Seqanswers but my search skills are lacking this morning so I can't find the thread. If you were to compare an unsorted vs. sorted SAM file the sizes should be identical because, as oiiio said, the information content is the same, it has simply been rearranged. SAM files are plain text files so provided the content is the same the file size will be the same regardless of order. BAM files are compressed; the order of the information in the file can have an effect on the compression of that data. This was the explanation I recall from that earlier thread. (Once the caffeine fully kicks in I'll see if I can find that thread.)

        Comment

        • lh3
          Senior Member
          • Feb 2008
          • 686

          #5
          BAM is compressed. Sorting helps to give a better compression ratio because similar sequences are grouped together.

          Comment

          • oiiio
            Senior Member
            • Jan 2011
            • 105

            #6
            Thanks everyone, and I also now believe that the order is improving compression.

            Comment

            • marcowanger
              Senior Member
              • Dec 2008
              • 273

              #7
              we found sorting by samtools removs unmapped reads.
              Marco

              Comment

              • lh3
                Senior Member
                • Feb 2008
                • 686

                #8
                Originally posted by marcowanger View Post
                we found sorting by samtools removs unmapped reads.
                No, it does not. It puts reads without coordinates at the end of the file.

                Comment

                • marcowanger
                  Senior Member
                  • Dec 2008
                  • 273

                  #9
                  Originally posted by lh3 View Post
                  No, it does not. It puts reads without coordinates at the end of the file.
                  oh? My colleague told me he found my samtools sorted BAM removed the unmapped reads. OK, then I got it wrong.

                  Thanks for informing me the actual situation.

                  Marco
                  Marco

                  Comment

                  • oiiio
                    Senior Member
                    • Jan 2011
                    • 105

                    #10
                    Yes, the unmapped reads are placed at the end of the file,

                    you can check this out with

                    Code:
                     samtools view input.bam | tail

                    Comment

                    • yaten2020
                      Junior Member
                      • Aug 2011
                      • 7

                      #11
                      hello guys
                      this is true.... sorting does not remove any data from the BAM file. its puts those reads at the end of the file. I checked it.....

                      Comment

                      • voidnyx
                        Junior Member
                        • Jun 2015
                        • 7

                        #12
                        I know this thread has been inactive for a while, but my question is basically the same.

                        I have a 120GB bam file from RNA-Seq data and used the samtools sort function with compression level 1 (so meaning the least compression).

                        I ended up with a file of only ~30GB size.

                        Now this is the first time I am working with this and I have no idea if this is a normal range for reduction in size, but my feeling is something went wrong.

                        Could anyone with experience tell me if the result is expected or not?


                        thanks,
                        Florian

                        Comment

                        • HESmith
                          Senior Member
                          • Oct 2009
                          • 512

                          #13
                          RNA-Seq contains a greater fraction of duplicate reads due to highly expressed genes, so sorting reduces the size more than DNA-Seq. If you want to check that nothing was lost, use 'samtools view' to compare the number of reads before/after sorting.

                          Comment

                          • dpryan
                            Devon Ryan
                            • Jul 2011
                            • 3478

                            #14
                            What HESmith said, but I'll add that if you really want to be 110% sure that nothing was lost/changed, you can use bamHash on both files. If the checksums are the same then they contain the same reads (just in a different order).

                            Comment

                            • gringer
                              David Eccles (gringer)
                              • May 2011
                              • 845

                              #15
                              Originally posted by HESmith View Post
                              If you want to check that nothing was lost, use 'samtools view' to compare the number of reads before/after sorting.
                              Using 'samtools index' and then 'samtools idxstats' on a sorted file will give you total counts for mapped and unmapped reads, which is slightly easier to check compared to looking at a few million lines to find what's missing.

                              Comment

                              Latest Articles

                              Collapse

                              • SEQadmin2
                                Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                                by SEQadmin2



                                Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                                Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                                ...
                                10-14-2026, 09:55 AM
                              • SEQadmin2
                                New Genomics Technologies Take Aim at Long-Standing Limits
                                by SEQadmin2


                                Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                                We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                                ...
                                09-28-2026, 10:25 AM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by SEQadmin2, 10-22-2026, 11:13 AM
                              0 responses
                              28 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 10-22-2026, 10:48 AM
                              0 responses
                              20 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 10-15-2026, 10:52 AM
                              0 responses
                              38 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 10-14-2026, 05:39 AM
                              0 responses
                              44 views
                              0 reactions
                              Last Post SEQadmin2  
                              Working...