Debian Codegraph @

sim4dbutils/detectChimera.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "sim4.H"
7
8 // Attempts to look for query that are chimeric. It is assumed that
9 // your query have been mapped to a target reference genome, such
10 // that little pieces will be mapped. The heuristic used is simple.
11 // The mapping intervals are merged together, and if there are two
12 // blocks that do not overlap, then it is chimeric. Intervals are
13 // decreased by 3bp before merging.
14
15 #define QUERY_LENGTH 2048
16
17 int
18 main(int argc, char **argv) {
19 bool beVerbose = false;
20 uint32 chimeraOverlap = 5;
21
22 int arg = 1;
23 while (arg < argc) {
24 if (strncmp(argv[arg], "-v", 2) == 0) {
25 beVerbose = true;
26 } else if (strncmp(argv[arg], "-o", 2) == 0) {
27 chimeraOverlap = strtouint32(argv[++arg], 0L);
28 } else {
29 fprintf(stderr, "Unknown arg '%s'\n", argv[arg]);
30 }
31 arg++;
32 }
33
34 intervalList<uint64> IL;
35 intervalList<uint64> ILfull;
36 uint32 ILid = 0;
37 char lastdefline[1024] = { 0 };
38
39 uint32 numPts = 0;
40 uint32 maxPts = 1024;
41 uint32 *begPt = new uint32 [maxPts];
42 uint32 *endPt = new uint32 [maxPts];
43
44 uint32 *genBeg = new uint32 [maxPts];
45 uint32 *genEnd = new uint32 [maxPts];
46
47 uint32 queryLength = 0;
48
49 char spaces[QUERY_LENGTH+1];
50 char lines[QUERY_LENGTH+1];
51 char equals[QUERY_LENGTH+1];
52
53 for (uint32 i=0; i<QUERY_LENGTH; i++) {
54 spaces[i] = ' ';
55 lines[i] = '-';
56 equals[i] = '=';
57 }
58 spaces[QUERY_LENGTH] = 0;
59 lines[QUERY_LENGTH] = 0;
60 equals[QUERY_LENGTH] = 0;
61
62 sim4polishReader *R = new sim4polishReader("-");
63 sim4polish *p = 0L;
64
65 while (R->nextAlignment(p)) {
66 if ((p->_estID != ILid) &&
67 (lastdefline[0])) {
68
69 #if 0
70 fprintf(stdout, "\n\n");
71
72 fprintf(stdout, "IL "uint32FMT"\n", IL.numberOfIntervals());
73 for (uint32 i=0; i<IL.numberOfIntervals(); i++)
74 fprintf(stderr, "IL["uint32FMTW(3)"] "uint64FMT" "uint64FMT"\n", i, IL.lo(i), IL.hi(i));
75
76 fprintf(stdout, "ILfull "uint32FMT"\n", ILfull.numberOfIntervals());
77 for (uint32 i=0; i<ILfull.numberOfIntervals(); i++)
78 fprintf(stderr, "ILfull["uint32FMTW(3)"] "uint64FMT" "uint64FMT"\n", i, ILfull.lo(i), ILfull.hi(i));
79 #endif
80
81 IL.merge();
82 ILfull.merge();
83
84 if ((IL.numberOfIntervals() > 1) &&
85 (ILfull.sumOfLengths() >= 0.9 * queryLength)) {
86 fprintf(stdout, "%s\n", lastdefline);
87
88 equals[queryLength] = 0;
89 fprintf(stdout, " %s\n", equals);
90 equals[queryLength] = '=';
91
92 // Bubble sort the positions.
93 //
94 for (uint32 a=0; a<numPts; a++) {
95 for (uint32 b=a+1; b<numPts; b++) {
96 if ((begPt[a] > begPt[b]) ||
97 ((begPt[a] == begPt[b]) && (endPt[a] > endPt[b]))) {
98 uint32 x = begPt[a];
99 uint32 y = endPt[a];
100 begPt[a] = begPt[b];
101 endPt[a] = endPt[b];
102 begPt[b] = x;
103 endPt[b] = y;
104
105 x = genBeg[a];
106 y = genEnd[a];
107 genBeg[a] = genBeg[b];
108 genEnd[a] = genEnd[b];
109 genBeg[b] = x;
110 genEnd[b] = y;
111 }
112 }
113 }
114
115
116 for (uint32 i=0; i<numPts && i<maxPts; i++) {
117 if (begPt[i] >= QUERY_LENGTH) {
118 fprintf(stdout, "WARNING: Next line (begin) truncated to %d positions!\n", QUERY_LENGTH);
119 begPt[i] = QUERY_LENGTH-1;
120 }
121 if (endPt[i] >= QUERY_LENGTH) {
122 fprintf(stdout, "WARNING: Next line (end) truncated to %d positions!\n", QUERY_LENGTH);
123 endPt[i] = QUERY_LENGTH-1;
124 }
125
126
127 spaces[begPt[i]] = 0;
128 lines[endPt[i] - begPt[i]] = 0;
129 fprintf(stdout, uint32FMTW(3)"-"uint32FMTW(3)" %s%s ("uint32FMT","uint32FMT")\n",
130 begPt[i], endPt[i], spaces, lines, genBeg[i], genEnd[i]);
131 spaces[begPt[i]] = ' ';
132 lines[endPt[i] - begPt[i]] = '-';
133 }
134
135 fprintf(stdout, "\n\n");
136 } // end of chimera detected
137
138 IL.clear();
139 ILfull.clear();
140 numPts = 0;
141 }
142
143 strcpy(lastdefline, p->_estDefLine);
144 ILid = p->_estID;
145
146 queryLength = p->_estLen;
147
148 uint32 beg = p->_exons[0]._estFrom - 1;
149 uint32 end = p->_exons[p->_numExons-1]._estTo;
150
151 if (numPts == maxPts) {
152 fprintf(stdout, "Wow! The next guy is a deep mapping! I'm only showing the\n");
153 fprintf(stdout, "first "uint32FMT" alignments.\n", maxPts);
154 } else if (numPts < maxPts) {
155 begPt[numPts] = beg;
156 endPt[numPts] = end;
157 genBeg[numPts] = p->_exons[0]._genFrom - 1;
158 genEnd[numPts] = p->_exons[p->_numExons-1]._genTo;
159 }
160 numPts++;
161
162 //fprintf(stdout, "beg,end = %d,%d\n", (int)beg, (int)end);
163
164 if (end - beg > 2 * chimeraOverlap) {
165 IL.add(beg + chimeraOverlap, end - beg - 2 * chimeraOverlap);
166 ILfull.add(beg, end - beg);
167 }
168 }
169
170 return(0);
171 }
172