def write_as_mesquite(self, out, **kwargs): """ For debugging purposes, write out a Mesquite-format file. """ from dendropy.dataio import nexuswriter nw = nexuswriter.NexusWriter(**kwargs) nw.is_write_block_titles = True out.write("#NEXUS\n\n") nw.write_taxa_block(self.taxon_set, out) out.write('\n') nw.write_taxa_block(self.contained_trees.taxon_set, out) if self.contained_trees.taxon_set.label: domain_title = self.contained_trees.taxon_set.label else: domain_title = self.contained_trees.taxon_set.oid contained_taxon_set = self.contained_trees.taxon_set contained_label = self.contained_trees.label out.write('\n') self._contained_to_containing_taxon_map.write_mesquite_association_block( out) out.write('\n') nw.write_trees_block( dataobject.TreeList([self], taxon_set=self.taxon_set), out) out.write('\n') nw.write_trees_block( dataobject.TreeList(self.contained_trees, taxon_set=contained_taxon_set, label=contained_label), out) out.write('\n')
def clear(self): """ Clears all contained trees and mapped edges. """ self.contained_trees = dataobject.TreeList( taxon_set=self._contained_to_containing_taxon_map.domain_taxa) self.clear_contained_edges()
def _set_contained_trees(self, trees): if hasattr(trees, 'taxon_set'): if self._contained_taxon_set is None: self._contained_taxon_set = trees.taxon_set elif self._contained_taxon_set is not trees.taxon_set: raise ValueError( "'contained_taxon_set' of ContainingTree is not the same TaxonSet object of 'contained_trees'" ) self._contained_trees = dataobject.TreeList( trees, taxon_set=self._contained_taxon_set) if self._contained_taxon_set is None: self._contained_taxon_set = self._contained_trees.taxon_set
def _get_contained_trees(self): if self._contained_trees is None: self._contained_trees = dataobject.TreeList( taxon_set=self._contained_taxon_set) return self._contained_trees
def read_filepath_into_dataset(self, file_path): _LOG.debug("Creating MultiFormatReader") ncl_nxs_reader_handle = nclwrapper.MultiFormatReader() _LOG.debug("Setting MultiFormatReader's WarningOutput Level") ncl_nxs_reader_handle.SetWarningOutputLevel( DENDROPY_NCL_WARNING_LEVEL) _LOG.debug( "Calling MultiFormatReader.cullIdenticalTaxaBlocks(True)") ncl_nxs_reader_handle.cullIdenticalTaxaBlocks(True) if self.dataset is None: self.dataset = dataobject.DataSet() if self.attached_taxon_set is not None and len( self.attached_taxon_set) == 0: self._taxa_to_fill = self.attached_taxon_set else: self._taxa_to_fill = None if self.attached_taxon_set is not None: self._register_taxa_context(ncl_nxs_reader_handle, [self.attached_taxon_set]) _LOG.debug("Calling MultiFormatReader.ReadFilepath(%s, %s)" % (file_path, self.format)) ncl_nxs_reader_handle.ReadFilepath(file_path, self.format) _LOG.debug("Calling MultiFormatReader.GetNumTaxaBlocks()") num_taxa_blocks = ncl_nxs_reader_handle.GetNumTaxaBlocks() for i in xrange(num_taxa_blocks): _LOG.debug("Calling MultiFormatReader.GetTaxaBlock(%d)" % i) ncl_tb = ncl_nxs_reader_handle.GetTaxaBlock(i) taxa_block = self._ncl_taxa_block_to_native(ncl_tb) self.dataset.add(taxa_block) #nab = ncl_nxs_reader_handle.GetNumAssumptionsBlocks(ncl_tb) #for k in xrange(nab): # a = ncl_nxs_reader_handle.GetAssumptionsBlock(ncl_tb, k) # cs = a.GetTaxSetNames() # print "TaxSets have the names " , str(cs) _LOG.debug( "Calling MultiFormatReader.GetNumCharactersBlocks()") num_char_blocks = ncl_nxs_reader_handle.GetNumCharactersBlocks( ncl_tb) for j in xrange(num_char_blocks): _LOG.debug( "Calling MultiFormatReader.GetCharactersBlock(taxablock, %d)" % j) ncl_cb = ncl_nxs_reader_handle.GetCharactersBlock( ncl_tb, j) char_block = self._ncl_characters_block_to_native( taxa_block, ncl_cb, ncl_nxs_reader_handle) if char_block: self.dataset.add(char_block) _LOG.debug("Calling MultiFormatReader.GetNumTreesBlocks()") ntrb = ncl_nxs_reader_handle.GetNumTreesBlocks(ncl_tb) for j in xrange(ntrb): trees_block = dataobject.TreeList() trees_block.taxon_set = taxa_block _LOG.debug("Calling MultiFormatReader.GetTreesBlock(%d)" % j) ncl_trb = ncl_nxs_reader_handle.GetTreesBlock(ncl_tb, j) for k in xrange(ncl_trb.GetNumTrees()): ftd = ncl_trb.GetFullTreeDescription(k) tokens = ftd.GetTreeTokens() rooted_flag = ftd.IsRooted() t = self._ncl_tree_tokens_to_native_tree( ncl_tb, taxa_block, tokens, rooted_flag=rooted_flag) if t: trees_block.append(t) self.dataset.add(trees_block) return self.dataset