def detect_build(build_scores, match_threshold=1):
    """
    `build_scores` is the output of `get_build_scores`.

    A build matches the variants if either `allele1` or `allele2` match the build's reference bases in at least `match_threshold` fraction of variants.
    Within a build, an allele column matches if that allele column matches the build's reference bases in at least `match_threshold` fraction of variants.

    Returns:
        (None, None) if multiple builds match or if no builds match.
        (build, 'allele1' or 'allele2') if one build matches and one allele column matches.
        (build, 'either') if one build matches but neither allele1 nor allele2 match.

    `build` is an instance of `Build`.  `build.hg_name` is "hg38" or similar.  `build.grch_name` is "GRCh38" or similar.

    Usage:
        variant_iterator = pheweb.load.detect_ref.make_variant_iterator('a.vcf', chrom_pos_a1_a2_cols=(0,1,3,4), limit_num_variants=1000) # by default, it skips "#" lines.
        build_scores = get_build_scores(variant_iterator) # by default, it tries [hg18, hg19, hg38].
        build, allele_col = detect_build(build_scores, match_threshold=0.999)
        if build is None:
            print("Sorry, this file doesn't match common genome builds.")
        elif allele_col == 'either':
            print("This file matches {} but neither allele column is consistently reference.  Maybe they are effect/non-affect?".format(build.hg_name))
        else:
            print("This file matches {} and column {} matches is the reference.".format(build.hg_name, allele_col))
    """
    matching_builds = []
    for build, score in build_scores.items():
        if score['either'] >= match_threshold:
            if score['a1'] >= match_threshold:
                matching_builds.append((build, 'a1'))
            elif score['a2'] >= match_threshold:
                matching_builds.append((build, 'a2'))
            else:
                matching_builds.append((build, 'either'))
    return matching_builds[0] if len(matching_builds) == 1 else (None, None)
