Example #1
0
    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)
Example #2
0
    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)