def count_bams(infiles, outfile): '''Count the number of alignments both pre and post dedup''' outlines = [] for infile in infiles: method = re.match("dedup_(.+).dir\/.+", infile) if method: method = method.groups()[0] else: method = "none" track = re.search("([^/]+).bam", infile).groups()[0] statement = '''samtools idxstats %(infile)s | awk '{sum+=$3} END{print sum}' ''' count, _ = P.execute(statement) outlines.append([method, track, count.strip()]) IOTools.writeLines(outfile, outlines, header=["method", "track", "count"])