def worker(self,i,j,k): samfile = pysam.Samfile(self.samfiles[i], "rb") assert samfile._hasIndex(), "Cannot open BAM index for file %s"%sf chrommap = self.chrreg.chrommap(self.samfiles[i]) # blocksize = int(math.ceil(len(self.loci)/self.tpb)) # for l in range(j*blocksize,min((j+1)*blocksize,len(self.loci))): for l in range(j,len(self.loci),self.tpb): snvchr, snvpos, ref, alt, snvextra = self.loci[l] cnts = Counter() total = Counter() reads = [] snvpos1 = snvpos - 1 try: snvlabel = chrommap(snvchr) if snvlabel != None: for pileupcolumn in samfile.pileup(snvlabel, snvpos1, snvpos1 + 1, truncate=True): total[i] += pileupcolumn.n for pileupread in pileupcolumn.pileups: try: al, pos, base, nseg = self.filter.test(pileupread) except BadRead, e: cnts[(i, e.message)] += 1 continue reads.append((al, pos, base, i)) cnts[(i, 'Good')] += 1 except ValueError, e: pass # raise e total[i] -= cnts[(i,"GapInQueryAtSNVLocus")] # del cnts[(i,"GapInQueryAtSNVLocus")] # print >>sys.stderr, (snvchr, snvpos, ref, alt, total, cnts) self._queue[k].put((snvchr, snvpos, ref, alt, total, reads, cnts))
def iterator(self): samfiles = [] chrommap = [] for al in self.samfiles: samfile = pysam.Samfile(al, "rb") assert samfile._hasIndex(), "Cannot open BAM index for file %s"%sf samfiles.append(samfile) chrommap.append(self.chrreg.chrommap(al)) for snvchr, snvpos, ref, alt, snvextra in self.loci: cnts = Counter() total = Counter() reads = [] snvpos1 = snvpos - 1 for i, samfile in enumerate(samfiles): try: snvlabel = chrommap[i](snvchr) if snvlabel != None: for pileupcolumn in samfile.pileup(snvlabel, snvpos1, snvpos1 + 1, truncate=True): total[i] += pileupcolumn.n for pileupread in pileupcolumn.pileups: try: al, pos, base, nseg = self.filter.test(pileupread) except BadRead, e: cnts[(i, e.message)] += 1 continue reads.append((al, pos, base, i)) cnts[(i, 'Good')] += 1 except ValueError: pass total[i] -= cnts[(i,"GapInQueryAtSNVLocus")] # del cnts[(i,"GapInQueryAtSNVLocus")] yield (snvchr, snvpos, ref, alt, total, reads, cnts)
def iterator(self): samfiles = [] chrommap = [] for al in self.samfiles: samfile = pysam.Samfile(al, "rb") assert samfile.has_index( ), "Cannot open BAM index for file %s" % sf samfiles.append(samfile) chrommap.append(self.chrreg.chrommap(al)) pileup_kwargs = self.filter.pileup_kwargs() for snvchr, snvpos, ref, alt, snvextra in self.loci: cnts = Counter() total = Counter() reads = [] snvpos1 = snvpos - 1 for i, samfile in enumerate(samfiles): try: snvlabel = chrommap[i](snvchr) if snvlabel != None: for pileupcolumn in samfile.pileup(snvlabel, snvpos1, snvpos1 + 1, truncate=True, **pileup_kwargs): self.filter.pileup_start(pileupcolumn) total[i] = pileupcolumn.n for pileupread in pileupcolumn.pileups: if self.readgroups != None: grp = (i, self.readgroups.group( pileupread.alignment)) if grp[1] == None: continue total[grp] += 1 else: grp = i try: al, pos, base = self.filter.extract_base( pileupread) except BadRead as e: cnts[(grp, e.args[0])] += 1 continue reads.append((al, pos, base, grp)) cnts[(grp, 'Good')] += 1 self.filter.pileup_end(pileupcolumn) except ValueError: pass yield (snvchr, snvpos, ref, alt, total, reads, cnts)
def worker(self, i, j, k): samfile = pysam.Samfile(self.samfiles[i], "rb") assert samfile.has_index(), "Cannot open BAM index for file %s" % sf chrommap = self.chrreg.chrommap(self.samfiles[i]) # blocksize = int(math.ceil(len(self.loci)/self.tpb)) # for l in range(j*blocksize,min((j+1)*blocksize,len(self.loci))): pileup_kwargs = self.filter.pileup_kwargs() for l in range(j, len(self.loci), self.tpb): snvchr, snvpos, ref, alt, snvextra = self.loci[l] cnts = Counter() total = Counter() reads = [] snvpos1 = snvpos - 1 try: snvlabel = chrommap(snvchr) if snvlabel != None: for pileupcolumn in samfile.pileup(snvlabel, snvpos1, snvpos1 + 1, truncate=True, **pileup_kwargs): self.filter.pileup_start(pileupcolumn) total[i] += pileupcolumn.n for pileupread in pileupcolumn.pileups: if self.readgroups != None: grp = (i, self.readgroups.group( pileupread.alignment)) total[grp] += 1 else: grp = i try: al, pos, base = self.filter.extract_base( pileupread) except BadRead as e: cnts[(i, e.args[0])] += 1 continue reads.append( (PileupAlignment(al.seq, al.is_reverse), pos, base, grp)) cnts[(i, 'Good')] += 1 self.filter.pileup_end(pileupcolumn) except ValueError as e: pass self._queue[k].put((snvchr, snvpos, ref, alt, total, reads, cnts)) return
#!/bin/env python2.7 import sys from os.path import join, dirname, realpath try: sys.path.append(join(dirname(realpath(__file__)), '..', '..', 'common', 'src')) except NameError: pass from pysamimport import pysam for filename in sys.argv[1:]: samfile = pysam.Samfile(filename, "rb") assert samfile.has_index(), "Cannot open BAM index for file %s"%filename print(filename,"\n "+", ".join(samfile.references)+".")
usedsnvheaders.add(h) cannonr = (",".join(["%s:%s" % t for t in sorted(r.items())])) snvkey = (chr, locus, ref, alt, cannonr) if snvkey not in snvdata: snvdata[snvkey] = (chr, locus, ref, alt, r) progress.update() progress.done() snvdata = sorted(snvdata.values()) extrasnvheaders = [h for h in extrasnvheaders if h in usedsnvheaders] progress.message("SNVs: %d" % len(snvdata)) samfiles = [] for al in opt.alignments: if al.lower().endswith('.bam'): samfile = pysam.Samfile(al, "rb") elif al.lower().endswith('.sam'): samfile = pysam.Samfile(al, "r") else: raise RuntimeError("Unexpected alignments file extension: %s." % al) samfiles.append(samfile) outheaders = snvheaders + [ _f for _f in """ SNVCount NoSNVCount Prob LogOdds P-Value Bonferroni FDR
def add_bamlabels(self,filename): samfile = pysam.Samfile(filename, "rb") assert samfile._hasIndex(), "Cannot open BAM index for file %s"%filename self._reg[filename] = ChromLabels(samfile.references) self._bam.append(filename)