def _write_consensus_strings(self, output):
        '''
        Writes the taxonomy of each leaf to a file. If the leaf has no
        taxonomy, a taxonomy string will be created using the annotations
        provided to the ancestor nodes of that leaf (meaning, it will be
        decorated).

        Parameters
        ----------
        output    : string
            File to which the taxonomy strings for each leaf in the tree will
            be written in Greengenes format, e.g.
                637960147    mcrA; Euryarchaeota_mcrA; Methanomicrobia
                637699780    mcrA; Euryarchaeota_mcrA; Methanomicrobia
        '''

        logging.info("Writing decorated taxonomy to file: %s" % (output))

        with open(output, 'w') as out:

            for tip in self.tree.leaf_nodes():
                tax_name = tip.taxon.label.replace(" ", "_")

                if tip.taxon.label in self.taxonomy:
                    tax_string = '; '.join(self.taxonomy[tax_name])
                else:
                    ancestor_list = []
                    for ancestor in tip.ancestor_iter():
                        if ancestor.label:
                            split_node_name = ancestor.label.split(':')
                            if len(split_node_name) == 2:
                                ancestor_list+=list(reversed(split_node_name[1].split('; ')))
                            elif len(split_node_name) == 1:
                                try:
                                    float(split_node_name[0])
                                except ValueError:
                                    ancestor_list+=list(reversed(split_node_name[0].split('; ')))
                            else:
                                raise Exception("Malformed node name: %s" % ancestor.label)
                    tax_list = list(reversed(ancestor_list))

                    if len(tax_list) < 1:
                        logging.warning("No taxonomy found for species %s!" % (tax_name))
                        tax_string = "Unknown"
                    else:
                        tax_string = '; '.join(tax_list)

                output_line = "%s\t%s\n" % (tax_name, tax_string)
                out.write(output_line)
def _write_consensus_strings ( self , output ) : logging . info ( "Writing decorated taxonomy to file: %s" % ( output ) ) with open ( output , 'w' ) as out : for tip in self . tree . leaf_nodes ( ) : tax_name = tip . taxon . label . replace ( " " , "_" ) if tip . taxon . label in self . taxonomy : tax_string = '; ' . join ( self . taxonomy [ tax_name ] ) else : ancestor_list = [ ] for ancestor in tip . ancestor_iter ( ) : if ancestor . label : split_node_name = ancestor . label . split ( ':' ) if len ( split_node_name ) == 2 : ancestor_list += list ( reversed ( split_node_name [ 1 ] . split ( '; ' ) ) ) elif len ( split_node_name ) == 1 : try : float ( split_node_name [ 0 ] ) except ValueError : ancestor_list += list ( reversed ( split_node_name [ 0 ] . split ( '; ' ) ) ) else : raise Exception ( "Malformed node name: %s" % ancestor . label ) tax_list = list ( reversed ( ancestor_list ) ) if len ( tax_list ) < 1 : logging . warning ( "No taxonomy found for species %s!" % ( tax_name ) ) tax_string = "Unknown" else : tax_string = '; ' . join ( tax_list ) output_line = "%s\t%s\n" % ( tax_name , tax_string ) out . write ( output_line )
Writes the taxonomy of each leaf to a file. If the leaf has no
        taxonomy, a taxonomy string will be created using the annotations
        provided to the ancestor nodes of that leaf (meaning, it will be
        decorated).

        Parameters
        ----------
        output    : string
            File to which the taxonomy strings for each leaf in the tree will
            be written in Greengenes format, e.g.
                637960147    mcrA; Euryarchaeota_mcrA; Methanomicrobia
                637699780    mcrA; Euryarchaeota_mcrA; Methanomicrobia