Package: tralda
traldaA Python library fortreealgorithms anddata structures.InstallationThe package requires Python 3.7 or higher.Easy installation with pipThetraldapackage is available onPyPI:pip install traldaFor details about how to install Python packages seehere.Installation with the setup fileAlternatively, you can download or clone the repo, go to the root folder of package and install it using the command:python setup.py installDependenciesThe package has several dependencies (which are installed automatically when usingpip):NetworkXNumpyUsage and descriptionTree data structureThe classTreeimplements the tree data structure which is essential for most of the modules in the package and can be imported from the subpackagetralda.datastructures.
It provides methods for tree traversals and manipulation, output in Newick format, as well as the linear-time computation of last common ancestors by Bender & Farach-Colton (classLCAwhich is initialized with an instance of typeTree).Treeinstances can be serialized in pickle or json format.Overview of the functions of the class TreeNode: (Click to expand)FunctionDescriptionattributes()generator for the node attributesadd_child(child_node)addchild_nodeas a childadd_child_right_of(child_node, right_of)addchild_nodeas a child to the right ofright_ofremove_child(child_node)remove the childchild_nodedetach()remove the node from its parent's childrenis_leaf()check if the node is a leafchild_subsequence(left_node, right_node)list of children betweenleft_nodeandright_nodeOverview of the functions of the class Tree: (Click to expand)FunctionDescriptionleaves()generator for the leaf nodespreorder()generator for preorder (=top-down) traversalpostorder()generator for postorder (=bottom-up) traversalinner_vertices()generator for inner nodesedges()generator for the edges of the treeeuler_generator()generator for an Euler tourleaf_dict()compute thelistof leaf nodes in the subtree of each node, and return them as adictcontract(edges)contract all edges in the collectionedgesget_triples()return a list of all triples that are displayed by the treedelete_and_reconnect(node)deletenodeand reconnect its children to its parentcopy()construct a copy of the tree (node attributes are only copied as references, but mutable data types should be avoided as node attribute values)to_newick()return astrrepresentation of the tree in Newick formatrandom_tree(N, binary=False)return a random tree withNleaves that is optionally forced to be binary; new children are stepwise attached to randomly selected nodes untilNare reachedto_nx()return a NetworkXDiGraphversion of the tree (with the ids of theTreeNodeinstances as nodes) and itsroot(also represented by the id)parse_nx(G, root)convert a tree encoded as a NetworkXDiGraph(together with theroot) back into aTreeserialize(filename, mode=None)serialize a tree in JSON or pickle format specified bymode; default isNone, in which case the mode is inferred from the filename endingload(filename, mode=None)load a tree from file in JSON or pickle format specified bymode; default isNone, in which case the mode is inferred from the filename endingis_binary()check if the tree is binaryis_phylogenetic()check if the tree is phylogenetic (all inner nodes have at least one child)equal_topology(other)check whether this tree andotherhave the same topology based on the leaves'labelattributesis_refinementcheck whether this tree refinesotherbased on the leaves'labelattributesOverview of the functions of the class LCA: (Click to expand)FunctionDescriptionget(a, b)get the last common ancestor of nodes a and bdisplays_triple(a, b, c)check whether the triple abare_comparable(u, v)check whetheruandvare comparable in terms of the ancestor relationancestor_or_equal(u, v)check whetheruis equal to or an ancestor ofvancestor_not_equal(u, v)check whetheruis a strict ancestor ofvdescendant_or_equal(u, v)check whetheruis equal to or a descendant ofvdescendant_not_equal(u, v)check whetheruis a strict descendant ofvconsistent_triples(triples)listwith the subset oftriplesthat are displayed by the treeconsistent_triple_generatorgenerator for the items intriplesthat are displayedExample usage: (Click to expand)from tralda.datastructures import Tree, LCA

# construct a random tree with 20 leaves
tree = Tree.random_tree(20)

# serialization, reload via Tree.load('path/to/file.json')
tree.serialize('path/to/file.json')

# linear-time processing of the tree for constant-time
# last common ancestor queries
lca_T = LCA(tree)

# l.c.a. queries via 'TreeNode' instances or labels (if the nodes
# in the tree have the label attribute set)
print( lca_T(4, 7) )

# triple queries (e.g. is 3 5 | 2 displayed?)
print( lca_T.displays_triple(3, 5, 2) )Supertree computationThe subpackagetralda.supertreeimplements a number of algorithms for the computation of supertrees:BUILD (Aho et al. 1981), classBuildor functionBUILD_supertreeBuildST (Deng & Fernández-Baca 2016), classBuildSTor functionbuild_stLoose_Cons_Tree (Jansson et al. 2016), classLooseConsensusTreeor functionloose_consensus_treeLinCR (Schaller et al. 2021), classLinCRor functionlinear_common_refinementThe latter two algorithms compute the loose consensus tree and the common refinement, resp., for a sequence of trees on the same set of leaves in linear time.Cographs and cotreesThe subpackagetralda.cographcontains an efficient algorithm for cograph recognition and heuristics for cograph editing:functionto_cotreerecognizes cographs and returns aTreerepresentation in the positive case (Corneil et al. 1985)functionedit_to_cographedits an arbitrary graph to a cograph (algorithm from Crespelle 2019) and returns aTreerepresentationOther data structuresThe following auxiliary data structures can be imported from the subpackagetralda.datastructures:linked list: classLinkedListdoubly-linked list: classDoublyLinkedListHDT dynamic graph data structure (Holm, de Lichtenberg & Thorup in 2001): classHDTGraphAVL trees: classesTreeSetandTreeDictimplement data structures for sorted sets and dictionaries, respectivelyCitation and referencesIf you usetraldain your project or code from it, please consider citing:Schaller, D., Hellmuth, M., Stadler, P.F. (2021) A Simple Linear-Time Algorithm for the Common Refinement of Rooted Phylogenetic Trees on a Common Leaf Set.Additional references to algorithms that were implemented are given in the source code.Please report any bugs and questions in theIssuessection.
Also, feel free to make suggestions for improvement and/or new functionalities.