INNER CODE UNIT · Python
_add_read_tags
timoast/sinto · sinto/addtags.py:11
def _add_read_tags(intervals, bam, sam, output, cb, trim_suffix, mode):
inputBam = pysam.AlignmentFile(bam, "rb")
header = inputBam.header.to_dict()
validKeys = [x for x in ["HD", "SQ", "RG"] if x in header.keys()]
newhead = dict((k, header[k]) for k in validKeys)
ident = "".join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(6)
)
outfmt = 'w' if sam else 'wb'
outputBam = pysam.AlignmentFile(output + ident, outfmt, header=newhead)
for i in intervals:
for r in inputBam.fetch(i[0], i[1], i[2]):
if mode == "tag":
cell_barcode, _ = utils.scan_tags(r.tags)
elif mode == "readname":
cell_barcode = r.qname.split(":")[0]
else:
raise Exception("Unknown mode. Use either tag or readname")