from typing import Dict


def recount_map_sample_to_study(metadata_file: str) -> Dict[str, str]:
    """
    Parse the recount3 metadata file and extract the sample to study mappings

    Arguments
    ---------
    metadata_file: The path to where the metadata is stored

    Returns
    -------
    sample_to_study: A mapping between samples and studies
    """
    with open(metadata_file) as in_file:
        header = in_file.readline()
        header = header.replace('"', '')
        header = header.strip().split('\t')

        # Add one to the indices to account for the index column in metadata not present in the
        # header
        sample_index = header.index('external_id') + 1
        study_index = header.index('study') + 1

        sample_to_study = {}
        for line in in_file:
            line = line.strip().split('\t')
            sample = line[sample_index]
            sample = sample.replace('"', '')
            study = line[study_index]
            study = study.replace('"', '')
            sample_to_study[sample] = study

    return sample_to_study