-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAbstractGraph.cpp
370 lines (330 loc) · 9.72 KB
/
AbstractGraph.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "AbstractGraph.h"
#include "UiVariables.h"
#include "glwidget.h"
#include <sstream>
#include <math.h>
#include <utility> //includes std::pair
#include "SkittleUtil.h"
#include <QtOpenGL>
/** *****************
Abstract Graph is the abstract class that all of the Graphs inherit from.
Graph are the main workhorses of Skittle. The are different ways of
displaying the genome sequence. This class should contain all of the
universal functionality of the graphs like receiving data, sending signals,
handling variable changes and painting using a TextureCanvas. The constructor requires
* a Ui reference so that it can 'connect' to controls in the Ui.
*/
AbstractGraph::AbstractGraph()
{
usingTextures = true;
textureBuffer = new TextureCanvas();
}
AbstractGraph::AbstractGraph(UiVariables* gui, GLWidget* gl)
{
glWidget = gl;
ui = gui;
string* seq = new string("AATCGATCGTACGCTACGATCGCTACGCAGCTAGGACGGATTAATCGATCGTACGCTACGATCGCTACGCAGCTAGGACGGATTAATCGATCGTACGCTACGATCGCTACGCAGCTAGGACGGATTAATCGATCGTACGCTACGATCGCTACGCAGCTAGGACGGATT");
sequence = seq;
hidden = true;
settingsTab = NULL;
toggleButton = NULL;
textureBuffer = new TextureCanvas();
usingTextures = true;
frameCount = 0;
display_object = 0;
upToDate = false;
}
AbstractGraph::~AbstractGraph()
{
if(toggleButton != NULL)
emit deleteButton(toggleButton);
glDeleteLists(display_object, 1);//for the Graphs that use a cached GL_Display_List,
//you need to delete it on exit so it doesn't suck up memory on the graphics card.
}
void AbstractGraph::checkVariables()
{
//This is a place holder for the case where a Graph needs to check its settings tab
}
void AbstractGraph::display()
{
checkVariables();
glPushMatrix();
glScaled(1,-1,1);
if(!upToDate)
calculateOutputPixels();
textureBuffer->display();
glPopMatrix();
}
GLuint AbstractGraph::render()
{
GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE);
glPushMatrix();
glScaled(1,-1,1);
if(!upToDate)
calculateOutputPixels();
textureBuffer->display();
glPopMatrix();
glEndList();
upToDate = true;
return list;
}
int AbstractGraph::height()
{
return current_display_size() / ui->getWidth();
}
void AbstractGraph::paint_square(point position, color c)
{
glPushMatrix();
glColor4d(c.r /255.0, c.g /255.0, c.b /255.0, .7);
glTranslated(position.x+1, position.y, position.z);
glBegin(GL_QUADS);
glVertex3d(.0, .0, 0);
glVertex3d(-1.0, .0, 0);
glVertex3d(-1, -1, 0);
glVertex3d(.0, -1, 0);
glEnd();
glPopMatrix();
}
void AbstractGraph::paint_image(point position, string filePath)
{
glPushMatrix();
// ui->print(filePath);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable (GL_TEXTURE_2D);
QPixmap tex(QString(filePath.c_str()));
GLuint textures = bindTexture(tex, GL_TEXTURE_2D);
// glGenTextures( 1, &textures );
glBindTexture(GL_TEXTURE_2D, textures);
//draw
// paint_square(position, color(.8));
glTranslated(position.x- tex.width()/6, position.y+ tex.height()/6, position.z);
glScaled(.3333, -.33333, 1);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin (GL_QUADS);
glTexCoord2f (0.0f,0.0f); /* upper left corner of image */
glVertex3f (0.0f, 0.0f, 0.0f);
glTexCoord2f (1.0f, 0.0f); /* upper right corner of image */
glVertex3f (tex.width(), 0.0f, 0.0f);
glTexCoord2f (1.0f, 1.0f); /* lower right corner of image */
glVertex3f (tex.width(), tex.height(), 0.0f);
glTexCoord2f (0.0f, 1.0f); /* lower left corner of image */
glVertex3f (0.0f, tex.height(), 0.0f);
glEnd ();
// glTexImage2D(GL_TEXTURE_2D, 0,
// GL_RGBA,
// textureWidth,
// textureHeight,
// 0,
// GL_RGBA,
// GL_UNSIGNED_BYTE,
// pointerToPixels);
glPopMatrix();
}
void AbstractGraph::paint_line(point startPoint, point endPoint, color c)
{
glPushMatrix();
glColor3d(c.r /255.0, c.g /255.0, c.b /255.0);
// glTranslated(-1.5, startPoint.y, startPoint.z);
glBegin(GL_QUADS);
glVertex3d(startPoint.x, startPoint.y, 0);
glVertex3d(endPoint.x, startPoint.y, 0);
glVertex3d(endPoint.x, endPoint.y, 0);
glVertex3d(startPoint.x, endPoint.y, 0);
glEnd();
glPopMatrix();
}
void AbstractGraph::loadTextureCanvas(bool raggedEdge)
{
storeDisplay( outputPixels, width(), raggedEdge );
}
void AbstractGraph::storeDisplay(vector<color>& pixels, int width, bool raggedEdge)
{
if(textureBuffer)
delete textureBuffer;
textureBuffer = new TextureCanvas( pixels, width, raggedEdge );
}
bool AbstractGraph::updateInt(int& subject, int& value)
{
if(value < 1)
value = 1;
if(subject != value)
{
subject = value;
upToDate = false;
return true;
}
return false;
}
bool AbstractGraph::updateDouble(double& subject, double& value)
{
if(value < 0.01)
value = 0.01;
if(subject != value)
{
subject = value;
upToDate = false;
return true;
}
return false;
}
void AbstractGraph::displayLegend(float canvasWidth, float canvasHeight)
{
}
string AbstractGraph::getFileName()
{
return "";
}
int AbstractGraph::current_display_size()
{
return min( ui->getSize(), max(0, ((int)sequence->size() - ui->getStart(glWidget))) );
}
//***********SLOTS*******************
void AbstractGraph::invalidate()
{
upToDate = false;
emit displayChanged();
}
void AbstractGraph::ensureVisible()
{
if(hidden)
toggleVisibility();
}
void AbstractGraph::toggleVisibility()
{
hidden = !hidden;
if(settingsTab != NULL)
{
if(hidden)
emit hideSettings(settingsTab);
else
emit showSettings(settingsTab);
}
setButtonFont();
emit displayChanged();
}
void AbstractGraph::setButtonFont()
{
if(toggleButton != NULL)
{
QFont font = toggleButton->font();
if(hidden){
font.setBold(false);
}
else{
font.setBold(true);
}
toggleButton->setFont(font);
}
}
void AbstractGraph::setSequence(const string* seq)
{
sequence = seq;
}
int AbstractGraph::width()
{
return ui->getWidth() / ui->getScale();
}
QScrollArea* AbstractGraph::settingsUi()
{//settingsUi should be called before toggleVisibility() the first time so that "settingsTab" will have a value
settingsTab = NULL;
return NULL;
}
string AbstractGraph::reverseComplement(string original)
{
string rc;
int size = original.size();
rc.resize(size, 'N');
for(int x = 0; x < size; ++x)
{
rc[x] = complement(original[size-x-1]);
}
return rc;
}
string AbstractGraph::mouseClick(point2D pt, bool selectTool)
{
int index = getRelativeIndexFromMouseClick(pt);
if( index > -1)
{
int sample_length = ui->getWidth();
index = adjustForSampleLengthBounds(index, sample_length);
if(selectTool)
return SELECT_StringFromMouseClick(index);
else
return FIND_StringFromMouseClick(index);
}
else{
return string();
}
}
string AbstractGraph::SELECT_MouseClick(point2D pt)
{
return mouseClick(pt, true);
}
string AbstractGraph::FIND_MouseClick(point2D pt)
{
return mouseClick(pt, false);
}
/** Returns the relative index on a square area. It will return -1 if
the point is invalid. */
int AbstractGraph::getRelativeIndexFromMouseClick(point2D pt)
{
if( pt.x < width() && pt.x >= 0 && pt.y <= height() )//check if it is inside the box
{
int index = pt.y * ui->getWidth() + pt.x * ui->getScale();
index = max(0, index);
return index;
}
else
return -1;
}
pair<int,int> AbstractGraph::getIndicesFromPoints(point2D startPoint, point2D endPoint)
{
if (rangeOverlap(startPoint.x,endPoint.x,0,width()))
{
int spx = min(max(startPoint.x,0),(width() - 1)); //force value between 0 and width
int epx = min(max(endPoint.x,0),(width() - 1));
//we use Relative index here for the graphs that overwrite that function
int startIndex = getRelativeIndexFromMouseClick(point2D(spx, startPoint.y)) + ui->getStart(glWidget);
int endIndex = getRelativeIndexFromMouseClick(point2D(epx, endPoint.y)) + ui->getStart(glWidget);
// startIndex = max(0, startIndex);
// endIndex = max(0, endIndex);
return pair<int,int>(startIndex,endIndex);
}
else
return pair<int,int>(-1,-1);
}
int AbstractGraph::getBeginningOfLineFromMouseClick(point2D pt)
{
if( pt.x < width() && pt.x >= 0 && pt.y <= height() )//check if it is inside the box
{
int index = pt.y * ui->getWidth();
index = max(0, index);
return index;
}
else
return -1;
}
int AbstractGraph::adjustForSampleLengthBounds(int index, int sample_length)
{
index = min((int)current_display_size()-sample_length-1, index);
index = min( index + ui->getStart(glWidget), ((int)sequence->size()) - sample_length-1 );
return index;
}
string AbstractGraph::SELECT_StringFromMouseClick(int index)
{
int sample_length = ui->getWidth();
std::stringstream ss;
ss << "Index: " << index << " Sequence: " << sequence->substr(index, sample_length);
//string chromosome = glWidget->chromosomeName;
//ss<< " <a href=\"http://genome.ucsc.edu/cgi-bin/hgTracks?hgsid=132202298&clade=mammal&org=Human&db=hg18&position="
//<<chromosome<<":"<<index<<"-"<<index+200<<"&pix=800&Submit=submit\">View in Genome Browser</a> [external link]";
// chr5 12389181 12390000
return ss.str();
}
string AbstractGraph::FIND_StringFromMouseClick(int index)
{
int sample_length = ui->getWidth();
return sequence->substr(index, min(500, sample_length));
}