def get_overview_contents(self, width: int, height: int) -> str: """ Generate an SVG comparing the query to all hit cluster Arguments: width: the width of the SVG height: the height of the SVG Returns: a string containing the SVG XML """ svg = Svg(x=0, y=0, width=width, height=height) viewbox = "0 0 %d %d" % (width, height) svg.set_viewBox(viewbox) svg.set_preserveAspectRatio("none") scaling = (width - 20) / self.max_length # -20 for margins offset = (self.max_length - len(self.query_cluster)) // 2 for group in self.query_cluster.get_svg_groups( h_offset=offset, scaling=scaling, colours=self.colour_lookup, overview=True, prefix=self.prefix): svg.addElement(group) for index, cluster in enumerate(self.hits): for group in cluster.get_svg_groups( v_offset=50 * (index + 1), h_offset=(self.max_length - len(cluster)) // 2, scaling=scaling, colours=self.colour_lookup, overview=True, prefix=self.prefix): svg.addElement(group) return svg.getXML()
def get_pairing_contents(self, index: int, width: int, height: int) -> str: """ Generate an SVG comparing the query to a single hit cluster Arguments: index: the index of the hit width: the width of the SVG height: the height of the SVG Returns: a string containing the SVG XML """ svg = Svg(x=0, y=0, width=width, height=height) viewbox = "0 0 %d %d" % (width, height) svg.set_viewBox(viewbox) svg.set_preserveAspectRatio("none") max_length = max([len(self.query_cluster), len(self.hits[index])]) scaling = (width - 20) / max_length for i, cluster in enumerate([self.query_cluster, self.hits[index]]): for group in cluster.get_svg_groups( v_offset=50 * i, h_offset=(max_length - len(cluster)) // 2, scaling=scaling, colours=self.colour_lookup): svg.addElement(group) return svg.getXML()
def makePathSvg(self) -> Svg: """ Returns the main `Svg` object for Path view. """ viewbox = "0 0 %s %s" % (self.w, self.h) path_svg = Svg(width=self.w, height=self.h) path_svg.set_viewBox(viewbox) path_svg.set_preserveAspectRatio("xMinYMid meet") path_svg.setAttribute('id', "Cadnano_Path") # Main layer name path_svg.addElement(self.defs) path_svg.addElement(self.g_pathgridlines) # bottom layer path_svg.addElement(self.g_patholigos) path_svg.addElement(self.g_pathendpoints) path_svg.addElement(self.g_pathvirtualhelices) path_svg.addElement(self.g_pathvirtualhelixlabels) # top layer path_svg.addElement(self.g_pathinsertions) path_svg.addElement(self.g_pathskips) if self.cn_doc.sequence_applied: path_svg.addElement(self.g_pathsequences) else: print('No sequences were applied. Max oligo length: %s' % self.cn_doc.max_oligo_length, file=sys.stderr) path_svg.save(self.output_path) return path_svg
def makeSliceSvg(self) -> Svg: slice_svg = Svg(width=self.w, height=self.h) viewbox = "0 0 %s %s" % (self.w, self.h) slice_svg.set_viewBox(viewbox) slice_svg.set_preserveAspectRatio("xMidYMid meet") slice_svg.setAttribute('id', "Cadnano_Slice") # Main layer name slice_svg.addElement(self.g_slicevirtualhelices) # bottom layer slice_svg.addElement(self.g_slicevirtualhelixlabels) # top layer slice_svg.save(self.output_path)
def __init__(self, timeline, scene, view_properties, appearence, **kwargs): self._timeline = timeline self._scene = scene self._appearence = appearence self._view_properties = view_properties self._svg = Svg(width=scene.width, height=scene.height) self._small_font_style = self._get_small_font_style() self._small_centered_font_style = self._get_small_centered_font_style() self._larger_font_style = self._get_larger_font_style() try: self._shadow_flag = kwargs["shadow"] except KeyError: self._shadow_flag = False
def draw_tree(width, height): global nameStyle nameStyle = StyleBuilder() nameStyle.setFontFamily(fontfamily=dc.nameFont) nameStyle.setFontSize("%spt" % dc.nameFontSize) nameStyle.setTextAnchor("left") nameStyle = nameStyle.getStyle() svg = Svg(width=width, height=height) # draw nodes for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] draw_node(svg, node, name) # draw branches for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] isLeaf = (node.children == []) if (isLeaf): continue numChildren = len(node.children) for (i, child) in enumerate(node.children): rootFrac = 0.4 + (0.2 * i) / (numChildren - 1) sinkFrac = 0.5 if (orientation == "T2B"): (rootX, rootY) = (node.x + rootFrac * dc.nodeWdt, node.y + dc.nodeHgt) (sinkX, sinkY) = (child.x + sinkFrac * dc.nodeWdt, child.y) draw_vert_branch(svg, "%s_branch_%d" % (name, i), rootX, rootY, sinkX, sinkY) else: # if (orientation == "L2R"): (rootX, rootY) = (node.x + dc.nodeWdt, node.y + rootFrac * dc.nodeHgt) (sinkX, sinkY) = (child.x, child.y + sinkFrac * dc.nodeHgt) draw_horz_branch(svg, "%s_branch_%d" % (name, i), rootX, rootY, sinkX, sinkY) return svg
def testLindenMayer(): s = Svg(0, 0, 2000, 2000) commands = 'F+F-F-FF+F+F-F+F+F-F-FF+F+F-F+F+F-F-FF+F+F-F+F+F-F-FF+F+F-F' t = Turtle() t.moveTo(Vector(500, 250)) t.penDown() angle = 90 distance = 40 for cmd in commands: print(cmd) if cmd == 'F': t.forward(distance) elif cmd == '+': t.right(angle) elif cmd == '-': t.left(angle) print(t.getPosition()) t.penDown() print(t.getXML()) s = t.addTurtlePathToSVG(s) s.save('./testoutput/testTurtle.svg')
def build_svg(self, outfile): # background image bg_image = PIL.Image.open(self.image_filename) width, height = bg_image.size # build a svg object, which size is the same as background svg = Svg(width=width, height=height) # add background image png_link = bg_image.filename if self.mode == 'online': path = os.path.basename(self.image_filename).split('.')[0] png_link = '{}/get/{}/image'.format(KEGGRest.KEGG_API_URL, path) elif self.mode == 'base64': with safe_open(self.image_filename, 'rb') as f: png_link = 'data:image/png;base64,' + base64.b64encode( f.read()) im = Image(x=0, y=0, width=width, height=height) im.set_xlink_href(png_link) svg.addElement(im) for shape, position, url, title in self.conf_data: a = A(target='new_window') a.set_xlink_title(title) a.set_xlink_href(KEGGRest.KEGG_BASE_URL + url) # check gene, and add a highlighted rect or circle color = get_gene_color(title, self.genedict) child = self.add_child(shape, position, color) if child: a.addElement(child) svg.addElement(a) svg.save(outfile, encoding='UTF-8', standalone='no')
def draw_tree(): if (dc.nameFontSize < 16): scale = dc.nameFontSize / 16.0 nameCapsHgt = dc.nameCapsHgt * scale nameDescHgt = dc.nameDescHgt * scale nameFontLineHgt = dc.nameFontLineHgt * scale nameStyle = StyleBuilder() nameStyle.setFontFamily(fontfamily=dc.nameFont) nameStyle.setFontSize("%spt" % dc.nameFontSize) nameStyle.setTextAnchor("left") nameStyle = nameStyle.getStyle() svg = Svg() # draw nodes for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] isLeaf = (node.left == None) yLine = node.y + nameCapsHgt + 1 ob = SvgRect(node.x, node.y, dc.nodeWidth, dc.nodeHeight, id="%s_box" % name) ob.set_stroke(dc.lineColor) ob.set_stroke_width(dc.lineThickness) if (isLeaf): ob.set_fill(dc.leafFillColor) else: ob.set_fill(dc.nodeFillColor) svg.addElement(ob) ob = SvgText("%s" % name, node.x + 1, yLine, id="%s_name" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsUnion")): ob = SvgText("U:" + bits_to_string(node.numBits, node.bitsUnion), node.x + 1, yLine, id="%s_Bunion" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsIntersection")): ob = SvgText( "I:" + bits_to_string(node.numBits, node.bitsIntersection), node.x + 1, yLine, id="%s_Bintersection" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsAll")): ob = SvgText("A:" + bits_to_string(node.numBits, node.bitsAll), node.x + 1, yLine, id="%s_Ball" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsSome")): ob = SvgText("S:" + bits_to_string(node.numBits, node.bitsSome), node.x + 1, yLine, id="%s_Bsome" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt # draw branches for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] if (node.left == None): continue (leftStartX, leftStartY) = (node.x + 0.4 * dc.nodeWidth, node.y + dc.nodeHeight) (rightStartX, rightStartY) = (node.x + 0.6 * dc.nodeWidth, node.y + dc.nodeHeight) (leftEndX, leftEndY) = (node.left.x + 0.5 * dc.nodeWidth, node.right.y) (rightEndX, rightEndY) = (node.right.x + 0.5 * dc.nodeWidth, node.right.y) draw_branch(svg, "%s_left_branch" % node, leftStartX, leftStartY, leftEndX, leftEndY) draw_branch(svg, "%s_right_branch" % node, rightStartX, rightStartY, rightEndX, rightEndY) return svg