Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • SWP
    Member
    • Nov 2010
    • 20

    samse repetitive hits question

    Hello-

    So, BWA samse randomly places reads which map to multiple locations equally well. Is there anyway to force BWA not to align such reads at all? I understand that randomly placing these reads makes sense in most cases since all locations score equally well; however, we are using NGS for medical diagnostics and our molecular geneticists will not sign out cases where reads are randomly placed. Their preference is for these reads to not be aligned at all, even if it means we have to Sanger sequence certain portions of genes. Any help would be appreciated, thanks!
  • drio
    Senior Member
    • Oct 2008
    • 323

    #2
    For those cases, MAPQ should be 0, so you can filter them out prior performing your downstream analysis.
    -drd

    Comment

    • SWP
      Member
      • Nov 2010
      • 20

      #3
      Thank you...

      Comment

      • dawe
        Senior Member
        • Apr 2009
        • 258

        #4
        Originally posted by SWP View Post
        Thank you...
        You can also try GibbsAM, this should help to uniquely map your alignments. I'm still evaluating it, so I can't tell anything more than the paper.
        If you want to give it a chance, I have some scripts to convert bwa output into GibbsAM input (and back to BAM, then).
        Also, the mapq=0 rule is not always true... I've found a number of tags aligned in multiple places (and with the XT:A:R tag) but with mapq > 0

        d

        Comment

        • SWP
          Member
          • Nov 2010
          • 20

          #5
          Yeah, I'd love to have the scripts and give it a shot.

          Comment

          • dawe
            Senior Member
            • Apr 2009
            • 258

            #6
            First of all you have to run bwa samse with -n X option, with X high enough (I've used 255). Then you can use this python script

            Code:
            #!/usr/bin/python
            
            import sys
            import pysam
            
            samfile = pysam.Samfile(sys.argv[1], 'rb')
            sep = "_"
            
            for x in samfile.fetch():
              if x.flag & 4: continue
              buf = []
              readChrom = samfile.references[x.rname]
              if '_random' not in readChrom and '_hap' not in readChrom:
                strand = '+'
                if x.flag & 16:
                  strand = '-'
                buf.append( '_'.join([readChrom, str(x.pos), strand]))
              
              XAList = [Tag for Tag in x.tags if Tag[0] == 'XA']
              if len(XAList):
                for p in XAList[0][1].split(';'):
                  a = p.split(',')
                  if '_random' in p or '_hap' in p: continue
                  if len(a) <= 1: continue 
                  if int(a[1]) >= 0:
                    buf.append('_'.join([a[0], a[1][1:], '+']))
                  else:
                    buf.append('_'.join([a[0], a[1][1:], '-']))
              if len(buf):
                print x.qname + '\t' + ','.join(buf)
            to convert your BAM into a "map" file.

            Code:
            $ python bam2map.py filein.bam > filein.map
            You can use filein.map with get_unique_file.pl from GibbsAM (part C). My code depends on pysam and prints output to stdout (and sorry if it's ugly code).
            Beware, GibbsAM is rather slow.
            After GibbsAM has done and you get your final output (results.txt) you can run this

            Code:
            import sys
            import pysam
            
            samfile = pysam.Samfile(sys.argv[1], 'rb')
            gfile = open(sys.argv[2], 'r')
            outfile = pysam.Samfile("-", 'wh', header = samfile.header)
            
            def cigar2tuple(cigarString):
              cdict = {'M':0, 'I':1, 'D':2, 'N':3, 'S':4, 'H':5, 'P':6}
              outlist = []
              pn = 0
              for n in range(len(cigarString)):
                l = cigarString[n]
                if l in 'MIDNSHP':
                  outlist.append((cdict[l], int(cigarString[pn:n])))
                  pn = n + 1
              return tuple(outlist)
            
            def correctNM(Taglist, newNM):
              newTagList = []
              for x in Taglist:
                if x[0] == 'NM':
                  newTagList.append(('NM', int(newNM)))
                else:
                  newTagList.append(x)
              return newTagList
            
            def fixTags(Taglist):
              newTagList = []
              for x in Taglist:
                if x[0] == 'XT':
                  newTagList.append(('XT', 'U'))
                elif x[0] == 'XA':
                  continue
                else:
                  newTagList.append(x)
              newTagList.append(('GF','M'))
              return newTagList
              
            mappings = {}
            
            for line in gfile:
              t = line.strip().split()
              if t[-1] == 'a':
                mappings[t[0]] = t[1] + ',' + t[3] + t[2]
            
            for x in samfile.fetch():
              try:
                d = mappings[x.qname]
              except KeyError:
                outfile.write(x)
                continue
              XATag = [Tag for Tag in x.tags if Tag[0] == 'XA'][0][1]
              x.tags = fixTags(x.tags)
              try:
                theString = [xa for xa in XATag.split(';') if d in xa][0]
                t = theString.split(',')
                x.rname = samfile.references.index(t[0])
                x.pos = int(t[1][1:])
                x.cigar = cigar2tuple(t[2])
                if  t[1][0] == '-':
                  x.flag = x.flag | 16
                else:
                  x.flag = x.flag & ~16
                x.tags = correctNM(x.tags, t[3])
              except IndexError:
                # the read is already in the correct position
                outfile.write(x)
                continue
            #  temp = [Tag for Tag in x.tags if Tag[0] != 'XA']
            #  x.tags = temp
              if not x.mapq:
                x.mapq = 1
              outfile.write(x)
            outfile.close()
            to transform your results.txt into a SAM file, just issue:

            Code:
            $ python gam2bam.py filein.bam results.txt > results.sam
            where filein.bam is your original BAM file (from bwa).
            Note that these scripts are somehow optimized for human genomes, it excludes _random and _hap chroms (it's a long story, but this happens because GibbsAM doesn't support those chroms...). If you are performing analysis with another genome you may need to modify the first code. I'm in touch with GibbsAM developers, I'm waiting this flaw to be fixed.

            HTH
            d

            Comment

            • Thomas Doktor
              Senior Member
              • Apr 2009
              • 105

              #7
              Unless I've misunderstood something, if you want the uniquely aligned hits, you'd want the reads which had just 1 best hit reported. BWA reports that as a custom tag in the SAM output, "X0:i:n" where n is the number of best hits. To get the unique hits you could simply do a grep:
              $ grep -P "X0:i:1\t" hits.sam > unique.hits.sam

              Does anyone else have some inputs?

              EDIT
              You could also just go for the XT:A:U tag which BWA reports for the unique alignments.
              Last edited by Thomas Doktor; 02-17-2011, 04:21 PM.

              Comment

              Latest Articles

              Collapse

              • 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...
                Today, 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
              • SEQadmin2
                Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                by SEQadmin2


                I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

                Here are nine questions we think about, in roughly the order they matter, before...
                06-18-2026, 07:11 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, Today, 10:08 AM
              0 responses
              6 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, Yesterday, 11:05 AM
              0 responses
              8 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-02-2026, 11:08 AM
              0 responses
              31 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 06-30-2026, 05:37 AM
              0 responses
              28 views
              0 reactions
              Last Post SEQadmin2  
              Working...