# Download file utility


def download_file(base_url, filename, download_dir):
    """
    Check if file is already downloaded, and download if not

    Parameters:
    base_url: the base http address of file to download
    filename: the file located at the base_url (will be name of download)
    download_dir: the location to save fh

    Output:
    Nothing - will save base_url/fh to download_dir/fh
    """
    import os
    from urllib.request import urlretrieve

    path = os.path.join(download_dir, filename)
    if not (os.path.exists(path)):
        urlretrieve(base_url + filename, path)


def process_repeats(base_dir, filename, genome):
    """
    Process the irregularly structured repeatmasker file
    """
    from subprocess import call

    full_loc = base_dir + filename
    call(['gunzip', full_loc])

    # Replace variable length space delimiters with tabs and output tsv file
    CMD = "sed 's/ \+/\\t/g' " + full_loc[:-3] + " > " + full_loc[:-3] + ".tsv"
    call(CMD, shell=True)

@@ -1001,8 +1001,204 @@
 l=True)%0A
+%0A%0Adef untar_mouse_domain(tar_file):%0A    %22%22%22%0A    Mouse domains are downloaded as a tar archive - extracts contents%0A    %22%22%22%0A    import tarfile%0A%0A    tar = tarfile.open(tar_file)%0A    tar.extractall()%0A

add function to untar mouse TAD domains