Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • cycomatto
    Junior Member
    • Dec 2011
    • 7

    samtools: "sort by name" and fast random access

    I understand that sorting a BAM file, e.g.
    Code:
    samtools sort my_file.bam my_file.sorted
    will allow for faster random access. This makes sense, as viewing reads aligned to a certain region of the genome is much simpler if the reads are ordered by genome coordinates.

    But if you sort a .bam file by read name, e.g.
    Code:
    samtools sort [B]-no[/B] my_file.bam my_file.sorted_by_name
    then how does this allow faster random access?

    I ask because there is no samtools functionality for observing reads by name - the "view" family of commands only accepts genome regions (coordinates).

    How would you random access a .bam file by read name?
    Last edited by cycomatto; 12-21-2011, 07:44 AM.
  • maubp
    Peter (Biopython etc)
    • Jul 2009
    • 1544

    #2
    SAM/BAM files can be sorted by mapping position (normal), which allows efficient access to regions (e.g. chr1 from 1000 to 2000) via the BAI index, or sorted by read name (not commonly used).

    If you want random access to reads by name, the BAM index file (BAI) is no help at all. You would need a separate index mapping read names to offsets. Sorting by read name would be helpful if you have multi-fragment reads (e.g. paired end reads), since then all the fragments would be together on disk so you'd only need one seek. If the reads were not sorted, you'd need to have a lookup table from read name to multiple offsets (one for each read fragment).

    For BAM files you could use the same "virtual offset" approach as BAI uses (essentially a double offset, for a BGZF/GZIP block start, and the within block offset). You might find this blog post interesting about how indexing BGZF files (such as BAM files) can be done:
    BAM files are compressed using a variant of GZIP (GNU ZIP) , called BGZF (Blocked GNU Zip Format). Anyone who has read the SAM/BAM Specifica...

    and http://seqanswers.com/forums/showthread.php?t=15347

    Comment

    • cycomatto
      Junior Member
      • Dec 2011
      • 7

      #3
      Originally posted by maubp View Post
      Sorting by read name would be helpful if you have multi-fragment reads (e.g. paired end reads), since then all the fragments would be together on disk so you'd only need one seek.
      This is precisely my situation (given one side of a paired-end read, find its mate).


      I am unclear why the following won't work: FIRST sorting by name, THEN index this name-sorted .bam.

      Doesn't the "index" operation perform this offset for you? So if our .bam is already sorted by name, the the "index" operation will create the virtual offsets you wrote about?

      Comment

      • maubp
        Peter (Biopython etc)
        • Jul 2009
        • 1544

        #4
        Originally posted by cycomatto View Post
        Doesn't the "index" operation perform this offset for you? So if our .bam is already sorted by name, the the "index" operation will create the virtual offsets you wrote about?
        The indexing provided by samtools/picard using the *.bai file ONLY works on BAM files sorted by mapping position.

        I was talking about how as a programmer you can use your own alternative indexing, where you map read names to BAM/BGZF 'virtual offsets'.

        Comment

        • cycomatto
          Junior Member
          • Dec 2011
          • 7

          #5
          So then, what is the point of sorting a .bam file by name? Why even include the option in samtools?

          Comment

          • kmcarr
            Senior Member
            • May 2008
            • 1181

            #6
            Originally posted by cycomatto View Post
            So then, what is the point of sorting a .bam file by name? Why even include the option in samtools?
            If you want to process the entire file sequentially and you need to operate on read pairs as a single unit having reads sorted by name (and hence read pairs will be adjacent in the file) greatly reduces complexity. An example of a program which works this way is htseq-count.

            Comment

            • ajay87
              Junior Member
              • Dec 2012
              • 4

              #7
              htseq-count also requires the file to be sorted

              Comment

              • xied75
                Senior Member
                • Feb 2012
                • 129

                #8
                Originally posted by cycomatto View Post
                So then, what is the point of sorting a .bam file by name? Why even include the option in samtools?
                samtools fixmate

                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.
                  ...
                  Yesterday, 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, Yesterday, 10:04 AM
                0 responses
                10 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-08-2026, 10:08 AM
                0 responses
                9 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-07-2026, 11:05 AM
                0 responses
                16 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-02-2026, 11:08 AM
                0 responses
                31 views
                0 reactions
                Last Post SEQadmin2  
                Working...