annotate tools/mutation/visualize.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Mutation Visualizer tool
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 from __future__ import division
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 import sys, csv, os, math
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 pkg_resources.require( "SVGFig" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 import svgfig as svg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 SVGPan = """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 * SVGPan library 1.2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 * ====================
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 * Given an unique existing element with id "viewport", including the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 * the library into any SVG adds the following capabilities:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 * - Mouse panning
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 * - Mouse zooming (using the wheel)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 * - Object dargging
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 * Known issues:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 * - Zooming (while panning) on Safari has still some issues
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 * Releases:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 * 1.2, Sat Mar 20 08:42:50 GMT 2010, Zeng Xiaohui
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 * Fixed a bug with browser mouse handler interaction
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 * 1.1, Wed Feb 3 17:39:33 GMT 2010, Zeng Xiaohui
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 * Updated the zoom code to support the mouse wheel on Safari/Chrome
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 * 1.0, Andrea Leofreddi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 * First release
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 * This code is licensed under the following BSD license:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 * Copyright 2009-2010 Andrea Leofreddi (a.leofreddi@itcharm.com). All rights reserved.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 * Redistribution and use in source and binary forms, with or without modification, are
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 * permitted provided that the following conditions are met:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 * 1. Redistributions of source code must retain the above copyright notice, this list of
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 * conditions and the following disclaimer.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 * of conditions and the following disclaimer in the documentation and/or other materials
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 * provided with the distribution.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 * The views and conclusions contained in the software and documentation are those of the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 * authors and should not be interpreted as representing official policies, either expressed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 * or implied, of Andrea Leofreddi.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 var root = document.documentElement;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 var state = 'none', stateTarget, stateOrigin, stateTf;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 setupHandlers(root);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 * Register handlers
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 function setupHandlers(root){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 setAttributes(root, {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 "onmouseup" : "add(evt)",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 "onmousedown" : "handleMouseDown(evt)",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 "onmousemove" : "handleMouseMove(evt)",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 "onmouseup" : "handleMouseUp(evt)",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 //"onmouseout" : "handleMouseUp(evt)", // Decomment this to stop the pan functionality when dragging out of the SVG element
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 });
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 if(navigator.userAgent.toLowerCase().indexOf('webkit') >= 0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 else
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 * Instance an SVGPoint object with given event coordinates.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 function getEventPoint(evt) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 var p = root.createSVGPoint();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 p.x = evt.clientX;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 p.y = evt.clientY;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 return p;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 * Sets the current transform matrix of an element.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 function setCTM(element, matrix) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 element.setAttribute("transform", s);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 * Dumps a matrix to a string (useful for debug).
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 function dumpMatrix(matrix) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\\n 0, 0, 1 ]";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 return s;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 * Sets attributes of an element.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 function setAttributes(element, attributes){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 for (i in attributes)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 element.setAttributeNS(null, i, attributes[i]);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 * Handle mouse move event.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 function handleMouseWheel(evt) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 if(evt.preventDefault)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 evt.preventDefault();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 evt.returnValue = false;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 var svgDoc = evt.target.ownerDocument;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 var delta;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 if(evt.wheelDelta)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 delta = evt.wheelDelta / 3600; // Chrome/Safari
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 else
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 delta = evt.detail / -90; // Mozilla
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 var z = 1 + delta; // Zoom factor: 0.9/1.1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 var g = svgDoc.getElementById("viewport");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 var p = getEventPoint(evt);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 p = p.matrixTransform(g.getCTM().inverse());
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 // Compute new scale matrix in current mouse position
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 var k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 setCTM(g, g.getCTM().multiply(k));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 stateTf = stateTf.multiply(k.inverse());
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 * Handle mouse move event.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 function handleMouseMove(evt) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 if(evt.preventDefault)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 evt.preventDefault();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 evt.returnValue = false;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 var svgDoc = evt.target.ownerDocument;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 var g = svgDoc.getElementById("viewport");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 if(state == 'pan') {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 // Pan mode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 var p = getEventPoint(evt).matrixTransform(stateTf);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 setCTM(g, stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 } else if(state == 'move') {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 // Move mode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 var p = getEventPoint(evt).matrixTransform(g.getCTM().inverse());
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 setCTM(stateTarget, root.createSVGMatrix().translate(p.x - stateOrigin.x, p.y - stateOrigin.y).multiply(g.getCTM().inverse()).multiply(stateTarget.getCTM()));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 stateOrigin = p;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 * Handle click event.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 function handleMouseDown(evt) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 if(evt.preventDefault)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 evt.preventDefault();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 evt.returnValue = false;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 var svgDoc = evt.target.ownerDocument;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 var g = svgDoc.getElementById("viewport");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 if(evt.target.tagName == "svg") {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 // Pan mode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 state = 'pan';
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 stateTf = g.getCTM().inverse();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 /*else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 // Move mode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 state = 'move';
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 stateTarget = evt.target;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 stateTf = g.getCTM().inverse();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 }*/
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 /**
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 * Handle mouse button release event.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 */
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 function handleMouseUp(evt) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 if(evt.preventDefault)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 evt.preventDefault();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 evt.returnValue = false;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 var svgDoc = evt.target.ownerDocument;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 if(state == 'pan' || state == 'move') {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 // Quit pan mode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 state = '';
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 COLS_PER_SAMPLE = 7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 HEADER_COLS = 4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 HEIGHT = 6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 WIDTH = 12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 BAR_WIDTH = 1.5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 GAP = 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 colors = {'A':'blue', 'C':'green', 'G':'orange', 'T':'red'}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 bases = ['A', 'C', 'G', 'T' ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 def stop_error(message):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 print >> sys.stderr, message
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 def validate_bases(n_a, n_c, n_g, n_t, total):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 if n_a > total:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 return 'A'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 elif n_c > total:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 return 'C'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 elif n_g > total:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 return 'G'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 elif n_t > total:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272 return 'T'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 return None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 def main(opts, args):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276 s = svg.SVG('g', id='viewport')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278 # display legend
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 for i, b in enumerate( bases ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 bt = svg.SVG("tspan", b, style="font-family:Verdana;font-size:20%")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 s.append(svg.SVG("text", bt, x=12+(i*10), y=3, stroke="none", fill="black"))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282 s.append(svg.SVG("rect", x=14+(i*10), y=0, width=4, height=3,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 stroke="none", fill=colors[b], fill_opacity=0.5))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285 reader = open(opts.input_file, 'U')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
286
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
287 samples = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
288 for i in range(int(len(args)/3)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
289 index = i*3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
290 samples.append(dict(name=args[index],
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
291 a_col=args[index+1],
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
292 totals_col=args[index+2]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
293
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
294 if opts.zoom == 'interactive':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
295 y = 35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
296 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
297 y = 25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
298 for i, sample in enumerate(samples):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
299 x = 23+(i*(WIDTH+GAP))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
300 t = svg.SVG("text", svg.SVG("tspan", sample['name'], style="font-family:Verdana;font-size:25%"),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
301 x=x, y=y, transform="rotate(-90 %i,%i)" % (x, y), stroke="none", fill="black")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
302 s.append(t)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
303
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
304 count=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
305 for line in reader:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
306 row = line.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
307 highlighted_position = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
308 show_pos = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
309 position = row[int(opts.position_col)-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
310 ref = row[int(opts.ref_col)-1].strip().upper()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
311 # validate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
312 if ref not in bases:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
313 stop_error( "The reference column (col%s) contains invalid character '%s' at row %i of the dataset." % ( opts.ref_col, ref, count ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
314 # display positions
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
315 if opts.zoom == 'interactive':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
316 textx = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
317 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
318 textx = 7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
319 bt = svg.SVG("tspan", str(position), style="font-family:Verdana;font-size:25%")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
320 s.append(svg.SVG("text", bt, x=textx, y=34+(count*(HEIGHT+GAP)), stroke="none", fill="black"))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
321 s.append(svg.SVG("rect", x=0, y=30+(count*(HEIGHT+GAP)), width=14, height=HEIGHT,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
322 stroke='none', fill=colors[ref.upper()], fill_opacity=0.2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
323
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
324 for sample_index, sample in enumerate(samples):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
325 n_a = int(row[int(sample['a_col'])-1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
326 n_c = int(row[int(sample['a_col'])+1-1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
327 n_g = int(row[int(sample['a_col'])+2-1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
328 n_t = int(row[int(sample['a_col'])+3-1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
329 total = int(row[int(sample['totals_col'])-1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
330 # validate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
331 base_error = validate_bases(n_a, n_c, n_g, n_t, total)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
332 if base_error:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
333 stop_error("For sample %i (%s), the number of base %s reads is more than the coverage on row %i." % (sample_index+1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
334 sample['name'],
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
335 base_error,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
336 count))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
337
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
338 if total:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
339 x = 16+(sample_index*(WIDTH+GAP))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
340 y = 30+(count*(HEIGHT+GAP))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
341 width = WIDTH
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
342 height = HEIGHT
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
343 if count%2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
344 s.append(svg.SVG("rect", x=x, y=y, width=width, height=height,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
345 stroke='none', fill='grey', fill_opacity=0.25))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
346 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
347 s.append(svg.SVG("rect", x=x, y=y, width=width, height=height,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
348 stroke='none', fill='grey', fill_opacity=0.25))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
349
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
350 for base, value in enumerate([n_a, n_c, n_g, n_t]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
351 width = int(math.ceil(value / total * WIDTH))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
352 s.append(svg.SVG("rect", x=x, y=y, width=width, height=BAR_WIDTH,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
353 stroke='none', fill=colors[bases[base]], fill_opacity=0.6))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
354 y = y + BAR_WIDTH
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
355
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
356 count=count+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
357
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
358 if opts.zoom == 'interactive':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
359 canv = svg.canvas(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
360 canv.save(opts.output_file)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
361 import fileinput
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
362 flag = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
363 for line in fileinput.input(opts.output_file, inplace=1):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
364 if line.startswith('<svg'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
365 print '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
366 flag = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
367 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
368 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
369 if flag:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
370 print '<script type="text/javascript">%s</script>' % SVGPan
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
371 flag = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
372 print line,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
373 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
374 zoom = int(opts.zoom)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
375 w = "%ipx" % (x*(10+zoom))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
376 h = "%ipx" % (y*(2+zoom))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
377 canv = svg.canvas(s, width=w, height=h, viewBox="0 0 %i %i" %(x+100, y+100))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
378 canv.save(opts.output_file)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
379
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
380 if __name__ == '__main__':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
381 parser = optparse.OptionParser()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
382 parser.add_option('-i', '--input-file', dest='input_file', action='store')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
383 parser.add_option('-o', '--output-file', dest='output_file', action='store')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
384 parser.add_option('-z', '--zoom', dest='zoom', action='store', default='1')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
385 parser.add_option('-p', '--position_col', dest='position_col', action='store', default='c0')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
386 parser.add_option('-r', '--ref_col', dest='ref_col', action='store', default='c1')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
387 (opts, args) = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
388 main(opts, args)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
389 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
390
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
391