def filter(self, bam): for read in self.parent.filter(bam): if not read.is_unmapped: read.tags = read.tags + [(self.tag, cigar_tostr(read.cigar))] else: read.tags = read.tags + [(self.tag, "*")] yield read
def filter(self, bam): for read in self.parent.filter(bam): if not read.is_unmapped: read.tags = read.tags + [(self.tag, cigar_tostr(read.cigar))] else: read.tags = read.tags + [(self.tag, '*')] yield read
def export_read(bamfile, read, fields, out=sys.stdout): cols = [] for field in fields: if field == '-name': cols.append(read.qname) elif field == '-ref': if read.tid == -1: cols.append('*') elif bamfile: cols.append(bamfile.getrname(read.tid)) else: cols.append('?') elif field == '-pos': cols.append(read.pos + 1) # output 1-based elif field == '-strand': cols.append('-' if read.is_reverse else '+') elif field == '-cigar': if read.cigar: cols.append(cigar_tostr(read.cigar)) else: cols.append('*') elif field == '-flags': cols.append(read.flag) elif field == '-seq': cols.append(read.seq) elif field == '-qual': cols.append(read.qual) elif field == '-mapq': cols.append(read.mapq) elif field == '-nextref': if read.rnext == -1: cols.append('*') elif bamfile: cols.append(bamfile.getrname(read.rnext)) else: cols.append('?') elif field == '-nextpos': if read.rnext == -1: cols.append(0) else: cols.append(read.pnext + 1) # output 1-based elif field == '-tlen': cols.append(read.tlen) elif field == '-isize': cols.append(read.isize) elif field[:5] == '-tag:': if field[5:] == '*': for tag, val in read.tags: if type(val) == int: cols.append('%s:i:%s' % (tag, val)) elif type(val) == float: cols.append('%s:f:%s' % (tag, val)) elif len(val) == 1: cols.append('%s:A:%s' % (tag, val)) else: cols.append('%s:Z:%s' % (tag, val)) else: try: cols.append(read.opt(field[5:])) except KeyError: cols.append('') out.write('%s\n' % '\t'.join([str(x) for x in cols]))
def export_read(bamfile, read, fields, out=sys.stdout): cols = [] for field in fields: if field == '-name': cols.append(read.qname) elif field == '-ref': if read.tid == -1: cols.append('*') elif bamfile: cols.append(bamfile.getrname(read.tid)) else: cols.append('?') elif field == '-pos': cols.append(read.pos + 1) # output 1-based elif field == '-strand': cols.append('-' if read.is_reverse else '+') elif field == '-cigar': if read.cigar: cols.append(cigar_tostr(read.cigar)) else: cols.append('*') elif field == '-flags': cols.append(read.flag) elif field == '-seq': cols.append(read.seq) elif field == '-qual': cols.append(read.qual) elif field == '-mapq': cols.append(read.mapq) elif field == '-nextref': if read.rnext == -1: cols.append('*') elif bamfile: cols.append(bamfile.getrname(read.rnext)) else: cols.append('?') elif field == '-nextpos': if read.rnext == -1: cols.append(0) else: cols.append(read.pnext + 1) # output 1-based elif field == '-tlen': cols.append(read.tlen) elif field == '-isize': cols.append(read.isize) elif field[:5] == '-tag:': if field[5:] == '*': for tag, val in read.tags: if type(val) == int: cols.append('%s:i:%s' % (tag, val)) elif type(val) == float: cols.append('%s:f:%s' % (tag, val)) elif len(val) == 1: cols.append('%s:A:%s' % (tag, val)) else: cols.append('%s:Z:%s' % (tag, val)) else: try: cols.append(read.opt(field[5:])) except KeyError: cols.append('') out.write('%s\n' % '\t'.join([str(x) for x in cols]))