def selectMatches(query_id, matches, options, queries_fasta=None):

    if options.loglevel >= 2:
        options.stdlog.write(
            "# attempting to select best match for %s\n" % query_id)

        if options.loglevel >= 3:
            for match in matches:
                options.stdlog.write("# match=%s\n" % str(match))

    new_matches = []

    if options.polyA:
        matches = detectPolyA(query_id, matches, options, queries_fasta)

    if options.matching_mode == "all":
        return matches, None

    elif options.matching_mode in ("best-coverage", "best-query-coverage", "best-sbjct-coverage",
                                   "best-pid",
                                   "best-covpid", "best-query-covpid", "best-sbjct-covpid",
                                   "best-min-covpid", "best-query-min-covpid", "best-sbjct-min-covpid",):
        if options.matching_mode == "best-coverage":
            f = lambda match: min(match.mQueryCoverage, match.mSbjctCoverage)
        elif options.matching_mode == "best-query-coverage":
            f = lambda match: match.mQueryCoverage
        elif options.matching_mode == "best-sbjct-coverage":
            f = lambda match: match.mSbjctCoverage
        elif options.matching_mode == "best-pid":
            f = lambda match: match.mPid
        elif options.matching_mode == "best-covpid":
            f = lambda match: min(
                match.mQueryCoverage, match.mSbjctCoverage) * match.mPid
        elif options.matching_mode == "best-query-covpid":
            f = lambda match: match.mQueryCoverage * match.mPid
        elif options.matching_mode == "best-sbjct-covpid":
            f = lambda match: match.mSbjctCoverage * match.mPid
        elif options.matching_mode == "best-min-covpid":
            f = lambda match: min(
                (match.mQueryCoverage, match.mSbjctCoverage, match.mPid))
        elif options.matching_mode == "best-query-min-covpid":
            f = lambda match: min(match.mQueryCoverage, match.mPid)
        elif options.matching_mode == "best-sbjct-min-covpid":
            f = lambda match: min(match.mSbjctCoverage, match.mPid)

        for match in matches:
            match.mMatchScore = f(match)

        # collect "significant" matches
        # this filter removes matches out of contention, i.e., matches
        # with very low score are not considered when assessing the uniqueness
        # of the highest scoring match
        matches.sort(lambda x, y: cmp(x.mMatchScore, y.mMatchScore))
        matches.reverse()
        best_score = min(matches[0].mMatchScore * options.collection_threshold,
                         matches[0].mMatchScore - options.collection_distance)

        for match in matches:
            # stop when matchscore drops below best score
            if match.mMatchScore < best_score:
                break
            new_matches.append(match)

        if not options.keep_all_best:

            if len(new_matches) > 1:

                if len(new_matches) == 2:
                    # accept matches against chrX and chrX_random (or vice
                    # versa)
                    if new_matches[0].mSbjctId == "%s_random" % new_matches[1].mSbjctId:
                        return new_matches[1:], None
                    elif new_matches[1].mSbjctId == "%s_random" % new_matches[0].mSbjctId:
                        return new_matches[:1], None
                    # or against chrUn or chrU.
                    else:
                        new_matches = [x for x in new_matches if not(
                            x.mSbjctId.endswith("Un") or x.mSbjctId.endswith("chrU"))]
                        if len(new_matches) == 1:
                            return new_matches, None

                if options.ignore_all_random:
                    new_matches = [x for x in new_matches if not(x.mSbjctId.endswith(
                        "_random") or x.mSbjctId.endswith("Un") or x.mSbjctId.endswith("chrU"))]
                    if len(new_matches) == 1:
                        return new_matches, None

                return [], "not unique: %s" % (" ".join(map(lambda x: str(x.mMatchScore), matches)))

    elif options.matching_mode == "unique":
        # only return matches if they are "unique", i.e. no other match
        if len(matches) == 1:
            new_matches.append(matches[0])
        else:
            return [], "not unique: %s" % (" ".join(map(lambda x: str(x.mMatchScore), matches)))

    matches = new_matches

    if options.best_per_sbjct:
        new_matches = []
        sbjcts = set()
        for match in matches:
            if match.mSbjctId in sbjcts:
                continue
            new_matches.append(match)
            sbjcts.add(match.mSbjctId)

        matches = new_matches

    return matches, None
find the best match.
dataset/ETHPy150Open CGATOxford/cgat/scripts/psl2map.py/selectMatches