def fastx_barcode_split(reader, outtempl, barcodes, edits=0, pos=0, allow_revcomp=False, gzip_output=False, stats_fname=None):
    '''
    Split FAST[QA] reads from {fname} using {barcodes} (hash) to write them to
    output files named like {templ}.
    '''

    outs = {}
    if gzip_output:
        outtempl += '.gz'
        outs[''] = gzip.open(outtempl % 'missing', 'w')
    else:
        outs[''] = open(outtempl % 'missing', 'w')

    tag_count = {}
    for tag in barcodes:
        if gzip_output:
            outs[tag] = gzip.open(outtempl % tag, 'w')
        else:
            outs[tag] = open(outtempl % tag, 'w')
        tag_count[tag] = 0

    matched = 0
    perfect = 0
    mismatched = 0
    mispositioned = 0
    missing = 0

    for record in reader.fetch():
        ismatch, (tag, aln, is_forward, reason) = check_tags(barcodes, record.seq, edits, pos, allow_revcomp)

        if not ismatch:
            missing += 1
            comment = '#guess: %s, %s%s (%s, %s, %smm) %s' % (tag, barcodes[tag][0], '' if is_forward else '[rc]', aln.q_pos, aln.extended_cigar_str, aln.mismatches, reason)
            record.subseq(None, None, comment).write(outs[''])
        else:
            matched += 1
            if aln.mismatches == 0:
                if aln.q_pos == 0 or aln.q_end == len(record.seq):
                    perfect += 1
                else:
                    mispositioned += 1
            else:
                mismatched += 1

            comment = '#%s%s (%s, %s)' % (tag, '' if is_forward else '[rc]', aln.q_pos, aln.mismatches,)

            if not barcodes[tag][2]:
                record.write(outs[tag])

            else:
                if is_forward:
                    if barcodes[tag][1] == '5':
                        newrec = record.subseq(aln.q_end, None, comment)
                    else:
                        subseqlen = len(barcodes[tag][0]) + edits + pos
                        newrec = record.subseq(None, -1 * (subseqlen - aln.q_pos), comment)
                else:
                    if barcodes[tag][1] == '5':
                        subseqlen = len(barcodes[tag][0]) + edits + pos
                        newrec = record.subseq(None, -1 * (subseqlen - aln.q_pos), comment)
                    else:
                        newrec = record.subseq(aln.q_end, None, comment)

                newrec.write(outs[tag])

            tag_count[tag] += 1

    for tag in outs:
        outs[tag].close()

    if stats_fname:
        with open(stats_fname, 'w') as f:
            f.write(missing % "Unmatched\t%s\n")
            f.write("Matched\t%s\n" % matched)
            f.write(" Perfect\t%s\n" % perfect)
            f.write(" Mismatched\t%s\n" % mismatched)
            f.write(" Mispositioned\t%s\n" % mispositioned)
            f.write('\n')
            for tag in tag_count:
                f.write("Tag\t%s\t%s\n" % (tag, tag_count[tag]))
dataset/ETHPy150Open ngsutils/ngsutils/ngsutils/fastq/barcode_split.py/fastx_barcode_split/swapped operands of `%`