Exemplo n.º 1
0
def get_genetic_map_tarball():
    """
    Returns a genetic map in hapmap format in a tarball as a bytes object.
    """
    with tempfile.TemporaryDirectory() as map_dir:
        for j in range(1, 10):
            # TODO Have a way to put in different maps??
            with open(os.path.join(map_dir, "prefix_chr{}.txt".format(j)),
                      "w") as f:
                print("Chromosome  Position(bp)    Rate(cM/Mb)     Map(cM)",
                      file=f)
                print("chr1        55550   2.981822        0.000000", file=f)
                print("chr1        82571   2.082414        0.080572", file=f)
                print("chr1        88169   0               0.092229", file=f)
        # For the tarfile to be in the right format, we must be in the right directory.
        with genetic_maps.cd(map_dir):
            # Now tar up this map_directory
            with tempfile.TemporaryFile('wb+') as tmp_file:
                with tarfile.open(fileobj=tmp_file, mode="w:gz") as tar_file:
                    for filename in os.listdir("."):
                        tar_file.add(filename)
                # Read back the tarball
                tmp_file.seek(0)
                tarball = tmp_file.read()
    return tarball
Exemplo n.º 2
0
 def get_maps(self, tarball):
     maps = {}
     with tempfile.TemporaryFile('wb+') as f:
         f.write(tarball)
         f.seek(0)
         with tarfile.open(fileobj=f, mode='r') as tar_file:
             with tempfile.TemporaryDirectory() as extract_dir:
                 with genetic_maps.cd(extract_dir):
                     tar_file.extractall()
                     for fn in os.listdir(extract_dir):
                         maps[fn] = msprime.RecombinationMap.read_hapmap(fn)
     return maps
Exemplo n.º 3
0
def get_genetic_map_tarball(custom_file_f=None, filter=None):
    """
    Returns a genetic map in hapmap format in a tarball as a bytes object.

    :param func custom_file_f: A function that accepts a single parameter
        (a folder name), which may be used to create additional files under
        the given folder. All files in the folder will be included in the
        returned tarball.
    :param func filter: A function which is passed as the ``filter`` argument
        to ``TarFile.add()``. This function can be used to change the info
        field for each file in the returned tarball. See tarfile documentation
        for more details.
    """
    with tempfile.TemporaryDirectory() as map_dir:
        for j in range(1, 10):
            # TODO Have a way to put in different maps??
            with open(os.path.join(map_dir, "prefix_chr{}.txt".format(j)),
                      "w") as f:
                print("Chromosome  Position(bp)    Rate(cM/Mb)     Map(cM)",
                      file=f)
                print("chr1        55550   2.981822        0.000000", file=f)
                print("chr1        82571   2.082414        0.080572", file=f)
                print("chr1        88169   0               0.092229", file=f)

        if custom_file_f is not None:
            # Do arbitrary things under map_dir.
            custom_file_f(map_dir)

        # For the tarfile to be in the right format, we must be in the right directory.
        with genetic_maps.cd(map_dir):
            # Now tar up this map_directory
            with tempfile.TemporaryFile('wb+') as tmp_file:
                with tarfile.open(fileobj=tmp_file, mode="w:gz") as tar_file:
                    for filename in os.listdir("."):
                        tar_file.add(filename, filter=filter)
                # Read back the tarball
                tmp_file.seek(0)
                tarball = tmp_file.read()
    return tarball