def decode_fastq(infile, barcode2name, mismatch=0, startpos=37, outprefix=None): """ Decode barcodes in given fastq file """ if not outprefix: outprefix = infile barcode2outhandle = { 'failed':open(outprefix+'.failed.m'+str(mismatch), 'w'), } for bc in barcode2name: outfile = outprefix+'.'+barcode2name[bc]+'.m'+str(mismatch) barcode2outhandle[bc] = open(outfile, 'w') for record in reader_fastq(infile): try: print record[1] currseq = record[1][startpos-1:startpos+len(bc)-1] except: continue barcode2dist = [(bc, barcode_distance(bc, currseq)) for bc in barcode2name] #print >> sys.stderr, record[0], record[1], barcode2dist min_dist = min([barcode2dist[i][1] for i in xrange(len(barcode2dist))]) bc_mindist = [barcode2dist[i][0] for i in xrange(len(barcode2dist)) if barcode2dist[i][1] == min_dist] if min_dist > mismatch or len(bc_mindist) > 1: barcode2outhandle['failed'].write('\n'.join(record)+'\n') else: barcode2outhandle[bc_mindist[0]].write('\n'.join(record)+'\n')
def decode(self): """decode and write results""" for record in reader_fastq(self.infile): fqseq = record.get_seq() currseq = fqseq[self.startpos:(self.startpos+self.lenbc)] if len(currseq) < self.lenbc: sys.stderr.write("%s not have enough length" % currseq) bc = self.decode_one(currseq) self.barcode2outhandle[bc].write("%s\n" % str(record)) for oh in self.barcode2outhandle.values(): oh.close()