phylogenetics/tools/df_editor.py
def to_external(out_file,df,id_col=None,out_type=None,overwrite=False):
    """
    Write a phylopandas data frame out to an external file.  If the user does
    not edit the sequence ids in the extenral file, whatever edits the user
    makes to the alignment or tree can be read back into the source data frame
    using the from_external function.

    out_file: output file.
    df: phylopandas dataframe
    id_col: id column to use as the id for each sequence
    out_type: what type of file to write.  if None, try to figure out from the
              file extension of the output file
    overwrite: overwrite an existing output file.
    """

    seq_out_methods = {'fasta':phy.seqio.write.to_fasta,
                       'phylip':phy.seqio.write.to_phylip,
                       'clustal':phy.seqio.write.to_clustal,
                       'embl':phy.seqio.write.to_embl,
                       'nexus_seq':phy.seqio.write.to_nexus_seq,
                       'swiss':phy.seqio.write.to_swiss,
                       'fastq':phy.seqio.write.to_fastq}

    tree_out_methods = {'newick':phy.treeio.write.to_newick,
                        'nexml':phy.treeio.write.to_nexml,
                        'nexus_tree':phy.treeio.write.to_nexus_tree}

    file_extension_synonym = {"fa":"fasta",
                              "fna":"fasta",
                              "ffn":"fasta",
                              "faa":"fasta",
                              "fa":"fasta",
                              "frn":"fasta",
                              "aln":"clustal",
                              "phy":"phylip"}

    # See if the output file exists
    if os.path.isfile(out_file) and not overwrite:
        err = "out_file '{}' already exists.\n".format(out_file)
        raise FileExistsError(err)

    if out_type is None:

        out_type = out_file.split(".")[-1].lower()

        try:
            out_type = file_extension_synonym[out_type]
        except KeyError:
            pass

        try:
            out_method = seq_out_methods[out_type]
        except:
            try:
                out_method = tree_out_methods[out_type]
            except KeyError:
                err = "\nCould not recognize output type from file extension.\n"
                err += "Specify type in out_type.\n"
                raise ValueError(err)

    try:
        out_method = seq_out_methods[out_type]
        write_type = "seq"
    except:
        try:
            out_method = tree_out_methods[out_type]
            write_type = "tree"

        except KeyError:
            err = "\n\nout_type '{}' not recognized. Must be one of:\n"

            to_show = list(seq_out_methods.keys())
            to_show.extend(tree_out_methods.keys())
            to_show.sort()
            for m in to_show:
                err += "    {}\n".format(m)

        raise ValueError(err)

    if id_col is None:
        id_col = "uid"

    df_to_write = df.copy()
    to_write_col = df[id_col]
    if id_col != "uid":
        to_write_col = ["{}_{}".format(df_to_write.uid[i],to_write_col[i])
                        for i in range(len(to_write_col))]
    df_to_write["to_write_col"] = to_write_col

    if write_type == "seq":
        out_method(df_to_write,id_col="to_write_col",filename=out_file)
    elif write_type == "tree":
        out_method(df_to_write,taxon_col="to_write_col",filename=out_file)
    else:
        pass

    Write a phylopandas data frame out to an external file.  If the user does
    not edit the sequence ids in the extenral file, whatever edits the user
    makes to the alignment or tree can be read back into the source data frame
    using the from_external function.

    out_file: output file.
    df: phylopandas dataframe
    id_col: id column to use as the id for each sequence
    out_type: what type of file to write.  if None, try to figure out from the
              file extension of the output file
    overwrite: overwrite an existing output file.
    
Write a phylopandas data frame out to an external file.  If the user does
not edit the sequence ids in the extenral file, whatever edits the user
makes to the alignment or tree can be read back into the source data frame
using the from_external function.

