def load_population_from_file(archive_path, extract_path="."): """Load a population from a snapshot. Load a population from a snapshot, which should be stored in a .tar archive. archive_path - path to archive extract_path - path to directory where files will be extracted """ popn_archive = tarfile.open(archive_path) clone_fname = mut_fname = param_fname = anlt_fname = None # get filenames for extraction # NB this may break if filenames in save_population_to_file() are changed members_to_extract = [] for member in popn_archive.getmembers(): if member.name.startswith('clones_'): clone_fname = "{}/{}".format(extract_path, member.name) members_to_extract.append(member) elif member.name.startswith('mutations_'): mut_fname = "{}/{}".format(extract_path, member.name) members_to_extract.append(member) elif member.name.startswith('params_'): param_fname = "{}/{}".format(extract_path, member.name) members_to_extract.append(member) elif member.name.startswith('anlt_'): anlt_fname = "{}/{}".format(extract_path, member.name) members_to_extract.append(member) fnames = [clone_fname, mut_fname, param_fname, anlt_fname] if not all(fnames): raise Exception("population archive is missing required files") # extract snapshot files popn_archive.extractall(path=extract_path, members=members_to_extract) popn_archive.close() # load parameters, mutations and clones t_curr, opt, popn_params = load_parameters_from_file(param_fname) analytics = Analytics.init_from_file(anlt_fname) all_muts, mutation_map = load_muts_from_file(opt, mut_fname) root_clone = load_clones_from_file(opt, mutation_map, clone_fname) # construct population from parameter set, # clone tree and mutation dictionary new_popn = Population.init_from_file(opt, analytics, popn_params, root_clone, all_muts) # now delete the individual snapshot files, # as we will always load popn from an archive for fname in fnames: delete_local_file(fname) return t_curr, opt, new_popn