Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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))