Coverage for lingpy/convert/graph.py : 99%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Conversion routines for the GML format. """
"""Helper function converts networkx graph to igraph graph object.""" i, Name=node, **{a: b for a, b in data.items() if a not in [b'Name', b'name', 'Name', 'name']})
graph.vs[edge.target]['name'], **edge.attributes())
util.write_text_file(filename + '.gml', nx.generate_gml(graph)) return
gls, graph, tree, filename='', ): """ Create GML-representation of a given gain-loss-scenario (GLS).
Parameters ---------- gls : list A list of tuples, indicating the origins of characters along a tree. graph : networkx.graph A graph that serves as a template for the plotting of the GLS. tree : cogent.tree.PhyloNode A tree object. """ # check for tree-formatting
# create a mapper for the ids and the string-names
# create a graph
# sort the gls according to the number of tips gls, key=lambda x: len(tree.getNodeMatchingName(x[0]).tips()), reverse=True )
# set the basic event frame, depending on the state of the root else:
# let all nodes inherit these parameters
# assign the root as starting point
# iterate over the nodes involved in change and assign the values to their # children else:
# get the names of the descendant nodes in the subtree
# iterate over all nodes to change
# change the size of the root of the subtree
# add the edges to the tree # for computers with new networkx version # if 'label' not in data:
treefile, filename='', ): """ Function converts a tree in newick format to a network in gml-format.
treefile : str Either a str defining the path to a file containing the tree in Newick-format, or the tree-string itself. filename : str (default='lingpy') The name of the output GML-file. If filename is set to c{None}, the function returns a :py:class:`~networkx.Graph`.
Returns ------- graph : networkx.Graph
"""
# create an empty graph
# load the tree else:
# get the node names of the tree
# get taxa for convenience
# iterate over the nodes and add them and the edges to the graph
# add the node (just as a precaution) else:
# get the parent of the node
# add the edge if the parent is not None
treestring, change=lambda x: x ** 1.75, degree=100, filename='', start=0, root='root' ): """ Function calculates a simple radial tree layout.
Parameters ---------- treefile : str Either a str defining the path to a file containing the tree in Newick-format, or the tree-string itself. filename : str (default=None) The name of the output file (GML-format). If set to c{None}, no output will be written to file. change : function (default = lambda x:2 * x**2) The function used to modify the radius in the polar projection of the tree.
Returns ------- graph : networkx.Graph A graph representation of the tree with coordinates specified in the graphics-attribute of the nodes.
Notes -----
This function creates a radial tree-layout from a given tree specified in Newick format.
""" # calculate the factor for projection from the degree
# get starting factor
# calculate the projection (should be centered) else:
# define private function for centering of nodes
# first sort all values since we need max and min of the theta values
# get minimum and maximum
# calculate the new coordinates, the radius is simply decreased by 1
# the theta-value is calculated by the following formula
# get the tree else:
# get the leaves
# get the paths in order to find out the radius of the tree
# get the max path
# get the initial coordinates
# assign leaves to queue
# make the visited list
# start the loop
# get the node
# increase the dimension by 1
else:
# get the parent and all children tree.getNodeMatchingName(node).Parent.Children]
# iterate over children else:
# goon, if this is possible else:
[coords[child] for child in children] ) else:
# convert tree to graph
# iterate over the graph and assign the data
# change coordinates
# get angle for text-rotation in degrees
# derive zorder from angle
# check for specific parts where the angle has to be adapted else:
# assign the data to the graph 'x': xN, 'y': yN, 'z': z, 'angle': angle, 's': s, 'zorder': int(zorder) }
# don't forget the label
|