def get_response_content(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: the response """ # get a properly formatted newick tree with branch lengths T, B, N = FtreeIO.newick_to_TBN(fs.tree) # get the vertex valuations all_valuations = TB_to_harmonic_valuations(T, B) valuations = all_valuations[fs.first_index:] nfigures = (fs.last_index - fs.first_index) + 1 # do the layout if fs.equal_arc_layout: v_to_location = FtreeAux.equal_arc_layout(T, B) elif fs.equal_daylight_layout: v_to_location = FtreeAux.equal_daylight_layout(T, B, 3) # draw the image physical_size = (fs.width, fs.height) tikzpicture = DrawEigenLacing.get_forest_image_ftree( T, B, N, v_to_location, physical_size, valuations, nfigures, fs.inner_margin, fs.reflect_trees, fs.show_vertex_labels, fs.show_subfigure_labels) packages = [] preamble = '\\usetikzlibrary{snakes}' return tikz.get_figure_response( tikzpicture, fs.tikzformat, g_figure_caption, g_figure_label, packages, preamble)
def get_tikz_tree(self, layout_mode): """ Return the tikz text for the tree figure. @param layout_mode: equal_arc vs equal_daylight @return: tikz text """ if layout_mode == 'equal_daylight': v_to_point = FtreeAux.equal_daylight_layout(self.T, self.B, 3) else: v_to_point = FtreeAux.equal_arc_layout(self.T, self.B) # define vertices vertex_lines = [] for v, (x, y) in v_to_point.items(): line = get_vertex_line(v, x, y) vertex_lines.append(line) # draw edges edge_lines = [] for va, vb in self.T: line = get_edge_line(va, vb) edge_lines.append(line) # draw the labels label_lines = [] if self.label_mode == 'label_mode_show': for v, (x, y) in v_to_point.items(): label_lines.append(get_label_line(x, y, self.N_short[v])) elif self.label_mode == 'label_mode_leaf_only': for v in self.leaves: x, y = v_to_point[v] if v in self.N: label_lines.append(get_label_line(x, y, self.N[v])) # return the tikz tikz_lines = vertex_lines + edge_lines + label_lines return '\n'.join(tikz_lines)
def get_response_content(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: the response """ # get a properly formatted newick tree with branch lengths T, B, N = FtreeIO.newick_to_TBN(fs.tree) # get the vertex valuations reflect = False all_valuations = TB_to_harmonic_valuations(T, B, reflect) fiedler_valuations = all_valuations[1] # do the layout v_to_location = FtreeAux.equal_daylight_layout(T, B, 3) # get the vertex list and the initial vertex locations vertices = Ftree.T_to_leaves(T) + Ftree.T_to_internal_vertices(T) X_in = np.array([tuple(v_to_location[v]) for v in vertices]) # fit the tree to the physical size physical_size = (fs.width, fs.height) theta = layout.get_best_angle(X_in, physical_size) X = layout.rotate_2d_centroid(X_in, theta) sz = layout.get_axis_aligned_size(X) sf = layout.get_scaling_factor(sz, physical_size) X *= sf # get the map from id to location for the final tree layout v_to_location = dict((v, tuple(r)) for v, r in zip(vertices, X)) # draw the image context = TikzContext() draw_plain_branches_ftree(T, B, context, v_to_location) draw_ticks_ftree(T, B, context, fiedler_valuations, v_to_location) draw_labels_ftree(T, N, context, v_to_location) context.finish() # get the response tikzpicture = context.get_text() return tikz.get_response(tikzpicture, fs.tikzformat)
def draw_ticks_ftree(T_in, B_in, context, v2_in, v_to_location): """ Increased barb radius. """ eps = 1e-8 T = set(T_in) B = dict(B_in) v2 = dict(v2_in) # break the branches according to valuation signs of v2 FtreeAux.break_branches_by_vertex_sign(T, B, v2, eps) # add the new locations for internal vertices v_to_x = dict((v, x) for v, (x, y) in v_to_location.items()) v_to_y = dict((v, y) for v, (x, y) in v_to_location.items()) FtreeAux.harmonically_interpolate(T, B, v_to_x) FtreeAux.harmonically_interpolate(T, B, v_to_y) vertices = Ftree.T_to_order(T) v_to_location = dict((v, (v_to_x[v], v_to_y[v])) for v in vertices) # draw the ticks v_to_neighbors = Ftree.T_to_v_to_neighbors(T) for v, location in v_to_location.items(): x, y = location neighbors = v_to_neighbors[v] if abs(v2[v]) > eps: continue if len(neighbors) == 2: barb_radius_cm = 0.1 va, vb = neighbors ax, ay = v_to_location[va] bx, by = v_to_location[vb] theta = math.atan2(by - ay, bx - ax) barbx1 = x + barb_radius_cm * math.cos(theta + math.pi/2) barby1 = y + barb_radius_cm * math.sin(theta + math.pi/2) barbx2 = x + barb_radius_cm * math.cos(theta - math.pi/2) barby2 = y + barb_radius_cm * math.sin(theta - math.pi/2) context.draw_dark_line(barbx1, barby1, barbx2, barby2) elif len(neighbors) > 2: if max(abs(v2[n]) for n in neighbors) < eps: continue context.draw_dark_dot(x, y)
def draw_ticks_ftree(T_in, B_in, context, v2_in, v_to_location): """ Increased barb radius. """ eps = 1e-8 T = set(T_in) B = dict(B_in) v2 = dict(v2_in) # break the branches according to valuation signs of v2 FtreeAux.break_branches_by_vertex_sign(T, B, v2, eps) # add the new locations for internal vertices v_to_x = dict((v, x) for v, (x, y) in v_to_location.items()) v_to_y = dict((v, y) for v, (x, y) in v_to_location.items()) FtreeAux.harmonically_interpolate(T, B, v_to_x) FtreeAux.harmonically_interpolate(T, B, v_to_y) vertices = Ftree.T_to_order(T) v_to_location = dict((v, (v_to_x[v], v_to_y[v])) for v in vertices) # draw the ticks v_to_neighbors = Ftree.T_to_v_to_neighbors(T) for v, location in v_to_location.items(): x, y = location neighbors = v_to_neighbors[v] if abs(v2[v]) > eps: continue if len(neighbors) == 2: barb_radius_cm = 0.1 va, vb = neighbors ax, ay = v_to_location[va] bx, by = v_to_location[vb] theta = math.atan2(by - ay, bx - ax) barbx1 = x + barb_radius_cm * math.cos(theta + math.pi / 2) barby1 = y + barb_radius_cm * math.sin(theta + math.pi / 2) barbx2 = x + barb_radius_cm * math.cos(theta - math.pi / 2) barby2 = y + barb_radius_cm * math.sin(theta - math.pi / 2) context.draw_dark_line(barbx1, barby1, barbx2, barby2) elif len(neighbors) > 2: if max(abs(v2[n]) for n in neighbors) < eps: continue context.draw_dark_dot(x, y)
def _get_v_to_layout_point(self): v_to_location = FtreeAux.equal_daylight_layout(self.T, self.B, 3) d = {} for v, p in v_to_location.items(): d[v] = self._custom_transform_layout_point(p) return d