forked from samtools/samtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bam_tview_curses.c
329 lines (289 loc) · 11 KB
/
bam_tview_curses.c
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
/* bam_tview_curses.c -- curses tview implementation.
Copyright (C) 2008-2013 Genome Research Ltd.
Portions copyright (C) 2013 Pierre Lindenbaum, Institut du Thorax, INSERM U1087, Université de Nantes.
Author: Heng Li <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notices and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <config.h>
#include "bam_tview.h"
#ifdef HAVE_CURSES
#if defined HAVE_NCURSESW_CURSES_H
#include <ncursesw/curses.h>
#elif defined HAVE_NCURSESW_H
#include <ncursesw.h>
#elif defined HAVE_NCURSES_CURSES_H
#include <ncurses/curses.h>
#elif defined HAVE_NCURSES_H
#include <ncurses.h>
#elif defined HAVE_CURSES_H
#include <curses.h>
#else
// Have the library, but no header file
#warning "Curses header file not found; tview with curses is disabled."
#undef HAVE_CURSES
#endif
#else
#warning "No curses library is available; tview with curses is disabled."
#endif
#ifdef HAVE_CURSES
typedef struct CursesTview {
tview_t view;
WINDOW *wgoto, *whelp;
} curses_tview_t;
#define FROM_TV(ptr) ((curses_tview_t*)ptr)
static void curses_destroy(tview_t* base) {
curses_tview_t* tv=(curses_tview_t*)base;
delwin(tv->wgoto); delwin(tv->whelp);
endwin();
base_tv_destroy(base);
free(tv);
}
/*
void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
void (*my_)(struct AbstractTview*,int,int,int);
void (*my_attron)(struct AbstractTview*,int);
void (*my_attroff)(struct AbstractTview*,int);
void (*my_clear)(struct AbstractTview*);
int (*my_colorpair)(struct AbstractTview*,int);
*/
static void curses_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...) {
unsigned int size=tv->mcol+2;
char* str=malloc(size);
if(str==0) exit(EXIT_FAILURE);
va_list argptr;
va_start(argptr, fmt);
vsnprintf(str,size, fmt, argptr);
va_end(argptr);
mvprintw(y,x,str);
free(str);
}
static void curses_mvaddch(struct AbstractTview* tv,int y,int x,int ch) {
mvaddch(y,x,ch);
}
static void curses_attron(struct AbstractTview* tv,int flag) {
attron(flag);
}
static void curses_attroff(struct AbstractTview* tv,int flag) {
attroff(flag);
}
static void curses_clear(struct AbstractTview* tv) {
clear();
}
static int curses_init_colors(int inverse)
{
if (inverse) {
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_BLACK, COLOR_GREEN);
init_pair(3, COLOR_BLACK, COLOR_YELLOW);
init_pair(4, COLOR_BLACK, COLOR_WHITE);
init_pair(5, COLOR_BLACK, COLOR_GREEN);
init_pair(6, COLOR_BLACK, COLOR_CYAN);
init_pair(7, COLOR_WHITE, COLOR_MAGENTA);
init_pair(8, COLOR_WHITE, COLOR_RED);
init_pair(9, COLOR_WHITE, COLOR_BLUE);
} else {
init_pair(1, COLOR_BLUE, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_WHITE, COLOR_BLACK);
init_pair(5, COLOR_GREEN, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(7, COLOR_MAGENTA, COLOR_BLACK);
init_pair(8, COLOR_RED, COLOR_BLACK);
init_pair(9, COLOR_BLUE, COLOR_BLACK);
}
return 0;
}
static int curses_colorpair(struct AbstractTview* tv,int flag) {
return COLOR_PAIR(flag);
}
static int curses_drawaln(struct AbstractTview* tv, int tid, hts_pos_t pos) {
return base_draw_aln(tv, tid, pos);
}
static void tv_win_goto(curses_tview_t *tv, int *tid, hts_pos_t *pos) {
char str[256], *p;
int i, l = 0;
tview_t *base=(tview_t*)tv;
wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
mvwprintw(tv->wgoto, 1, 2, "Goto: ");
for (;;) {
int invalid = 0;
int c = wgetch(tv->wgoto);
wrefresh(tv->wgoto);
if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
if(l > 0) --l;
} else if (c == KEY_ENTER || c == '\012' || c == '\015') {
int _tid = -1;
hts_pos_t _beg, _end;
if (str[0] == '=') {
_beg = strtoll(str+1, &p, 10) - 1;
if (_beg > 0) {
*pos = _beg;
return;
}
} else {
if (sam_parse_region(base->header, str, &_tid, &_beg, &_end, 0) && _tid >= 0) {
*tid = _tid; *pos = _beg;
return;
}
}
// If we get here, the region string is invalid
invalid = 1;
} else if (isgraph(c)) {
if (l < TV_MAX_GOTO) str[l++] = c;
} else if (c == '\027') l = 0;
else if (c == '\033') return;
str[l] = '\0';
for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
if (invalid) mvwprintw(tv->wgoto, 1, TV_MAX_GOTO - 1, "[Invalid]");
mvwprintw(tv->wgoto, 1, 8, "%s", str);
}
}
static void tv_win_help(curses_tview_t *tv) {
int r = 1;
tview_t* base=(tview_t*)base;
WINDOW *win = tv->whelp;
wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
mvwprintw(win, r++, 2, " -=- Help -=- ");
r++;
mvwprintw(win, r++, 2, "? This window");
mvwprintw(win, r++, 2, "Arrows Small scroll movement");
mvwprintw(win, r++, 2, "h,j,k,l Small scroll movement");
mvwprintw(win, r++, 2, "H,J,K,L Large scroll movement");
mvwprintw(win, r++, 2, "ctrl-H Scroll 1k left");
mvwprintw(win, r++, 2, "ctrl-L Scroll 1k right");
mvwprintw(win, r++, 2, "space Scroll one screen");
mvwprintw(win, r++, 2, "backspace Scroll back one screen");
mvwprintw(win, r++, 2, "g Go to specific location");
mvwprintw(win, r++, 2, "m Color for mapping qual");
mvwprintw(win, r++, 2, "n Color for nucleotide");
mvwprintw(win, r++, 2, "b Color for base quality");
mvwprintw(win, r++, 2, "c Color for cs color");
mvwprintw(win, r++, 2, "z Color for cs qual");
mvwprintw(win, r++, 2, ". Toggle on/off dot view");
mvwprintw(win, r++, 2, "s Toggle on/off ref skip");
mvwprintw(win, r++, 2, "r Toggle on/off rd name");
mvwprintw(win, r++, 2, "N Turn on nt view");
mvwprintw(win, r++, 2, "C Turn on cs view");
mvwprintw(win, r++, 2, "i Toggle on/off ins");
mvwprintw(win, r++, 2, "v Inverse video");
mvwprintw(win, r++, 2, "q Exit");
r++;
mvwprintw(win, r++, 2, "Underline: Secondary or orphan");
mvwprintw(win, r++, 2, "Blue: 0-9 Green: 10-19");
mvwprintw(win, r++, 2, "Yellow: 20-29 White: >=30");
wrefresh(win);
wgetch(win);
}
static int curses_underline(tview_t* tv) {
return A_UNDERLINE;
}
static int curses_loop(tview_t* tv) {
int tid;
hts_pos_t pos;
curses_tview_t *CTV=(curses_tview_t *)tv;
tid = tv->curr_tid; pos = tv->left_pos;
while (1) {
int c = getch();
switch (c) {
case '?': tv_win_help(CTV); break;
case '\033':
case 'q': goto end_loop;
case '/':
case 'g': tv_win_goto(CTV, &tid, &pos); break;
case 'm': tv->color_for = TV_COLOR_MAPQ; break;
case 'b': tv->color_for = TV_COLOR_BASEQ; break;
case 'n': tv->color_for = TV_COLOR_NUCL; break;
case 'c': tv->color_for = TV_COLOR_COL; break;
case 'z': tv->color_for = TV_COLOR_COLQ; break;
case 'v': curses_init_colors(tv->inverse = !tv->inverse); break;
case 's': tv->no_skip = !tv->no_skip; break;
case 'r': tv->show_name = !tv->show_name; break;
case KEY_LEFT:
case 'h': --pos; break;
case KEY_RIGHT:
case 'l': ++pos; break;
case KEY_SLEFT:
case 'H': pos -= 20; break;
case KEY_SRIGHT:
case 'L': pos += 20; break;
case '.': tv->is_dot = !tv->is_dot; break;
case 'N': tv->base_for = TV_BASE_NUCL; break;
case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
case 'i': tv->ins = !tv->ins; break;
case '\010': pos -= 1000; break;
case '\014': pos += 1000; break;
case ' ': pos += tv->mcol; break;
case KEY_UP:
case 'j': --tv->row_shift; break;
case 'J': tv->row_shift -= 20; break;
case KEY_DOWN:
case 'k': ++tv->row_shift; break;
case 'K': tv->row_shift += 20; break;
case KEY_BACKSPACE:
case '\177': pos -= tv->mcol; break;
case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
default: continue;
}
if (pos < 0) pos = 0;
if (tv->row_shift < 0) tv->row_shift = 0;
tv->my_drawaln(tv, tid, pos);
}
end_loop:
return 0;
}
tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples,
const htsFormat *fmt) {
curses_tview_t *tv = (curses_tview_t*)calloc(1, sizeof(curses_tview_t));
tview_t* base=(tview_t*)tv;
if(tv==0) {
fprintf(stderr,"Calloc failed\n");
return 0;
}
base_tv_init(base,fn,fn_fa,NULL,samples,fmt);
/* initialize callbacks */
#define SET_CALLBACK(fun) base->my_##fun=curses_##fun;
SET_CALLBACK(destroy);
SET_CALLBACK(mvprintw);
SET_CALLBACK(mvaddch);
SET_CALLBACK(attron);
SET_CALLBACK(attroff);
SET_CALLBACK(clear);
SET_CALLBACK(colorpair);
SET_CALLBACK(drawaln);
SET_CALLBACK(loop);
SET_CALLBACK(underline);
#undef SET_CALLBACK
initscr();
keypad(stdscr, TRUE);
clear();
noecho();
cbreak();
getmaxyx(stdscr, base->mrow, base->mcol);
tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
tv->whelp = newwin(30, 40, 5, 5);
start_color();
curses_init_colors(0);
return base;
}
#else // !HAVE_CURSES
extern tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *fn_idx, const char *samples,
const htsFormat *fmt);
tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples,
const htsFormat *fmt) {
return text_tv_init(fn,fn_fa,NULL,samples,fmt);
}
#endif