Created
March 25, 2019 16:34
-
-
Save lindenb/13b8f022f9a09ba4c067f8f3c87a924c to your computer and use it in GitHub Desktop.
bam x11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <X11/Xlib.h> | |
| #include <cstdio> | |
| #include <cstdlib> | |
| #include <cstring> | |
| #include <string> | |
| #include <vector> | |
| #include <iostream> | |
| #include <fstream> | |
| #include <limits.h> | |
| #include <unistd.h> | |
| #include <getopt.h> | |
| #include <htslib/sam.h> | |
| #include <htslib/faidx.h> | |
| #include <htslib/kstring.h> | |
| #include <htslib/khash_str2int.h> | |
| using namespace std; | |
| class App; | |
| class BamW | |
| { | |
| public: | |
| App* owner; | |
| string filename; | |
| hts_idx_t *idx = NULL; | |
| vector<double> depth; | |
| int y; | |
| BamW(App* owner,string fn,htsFormat* fmt,int y):owner(owner),filename(fn),idx(0),y(y) | |
| { | |
| sam_open_format(fn.c_str(), "r",fmt); // open BAM | |
| idx = sam_index_load(data[i]->fp, argv[optind+i]); | |
| } | |
| ~BamW() { | |
| ::hts_idx_destroy(idx); | |
| ::sam_close(data[i]->fp); | |
| } | |
| void rescan() { | |
| data[i]->iter = sam_itr_querys(idx, data[i]->hdr, reg); // set the iterator | |
| depth.clear(); | |
| depth.resize(owner->window_width,0.0); | |
| } | |
| }; | |
| class App | |
| { | |
| public: | |
| vector<BamW*> bams; | |
| vector<string> regions; | |
| size_t region_idx; | |
| Display *display; | |
| Screen *screen; | |
| Window window; | |
| unsigned int window_width; | |
| unsigned int window_height; | |
| App() { | |
| } | |
| int doWork(int argc,char** argv); | |
| ~App(); | |
| }; | |
| void App::resized() { | |
| int x,y; | |
| unsigned int w,h, bw, d; | |
| ::XGetGeometry(display, window, &wr, &x, &y, &w, &h, &bw, &d); | |
| if(w!=this->window_width || h!=his->window_height) { | |
| this->window_width = w; | |
| this->window_height = h; | |
| repaint(); | |
| } | |
| } | |
| void App:repaint() { | |
| ::XClearWindow(this->display, this->window); | |
| } | |
| int App::doWork(int argc,char** argv) { | |
| int n; | |
| htsFormat format; | |
| char *bam_list = NULL; | |
| static const struct option lopts[] = { | |
| {"bams",required_argument,NULL,'B'}, | |
| { NULL, 0, NULL, 0 } | |
| }; | |
| // parse the command line | |
| while ((n = getopt_long(argc, argv, "B:", lopts, NULL)) >= 0) { | |
| } | |
| if(bam_list == NULL) { | |
| cerr << "List of bams is undefined" << endl; | |
| return EXIT_FAILURE; | |
| } | |
| ifstream bamin(bam_list); | |
| if(!bamin.is_open()) { | |
| cerr << "Cannot open " << bam_list << endl; | |
| return EXIT_FAILURE; | |
| } | |
| string line; | |
| while(getline(bamin,line)) { | |
| if(line.empty() || line[0]=='#') continue; | |
| BamW* bamFile = new BamW(this,line,&format,this->bams.size()); | |
| this->bams.push_back(bamFile); | |
| } | |
| bamin.close(); | |
| if(this->bams.empty()) { | |
| cerr << "List of bams is empty." << endl; | |
| return EXIT_FAILURE; | |
| } | |
| return 0; | |
| } | |
| App::~App() { | |
| size_t i; | |
| for(i=0;i< this->bams.size();i++) delete this->bams[i]; | |
| this->bams.clear(); | |
| } | |
| int main(int argc,char** argv) { | |
| Display *display; | |
| Screen *screen; | |
| Window w; | |
| XEvent e; | |
| const char *msg = "Hello, World!"; | |
| int s; | |
| display = XOpenDisplay(NULL); | |
| if (display == NULL) { | |
| fprintf(stderr, "Cannot open display\n"); | |
| exit(EXIT_FAILURE); | |
| } | |
| s = DefaultScreen(display); | |
| printf("%d.",s); | |
| screen = ScreenOfDisplay(display, 0); | |
| w = XCreateSimpleWindow(display, RootWindow(display, s), 150, 150, screen->width-300, screen->height-300, 1, | |
| BlackPixel(display, s), WhitePixel(display, s)); | |
| XSelectInput(display, w, ExposureMask | KeyPressMask); | |
| XMapWindow(display, w); | |
| while (1) { | |
| XNextEvent(display, &e); | |
| if (e.type == Expose) { | |
| this->resized(); | |
| XFillRectangle(display, w, DefaultGC(display, s), 20, 20, 10, 10); | |
| XDrawString(display, w, DefaultGC(display, s), 10, 50, msg, strlen(msg)); | |
| } | |
| if (e.type == KeyPress) | |
| { | |
| if (e.xkey.keycode == XKeysymToKeycode(video.screens[0].display, XK_Q)) | |
| { | |
| break; | |
| } | |
| if (e.xkey.keycode == XKeysymToKeycode(video.screens[0].display, XK_leftarrow)) | |
| { | |
| region_idx = (region_idx==0UL?regions.size()-1:region_idx-1); | |
| repaint(); | |
| } | |
| if (e.xkey.keycode == XKeysymToKeycode(video.screens[0].display, XK_rightarrow)) | |
| { | |
| region_idx = (region_idx+1>=regions.size():0:region_idx+1); | |
| repaint(); | |
| } | |
| } | |
| } | |
| XCloseDisplay(display); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment