-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSeqWindow.cpp
More file actions
378 lines (358 loc) · 12.4 KB
/
SeqWindow.cpp
File metadata and controls
378 lines (358 loc) · 12.4 KB
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
371
372
373
374
375
376
377
378
#include<seq_view.h>
namespace SeqView {
void SeqWindow::_scroll(int64_t newleft, int64_t newtop) {
first_seq = newtop;
first_pos = newleft;
modified = true;
display();
}
// Given the current window size and the width
// of the names, figure out how many sequences and positions
// should be displayed. Also recalculate last_pos and last_seq.
void SeqWindow::_recalculate_num_displayed() {
// Need to reserve three lines - two for positions and one
// for the filename
// Need to reserve names_width + 1 columns for names
num_pos_displayed = width - names_width - 1;
num_seqs_displayed = height - 3;
last_pos = (first_pos + num_pos_displayed - 1) * transl_adj;
last_seq = first_seq + num_seqs_displayed - 1;
modified = true;
}
void SeqWindow::_display_names() {
if(isfocal)
wattron(window, A_BOLD);
wattron(window, COLOR_PAIR(7));
std::vector<string> names = seqs.nameSlice(first_seq, last_seq);
for(int i = 0; i < num_seqs_displayed - 1; i++) {
mvwprintw(window, i+2, 0, string(names_width + 1, ' ').c_str());
if((unsigned)names_width > names[i].length())
mvwprintw(window, i+2, 0, names[i].c_str());
else
mvwprintw(window, i + 2, 0,
names[i].substr(0, names_width).c_str());
}
if(isfocal)
wattroff(window, A_BOLD);
wattroff(window, COLOR_PAIR(7));
}
void SeqWindow::_display_positions() {
if(isfocal)
wattron(window, A_BOLD);
wattron(window, COLOR_PAIR(7));
for(int i = 0; i < width; i++) {
mvwprintw(window, 0, i, " ");
mvwprintw(window, 1, i, " ");
}
int col = names_width + 1;
int frame = seqs.get_frame();
for(int i = first_pos + 1; i < last_pos + 2; i++) {
i+=(transl_adj-1);
if(!((i/transl_adj) % 10)) {
mvwprintw(window, 1, col, "|");
mvwprintw(window, 0, col, "%i", i/transl_adj);
} else if(!(i % 5)) {
mvwprintw(window, 1, col, ":");
} else {
mvwprintw(window, 1, col, ".");
}
if(display_mode == CODON) {
if((i - frame + 1) % 3 == 0) {
col++;
mvwprintw(window, 1, col, " ");
}
}
if(display_mode == TEN) {
if(i % 10 == 0) {
col++;
mvwprintw(window, 1, col, " ");
}
}
// TODO: fix numbering for translation mode
col++;
if(col >= width)
break;
}
// Sometimes the numbers spill over to the next line - this
// gets rid of that.
mvwprintw(window, 1, 0, string(names_width, ' ').c_str());
if(isfocal)
wattroff(window, A_BOLD);
wattroff(window, COLOR_PAIR(7));
}
void SeqWindow::_display_seqs() {
// This function is more tricky - will require some added
// arguments for formatting eventually.
// Also may require some adjustment to color amino acid
// sequences properly.
std::pair< std::vector<string>, std::vector<bool> >
s = seqs.slice(first_pos, last_pos, first_seq, last_seq,
display_mode, compare);
std::vector<string> sequences = s.first;
std::vector<bool> comps = s.second;
int row = 2;
char ch;
int col;
if(bolded)
wattron(window, A_BOLD);
for(int i = 0; i < num_seqs_displayed - 1; i++) {
for(unsigned j = 0; j < sequences[i].length(); j++) {
ch = sequences[i][j];
if(compare != NOCOMPARE && comps[j] && (i + first_seq) < seqs.numseqs() && ch != ' ')
wattron(window, A_REVERSE);
ColMap cmap;
if(display_mode == TRANSLATE) {
cmap = aa_colors;
} else {
cmap = dna_colors;
}
ColMapIt cit = cmap.find(ch);
if(cit != dna_colors.end()) {
col = cit->second;
} else {
col = cmap.find('*')->second; // Don't know why dna_colors['*'] doesn't work
}
wattron(window, COLOR_PAIR(col));
mvwaddch(window, row, names_width + 1 + j, ch);
wattroff(window, COLOR_PAIR(col));
if(compare != NOCOMPARE && comps[j])
wattroff(window, A_REVERSE);
}
row++;
}
if(bolded)
wattroff(window, A_BOLD);
}
void SeqWindow::_display_filename() {
// display at bottom
wattron(window, COLOR_PAIR(7) | A_UNDERLINE);
if(isfocal)
wattron(window, A_REVERSE | A_BOLD);
mvwprintw(window, height - 2, 0, seqs.filename.c_str());
for(int i = seqs.filename.length() + 1; i <= width; i++)
mvwprintw(window, height - 2, i - 1, " ");
if(isfocal)
wattroff(window, A_REVERSE | A_BOLD);
wattroff(window, COLOR_PAIR(7) | A_UNDERLINE);
}
SeqWindow::SeqWindow() {
modified = true;
compare = NOCOMPARE;
pcomp = NUCAMB;
bolded = false;
transl_adj = 1;
}
SeqWindow::SeqWindow(int upperleftX, int upperleftY,
int _width, int _height, SeqSet &sq) {
window = newwin(_height, _width, upperleftY, upperleftX);
width = _width;
height = _height;
seqs = sq;
scrollmode = 1;
display_mode = NORMAL;
names_width = 15;
first_pos = 0;
first_seq = 0;
update_size();
_recalculate_num_displayed();
isfocal = true;
bolded = false;
modified = true;
compare = NOCOMPARE;
pcomp = NUCAMB;
transl_adj = 1;
display();
}
SeqWindow::SeqWindow(int upperleftX, int upperleftY,
int _width, int _height, string filename,
ParserFunction parser) {
SeqSet sq;
parser(filename, sq);
window = newwin(_height, _width, upperleftY, upperleftX);
width = _width + 1;
height = _height + 1;
seqs = sq;
scrollmode = 1;
display_mode = NORMAL;
names_width = 15;
first_pos = 0;
first_seq = 0;
update_size();
_recalculate_num_displayed();
bolded = false;
modified = true;
compare = NOCOMPARE;
pcomp = NUCAMB;
transl_adj = 1;
display();
}
SeqWindow::~SeqWindow() {
wattron(window, COLOR_PAIR(8));
for(int i = 0; i < height; i++) {
mvwprintw(window, i, 0, string(width, ' ').c_str());
}
wattroff(window, COLOR_PAIR(8));
wrefresh(window);
delwin(window);
}
int64_t SeqWindow::numseqs() {
return seqs.numseqs();
}
void SeqWindow::set_focus(bool focal) {
isfocal = focal;
modified = true;
}
// scroll mode is in powers of 10
void SeqWindow::set_scroll_mode(int mode) {
if(mode < 0 || mode > 9)
return;
scrollmode = pow(10, mode);
}
void SeqWindow::update_size() {
getmaxyx(window, height, width);
height += 1;
width += 1;
}
void SeqWindow::change_name_width(int newwidth) {
if(newwidth < width - 5 && newwidth > 1) {
names_width = newwidth;
update_size();
_recalculate_num_displayed();
}
}
// Deal with commands that change SeqSet params
void SeqWindow::handle_command(Command command) {
Com com_name = command.first;
int param = command.second;
int newpos;
if(com_name == SCROLLUP) {
if(first_seq > 0) {
newpos = first_seq - param;
if(newpos < 0)
newpos = 0;
_scroll(first_pos, newpos);
}
} else if(com_name == SCROLLDOWN) {
if(first_seq < seqs.numseqs()) {
newpos = first_seq + param;
if(newpos >= seqs.numseqs())
newpos = seqs.numseqs() - 1;
_scroll(first_pos, newpos);
}
} else if(com_name == SCROLLLEFT) {
if(first_pos > 0) {
newpos = first_pos - scrollmode * param * transl_adj;
if(newpos < 0)
newpos = 0;
_scroll(newpos, first_seq);
}
} else if(com_name == SCROLLRIGHT) {
if(first_pos < seqs.length()) {
newpos = first_pos + scrollmode * param * transl_adj;
if(newpos >= seqs.length())
newpos = seqs.length() - 1;
_scroll(newpos, first_seq);
}
} else if(com_name == SCROLLTOP) {
_scroll(first_pos, 0);
} else if(com_name == SCROLLBOTTOM) {
_scroll(first_pos, seqs.numseqs() - 1);
} else if(com_name == GOTOBEGIN) {
_scroll(0, first_seq);
} else if(com_name == GOTOEND) {
_scroll((seqs.length() - 1), first_seq);
} else if(com_name == GOTO) {
newpos = param - 1 - (width - names_width) / 2;
if(display_mode == CODON)
newpos += (width - names_width) / 6;
if(display_mode == TEN)
newpos += (width - names_width) / 20;
if(newpos < 0)
newpos = 0;
if(newpos > seqs.length() - 1)
newpos = seqs.length() - 1;
_scroll(newpos, first_seq);
} else if(com_name == SCROLLMODE) {
set_scroll_mode(param);
} else if(com_name == NAMEWIDTH) {
if(param > 0) {
change_name_width(param);
} else if(param == -1) {
change_name_width(names_width-1);
} else if(param == 0) {
change_name_width(names_width+1);
}
} else if(com_name == DISPLAYMODE) {
transl_adj = 1;
if(param == 1) {
display_mode = NORMAL;
} else if(param == 2) {
display_mode = CODON;
} else if(param == 3) {
display_mode = TEN;
} else if(param == 4) {
//first_pos /= 3;
transl_adj = 3;
display_mode = TRANSLATE;
}
modified = true;
} else if(com_name == SETFRAME) {
seqs.set_frame(param);
modified = true;
} else if(com_name == COMPARETOGGLE) {
if(compare == NOCOMPARE) {
compare = pcomp;
} else {
pcomp = compare;
compare = NOCOMPARE;
}
modified = true;
} else if(com_name == COMPARE) {
if(param == NOCOMPARE) {
pcomp = compare;
compare = NOCOMPARE;
} else {
compare = (ComparisonMode) param;
pcomp = (ComparisonMode) param;
}
} else if(com_name == TOGGLEBOLD) {
bolded = !bolded;
modified = true;
}
}
void SeqWindow::resize(int upperleftX, int upperleftY,
int newwidth, int newheight) {
wattron(window, COLOR_PAIR(8));
for(int i = 0; i < height; i++) {
mvwprintw(window, i, 0, string(width, ' ').c_str());
}
wrefresh(window);
delwin(window);
window = newwin(newheight, newwidth,
upperleftY, upperleftX);
height = newheight;
width = newwidth;
update_size();
_recalculate_num_displayed();
modified = true;
display();
}
// Refresh, get the slice of the sequence, add the formatting,
// and use wprintw to put everything on the screen.
void SeqWindow::display() {
int w = width;
int h = height;
update_size();
if(w != width || h != height)
modified = true;
if(modified) {
_recalculate_num_displayed();
_display_names();
_display_positions();
_display_seqs();
_display_filename();
wrefresh(window);
} else {
}
}
}