import logging


def build_and_check_sets_experiments(all_vars):
    """

    Description:
        First we create a variable called 'setExps',
        which is a dict that goes from set name
        to a list of experiments belonging
        to that set (set is equivalent to 'lane').
        We then double check that every experiment
        name is unique.
        Then we print to the user the total number
        of experiments, total number of genes,
        and total number of sets.
    """
    # Build list of experiments for each set
    setExps = {}
    for exp in all_vars["exps"]:
        # Each exp is a hash of column Names to column values from 
        # the experiments file.
        if exp["SetName"] in setExps:
            setExps[exp["SetName"]].append(exp)
        else:
            setExps[exp["SetName"]] = [exp]

    # And check that each index is unique for each set
    for k in setExps.keys():
        s, exps = k, setExps[k]
        indexSeen = {}
        for exp in exps:
            index = exp["Index"]
            if index in indexSeen:
                raise Exception(
                    "Duplicate experiment entries "
                    "for index {} in set {}".format(index, s)
                )
            else:
                indexSeen[index] = 1

    all_vars["setExps"] = setExps

    # Print Info:
    test_or_proc_str = "Test found: " if "test" in all_vars and \
            all_vars["test"] is not None else "Processing: "


    logging.info(
        "{} {} experiments, {} genes, {} sets for {}\n".format(
            test_or_proc_str,
            len(all_vars["exps"]),
            len(all_vars["genes"]),
            len(all_vars["sets"]),
            all_vars['org']
        )
    )

    return all_vars