Debian Codegraph @

sim4dbutils/parseSNP.C

raw ◀ ▶
1 Debian patch touch this file
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <math.h>
6 #include <ctype.h>
7 #include <errno.h>
8
9 #include "bio.h"
10 #include "sim4.H"
11
12 // Writes things with mappings that don't contain the snp itself to a
13 // failure file. Otherwise, if the mapping is above the threshold, a
14 // line describing the snp is output.
15
16 sim4polishWriter *multiMultiFile = 0L; // multiple hits, at least one is multiple exon
17 sim4polishWriter *multiSingleFile = 0L; // multiple hits, all are single exon
18 sim4polishWriter *singleMultiFile = 0L; // single hit, and it has more than one exon
19 sim4polishWriter *singleSingleFile = 0L; // single hit, single exon
20
21 int smpass = 0;
22 int sspass = 0;
23 int mmpass = 0;
24 int mspass = 0;
25
26 int smfail = 0;
27 int ssfail = 0;
28 int mmfail = 0;
29 int msfail = 0;
30
31 int failedsnps = 0;
32 int failedmatches = 0;
33
34 FILE *validSNPMapFile = 0L;
35 sim4polishWriter *failedSNPMapFile = 0L;
36
37 char fieldDelimiter = 0;
38 const char *sizeTag = "/size=";
39 const char *posTag = "/pos=";
40 int positionOffset = 0;
41
42 int outputFormat = 1;
43
44 static
45 char *
46 findSNPid(char *defline) {
47 char *ret = 0L;
48 int sta = 0;
49 int len = 0;
50 int i = 0;
51
52 if (fieldDelimiter == 0) {
53 for (len=1; defline[len] && !isspace(defline[len]); len++)
54 ;
55 } else {
56 for (len=1; defline[len] && defline[len] != fieldDelimiter; len++)
57 ;
58 }
59
60 #if 0
61 // This was used for a set of SNPs with a non-standard defline
62 // structure. It returns the field between the first '|' and the
63 // next '_'.
64 //
65 for (len=1; defline[len] && defline[len] != '_'; len++)
66 ;
67 for (sta=len-1; sta > 0 && defline[sta] != '|'; sta--)
68 ;
69 #endif
70
71 ret = new char [len+1];
72
73 for (i=sta; i<len-1; i++)
74 ret[i-sta] = defline[i+1];
75
76 ret[len-sta-1] = 0;
77
78 return(ret);
79 }
80
81 static
82 char *
83 findGENid(char *defline) {
84 char *ret = 0L;
85 int len = 0;
86 int i = 0;
87
88 for (len=1; defline[len] && !isspace(defline[len]); len++)
89 ;
90
91 ret = new char [len+1];
92
93 for (i=0; i<len-1; i++)
94 ret[i] = defline[i+1];
95
96 ret[len-1] = 0;
97
98 return(ret);
99 }
100
101
102
103 static
104 int
105 findPosition(char *defline) {
106 char *p = 0L;
107
108 p = strstr(defline, posTag);
109
110 // Look for standard posTags if we didn't find the one the user wanted.
111
112 if (p == 0L)
113 p = strstr(defline, "allelePos=");
114 if (p == 0L)
115 p = strstr(defline, "/pos=");
116 if (p == 0L)
117 fprintf(stderr, "posTag '%s' (also looked for 'allelePos=' and '/pos=') not found in defline '%s'!\n", posTag, defline), exit(1);
118
119 while (*p && !isdigit(*p))
120 p++;
121
122 if (*p == 0) {
123 fprintf(stderr, "Found posTag '%s' in defline '%s', but didn't find any numbers!\n", posTag, defline);
124 exit(1);
125 }
126
127 return(atoi(p) + positionOffset);
128 }
129
130
131
132
133 // Returns 1 if SNP was valid and printed,
134 // 0 otherwise.
135 //
136 static
137 int
138 printSNP(FILE *F, sim4polish *p) {
139 uint32 pos = findPosition(p->_estDefLine);
140 uint32 exonWithSNP = ~uint32ZERO;
141 uint32 i = 0;
142 uint32 seqOffset = 0;
143
144 // If the match is complement, then the alignment is printed using
145 // the reverse complemented SNP sequence, and so we need to find
146 // the offset at the end of the sequence (not always the same as
147 // the offset at the start of the sequence).
148 //
149 // XXX: Previous version had this as "p->_estLen - pos + siz", which
150 // seems wrong. This version does what appears to be reverse
151 // complement - size. I don't understand if this is a "size" or
152 // just a "1" thing.
153 //
154 seqOffset = pos;
155 if (p->_matchOrientation == SIM4_MATCH_COMPLEMENT)
156 seqOffset = p->_estLen - pos - 1;
157
158 // Find the exon with the SNP
159 //
160 for (i=0; i<p->_numExons; i++)
161 if (((p->_exons[i]._estFrom-1) <= seqOffset) && (seqOffset <= (p->_exons[i]._estTo-1)))
162 exonWithSNP = i;
163
164 if (exonWithSNP == ~uint32ZERO)
165 return(0);
166
167 // If we are printing to a file, continue to find the location, otherwise,
168 // just return.
169 //
170 if (F) {
171 char *SNPid = findSNPid(p->_estDefLine);
172 char *GENid = findGENid(p->_genDefLine);
173
174 char SNPbase = 0;
175 char GENbase = 0;
176
177 // Now, we examine the alignment strings to decide exactly
178 // where the SNP is located in the genomic.
179 //
180 // bpToExaine - the number of bases we need to skip in the
181 // alignment (counted in the snp), +1 because we are currently at
182 // the bp before the alignment (so we need to skip one more space).
183 //
184 // XXX: these used to be int!
185 //
186 uint32 bpToExamine = seqOffset - (p->_exons[exonWithSNP]._estFrom - 1) + 1;
187 uint32 examinePos = 0;
188 uint32 genPosition = p->_exons[exonWithSNP]._genFrom - 1;
189
190 // Recent runs of dbSNP showed that we are off by one (too many if forward, too few if complement). This is a hack to fix it.
191 //
192 if (p->_matchOrientation == SIM4_MATCH_COMPLEMENT)
193 bpToExamine++;
194 else
195 bpToExamine--;
196
197 while (bpToExamine > 0) {
198
199 // If the SNP alignment eats up a base pair, decrement
200 // the number of bp left to examine.
201 //
202 if (p->_exons[exonWithSNP]._estAlignment[examinePos] != '-')
203 bpToExamine--;
204
205 // If the the genomic alignment is not a gap, increment the
206 // position.
207 //
208 if (p->_exons[exonWithSNP]._genAlignment[examinePos] != '-')
209 genPosition++;
210
211 examinePos++;
212 }
213
214 // Adjust the quality values, treating the SNP as a match always.
215 //
216 SNPbase = p->_exons[exonWithSNP]._estAlignment[examinePos-1];
217 GENbase = p->_exons[exonWithSNP]._genAlignment[examinePos-1];
218
219 p->_exons[exonWithSNP]._estAlignment[examinePos-1] = 'A';
220 p->_exons[exonWithSNP]._genAlignment[examinePos-1] = 'A';
221
222 p->s4p_updateAlignmentScores();
223
224 p->_exons[exonWithSNP]._estAlignment[examinePos-1] = SNPbase;
225 p->_exons[exonWithSNP]._genAlignment[examinePos-1] = GENbase;
226
227
228 if (outputFormat == 1) {
229 fprintf(F, "%s %s "uint32FMT" %c/%c %s global["uint32FMT" "uint32FMT"] exon["uint32FMT" "uint32FMT" "uint32FMT" "uint32FMT"]\n",
230 SNPid,
231 GENid,
232 genPosition,
233 SNPbase,
234 GENbase,
235 (p->_matchOrientation == SIM4_MATCH_FORWARD) ? "forward" : "complement",
236 p->_percentIdentity,
237 p->_querySeqIdentity,
238 p->_numExons,
239 exonWithSNP,
240 p->_exons[exonWithSNP]._percentIdentity,
241 (uint32)floor(100.0 * (double)p->_exons[exonWithSNP]._numMatches / (double)p->_estLen));
242 } else if (outputFormat == 2) {
243
244 // The format is all on one line, data fields separated by tab.
245 // No spaces -- "sa=C" instead of "sa = C"
246 //
247 // SNPid
248 // GENid
249 // genomic position of SNP
250 // sa=c -- snp allele
251 // ga=c -- genome allele
252 // mo={f|r} -- mapping orientation
253 // pi=n -- percent identity
254 // pc=n -- percent coverage
255 // nb=n -- number of alignment blocks
256 // bl=n -- alignment block with the snp
257 // bp=n -- position of the snp in the alignment block
258 // bi=n -- percent identity of the block
259 // bc=n -- percent coverage of the block
260 //
261 // The first three items are mandatory, are always in that
262 // order, and are always the first three. The others are
263 // optional, and can occur in any order. There might be more
264 // present than listed here.
265 //
266 // The order and content should be consistent for any given
267 // version of the software.
268 //
269 fprintf(F, "%s %s "uint32FMT" sa=%c ga=%c mo=%c pi="uint32FMT" pc="uint32FMT" nb="uint32FMT" bl="uint32FMT" bp="uint32FMT" bi="uint32FMT" bc="uint32FMT"\n",
270 "a", //SNPid,
271 "b", //GENid,
272 genPosition,
273 p->_exons[exonWithSNP]._estAlignment[examinePos-1], // sa
274 p->_exons[exonWithSNP]._genAlignment[examinePos-1], // ga
275 (p->_matchOrientation == SIM4_MATCH_FORWARD) ? 'f' : 'r', // mo
276 p->_percentIdentity, // pi
277 p->_querySeqIdentity, // pc
278 p->_numExons, // nb
279 exonWithSNP, // bl
280 examinePos, // bp
281 p->_exons[exonWithSNP]._percentIdentity, // bi
282 (uint32)floor(100.0 * (double)p->_exons[exonWithSNP]._numMatches / (double)p->_estLen)); // bc
283 } else {
284 }
285
286 delete [] SNPid;
287 delete [] GENid;
288 }
289
290 return(1);
291 }
292
293
294
295 // Just a wrapper around the real best picker, so that we can easily
296 // destroy polishes when we're done.
297 //
298 static
299 void
300 parseSNP(sim4polish **p, int pNum) {
301 int numMulti = 0;
302 int i;
303
304 // Count the number of matches that have more than one exon
305 //
306 for (i=0; i<pNum; i++)
307 if (p[i]->_numExons > 1)
308 numMulti++;
309
310 if (pNum == 1) {
311
312 //
313 // Exactly one match for this SNP
314 //
315
316 if (numMulti == 0) {
317
318 // Match has one exon
319
320 if (singleSingleFile)
321 singleSingleFile->writeAlignment(p[0]);
322
323 if (printSNP(validSNPMapFile, p[0])) {
324 sspass++;
325 } else {
326 ssfail++;
327 if (failedSNPMapFile)
328 failedSNPMapFile->writeAlignment(p[0]);
329 }
330 } else {
331
332 // Match has more than one exon
333
334 if (singleMultiFile)
335 singleMultiFile->writeAlignment(p[0]);
336
337 if (printSNP(validSNPMapFile, p[0])) {
338 smpass++;
339 } else {
340 smfail++;
341 if (failedSNPMapFile)
342 failedSNPMapFile->writeAlignment(p[0]);
343 }
344 }
345 } else {
346
347 //
348 // More than one match for this SNP
349 //
350
351 if (numMulti == 0) {
352 int pass=0, fail=0;
353
354 // All the matches are single exon
355
356 if (multiSingleFile)
357 for (i=0; i<pNum; i++)
358 multiSingleFile->writeAlignment(p[i]);
359
360 for (i=0; i<pNum; i++)
361 if (printSNP(validSNPMapFile, p[i])) {
362 pass++;
363 } else {
364 fail++;
365 if (failedSNPMapFile)
366 failedSNPMapFile->writeAlignment(p[i]);
367 }
368
369 if (pass==1) sspass++;
370 if (pass > 1) mspass++;
371 if (!pass && fail) msfail++;
372 } else {
373 int pass=0, fail=0;
374
375 // At least one match has more than one exon -- the correct one
376 // might be a single exon, but we don't know which is which.
377
378 if (multiMultiFile)
379 for (i=0; i<pNum; i++)
380 multiMultiFile->writeAlignment(p[i]);
381
382 for (i=0; i<pNum; i++)
383 if (printSNP(validSNPMapFile, p[i])) {
384 pass++;
385 } else {
386 fail++;
387 if (failedSNPMapFile)
388 failedSNPMapFile->writeAlignment(p[i]);
389 }
390
391 if (pass==1) smpass++;
392 if (pass > 1) mmpass++;
393 if (!pass && fail) mmfail++;
394 }
395 }
396
397 for (i=0; i<pNum; i++)
398 delete p[i];
399 }
400
401
402 int
403 main(int argc, char **argv) {
404 int pNum = 0;
405 int pAlloc = 8388608;
406 uint32 estID = 0;
407
408 uint32 percentID = 0;
409 uint32 percentCO = 0;
410
411 validSNPMapFile = 0L;
412 failedSNPMapFile = 0L;
413
414 int arg = 1;
415 int err = 0;
416 while (arg < argc) {
417 if (strncmp(argv[arg], "-i", 2) == 0) {
418 percentID = atoi(argv[++arg]);
419
420 } else if (strncmp(argv[arg], "-c", 2) == 0) {
421 percentCO = atoi(argv[++arg]);
422
423 } else if (strncmp(argv[arg], "-F", 2) == 0) {
424 failedSNPMapFile = new sim4polishWriter(argv[++arg], sim4polishS4DB);
425
426 } else if (strncmp(argv[arg], "-O", 2) == 0) {
427 errno = 0;
428 validSNPMapFile = fopen(argv[++arg], "w");
429 if (errno)
430 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", argv[arg], strerror(errno)), exit(1);
431
432 } else if (strncmp(argv[arg], "-D", 2) == 0) {
433 char name[FILENAME_MAX];
434
435 arg++;
436
437 sprintf(name, "%s-multi-multi", argv[arg]);
438 multiMultiFile = new sim4polishWriter(name, sim4polishS4DB);
439
440 sprintf(name, "%s-multi-single", argv[arg]);
441 multiSingleFile = new sim4polishWriter(name, sim4polishS4DB);
442
443 sprintf(name, "%s-single-multi", argv[arg]);
444 singleMultiFile = new sim4polishWriter(name, sim4polishS4DB);
445
446 sprintf(name, "%s-single-single", argv[arg]);
447 singleSingleFile = new sim4polishWriter(name, sim4polishS4DB);
448
449 } else if (strncmp(argv[arg], "-d", 2) == 0) {
450 fieldDelimiter = argv[++arg][0];
451
452 } else if (strncmp(argv[arg], "-p", 2) == 0) {
453 posTag = argv[++arg];
454
455 } else if (strncmp(argv[arg], "-s", 2) == 0) {
456 sizeTag = argv[++arg];
457
458 } else if (strncmp(argv[arg], "-o", 2) == 0) {
459 positionOffset = atoi(argv[++arg]);
460
461 } else if (strncmp(argv[arg], "-format", 2) == 0) {
462 outputFormat = atoi(argv[++arg]);
463
464 } else {
465 fprintf(stderr, "unknown option: %s\n", argv[arg]);
466 err++;
467 }
468
469 arg++;
470 }
471
472 if (err) {
473 fprintf(stderr, "usage: %s [options]\n", argv[0]);
474 fprintf(stderr, " -i min-identity filter matches on percent identity\n");
475 fprintf(stderr, " -c min-coverage filter matches on percent coverage\n");
476 fprintf(stderr, " -F failed save matches that do not contain the\n");
477 fprintf(stderr, " to the file 'failed'\n");
478 fprintf(stderr, " -O output save the parsed SNPs to the file\n");
479 fprintf(stderr, " 'output'\n");
480 fprintf(stderr, " -D prefix report debugging stuff into files\n");
481 fprintf(stderr, " prefixed with 'prefix'\n");
482 fprintf(stderr, " -d delimiter Use the single character delimiter as\n");
483 fprintf(stderr, " the end of the defline ID field. The\n");
484 fprintf(stderr, " default is to split on any whitespace.\n");
485 fprintf(stderr, "\n");
486 fprintf(stderr, " -s sizeTag Use this tag as the size of the snp.\n");
487 fprintf(stderr, " '/size=' is tried by default.\n");
488 fprintf(stderr, "\n");
489 fprintf(stderr, " -p posTag Use this tag as the position of the snp.\n");
490 fprintf(stderr, " 'allelePos=' and '/pos=' are tried by\n");
491 fprintf(stderr, " default, and if posTag is not found.\n");
492 fprintf(stderr, "\n");
493 fprintf(stderr, " TAGS: The number immediately after the first\n");
•494 fprintf(stderr, " occurrence of the tag will be used.\n");
495 fprintf(stderr, "\n");
496 fprintf(stderr, " -o offset An additive offset to the SNP position.\n");
497 fprintf(stderr, " The default is 0.\n");
498 fprintf(stderr, "\n");
499 fprintf(stderr, " -format n 1 - use the original (default) format\n");
500 fprintf(stderr, " 2 - use an extended format, includes the\n");
501 fprintf(stderr, " position in the alignment string\n");
502 fprintf(stderr, "\n");
503 fprintf(stderr, " -h Show this help.\n");
504 fprintf(stderr, "\n");
505 fprintf(stderr, "\n" );
506 fprintf(stderr, " only -O is required. Input is read from stdin.\n");
507 fprintf(stderr, "\n");
508 fprintf(stderr, " NOTE! Sizes and sizeTag is NOT IMPLEMENTED!\n");
509 fprintf(stderr, " All SNPs are of size == 1\n");
510 fprintf(stderr, "\n");
511 fprintf(stderr, " If you parse base-based SNPs, the result is returned base-based.\n");
512 fprintf(stderr, " You should use an ofset of 0.\n");
513 fprintf(stderr, "\n");
514 fprintf(stderr, " If you parse space-based SNPs, the result is returned base-based.\n");
515 fprintf(stderr, " You should use an offset of 1.\n");
516 fprintf(stderr, "\n");
517 }
518
519 if ((outputFormat != 1) && (outputFormat != 2)) {
520 fprintf(stderr, "Invalid output format. Must be 1 or 2.\n");
521 exit(1);
522 }
523
524
525 // Read polishes, parsing when we see a change in the estID.
526 // Really, we could parse one by one, but it's nice to know if the
527 // thing mapped more than once.
528 //
529 // We could also extend this to discard matches that look
530 // suspicious -- or maybe pick the single best match for each.
531
532 sim4polishReader *R = new sim4polishReader("-");
533 sim4polish **p = new sim4polish * [pAlloc];
534 sim4polish *q = 0L;
535
536 while (R->nextAlignment(q)) {
537 if (q->_estID < estID) {
538 fprintf(stderr, "ERROR: Polishes not sorted by SNP idx! this="uint32FMT", looking for "uint32FMT"\n",
539 q->_estID, estID);
540 exit(1);
541 }
542
543 if ((q->_estID != estID) && (pNum > 0)) {
544 parseSNP(p, pNum);
545 pNum = 0;
546 }
547
548 if (pNum >= pAlloc) {
549 sim4polish **P = new sim4polish * [pAlloc * 2];
550 memcpy(p, P, sizeof(sim4polish *) * pAlloc);
551 delete [] p;
552 p = P;
553 pAlloc *= 2;
554 }
555
556 estID = q->_estID;
557
558 if ((q->_percentIdentity >= percentID) &&
559 (q->_querySeqIdentity >= percentCO)) {
560 p[pNum++] = q;
561 } else {
562 delete q;
563 }
564
565 q = 0L; // Otherwise we delete the one we just saved!
566 }
567
568 if (pNum > 0)
569 parseSNP(p, pNum);
570
571 fprintf(stdout, "SNPs with:\n");
572 fprintf(stdout, " single hit, single exon: %6d\n", sspass);
573 fprintf(stdout, " single hit, multiple exons: %6d\n", smpass);
574 fprintf(stdout, " multiple hits, single exon: %6d\n", mspass);
575 fprintf(stdout, " multiple hits, multiple exons: %6d\n", mmpass);
576 fprintf(stdout, "SNPs that failed:\n");
577 fprintf(stdout, " single hit, single exon: %6d\n", ssfail);
578 fprintf(stdout, " single hit, multiple exons: %6d\n", smfail);
579 fprintf(stdout, " multiple hits, single exon: %6d\n", msfail);
580 fprintf(stdout, " multiple hits, multiple exons: %6d\n", mmfail);
581
582 fclose(validSNPMapFile);
583 delete failedSNPMapFile;
584
585 delete multiMultiFile;
586 delete multiSingleFile;
587 delete singleMultiFile;
588 delete singleSingleFile;
589
590 return(0);
591 }
592