import re


def query_and_ref_names_from_path(filepath):
    """
    Return a tuple of the query and product names from a path including bin
    names separated by "_to".

     E.g. "potential_relpath/bin_a_to_bin_b" --> bin_a, bin_b

    :param filepath: string to search
    :return: dict containing the query_bin, ref_bin
    """

    # \w matches a "word" character: a letter or digit or underbar [a-zA-Z0-9_]
    search = '([\w\.#-]+)_to_([\w\.#-]+).tsv'
    match = re.search(search, filepath)
    assert match, 'match not found for {} in {}'.format(search, filepath)
    if match:
        return {'query': match.group(1), 'ref':match.group(2)}
    else:
        return None