phylogenetics/tools/df_editor.py
def from_external(input_file,df,input_type=None):
    """
    Read a phylopandas data frame out to an external file.  If the user wrote
    the external file out using to_external and did not edit the sequence ids
    in the extenral file, whatever edits the user made to the alignment or tree
    can be read back into the source data frame using this function.

    input_file: input file.
    df: phylopandas dataframe (or copy) used to generate the external file
    input_type: what type of file to read.  if None, try to figure out from the
                file extension of the input file
    """
    in_methods_seq = {'fasta':phy.seqio.read.read_fasta,
                      'phylip':phy.seqio.read.read_phylip,
                      'clustal':phy.seqio.read.read_clustal,
                      'embl':phy.seqio.read.read_embl,
                      'nexus_seq':phy.seqio.read.read_nexus_seq,
                      'swiss':phy.seqio.read.read_swiss,
                      'fastq':phy.seqio.read.read_fastq}

    in_methods_tree = {'newick':phy.treeio.read.read_newick,
                       'nexml':phy.treeio.read.read_nexml,
                       'nexus_tree':phy.treeio.read.read_nexus_tree}

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

    if input_type is None:

        input_type = input_file.split(".")[-1].lower()

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

        try:
            in_method = in_methods_seq[input_type]
        except KeyError:

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

    try:
        in_method = in_methods_seq[input_type]
        input_class = "sequence"
    except KeyError:
        try:
            in_method = in_methods_tree[input_type]
            input_class = "parent"
        except KeyError:

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

            to_show = in_methods.keys()
            to_show.sort()
            for m in to_show:
                err += "    {}\n".format(m)

            raise ValueError(err)

    new_data = in_method(input_file)
    uid = [entry.split("_")[0] for entry in new_data.id]

    new_data = pd.DataFrame({"uid":uid,
                             input_class:new_data[input_class]})

    # Drop the old information from the original frame
    to_merge = df.copy()
    to_merge = to_merge.drop(labels=[input_class],axis=1)

    # Merge sequence information back in, this time aligned.
    output = pd.merge(to_merge, new_data, on=['uid'], how='left')

    return output

    Read a phylopandas data frame out to an external file.  If the user wrote
    the external file out using to_external and did not edit the sequence ids
    in the extenral file, whatever edits the user made to the alignment or tree
    can be read back into the source data frame using this function.

    input_file: input file.
    df: phylopandas dataframe (or copy) used to generate the external file
    input_type: what type of file to read.  if None, try to figure out from the
                file extension of the input file
    
Read a phylopandas data frame out to an external file.  If the user wrote
the external file out using to_external and did not edit the sequence ids
in the extenral file, whatever edits the user made to the alignment or tree
can be read back into the source data frame using this function.

input file.
df: phylopandas dataframe (or copy) used to generate the external file
input_type: what type of file to read.  if None, try to figure out from the
file extension of the input file
Read a phylopandas data frame out to an external file . If the user wrote the external file out using to_external and did not edit the sequence ids in the extenral file whatever edits the user made to the alignment or tree can be read back into the source data frame using this function . input file . df : phylopandas dataframe ( or copy ) used to generate the external file input_type : what type of file to read . if None try to figure out from the file extension of the input file
def from_external(input_file,df,input_type=None):
    in_methods_seq = {'fasta':phy.seqio.read.read_fasta,
                      'phylip':phy.seqio.read.read_phylip,
                      'clustal':phy.seqio.read.read_clustal,
                      'embl':phy.seqio.read.read_embl,
                      'nexus_seq':phy.seqio.read.read_nexus_seq,
                      'swiss':phy.seqio.read.read_swiss,
                      'fastq':phy.seqio.read.read_fastq}
    in_methods_tree = {'newick':phy.treeio.read.read_newick,
                       'nexml':phy.treeio.read.read_nexml,
                       'nexus_tree':phy.treeio.read.read_nexus_tree}
    file_extension_synonym = {"fa":"fasta",
                              "fna":"fasta",
                              "ffn":"fasta",
                              "faa":"fasta",
                              "fa":"fasta",
                              "frn":"fasta",
                              "aln":"clustal",
                              "phy":"phylip"}
    if input_type is None:
        input_type = input_file.split(".")[-1].lower()
        try:
            input_type = file_extension_synonym[input_type]
        except KeyError:
            pass
        try:
            in_method = in_methods_seq[input_type]
        except KeyError:
            try:
                in_method = in_methods_tree[input_type]
            except KeyError:
                err = "\nCould not recognize output type from file extension.\n"
                err += "Specify type in out_type.\n"
                raise ValueError(err)
    try:
        in_method = in_methods_seq[input_type]
        input_class = "sequence"
    except KeyError:
        try:
            in_method = in_methods_tree[input_type]
            input_class = "parent"
        except KeyError:
            err = "\n\ninput_type '{}' not recognized. Must be one of:\n"
            to_show = in_methods.keys()
            to_show.sort()
            for m in to_show:
                err += "    {}\n".format(m)
            raise ValueError(err)
    new_data = in_method(input_file)
    uid = [entry.split("_")[0] for entry in new_data.id]
    new_data = pd.DataFrame({"uid":uid,
                             input_class:new_data[input_class]})
    to_merge = df.copy()
    to_merge = to_merge.drop(labels=[input_class],axis=1)
    output = pd.merge(to_merge, new_data, on=['uid'], how='left')
    return output
