Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • f1ee731
  • /
  • parsers
  • /
  • phylogeny_parser.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:4406ae7df35932af202ae1defe2d9bfd7f7a263a
directory badge
swh:1:dir:e61959ad932de6bf239a8d80bb8d34b03d3e5262

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
phylogeny_parser.py
#(c) 2013-2014 by Authors
#This file is a part of Ragout program.
#Released under the BSD license (see LICENSE file)

"""
This module parses newick string and contains some helper function
to deal with trees
"""

import newick
import newick.parser
from newick.tree import Tree, Leaf

class PhyloException(Exception):
    pass


class _RagoutTreeBuilder(newick.parser.AbstractHandler):
    """
    A custom parser handler for newick library
    """
    def __init__(self):
        self.stack = []
        self.root = None

    def new_tree_begin(self):
        t = Tree()
        if len(self.stack) == 0:
            self.root = t
        self.stack.append(t)
        self.stack[-1].terminal = False

    def new_tree_end(self, identifier = None):
        self.stack[-1].identifier = identifier

    def new_edge(self,bootstrap,length):
        n = self.stack.pop()
        if length is None:
            length = 1
        self.stack[-1].add_edge((n,bootstrap,length))

    def new_leaf(self,l):
        if len(self.stack) == 0:        # special case of singleton tree
            self.root = l
        self.stack.append(Leaf(l))
        self.stack[-1].terminal = True

    def get_result(self):
        return self.root


def parse_tree(newick_str):
    tree = None
    try:
        tree = newick.parser.parse(newick_str, _RagoutTreeBuilder())
    except newick.lexer.LexerError as e:
        raise PhyloException("Error parsing tree")
    return tree


def get_leaves_names(newick_str):
    """
    Get names of treminal tree nodes
    """
    tree = parse_tree(newick_str)
    if tree is None:
        return None

    return list(map(lambda n: n.identifier, tree.get_leaves()))

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API