output file.
df: phylopandas dataframe
id_col: id column to use as the id for each sequence
out_type: what type of file to write.  if None, try to figure out from the
file extension of the output file
overwrite: overwrite an existing output file.
Write a phylopandas data frame out to an external file . If the user does not edit the sequence ids in the extenral file whatever edits the user makes to the alignment or tree can be read back into the source data frame using the from_external function . output file . df : phylopandas dataframe id_col : id column to use as the id for each sequence out_type : what type of file to write . if None try to figure out from the file extension of the output file overwrite : overwrite an existing output file .
def to_external(out_file,df,id_col=None,out_type=None,overwrite=False):
    seq_out_methods = {'fasta':phy.seqio.write.to_fasta,
                       'phylip':phy.seqio.write.to_phylip,
                       'clustal':phy.seqio.write.to_clustal,
                       'embl':phy.seqio.write.to_embl,
                       'nexus_seq':phy.seqio.write.to_nexus_seq,
                       'swiss':phy.seqio.write.to_swiss,
                       'fastq':phy.seqio.write.to_fastq}
    tree_out_methods = {'newick':phy.treeio.write.to_newick,
                        'nexml':phy.treeio.write.to_nexml,
                        'nexus_tree':phy.treeio.write.to_nexus_tree}
    file_extension_synonym = {"fa":"fasta",
                              "fna":"fasta",
                              "ffn":"fasta",
                              "faa":"fasta",
                              "fa":"fasta",
                              "frn":"fasta",
                              "aln":"clustal",
                              "phy":"phylip"}
    if os.path.isfile(out_file) and not overwrite:
        err = "out_file '{}' already exists.\n".format(out_file)
        raise FileExistsError(err)
    if out_type is None:
        out_type = out_file.split(".")[-1].lower()
        try:
            out_type = file_extension_synonym[out_type]
        except KeyError:
            pass
        try:
            out_method = seq_out_methods[out_type]
        except:
            try:
                out_method = tree_out_methods[out_type]
            except KeyError:
                err = "\nCould not recognize output type from file extension.\n"
                err += "Specify type in out_type.\n"
                raise ValueError(err)
    try:
        out_method = seq_out_methods[out_type]
        write_type = "seq"
    except:
        try:
            out_method = tree_out_methods[out_type]
            write_type = "tree"
        except KeyError:
            err = "\n\nout_type '{}' not recognized. Must be one of:\n"
            to_show = list(seq_out_methods.keys())
            to_show.extend(tree_out_methods.keys())
            to_show.sort()
            for m in to_show:
                err += "    {}\n".format(m)
        raise ValueError(err)
    if id_col is None:
        id_col = "uid"
    df_to_write = df.copy()
    to_write_col = df[id_col]
    if id_col != "uid":
        to_write_col = ["{}_{}".format(df_to_write.uid[i],to_write_col[i])
                        for i in range(len(to_write_col))]
    df_to_write["to_write_col"] = to_write_col
    if write_type == "seq":
        out_method(df_to_write,id_col="to_write_col",filename=out_file)
    elif write_type == "tree":
        out_method(df_to_write,taxon_col="to_write_col",filename=out_file)
    else:
        pass
def to_external ( out_file , df , id_col = None , out_type = None , overwrite = False ) : seq_out_methods = { 'fasta' : phy . seqio . write . to_fasta , 'phylip' : phy . seqio . write . to_phylip , 'clustal' : phy . seqio . write . to_clustal , 'embl' : phy . seqio . write . to_embl , 'nexus_seq' : phy . seqio . write . to_nexus_seq , 'swiss' : phy . seqio . write . to_swiss , 'fastq' : phy . seqio . write . to_fastq } tree_out_methods = { 'newick' : phy . treeio . write . to_newick , 'nexml' : phy . treeio . write . to_nexml , 'nexus_tree' : phy . treeio . write . to_nexus_tree } file_extension_synonym = { "fa" : "fasta" , "fna" : "fasta" , "ffn" : "fasta" , "faa" : "fasta" , "fa" : "fasta" , "frn" : "fasta" , "aln" : "clustal" , "phy" : "phylip" } if os . path . isfile ( out_file ) and not overwrite : err = "out_file '{}' already exists.\n" . format ( out_file ) raise FileExistsError ( err ) if out_type is None : out_type = out_file . split ( "." ) [ - 1 ] . lower ( ) try : out_type = file_extension_synonym [ out_type ] except KeyError : pass try : out_method = seq_out_methods [ out_type ] except : try : out_method = tree_out_methods [ out_type ] except KeyError : err = "\nCould not recognize output type from file extension.\n" err += "Specify type in out_type.\n" raise ValueError ( err ) try : out_method = seq_out_methods [ out_type ] write_type = "seq" except : try : out_method = tree_out_methods [ out_type ] write_type = "tree" except KeyError : err = "\n\nout_type '{}' not recognized. Must be one of:\n" to_show = list ( seq_out_methods . keys ( ) ) to_show . extend ( tree_out_methods . keys ( ) ) to_show . sort ( ) for m in to_show : err += "    {}\n" . format ( m ) raise ValueError ( err ) if id_col is None : id_col = "uid" df_to_write = df . copy ( ) to_write_col = df [ id_col ] if id_col != "uid" : to_write_col = [ "{}_{}" . format ( df_to_write . uid [ i ] , to_write_col [ i ] ) for i in range ( len ( to_write_col ) ) ] df_to_write [ "to_write_col" ] = to_write_col if write_type == "seq" : out_method ( df_to_write , id_col = "to_write_col" , filename = out_file ) elif write_type == "tree" : out_method ( df_to_write , taxon_col = "to_write_col" , filename = out_file ) else : pass
Write a phylopandas data frame out to an external file.
Write a phylopandas data frame out to an external file .
"""
    Write a phylopandas data frame out to an external file.  If the user does
    not edit the sequence ids in the extenral file, whatever edits the user
    makes to the alignment or tree can be read back into the source data frame
    using the from_external function.

    out_file: output file.
    df: phylopandas dataframe
    id_col: id column to use as the id for each sequence
    out_type: what type of file to write.  if None, try to figure out from the
              file extension of the output file
    overwrite: overwrite an existing output file.
    """ # See if the output file exists
out_file
df
id_col
out_type
overwrite