def R_to_newick(R): """ @param R: a directed topology @return: a newick string """ r = Ftree.R_to_root(R) return _v_to_newick(Ftree.R_to_v_to_sinks(R), r) + ';'
def equal_arc_layout(T, B): """ @param T: tree topology @param B: branch lengths @return: a map from vertex to location """ # arbitrarily root the tree R = Ftree.T_to_R_canonical(T) r = Ftree.R_to_root(R) # map vertices to subtree tip count v_to_sinks = Ftree.R_to_v_to_sinks(R) v_to_count = {} for v in Ftree.R_to_postorder(R): sinks = v_to_sinks.get(v, []) if sinks: v_to_count[v] = sum(v_to_count[sink] for sink in sinks) else: v_to_count[v] = 1 # create the equal arc angles v_to_theta = {} _force_equal_arcs( v_to_sinks, v_to_count, v_to_theta, r, -math.pi, math.pi) # convert angles to coordinates v_to_source = Ftree.R_to_v_to_source(R) v_to_location = {} _update_locations( R, B, v_to_source, v_to_sinks, v_to_theta, v_to_location, r, (0, 0), 0) return v_to_location
def equal_daylight_layout(T, B, iteration_count): """ @param T: topology @param B: branch lengths """ R = Ftree.T_to_R_canonical(T) r = Ftree.R_to_root(R) # create the initial equal arc layout v_to_location = equal_arc_layout(T, B) # use sax-like events to create a parallel tree in the C extension v_to_sinks = Ftree.R_to_v_to_sinks(R) v_to_dtree_id = {} dtree = day.Day() count = _build_dtree( dtree, r, v_to_sinks, v_to_location, v_to_dtree_id, 0) # repeatedly reroot and equalize v_to_neighbors = Ftree.T_to_v_to_neighbors(T) for i in range(iteration_count): for v in Ftree.T_to_inside_out(T): neighbor_count = len(v_to_neighbors[v]) if neighbor_count > 2: dtree.select_node(v_to_dtree_id[v]) dtree.reroot() dtree.equalize() # extract the x and y coordinates from the dtree v_to_location = {} for v, dtree_id in v_to_dtree_id.items(): dtree.select_node(dtree_id) x = dtree.get_x() y = dtree.get_y() v_to_location[v] = (x, y) return v_to_location
def RB_to_newick(R, B): """ @param R: a directed topology @param B: branch lengths @return: a newick string """ r = Ftree.R_to_root(R) v_to_source = Ftree.R_to_v_to_source(R) v_to_sinks = Ftree.R_to_v_to_sinks(R) return _Bv_to_newick(v_to_source, v_to_sinks, B, r) + ';'
def get_response_content(fs): # read the tree T, B, N = FtreeIO.newick_to_TBN(fs.tree) leaves = Ftree.T_to_leaves(T) internal = Ftree.T_to_internal_vertices(T) # get the distinguished vertex of articulation r = get_unique_vertex(N, fs.vertex) if r not in internal: raise ValueError( 'the distinguished vertex should have degree at least two') # Partition the leaves with respect to the given root. # Each set of leaves will eventually define a connected component. R = Ftree.T_to_R_specific(T, r) v_to_sinks = Ftree.R_to_v_to_sinks(R) # break some edges R_pruned = set(R) neighbors = Ftree.T_to_v_to_neighbors(T)[r] for adj in neighbors: R_pruned.remove((r, adj)) T_pruned = Ftree.R_to_T(R_pruned) # get the leaf partition ordered_leaves = [] leaf_lists = [] for adj in neighbors: R_subtree = Ftree.T_to_R_specific(T_pruned, adj) C = sorted(b for a, b in R_subtree if b not in v_to_sinks) ordered_leaves.extend(C) leaf_lists.append(C) # define the vertices to keep and those to remove keepers = ordered_leaves + [r] # get the schur complement L_schur = Ftree.TB_to_L_schur(T, B, keepers) # get principal submatrices of the schur complement principal_matrices = [] accum = 0 for component_leaves in leaf_lists: n = len(component_leaves) M = L_schur[accum:accum+n, accum:accum+n] principal_matrices.append(M) accum += n # write the report out = StringIO() print >> out, 'algebraic connectivity:' print >> out, get_algebraic_connectivity(T, B, leaves) print >> out print >> out print >> out, 'perron values:' print >> out for M, leaf_list in zip(principal_matrices, leaf_lists): value = scipy.linalg.eigh(M, eigvals_only=True)[0] name_list = [N[v] for v in leaf_list] print >> out, name_list print >> out, value print >> out return out.getvalue()
def RBN_to_newick(R, B, N): """ @param R: a directed topology @param B: branch lengths @param N: map from vertices to names @return: a newick string """ r = Ftree.R_to_root(R) v_to_source = Ftree.R_to_v_to_source(R) v_to_sinks = Ftree.R_to_v_to_sinks(R) return _BNv_to_newick(v_to_source, v_to_sinks, B, N, r) + ';'
def sample_brownian_motion(R, B): """ Sample brownian motion on a tree. @param R: directed tree @param B: branch lengths @return: map from vertex to sample """ r = Ftree.R_to_root(R) v_to_sample = {r: 0} v_to_sinks = Ftree.R_to_v_to_sinks(R) for v in Ftree.R_to_preorder(R): for sink in v_to_sinks[v]: u_edge = frozenset((v, sink)) mu = v_to_sample[v] var = B[u_edge] v_to_sample[sink] = random.gauss(mu, math.sqrt(var)) return v_to_sample