Unconfigured Ad

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jullee
    Member
    • Apr 2014
    • 19

    #1

    How to compare Read IDs from different Bam files

    Hi

    I have aligned (and filtered) my paired-end sequence data to two alternative reference genomes (e.g. different species). I would like to get a list of reads that align to both references and a list of reads that only align to one. My approach so far is as follows:

    1) convert my final bam files to sam files

    samtools view -h -o Ref1.sam Ref1.bam

    samtools view -h -o Ref2.sam Ref2.bam

    2) extract column 1 (sequence IDs) from both files (additional grep step to remove the header)

    awk '{print $1}' Ref1.sam > Ref1Ids
    grep -v '^@' Ref1Ids > Ref1Ids_Final

    (repeat for Ref2 sam file)

    3) ?

    Now I'm thinking about using basic linux commands to compare the text files listing sequence IDs. But I'm having trouble figuring out how. I think the "comm" command requires that the files be "sorted" but when I use the "sort" command on my files, the resulting order of read IDs in the files doesn't make sense (e.g. read names are composed of both strings and numbers; the strings are constant but the numbers vary and are out of a logical order following the sort command).

    Anyone have any suggestions? Or a better way to get a list of read IDs that differ between two bam alignment files?

    Thanks!
  • lindenb
    Senior Member
    • Apr 2010
    • 143

    #2
    I wrote a tool to compare two BAMS: https://github.com/lindenb/jvarkit/wiki/CmpBams

    Comment

    • kmcarr
      Senior Member
      • May 2008
      • 1181

      #3
      Hi Jullee,

      Here is how I would do it:

      Code:
      samtools view -F 4 Ref1.bam | cut -f1 | sort -u > Ref1_reads.txt
      samtools view -F 4 Ref2.bam | cut -f1 | sort -u > Ref2_reads.txt
      
      comm -12 Ref1_reads.txt Ref2_reads.txt > AlignToBoth.txt
      comm -23 Ref1_reads.txt Ref2_reads.txt > AlignToRef1Only.txt
      comm -13 Ref1_reads.txt Ref2_reads.txt > AlignToRef2Only.txt
      Let me break this down:

      The samtools command is converting the the BAM to SAM format but filtering out any reads which are not aligned to the reference (-F 4).

      You should not output the header information (-h) since it is meaningless for your purposes (and you got rid of it later in your pipeline anyway).

      I do not bother saving the intermediate .sam file since I really don't need to, just pipe the output into the next step.

      Just take the read ID in column 1 with cut. (You used awk for this but whatever you like.)

      Sort the ID's and only keep one, unique copy of each (-u). Save the output.

      You were concerned about the order output by sort; don't be. sort in this case is outputting the read IDs in lexical (dictionary) order. This is the order which is required by comm.

      The comm commands are pretty straightforward, outputting just one of the three columns in each run. You could run comm just once and then separate the columns but I find this method a little easier.

      ONE BIG NOTE: This pipeline will collapse the paired read IDs into a single entry meaning that only one of the reads from a pair need align to either of the references to be counted.

      Comment

      • jullee
        Member
        • Apr 2014
        • 19

        #4
        Thank you kmcarr and lindenb for the helpful replies! I will try these out.

        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
        29 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-22-2026, 10:48 AM
        0 responses
        22 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-15-2026, 10:52 AM
        0 responses
        39 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-14-2026, 05:39 AM
        0 responses
        45 views
        0 reactions
        Last Post SEQadmin2  
        Working...