def check_tags(barcodes, seq, edit, pos, allow_revcomp=False, verbose=False):
    '''
    For each barcode, pull out the appropriate 5' or 3' sub sequence from {seq}. Then
    run a local alignment of the barcode to the subseq. If a good match is found, return
    it; otherwise, find the best match and return that.

    returns a multi-tuple:
        valid, (tag, valid_seq, edits, reason_if_fail)

    For the alignments, the reference is the barcode, the query is the subset of the read
    that is possibly the barcode (5'/3' subseq)
    '''

    best = None

    # check perfect matches first...
    # for tag in barcodes:
    #     barcodeseq, orientation = barcodes[tag]
    #     if orientation == '5':
    #         if seq[:len(barcodeseq)] == barcodeseq:
    #             return True, (tag, seq[len(barcodeseq):], 0, '')
    #     else:
    #         if seq[-len(barcodeseq):] == barcodeseq:
    #             return True, (tag, seq[:-len(barcodeseq)], 0, '')

    for tag in barcodes:
        barcodeseq, orientation, strip = barcodes[tag]
        if orientation == '5':
            testseq = seq[:len(barcodeseq) + edit + pos]
        else:
            testseq = seq[-1 * (len(barcodeseq) + edit + pos):]

        aln = sw.align(barcodeseq, testseq)
        valid, reason = _tag_aln_check(aln, len(testseq), len(barcodeseq), orientation, edit, pos)
        if verbose:
            print 'Testing tag: %s vs %s' % (str(barcodes[tag]), testseq)
            aln.dump()
            print valid, reason
        if valid:
            return True, (tag, aln, True, '')

        if not best or aln.score > best[1].score:
            best = (tag, aln, True, reason)

        if allow_revcomp:
            if orientation == '5':
                testseq = seq[-1 * (len(barcodeseq) + edit + pos):]
            else:
                testseq = seq[:len(barcodeseq) + edit + pos]

            aln = sw.align(revcomp(barcodeseq), testseq)
            valid, reason = _tag_aln_check(aln, len(testseq), len(barcodeseq), '5' if orientation == '3' else '3', edit, pos)
            if verbose:
                print 'Testing tag: %s [rc] vs %s' % (str(barcodes[tag]), testseq)
                aln.dump()
                print valid, reason
            if valid:
                return True, (tag, aln, False, '')

            if not best or best[1].score > aln.score:
                best = (tag, aln, False, reason)

    if verbose:
        print 'BEST: ', best
        best[1].dump()

    return False, best
dataset/ETHPy150Open ngsutils/ngsutils/ngsutils/fastq/barcode_split.py/check_tags/swapped operands of `>`