def save_enriched_motifs(df, fname: str) -> None: """ Save enriched motifs. Supported file formats are CSV, TSV, GMT, DAT (pickle), JSON or YAML. :param df: :param fname: :return: """ extension = PurePath(fname).suffixes if is_valid_suffix(extension, 'ctx'): df.to_csv(fname, sep=suffixes_to_separator(extension)) else: regulons = df2regulons(df) if '.json' in extension: name2targets = { r.name: list(r.gene2weight.keys()) for r in regulons } with openfile(fname, 'w') as f: f.write(json.dumps(name2targets)) elif '.dat' in extension: with openfile(fname, 'wb') as f: pickle.dump(regulons, f) elif '.gmt' in extension: GeneSignature.to_gmt(fname, regulons) elif is_valid_suffix(extension, 'ctx_yaml'): save_to_yaml(regulons, fname) else: raise ValueError("Unknown file format \"{}\".".format(fname))
def save_enriched_motifs(df, fname: str) -> None: """ Save enriched motifs. Supported file formats are CSV, TSV, GMT, DAT (pickle), JSON or YAML. :param df: :param fname: :return: """ extension = os.path.splitext(fname)[1].lower() if extension in FILE_EXTENSION2SEPARATOR.keys(): df.to_csv(fname, sep=FILE_EXTENSION2SEPARATOR[extension]) else: regulons = df2regulons(df) if extension == '.json': name2targets = { r.name: list(r.gene2weight.keys()) for r in regulons } with open(fname, 'w') as f: f.write(json.dumps(name2targets)) elif extension == '.dat': with open(fname, 'wb') as f: pickle.dump(regulons, f) elif extension == '.gmt': GeneSignature.to_gmt(fname, regulons) elif extension in {'.yaml', '.yml'}: save_to_yaml(regulons, fname) else: raise ValueError("Unknown file format \"{}\".".format(fname))
modules, MOTIF_ANNOTATIONS_FNAME, client_or_address="custom_multiprocessing", num_workers=nCores) print("DEFINED regulons") else: if calcRegulonsWithIntermediateDf: df = prune2df(dbs, modules, MOTIF_ANNOTATIONS_FNAME) print("defined df") #regulons = df2regulons(df, NOMENCLATURE) regulons = df2regulons(df) else: regulons = prune(dbs, modules, MOTIF_ANNOTATIONS_FNAME) print("DEFINED regulons, type:") print(type(regulons)) #save regulons as binary and as text file save_to_yaml(regulons, REGULONS_BIN_FNAME) regulons_txt = open(REGULONS_FNAME, "w") regulons_txt.write(str(regulons)) regulons_txt.close() print("SAVED regulons") regulons = load_from_yaml(REGULONS_BIN_FNAME) print("LOADED regulons, type:") print(type(regulons)) #print(regulons)