Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • samtools filter PE reads on mapping quality of only one of the reads

    Hi,

    I'm trying to filter a BAM file using samtools for mapping quality, but I want to keep both reads in a pair if at least one of them has MAPQ>=10.

    My understanding is that using
    Code:
    -q 10
    as an option would remove all reads with MAPQ<10 regardless of whether the other read in the pair is of high quality. Is there a way to get around that?

    Thanks

  • #2
    There's no way to do that directly with samtools, you'll need to script something. It'd be pretty straight forward with pysam, if you know a bit of python (I'm in the middle of a bunch of C code at the moment or I'd through together a quick script for you).

    Comment


    • #3
      ahh, I was afraid that would be the case...Thanks for your reply anyway!
      I don't know python so I'm going to have to do it painfully in R.

      Comment


      • #4
        Too bad, I recall that the R method of dealing with SAM/BAM files is to read the whole thing into memory, which would really suck. Post the script you use when you have it working in case someone else ends up needing this in the future.

        Comment


        • #5
          Yep, that's exactly why it's going to be painful...Sure, I'll post it if/when it works!

          Comment


          • #6
            I ended up getting side-tracked by a dissection anyway, so here's a quick method using python and the pysam package. It assumes that you have everything name-sorted and that there aren't any random single-end reads in the file:

            Code:
            #!/usr/bin/env python
            import pysam
            import argparse
            import sys
            
            parser = argparse.ArgumentParser(description='Filter a paired-end BAM file such that both reads have MAPQs >= a threshold.')
            parser.add_argument('threshold', type=int, help="MAPQ threshold")
            parser.add_argument('input_file', help="input file name")
            parser.add_argument('output_file', help="output file name")
            args = parser.parse_args()
            if(args.threshold == None or args.input_file == None or args.output_file == None) :
                parser.print_help()
                sys.exit()
            
            #One could look at the file name too see if we're dealing with a SAM or BAM file, but this is simpler
            ifile = pysam.Samfile(args.input_file, "rb")
            ofile = pysam.Samfile(args.output_file, "wb", template=ifile)
            
            #iterate
            while(1) :
                try :
                    r1 = ifile.next()
                    r2 = ifile.next()
                except :
                    break
            
                if(r1.mapq >= int(args.threshold) and r2.mapq >= int(args.threshold)) :
                    ofile.write(r1)
                    ofile.write(r2)
            
            ifile.close()
            ofile.close()

            Comment


            • #7
              Thanks a lot for doing this.
              Quick question: what I'd like is to filter the file so that AT LEAST ONE of the reads has MAPQs>= threshold (as opposed to BOTH);

              For that should I just change this section of your code:
              Code:
              if(r1.mapq >= int(args.threshold) and r2.mapq >= int(args.threshold)) :
              to this:?
              Code:
              if(r1.mapq >= int(args.threshold) or r2.mapq >= int(args.threshold)) :
              or would that not keep pairs where both are above the threshold?
              thanks again!

              Comment


              • #8
                That should work. "or" isn't an "exclusive or" in python.

                Comment

                Latest Articles

                Collapse

                • seqadmin
                  Recent Advances in Sequencing Technologies
                  by seqadmin







                  Innovations in next-generation sequencing technologies and techniques are driving more precise and comprehensive exploration of complex biological systems. Current advancements include improved accessibility for long-read sequencing and significant progress in single-cell and 3D genomics. This article explores some of the most impactful developments in the field over the past year.

                  Long-Read Sequencing
                  Long-read sequencing has...
                  12-02-2024, 01:49 PM
                • seqadmin
                  Genetic Variation in Immunogenetics and Antibody Diversity
                  by seqadmin



                  The field of immunogenetics explores how genetic variations influence immune responses and susceptibility to disease. In a recent SEQanswers webinar, Oscar Rodriguez, Ph.D., Postdoctoral Researcher at the University of Louisville, and Ruben Martínez Barricarte, Ph.D., Assistant Professor of Medicine at Vanderbilt University, shared recent advancements in immunogenetics. This article discusses their research on genetic variation in antibody loci, antibody production processes,...
                  11-06-2024, 07:24 PM

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by seqadmin, 12-02-2024, 09:29 AM
                0 responses
                138 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 12-02-2024, 09:06 AM
                0 responses
                49 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 12-02-2024, 08:03 AM
                0 responses
                38 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 11-22-2024, 07:36 AM
                0 responses
                69 views
                0 likes
                Last Post seqadmin  
                Working...
                X