def from_external ( input_file , df , input_type = None ) : in_methods_seq = { 'fasta' : phy . seqio . read . read_fasta , 'phylip' : phy . seqio . read . read_phylip , 'clustal' : phy . seqio . read . read_clustal , 'embl' : phy . seqio . read . read_embl , 'nexus_seq' : phy . seqio . read . read_nexus_seq , 'swiss' : phy . seqio . read . read_swiss , 'fastq' : phy . seqio . read . read_fastq } in_methods_tree = { 'newick' : phy . treeio . read . read_newick , 'nexml' : phy . treeio . read . read_nexml , 'nexus_tree' : phy . treeio . read . read_nexus_tree } file_extension_synonym = { "fa" : "fasta" , "fna" : "fasta" , "ffn" : "fasta" , "faa" : "fasta" , "fa" : "fasta" , "frn" : "fasta" , "aln" : "clustal" , "phy" : "phylip" } if input_type is None : input_type = input_file . split ( "." ) [ - 1 ] . lower ( ) try : input_type = file_extension_synonym [ input_type ] except KeyError : pass try : in_method = in_methods_seq [ input_type ] except KeyError : try : in_method = in_methods_tree [ input_type ] except KeyError : err = "\nCould not recognize output type from file extension.\n" err += "Specify type in out_type.\n" raise ValueError ( err ) try : in_method = in_methods_seq [ input_type ] input_class = "sequence" except KeyError : try : in_method = in_methods_tree [ input_type ] input_class = "parent" except KeyError : err = "\n\ninput_type '{}' not recognized. Must be one of:\n" to_show = in_methods . keys ( ) to_show . sort ( ) for m in to_show : err += "    {}\n" . format ( m ) raise ValueError ( err ) new_data = in_method ( input_file ) uid = [ entry . split ( "_" ) [ 0 ] for entry in new_data . id ] new_data = pd . DataFrame ( { "uid" : uid , input_class : new_data [ input_class ] } ) to_merge = df . copy ( ) to_merge = to_merge . drop ( labels = [ input_class ] , axis = 1 ) output = pd . merge ( to_merge , new_data , on = [ 'uid' ] , how = 'left' ) return output
Read a phylopandas data frame out to an external file.
Read a phylopandas data frame out to an external file .
"""
    Read a phylopandas data frame out to an external file.  If the user wrote
    the external file out using to_external and did not edit the sequence ids
    in the extenral file, whatever edits the user made to the alignment or tree
    can be read back into the source data frame using this function.

    input_file: input file.
    df: phylopandas dataframe (or copy) used to generate the external file
    input_type: what type of file to read.  if None, try to figure out from the
                file extension of the input file
    """ # Drop the old information from the original frame # Merge sequence information back in, this time aligned.
input_file
df
input_type