def merge_after_pysam(data, clust):
try:<EOL><INDENT>r1file = tempfile.NamedTemporaryFile(mode='<STR_LIT:wb>', delete=False,<EOL>dir=data.dirs.edits,<EOL>suffix="<STR_LIT>")<EOL>r2file = tempfile.NamedTemporaryFile(mode='<STR_LIT:wb>', delete=False,<EOL>dir=data.dirs.edits,<EOL>suffix="<STR_LIT>")<EOL>r1dat = []<EOL>r2dat = []<EOL>for locus in clust:<EOL><INDENT>sname, seq = locus.split("<STR_LIT:\n>")<EOL>sname = "<STR_LIT:@>" + sname[<NUM_LIT:1>:]<EOL>r1, r2 = seq.split("<STR_LIT>")<EOL>r1dat.append("<STR_LIT>".format(sname, r1, "<STR_LIT:+>", "<STR_LIT:B>"*(len(r1))))<EOL>r2dat.append("<STR_LIT>".format(sname, r2, "<STR_LIT:+>", "<STR_LIT:B>"*(len(r2))))<EOL><DEDENT>r1file.write("<STR_LIT:\n>".join(r1dat))<EOL>r2file.write("<STR_LIT:\n>".join(r2dat))<EOL>r1file.close()<EOL>r2file.close()<EOL>merged_file = tempfile.NamedTemporaryFile(mode='<STR_LIT:wb>',<EOL>dir=data.dirs.edits,<EOL>suffix="<STR_LIT>").name<EOL>clust = []<EOL>merge_pairs(data, [(r1file.name, r2file.name)], merged_file, <NUM_LIT:0>, <NUM_LIT:1>)<EOL>with open(merged_file) as infile:<EOL><INDENT>quarts = itertools.izip(*[iter(infile)]*<NUM_LIT:4>)<EOL>while <NUM_LIT:1>:<EOL><INDENT>try:<EOL><INDENT>sname, seq, _, _ = quarts.next()<EOL>if not "<STR_LIT>" in sname.rsplit("<STR_LIT:;>", <NUM_LIT:1>)[<NUM_LIT:1>]:<EOL><INDENT>try:<EOL><INDENT>R1, R2 = seq.split("<STR_LIT>")<EOL>seq = R1 + "<STR_LIT>" + revcomp(R2)<EOL><DEDENT>except ValueError as inst:<EOL><INDENT>LOGGER.error("<STR_LIT>".format(sname, seq))<EOL>raise<EOL><DEDENT><DEDENT><DEDENT>except StopIteration:<EOL><INDENT>break<EOL><DEDENT>sname = "<STR_LIT:>>" + sname[<NUM_LIT:1>:]<EOL>clust.extend([sname.strip(), seq.strip()])<EOL><DEDENT><DEDENT><DEDENT>except:<EOL><INDENT>LOGGER.info("<STR_LIT>")<EOL>raise<EOL><DEDENT>finally:<EOL><INDENT>for i in [r1file.name, r2file.name, merged_file]:<EOL><INDENT>if os.path.exists(i):<EOL><INDENT>log_level = logging.getLevelName(LOGGER.getEffectiveLevel())<EOL>os.remove(i)<EOL><DEDENT><DEDENT><DEDENT>return clust<EOL>
This is for pysam post-flight merging. The input is a cluster
for an individual locus. We have to split the clusters, write
R1 and R2 to files then call merge_pairs(). This is not ideal,
it's slow, but it works. This is the absolute worst way to do this,
it bounces all the files for each locus off the disk. I/O _hog_.