| 1 |
|
| #include <string.h> |
| 2 |
|
| #include <stdarg.h> |
| 3 |
|
| #include <stdio.h> |
| 4 |
|
| #include <stdlib.h> |
| 5 |
|
| #include <errno.h> |
| 6 |
|
| #include <string> |
| 7 |
|
| #include <vector> |
| 8 |
|
| |
| 9 |
|
| #include <sys/time.h> |
| 10 |
|
| #include <unistd.h> |
| 11 |
|
| |
| 12 |
|
| #include <sparsehash/dense_hash_map> |
| 13 |
|
| |
| 14 |
|
| #include "fastq-lib.h" |
| 15 |
|
| #include "utils.h" |
| 16 |
|
| #include "tidx.h" |
| 17 |
|
| |
| 18 |
|
| void usage(FILE *f); |
| 19 |
|
| |
| 20 |
|
| using namespace std; |
| 21 |
|
| using namespace google; |
| 22 |
|
| |
| 23 |
0 |
| int main (int argc, char **argv) { |
| 24 |
0 |
| bool debug = false; |
| 25 |
0 |
| bool echo = false; |
| 26 |
0 |
| bool build = false; |
| 27 |
0 |
| vector<const char *> vin; |
| 28 |
0 |
| const char *ain= NULL; |
| 29 |
0 |
| const char *sep = "\t"; |
| 30 |
0 |
| const char *msep = "^"; |
| 31 |
0 |
| const char *trim = "chr"; |
| 32 |
0 |
| char *point = NULL; |
| 33 |
0 |
| int nchr = 1, nbeg = 2, nend = 3; |
| 34 |
0 |
| char skip_c = '#'; |
| 35 |
0 |
| char sub_e = 0; |
| 36 |
0 |
| int skip_i = 0; |
| 37 |
0 |
| bool dump = 0; |
| 38 |
|
| |
| 39 |
|
| char c; |
| 40 |
0 |
▶ | while ( (c = getopt (argc, argv, "Dlhdt:r:c:b:T:e:p:i:s:a:nB")) != -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( (c = getopt (argc, argv, "Dlhdt:r:c:b:T:e:p:i:s:a:nB")) != -1) { |
0 |
0 |
0 |
|
| 41 |
0 |
| switch (c) { |
| 42 |
0 |
| case 'd': |
| 43 |
0 |
| debug=true; break; |
| 44 |
0 |
| case 'D': |
| 45 |
0 |
| dump=true; break; |
| 46 |
0 |
| case 'n': |
| 47 |
0 |
| echo=false; break; |
| 48 |
0 |
| case 'l': |
| 49 |
0 |
| sub_e=true; break; |
| 50 |
0 |
| case 'B': |
| 51 |
0 |
| build = true; break; |
| 52 |
0 |
| case 'h': |
| 53 |
0 |
▶ | usage(stdout); exit(0); |
Branch: 1 branch
| Branch | True | False | Total |
| usage(stdout); exit(0); |
0 |
0 |
0 |
|
| 54 |
0 |
| case 't': |
| 55 |
0 |
| sep = optarg; break; |
| 56 |
0 |
| case 'p': |
| 57 |
0 |
| point = optarg; break; |
| 58 |
0 |
| case 's': |
| 59 |
0 |
▶ | if (isdigit(*optarg)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (isdigit(*optarg)) |
0 |
0 |
0 |
|
| 60 |
0 |
| skip_i = atoi(optarg); |
| 61 |
|
| else |
| 62 |
0 |
| skip_c = *optarg; |
| 63 |
0 |
| break; |
| 64 |
0 |
| case 'r': |
| 65 |
0 |
| msep = optarg; break; |
| 66 |
0 |
| case 'T': |
| 67 |
0 |
| trim = optarg; break; |
| 68 |
0 |
| case 'c': |
| 69 |
0 |
| nchr = atoi(optarg); break; |
| 70 |
0 |
| case 'i': |
| 71 |
0 |
▶ | vin.push_back(optarg); break; |
Branch: 1 branch
| Branch | True | False | Total |
| vin.push_back(optarg); break; |
0 |
0 |
0 |
|
| 72 |
0 |
| case 'a': |
| 73 |
0 |
| ain = optarg; break; |
| 74 |
0 |
| case 'b': |
| 75 |
0 |
| nbeg = atoi(optarg); break; |
| 76 |
0 |
| case 'e': |
| 77 |
0 |
| nend = atoi(optarg); break; |
| 78 |
0 |
| case '?': |
| 79 |
0 |
▶ | if (strchr("tncbe", optopt)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strchr("tncbe", optopt)) |
0 |
0 |
0 |
|
| 80 |
0 |
▶ | fprintf (stderr, "Option -%c requires an argument.\n", optopt); |
Branch: 1 branch
| Branch | True | False | Total |
| fprintf (stderr, "Option -%c requires an argument.\n", optopt); |
0 |
0 |
0 |
|
| 81 |
0 |
▶ | else if (isprint(optopt)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (isprint(optopt)) |
0 |
0 |
0 |
|
| 82 |
0 |
▶ | fprintf (stderr, "Unknown option `-%c'.\n", optopt); |
Branch: 1 branch
| Branch | True | False | Total |
| fprintf (stderr, "Unknown option `-%c'.\n", optopt); |
0 |
0 |
0 |
|
| 83 |
|
| else |
| 84 |
0 |
▶ | fprintf (stderr, |
Branch: 1 branch
| Branch | True | False | Total |
| fprintf (stderr, |
0 |
0 |
0 |
|
| 85 |
|
| "Unknown option character `\\x%x'.\n", |
| 86 |
|
| optopt); |
| 87 |
0 |
▶ | usage(stderr); |
Branch: 1 branch
| Branch | True | False | Total |
| usage(stderr); |
0 |
0 |
0 |
|
| 88 |
0 |
| return 1; |
| 89 |
|
| } |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
0 |
▶ | if (!vin.size()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vin.size()) { |
0 |
0 |
0 |
|
| 93 |
0 |
▶ | if (argc>1) warn("Error: at least one -i index file is required\n"); usage(stderr); exit(1); |
Branch: 3 branches
| Branch | True | False | Total |
| if (argc>1) warn("Error: at least one -i index file is required\n"); usage(stderr); exit(1); |
0 |
0 |
0 |
| if (argc>1) warn("Error: at least one -i index file is required\n"); usage(stderr); exit(1); |
0 |
0 |
0 |
| if (argc>1) warn("Error: at least one -i index file is required\n"); usage(stderr); exit(1); |
0 |
0 |
0 |
|
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
0 |
▶ | if (! build && ! ain && !point && !dump) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (! build && ! ain && !point && !dump) { |
0 |
0 |
0 |
| if (! build && ! ain && !point && !dump) { |
0 |
0 |
0 |
| if (! build && ! ain && !point && !dump) { |
0 |
0 |
0 |
| if (! build && ! ain && !point && !dump) { |
0 |
0 |
0 |
|
| 97 |
0 |
▶ | fail("Error: one of -D -B, -p or -a is required\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| fail("Error: one of -D -B, -p or -a is required\n"); |
0 |
0 |
0 |
|
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
0 |
▶ | if ((!!build + !!ain + !!point + !!dump) > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((!!build + !!ain + !!point + !!dump) > 1) { |
0 |
0 |
0 |
|
| 101 |
0 |
▶ | warn("Error: only one of -B, -p or -a is allowed\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Error: only one of -B, -p or -a is allowed\n"); |
0 |
0 |
0 |
|
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
0 |
| --nchr; --nbeg; --nend; |
| 105 |
|
| |
| 106 |
0 |
▶ | if ( build ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( build ) { |
0 |
0 |
0 |
|
| 107 |
|
| int f_i; |
| 108 |
0 |
▶ | for (f_i=0;f_i<vin.size();++f_i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (f_i=0;f_i<vin.size();++f_i) { |
0 |
0 |
0 |
|
| 109 |
0 |
▶ | tidx x; |
Branch: 1 branch
| Branch | True | False | Total |
| tidx x; |
0 |
0 |
0 |
|
| 110 |
0 |
▶ | if (debug) |
Branch: 1 branch
| Branch | True | False | Total |
| if (debug) |
0 |
0 |
0 |
|
| 111 |
0 |
| x.debug=true; |
| 112 |
0 |
▶ | x.build(vin[f_i], sep, nchr, nbeg, nend, skip_i, skip_c, sub_e); |
Branch: 1 branch
| Branch | True | False | Total |
| x.build(vin[f_i], sep, nchr, nbeg, nend, skip_i, skip_c, sub_e); |
0 |
0 |
0 |
|
| 113 |
0 |
| } |
| 114 |
|
| } else { |
| 115 |
0 |
| struct line l; meminit(l); |
| 116 |
0 |
| int nl = 0; |
| 117 |
|
| int f_i; |
| 118 |
0 |
▶ | vector<tidx *>vmap; vmap.resize(vin.size()); |
Branch: 1 branch
| Branch | True | False | Total |
| vector<tidx *>vmap; vmap.resize(vin.size()); |
0 |
0 |
0 |
|
| 119 |
0 |
▶ | for (f_i=0;f_i<vin.size();++f_i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (f_i=0;f_i<vin.size();++f_i) { |
0 |
0 |
0 |
|
| 120 |
0 |
▶ | vmap[f_i]=new tidx(vin[f_i]); |
Branch: 3 branches
| Branch | True | False | Total |
| vmap[f_i]=new tidx(vin[f_i]); |
0 |
0 |
0 |
| vmap[f_i]=new tidx(vin[f_i]); |
0 |
0 |
0 |
| vmap[f_i]=new tidx(vin[f_i]); |
0 |
0 |
0 |
|
| 121 |
0 |
▶ | if (debug) |
Branch: 1 branch
| Branch | True | False | Total |
| if (debug) |
0 |
0 |
0 |
|
| 122 |
0 |
| vmap[f_i]->debug=true; |
| 123 |
0 |
▶ | if (dump) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dump) { |
0 |
0 |
0 |
|
| 124 |
0 |
▶ | vmap[f_i]->dump(stdout); |
Branch: 1 branch
| Branch | True | False | Total |
| vmap[f_i]->dump(stdout); |
0 |
0 |
0 |
|
| 125 |
|
| } |
| 126 |
|
| } |
| 127 |
0 |
▶ | if (dump) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dump) { |
0 |
0 |
0 |
|
| 128 |
0 |
| exit(0); |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
0 |
▶ | if ( point ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( point ) { |
0 |
0 |
0 |
|
| 132 |
0 |
| char * p = strchr(point, ':'); |
| 133 |
0 |
▶ | if (!p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) { |
0 |
0 |
0 |
|
| 134 |
0 |
▶ | fail("Error: -p requires chr:pos argument\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| fail("Error: -p requires chr:pos argument\n"); |
0 |
0 |
0 |
|
| 135 |
|
| } |
| 136 |
0 |
| *p++ = '\0'; |
| 137 |
0 |
| long pos = atol(p); |
| 138 |
0 |
| int found = 0; |
| 139 |
0 |
▶ | for (f_i=0;f_i<vin.size();++f_i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (f_i=0;f_i<vin.size();++f_i) { |
0 |
0 |
0 |
|
| 140 |
0 |
▶ | string tmp = vmap[f_i]->lookup(point, pos, msep); |
Branch: 1 branch
| Branch | True | False | Total |
| string tmp = vmap[f_i]->lookup(point, pos, msep); |
0 |
0 |
0 |
|
| 141 |
0 |
▶ | if (tmp.size()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp.size()) { |
0 |
0 |
0 |
|
| 142 |
0 |
| ++found; |
| 143 |
0 |
▶ | fputs(tmp.c_str()+(found==1),stdout); // echo |
Branch: 1 branch
| Branch | True | False | Total |
| fputs(tmp.c_str()+(found==1),stdout); // echo |
0 |
0 |
0 |
|
| 144 |
|
| } |
| 145 |
0 |
| } |
| 146 |
0 |
▶ | if (found) fputc('\n',stdout); |
Branch: 2 branches
| Branch | True | False | Total |
| if (found) fputc('\n',stdout); |
0 |
0 |
0 |
| if (found) fputc('\n',stdout); |
0 |
0 |
0 |
|
| 147 |
0 |
| return !found; |
| 148 |
|
| } else { |
| 149 |
0 |
▶ | FILE *fin = !strcmp(ain,"-") ? stdin : fopen(ain, "r"); |
Branch: 2 branches
| Branch | True | False | Total |
| FILE *fin = !strcmp(ain,"-") ? stdin : fopen(ain, "r"); |
0 |
0 |
0 |
| FILE *fin = !strcmp(ain,"-") ? stdin : fopen(ain, "r"); |
0 |
0 |
0 |
|
| 150 |
0 |
▶ | if (!fin) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fin) |
0 |
0 |
0 |
|
| 151 |
0 |
▶ | fail("error '%s':%s", ain,strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| fail("error '%s':%s", ain,strerror(errno)); |
0 |
0 |
0 |
|
| 152 |
|
| |
| 153 |
0 |
▶ | while (read_line(fin, l)>0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (read_line(fin, l)>0) { |
0 |
0 |
0 |
| while (read_line(fin, l)>0) { |
0 |
0 |
0 |
|
| 154 |
0 |
| ++nl; |
| 155 |
|
| |
| 156 |
0 |
▶ | chomp_line(l); |
Branch: 1 branch
| Branch | True | False | Total |
| chomp_line(l); |
0 |
0 |
0 |
|
| 157 |
|
| |
| 158 |
0 |
▶ | fputs(l.s,stdout); // echo |
Branch: 1 branch
| Branch | True | False | Total |
| fputs(l.s,stdout); // echo |
0 |
0 |
0 |
|
| 159 |
|
| |
| 160 |
0 |
▶ | vector<char *> v = split(l.s, sep); // todo, only get the keys desired, don't destroy |
Branch: 1 branch
| Branch | True | False | Total |
| vector<char *> v = split(l.s, sep); // todo, only get the keys desired, don't destroy |
0 |
0 |
0 |
|
| 161 |
|
| |
| 162 |
0 |
| string res; |
| 163 |
0 |
▶ | if (v.size() > nchr && v.size() > nbeg) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (v.size() > nchr && v.size() > nbeg) { |
0 |
0 |
0 |
| if (v.size() > nchr && v.size() > nbeg) { |
0 |
0 |
0 |
| if (v.size() > nchr && v.size() > nbeg) { |
0 |
0 |
0 |
|
| 164 |
0 |
▶ | for (f_i=0;f_i<vin.size();++f_i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (f_i=0;f_i<vin.size();++f_i) { |
0 |
0 |
0 |
|
| 165 |
0 |
▶ | string tmp = vmap[f_i]->lookup(v[nchr], atol(v[nbeg]), msep); |
Branch: 1 branch
| Branch | True | False | Total |
| string tmp = vmap[f_i]->lookup(v[nchr], atol(v[nbeg]), msep); |
0 |
0 |
0 |
|
| 166 |
0 |
▶ | if (tmp.size()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp.size()) { |
0 |
0 |
0 |
|
| 167 |
0 |
▶ | res = res + tmp; |
Branch: 1 branch
| Branch | True | False | Total |
| res = res + tmp; |
0 |
0 |
0 |
|
| 168 |
|
| } |
| 169 |
0 |
| } |
| 170 |
|
| } |
| 171 |
0 |
▶ | fputs(res.c_str(),stdout); // echo |
Branch: 1 branch
| Branch | True | False | Total |
| fputs(res.c_str(),stdout); // echo |
0 |
0 |
0 |
|
| 172 |
0 |
▶ | fputc('\n',stdout); |
Branch: 1 branch
| Branch | True | False | Total |
| fputc('\n',stdout); |
0 |
0 |
0 |
|
| 173 |
0 |
| } |
| 174 |
0 |
▶ | free_line(&l); |
Branch: 1 branch
| Branch | True | False | Total |
| free_line(&l); |
0 |
0 |
0 |
|
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
0 |
▶ | for (f_i=0;f_i<vin.size();++f_i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (f_i=0;f_i<vin.size();++f_i) |
0 |
0 |
0 |
|
| 178 |
0 |
▶ | delete vmap[f_i]; |
Branch: 1 branch
| Branch | True | False | Total |
| delete vmap[f_i]; |
0 |
0 |
0 |
|
| 179 |
0 |
▶ | } |
Branch: 1 branch
| Branch | True | False | Total |
| } |
0 |
0 |
0 |
|
| 180 |
0 |
▶ | } |
Branch: 1 branch
| Branch | True | False | Total |
| } |
0 |
0 |
0 |
|
| 181 |
|
| |
| 182 |
0 |
| void usage(FILE *f) { |
| 183 |
0 |
| fputs( |
| 184 |
|
| "Usage: tidx [options] -i IFILE [-i IFILE2...] -a AFILE\n" |
| 185 |
|
| " or: tidx [options] -B -i IFILE\n" |
| 186 |
|
| "\n" |
| 187 |
|
| "Fragments and merges overlapping regions in an file with start-stop values.\n" |
| 188 |
|
| "Creating a simple, fast, compressed index\n" |
| 189 |
|
| "\n" |
| 190 |
|
| "Also can load that index, and search AFILE for intersecting lines\n" |
| 191 |
|
| "\n" |
| 192 |
|
| "If the 'group' column is zero, no grouping will be used\n" |
| 193 |
|
| "\n" |
| 194 |
|
| "If just -b is present during a search, then only that column\n" |
| 195 |
|
| "is searched.\n" |
| 196 |
|
| "\n" |
| 197 |
|
| "If both -b and -e are present during a search, then all regions\n" |
| 198 |
|
| "that overlap will be returned.\n" |
| 199 |
|
| "\n" |
| 200 |
|
| "Options and (defaults):\n" |
| 201 |
|
| "\n" |
| 202 |
|
| "-i IFILE Text file to index (can specify more than one)\n" |
| 203 |
|
| "-B Build index, don't annotate\n" |
| 204 |
|
| "-a FILE Read text file and annotate\n" |
| 205 |
|
| "-p CHR:POS Lookup a single point (slow!)\n" |
| 206 |
|
| "-r STRING Annotation response separator (^)\n" |
| 207 |
|
| "-t CHAR(s) Field separator (TAB)\n" |
| 208 |
|
| "-c INT Group by (chromosome) column (1)\n" |
| 209 |
|
| "-b INT Begin region column (2) (or position for annot)\n" |
| 210 |
|
| "-e INT End region column (3)\n" |
| 211 |
|
| "-s INT or CHAR Skip rows starting with CHAR (#), or skip INT rows\n" |
| 212 |
|
| "-l Less than end, not less than or equal-to\n" |
| 213 |
|
| "-n Don't echo input lines\n" |
| 214 |
|
| //"-d Verbose debug output\n" |
| 215 |
|
| //"-D Dump input table (debug)\n" |
| 216 |
|
| //"-p CHR:POS Single point lookup (debug, slow)\n" |
| 217 |
|
| "\n" |
| 218 |
|
| ,f); |
| 219 |
0 |
| } |
| 220 |
|
| |