-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequenceView.cpp
356 lines (291 loc) · 11.3 KB
/
sequenceView.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
// Copyright 2012 Egor Dolzhenko
//
// This file is a part of STAGR.
//
// STAGR is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QtGui>
#include "sequenceView.h"
SequenceView::SequenceView(QWidget* parent, HspLoci *hspLoci, QString newSequence)
:QGraphicsView(parent)
{
hsps = hspLoci;
//references to arrays containing alignment coordinates
sequenceLength = hspLoci->span() + 1; //don't need to copy
numberAlignments = hspLoci->getSize();
sequence = newSequence;
//width of each nucleotide
nucleotideWidth = 7;
//setup scene
scene = new QGraphicsScene(this);
setAlignment(Qt::AlignCenter);
setRenderHints(QPainter::Antialiasing|
QPainter::TextAntialiasing);
setScene(scene);
//timer is used for disabling display
//of nucleotide sequence during resizing
timer = new QTimer(this);
timer->setInterval(100);
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), this, SLOT(timerSlot()));
timerIterations = 0;
showNucleotides = true;
windowHeight = 0;
windowWidth = 0;
alwaysShowNucleotides = true;
}
void SequenceView::createScene(int width, int height)
{
//each track gets only one match at any given time
int alignmentTracks[numberAlignments];
//loci of the end of the track; useful to determine when a track becomes free so that
//another match can use it
int tracksEnd[numberAlignments];
//stores index of the highest track; useful to know when drawing the main sequence
int maxTrackIndex = 0;
//initialization of both arrays
for(int i = 0; i < numberAlignments; i++)
{
alignmentTracks[i] = 0;
tracksEnd[i] = 0;
}
//the following two nested loops assign tracks to alignments
//for every alignment
for(int matchIndex = 0; matchIndex < numberAlignments; matchIndex++)
{
//check every track
for(int trackIndex = 0; trackIndex < numberAlignments; trackIndex++)
{
//until find track that ends before this match starts
if(tracksEnd[trackIndex] <= hsps->uCoord(matchIndex)) //matchesStart[matchIndex]
{
//in this case assign the track to the current match
alignmentTracks[matchIndex] = trackIndex;
//and update the end of the track to be same as end of the match
tracksEnd[trackIndex] = hsps->dCoord(matchIndex); //matchesEnd[matchIndex]
//further, if track index is the highest seen so far, update
//maxTrackIndex with it
if(trackIndex > maxTrackIndex) maxTrackIndex = trackIndex;
break;
}
}
}
Block *block;
BlockType blockType;
// how many nucleotides will fit in each row
int nucleotidesPerRow = width/nucleotideWidth;
// required number of rows (each row consisit of spacers, tracks, and reference sequence) (rename rowsForMainSequence)
int numRows = sequenceLength / nucleotidesPerRow;
// if sequenceLength % width != 0 need an extra row
if (sequenceLength % nucleotidesPerRow != 0) numRows++;
// height of an individual track
int trackHeight = 20;
// height of a sequence
int sequenceHeight = 20;
// space between tracks
int gapBetweenTracks = 5;
// to be used as y coordinate in placing blocks
int currentHeight = 0;
//row height
int rowHeight = trackHeight*(maxTrackIndex + 1) + sequenceHeight +
gapBetweenTracks*(maxTrackIndex + 2);
QFont font1("Monospace");
font1.setStyleHint(QFont::TypeWriter);
setFont(font1);
QString firstRow = sequence.mid(0, nucleotidesPerRow);
font1 = setFontSize(nucleotideWidth, nucleotidesPerRow, firstRow, font1);
QGraphicsSimpleTextItem *text2;
//ACTGACTGACTGACTGACTGACTGACTGACTGACTGACTGACTGACTGACTGACTGACTGACTG
//1 -------------------------------------------------------------60
//61 -----------------------------------------------------------120
//121 ----------------------------------------------------------180
// match 1 thru 20
//for each row consider all possible matches and determine whether any
//blocks should be placed
for(int rowIndex = 0; rowIndex < numRows; rowIndex++)
{
for(int matchIndex = 0; matchIndex < numberAlignments; matchIndex++)
{
//color of the current match
QColor color = colorForMatchIndex(matchIndex);
//index of the first nucleotide in the current row
int rowBegin = rowIndex*nucleotidesPerRow + 1;
//index of the last nucleotide in the current row
int rowEnd = (rowIndex + 1)*nucleotidesPerRow;
//index of the first nucleotide in the match
int matchStart = hsps->uCoord(matchIndex); //matchesStart[matchIndex]
//index of the last nucleotide in the match
int matchEnd = hsps->dCoord(matchIndex); //matchesEnd[matchIndex]
//change index to make sure track with smaller index is closer to the
//sequence (change later)
int currentTrack = maxTrackIndex - alignmentTracks[matchIndex];
//use current track index to calculate the current track height
int currentTrackHeight = rowHeight*rowIndex + currentTrack*(trackHeight+gapBetweenTracks)
+ gapBetweenTracks;
//begins before this row but ends in this row
if( (matchStart < rowBegin) &&
(rowBegin <= matchEnd ) && (matchEnd <= rowEnd) )
{
if(hsps->dir(matchIndex) == LEFT) blockType = BLOCK_INCOMPLETE_LEFT;
else blockType = BLOCK_POINT_RIGHT_INCOMPLETE;
block = new Block((matchEnd - rowBegin + 1)*nucleotideWidth,
trackHeight, blockType, color);
block->setPos(QPoint(0,currentTrackHeight));
scene->addItem(block);
Annotate(0,currentTrackHeight, (matchEnd - rowBegin + 1)*nucleotideWidth, trackHeight, matchIndex);
}
//begins and ends in this row
else if ( (rowBegin <= matchStart) && ( matchEnd <= rowEnd ) )
{
if(hsps->dir(matchIndex) == LEFT) blockType = BLOCK_POINT_LEFT;
else blockType = BLOCK_POINT_RIGHT;
block = new Block((matchEnd - matchStart + 1)*nucleotideWidth,
trackHeight, blockType, color);
block->setPos(QPoint((matchStart - rowBegin)*nucleotideWidth, currentTrackHeight));
scene->addItem(block);
Annotate((matchStart - rowBegin)*nucleotideWidth, currentTrackHeight, (matchEnd - matchStart + 1)*nucleotideWidth, trackHeight, matchIndex);
}
//begins in this row but does not end in it
else if ( (rowBegin <= matchStart) && ( matchStart <= rowEnd )
&& (rowEnd < matchEnd) )
{
if(hsps->dir(matchIndex) == LEFT) blockType = BLOCK_POINT_LEFT_INCOMPLETE;
else blockType = BLOCK_INCOMPLETE_RIGHT;
block = new Block((rowEnd - matchStart + 1)*nucleotideWidth, trackHeight, blockType, color );
block->setPos(QPoint((matchStart-rowBegin)*nucleotideWidth,currentTrackHeight));
scene->addItem(block);
Annotate((matchStart - rowBegin)*nucleotideWidth,currentTrackHeight, (rowEnd - matchStart + 1)*nucleotideWidth, trackHeight, matchIndex);
}
//begins before this row and ends after this row
else if ( (matchStart < rowBegin) && (rowEnd < matchEnd) )
{
block = new Block(nucleotidesPerRow*nucleotideWidth,
trackHeight, BLOCK_INCOMPLETE_LEFT_RIGHT , color); //
block->setPos(QPoint(0, currentTrackHeight));
scene->addItem(block);
Annotate(0,currentTrackHeight,nucleotidesPerRow*nucleotideWidth,trackHeight, matchIndex);
}
}
int currentSequenceHeight = rowHeight*(rowIndex + 1) - sequenceHeight;
if(rowIndex != numRows - 1)
{
block = new Block((nucleotidesPerRow)*nucleotideWidth, sequenceHeight, BLOCK, Qt::white);
block->setPos(QPoint(0,currentSequenceHeight));
scene->addItem(block);
if( (showNucleotides)&&(alwaysShowNucleotides) )
{
text2 = new QGraphicsSimpleTextItem();
text2->setText(sequence.mid(rowIndex*nucleotidesPerRow, nucleotidesPerRow)); //QString( )
text2->setFont(font1);
text2->setPos(2,currentSequenceHeight + (sequenceHeight - text2->boundingRect().height())/2); //sequenceHeight/4
scene->addItem(text2);
}
}
else
{
block = new Block((sequenceLength - (numRows - 1)*nucleotidesPerRow)*nucleotideWidth, sequenceHeight, BLOCK, Qt::white);
block->setPos(QPoint(0,currentSequenceHeight));
scene->addItem(block);
if( (showNucleotides)&&(alwaysShowNucleotides) )
{
text2 = new QGraphicsSimpleTextItem();
text2->setText(sequence.mid(rowIndex*nucleotidesPerRow, sequenceLength - (numRows - 1)*nucleotidesPerRow));
text2->setFont(font1);
text2->setPos(2, currentSequenceHeight + (sequenceHeight - text2->boundingRect().height())/2);
scene->addItem(text2);
}
}
}
scene->setSceneRect(-10, 0, width + 10, rowHeight*numRows + gapBetweenTracks);
}
void SequenceView::clearScene()
{
QList<QGraphicsItem *> list = scene->items();
QList<QGraphicsItem *>::Iterator iterator = list.begin();
for ( ; iterator != list.end(); ++iterator)
{
if ( *iterator )
{
scene->removeItem(*iterator);
delete *iterator;
}
}
}
void SequenceView::resizeEvent(QResizeEvent* event)
{
timer->stop();
timer->start();
showNucleotides = false;
clearScene();
createScene(event->size().width()-70, event->size().height() - 150);
windowWidth = event->size().width()-70;
windowHeight = event->size().height()-150;
QGraphicsView::resizeEvent(event);
}
QColor SequenceView::colorForMatchIndex(int index)
{
QColor colors[5];
colors[0] = QColor(255, 77, 65);
colors[1] = QColor(242, 147, 31);
colors[2] = QColor(229, 202, 33);
colors[3] = QColor(145, 178, 33);
colors[4] = QColor(30, 140, 101);
return colors[index % 5];
}
void SequenceView::Annotate(int x, int y, int width, int height, int matchIndex)
{
QGraphicsSimpleTextItem *text = new QGraphicsSimpleTextItem(hsps->id(matchIndex)); //matchNames[matchIndex]
text->setPos(x + 3, y + (height - text->boundingRect().height())/2);
if((x + 3 + text->boundingRect().width()) <= x + width) scene->addItem(text);
}
QFont SequenceView::setFontSize(int nucleotideWidth, int nucleotidesPerRow, QString firstRow, QFont font1)
{
float factor = (float)nucleotideWidth / (float)fontMetrics().width('A');
QFont f("Monospace");
f.setStyleHint(QFont::TypeWriter);
f.setPointSizeF(f.pointSizeF()*factor);
setFont(f);
f.setLetterSpacing(QFont::AbsoluteSpacing, (float)(nucleotideWidth*nucleotidesPerRow - fontMetrics().width(firstRow) - 3)/(float)firstRow.length());
return f;
}
void SequenceView::timerSlot()
{
showNucleotides = true;
clearScene();
createScene(windowWidth, windowHeight);
}
void SequenceView::keyPressEvent(QKeyEvent *event)
{
if( (event->key() == Qt::Key_Equal) && (event->modifiers() == Qt::ControlModifier) )
{
if( nucleotideWidth >= 8) alwaysShowNucleotides = true;
if( nucleotideWidth <= 11)
{
QResizeEvent *re = new QResizeEvent(size(), size());
nucleotideWidth++; resizeEvent(re);
}
}
if( (event->key() == Qt::Key_Minus) && (event->modifiers() == Qt::ControlModifier) )
{
if(nucleotideWidth < 8)
{
alwaysShowNucleotides = false;
}
if( nucleotideWidth > 1)
{
nucleotideWidth--;
QResizeEvent *re = new QResizeEvent(size(), size());
resizeEvent(re);
}
}
}