def _debug_dummy_plot( taxonomy: Taxonomy, htmlfile: Filename, scoring: Scoring = Scoring.SHEL, ): """ Generate dummy Krona plot via Krona 2.0 XML spec and exit """ print(gray(f'Generating dummy Krona plot {htmlfile}...'), end='') sys.stdout.flush() samples: List[Sample] = [ Sample('SINGLE'), ] krona: KronaTree = KronaTree( samples, min_score=Score(35), max_score=Score(100), scoring=scoring, ) polytree: MultiTree = MultiTree(samples=samples) polytree.grow(ontology=taxonomy) polytree.toxml(ontology=taxonomy, krona=krona) krona.tohtml(htmlfile, pretty=True) print(green('OK!'))
def generate_krona(): """Generate Krona plot with all the results via Krona 2.0 XML spec""" print(gray('\nBuilding the taxonomy multiple tree... '), end='') sys.stdout.flush() krona: KronaTree = KronaTree( samples, num_raw_samples=len(raw_samples), stats=stats, min_score=Score( min([ min(scores[sample].values()) for sample in samples if len(scores[sample]) ])), max_score=Score( max([ max(scores[sample].values()) for sample in samples if len(scores[sample]) ])), scoring=scoring, ) polytree.grow(ontology=ncbi, abundances=counts, accs=accs, scores=scores) print(green('OK!')) print(gray('Generating final plot (') + magenta(htmlfile) + gray(')... '), end='') sys.stdout.flush() polytree.toxml(ontology=ncbi, krona=krona) krona.tohtml(htmlfile, pretty=False) print(green('OK!'))
def toxml( self, taxonomy: Taxonomy, krona: KronaTree, node: Elm = None, ) -> None: """ Recursive method to generate XML. Args: taxonomy: Taxonomy object. krona: Input/Output KronaTree object to be populated. node: Base node (None to use the root of krona argument). Returns: None """ for tid in self: if node is None: node = krona.getroot() num_samples = len(self.samples) new_node: Elm = krona.node( parent=node, name=taxonomy.get_name(tid), values={ COUNT: { self.samples[i]: str(self[tid].accs[i]) for i in range(num_samples) }, UNASSIGNED: { self.samples[i]: str(self[tid].counts[i]) for i in range(num_samples) }, TID: str(tid), RANK: taxonomy.get_rank(tid).name.lower(), SCORE: { self.samples[i]: (f'{self[tid].score[i]:.1f}' if self[tid].score[i] != NO_SCORE else '0') for i in range(num_samples) }, }) if self[tid]: self[tid].toxml(taxonomy=taxonomy, krona=krona, node=new_node)
def toxml(self, taxonomy: Taxonomy, krona: KronaTree, node: Elm = None, mindepth: int = 0, maxdepth: int = 0, include: Union[Tuple, Set[TaxId]] = (), exclude: Union[Tuple, Set[TaxId]] = (), _in_branch: bool = False) -> None: """ Recursively convert to XML between min and max depth levels. Args: taxonomy: Taxonomy object. krona: Input/Output KronaTree object to be populated. node: Base node (None to use the root of krona argument). mindepth: 0 gets the taxa from the very beginning depth. maxdepth: 0 does not stop the search at any depth level. include: contains the root taxid of the subtrees to be included. If it is empty (default) all the taxa is included (except explicitly excluded). exclude: contains the root taxid of the subtrees to be excluded _in_branch: is like a static variable to tell the recursive function that it is in a subtree that is a branch of a taxon in the include list. Returns: None """ mindepth -= 1 if maxdepth != 1: maxdepth -= 1 for tid in self: in_branch: bool = ( ( _in_branch or # called with flag activated? or not include or # include by default? or (tid in include)) # tid is to be included? and tid not in exclude # and not in exclude list ) new_node: Elm if mindepth <= 0 and in_branch: if node is None: node = krona.getroot() new_node = krona.node( node, taxonomy.get_name(tid), { COUNT: { krona.samples[0]: str(self[tid].acc) }, UNASSIGNED: { krona.samples[0]: str(self[tid].counts) }, TID: str(tid), RANK: taxonomy.get_rank(tid).name.lower(), SCORE: { krona.samples[0]: str(self[tid].score) } }) if self[tid]: self[tid].toxml(taxonomy, krona, new_node, mindepth, maxdepth, include, exclude, in_branch)