-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_plotter.py
203 lines (179 loc) · 8.27 KB
/
graph_plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Classes for plotting the graph with plotly
"""
import bandage_graph
import json
import math
import plotly
import plotly.graph_objs as go
class GraphicsItemEdge:
def __init__(self, edge, settings):
self.m_edge = edge
self.m_startingLocation = None
self.m_beforeStartingLocation = None
self.m_endingLocation = None
self.m_afterEndingLocation = None
self.m_controlPoint1 = None
self.m_controlPoint2 = None
self.m_settings = settings
self.shape = self.GetShape()
def setControlPointLocations(self):
starting_node = self.m_edge.startingNode
ending_node = self.m_edge.endingNode
if starting_node.hasGraphicsItem():
self.m_startingLocation = starting_node.GetGraphicsItemNode().getLast()
self.m_beforeStartingLocation = starting_node.GetGraphicsItemNode().getSecondLast()
elif starting_node.getReverseComplement().hasGraphicsItem():
self.m_startingLocation = starting_node.getReverseComplement().GetGraphicsItemNode().getLast()
self.m_beforeStartingLocation = starting_node.getReverseComplement().GetGraphicsItemNode().getSecond()
else: pass
if ending_node.hasGraphicsItem():
self.m_endingLocation = ending_node.GetGraphicsItemNode().getFirst()
self.m_afterEndingLocation = ending_node.GetGraphicsItemNode().getSecond()
elif ending_node.getReverseComplement().hasGraphicsItem():
self.m_endingLocation = ending_node.getReverseComplement().GetGraphicsItemNode().getLast()
self.m_afterEndingLocation = ending_node.getReverseComplement().GetGraphicsItemNode().getSecondLast()
else: pass
def GetEdgeDistance(self):
return math.sqrt((self.m_startingLocation[0]-self.m_endingLocation[0]) **2 + \
(self.m_startingLocation[1]-self.m_endingLocation[1])**2)
def extendLine(self, start, end, extensionLength):
extensionRatio = extensionLength/self.GetEdgeDistance()
difference = [(end[0]-start[0])*extensionRatio, (end[1]-start[1])*extensionRatio]
return [end[0]+difference[0], end[1]+difference[1]]
def GetShape(self):
self.setControlPointLocations()
starting_node = self.m_edge.startingNode
ending_node = self.m_edge.endingNode
edge_distance = self.GetEdgeDistance()
extensionLength = self.m_settings["EDGELEN"]
if extensionLength > edge_distance/2:
extensionLength = edge_distance/2
self.m_controlPoint1 = self.extendLine(self.m_beforeStartingLocation, self.m_startingLocation, extensionLength)
self.m_controlPoint2 = self.extendLine(self.m_afterEndingLocation, self.m_endingLocation, extensionLength)
# If edge connects a node to itself, need special edge
if starting_node == ending_node:
# TODO
return None
# If single mode and edge connects node to its reverse
# complement, also need special edge
if starting_node == ending_node.getReverseComplement():
# TODO
return None
# Else just a single cubic Bezier curve
path = "M %s %s"%(self.m_startingLocation[0], self.m_startingLocation[1])
path += " C %s %s %s %s %s %s"%(self.m_controlPoint1[0], self.m_controlPoint2[1],
self.m_controlPoint2[0], self.m_controlPoint2[1],
self.m_endingLocation[0], self.m_endingLocation[1])
# Return the path shape
shape = dict(
type="path",
path=path,
line_color="red",
line_width=2
)
return shape
class GraphicsItemNode:
def __init__(self, node, GA, settings):
self.m_node = node
self.m_graphAttributes = GA
self.m_settings = settings
self.points = []
self.shape = self.GetShape()
self.max_x = max([item[0] for item in self.points])
self.max_y = max([item[1] for item in self.points])
def GetShape(self):
self.points = []
# First get all points in the pagh
for ogdf_node in self.m_node.GetOgdfNode().m_ogdfNodes:
self.points.append((self.m_graphAttributes.x(ogdf_node), self.m_graphAttributes.y(ogdf_node)))
if len(self.points) < 2: return None
# Now turn into an SVG path
path = "M %s %s"%(self.points[0][0], self.points[0][1])
for i in range(1, len(self.points)):
path = path + " L %s %s"%(self.points[i][0], self.points[i][1])
# Return the path shape
shape = dict(
type="path",
path=path,
line_color="MediumPurple",
line_width=10,
id=self.m_node.nodeName
# label=dict(text=self.m_node.nodeName)
)
return shape
def getFirst(self):
return self.points[0]
def getSecond(self):
return self.points[1]
def getLast(self):
return self.points[-1]
def getSecondLast(self):
return self.points[-2]
class GraphPlotter:
def __init__(self, pggraph, settings):
self.m_pggraph = pggraph
self.max_x = 0
self.max_y = 0
self.m_settings = settings
def BuildGraphicsItems(self):
for node in self.m_pggraph.pgnodes.values():
if node.isDrawn():
graphics_item_node = GraphicsItemNode(node, self.m_pggraph.m_graphAttributes, self.m_settings)
if graphics_item_node.max_x > self.max_x:
self.max_x = graphics_item_node.max_x
if graphics_item_node.max_y > self.max_y:
self.max_y = graphics_item_node.max_y
node.SetGraphicsItemNode(graphics_item_node)
# Then get edges
for edge in self.m_pggraph.pgedges.values():
if edge.isDrawn():
graphics_item_edge = GraphicsItemEdge(edge, self.m_settings)
edge.SetGraphicsItemEdge(graphics_item_edge)
def BuildSvg(self):
self.BuildGraphicsItems()
svgFile = open("subgraph.svg", "w")
svgFile.write("<svg width=\"100%%\" height=\"100%%\" viewBox=\"0 0 %s %s\" xmlns=\"http://www.w3.org/2000/svg\" preserveAspectRatio=\"xMidYMid meet\">\n"%(self.max_x*1.1, self.max_y*1.1))
for node in self.m_pggraph.pgnodes.values():
if node.isDrawn():
shape = node.GetGraphicsItemNode().GetShape()
if shape is not None:
svgFile.write(f"\t<path id=\"{shape['id']}\" d=\"{shape['path']}\" fill=\"none\" stroke=\"{shape['line_color']}\" stroke-width=\"{shape['line_width']}\"/>\n")
for edge in self.m_pggraph.pgedges.values():
if edge.isDrawn():
shape = edge.GetGraphicsItemEdge().GetShape()
if shape is not None:
svgFile.write(f"\t<path d=\"{shape['path']}\" fill=\"none\" stroke=\"{shape['line_color']}\" stroke-width=\"{shape['line_width']}\"/>\n")
svgFile.write("</svg>")
def image():
# TODO: some "MINNODELENGTH" would throw an error
setting = {"EXACT_OVERLAP": True, "DEBUG_SMALL_GRAPHS": False, "MINNODELENGTH": 1.0, "NODESEGLEN": 20, "EDGELEN": 5, "NODELENPERMB":1000}
pggraph = bandage_graph.PGGraph("no_cutpoint.gfa", setting)
pggraph.BuildOGDFGraph()
pggraph.LayoutGraph()
graphPlotter = GraphPlotter(pggraph, setting)
graphPlotter.BuildSvg()
# def GetGraphJSON(self):
# # TODO - this is just a list of SVG paths
# # currently plotting with plotly, but maybe will have
# # more control if we just plot with d3 or some other library?
# # plotly doesn't seem to allow interaction with shapes at least
# # not easily
# graph_shapes = self.GetGraphShapes()
# fig = go.Figure()
# # Update axes properties
# fig.update_xaxes(
# range=[0, self.max_x*1.1],
# zeroline=False,
# showgrid=False,
# showticklabels=False
# )
# fig.update_yaxes(
# range=[0, self.max_y*1.1],
# zeroline=False,
# showgrid=False,
# showticklabels=False
# )
# fig.update_layout(shapes=graph_shapes, plot_bgcolor="rgba(0,0,0,0)")
# plotly_plot_json = json.dumps(fig.to_plotly_json(), cls=plotly.utils.PlotlyJSONEncoder)
# return plotly_plot_json