Debian Codegraph @

atac-driver/mismatchCounter/mismatchCounter.C

raw symbols SCIP
1 // This file is part of A2Amapper.
2 // Copyright (c) 2005 J. Craig Venter Institute
3 // Author: Brian Walenz
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received (LICENSE.txt) a copy of the GNU General Public
16 // License along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #include "bio++.H"
24 #include "seqCache.H"
25 #include "atac.H"
26
27 #define ANNOTATE
28 #define EXTRAMATCHES
29
30 // Generates a histogram of the exact match block sizes
31 // Counts to global number of mismatches
32 // Annotates each match with the number of mismatches
33 // Checks for identities outside matches
34
35
36 void
37 updateExactBlockHistogram(uint32 *blockHistogram, uint32 blockMatches) {
38
39 if (blockMatches > 8 * 1024 * 1024)
40 blockHistogram[0]++;
41 else
42 blockHistogram[blockMatches]++;
43 }
44
45
46
47 int
48 main(int argc, char *argv[]) {
49
50 int arg=1;
51 while (arg < argc) {
52 if (strcmp(argv[arg], "-h") == 0) {
53 // Generate a histogram of exact-match lengths
54 } else if (strcmp(argv[arg], "-a") == 0) {
55 // Annotate each match with the percent error, compute
56 // the global percent error.
57 } else if (strcmp(argv[arg], "-e") == 0) {
58 // Generate a histogram of the percent error in each match
59 } else if (strcmp(argv[arg], "-c") == 0) {
60 // Check the edges of each match to ensure there isn't a match
61 } else {
62 fprintf(stderr, "usage: %s [-h exact-match-histogram] [-a] [-e error-histogram] [-c]\n", argv[0]);
63 fprintf(stderr, " -h: histogram of the length of the exact match blocks\n");
64 fprintf(stderr, " -a: annotate each match with the percent error, write to stdout\n");
65 fprintf(stderr, " -e: histogram of the error rate of each match\n");
66 fprintf(stderr, " -c: check that the next base on each side is a mismatch\n");
67 exit(1);
68 }
69 arg++;
70 }
71
72 uint32 globalSequence = 0;
73 uint32 globalMismatches = 0;
74 uint32 blockMatches = 0;
75 uint32 *blockHistogram = new uint32 [8 * 1024 * 1024];
76
77 for (uint32 x=0; x<8*1024*1024; x++)
78 blockHistogram[x] = 0;
79
80 atacFile AF("-");
81 atacMatchList &ML = *AF.matches();
82
83 seqCache *C1 = new seqCache(AF.assemblyFileA(), 1, false);
84 seqCache *C2 = new seqCache(AF.assemblyFileA(), 1, false);
85
86 for (uint32 mi=0; mi<ML.numberOfMatches(); mi++) {
87 atacMatch *m = ML.getMatch(mi);
88
89 seqInCore *S1 = C1->getSequenceInCore(m->iid1);
90 seqInCore *S2 = C2->getSequenceInCore(m->iid2);
91
92 FastAAccessor A1(S1, false);
93 FastAAccessor A2(S2, (m->fwd1 != m->fwd2));
94
95 A1.setRange(m->pos1, m->len1);
96 A2.setRange(m->pos2, m->len2);
97
98 uint32 localMismatches = 0;
99
100 #ifdef EXTRAMATCHES
101 uint32 extraMatchesL = 0;
102 uint32 extraMatchesR = 0;
103
104 // Check for matches on either side of the region.
105
106 A1.setPosition(m->pos1);
107 A2.setPosition(m->pos2);
108 --A1;
109 --A2;
110 while (A1.isValid() &&
111 A2.isValid() &&
112 (letterToBits[(int)*A1] != 0xff)&&
113 (letterToBits[(int)*A2] != 0xff) &&
114 IUPACidentity[(int)*A1][(int)*A2]) {
115 extraMatchesL++;
116 --A1;
117 --A2;
118 }
119
120 A1.setPosition(m->pos1 + m->len1 - 1);
121 A2.setPosition(m->pos2 + m->len2 - 1);
122 ++A1;
123 ++A2;
124 while (A1.isValid() &&
125 A2.isValid() &&
126 (letterToBits[(int)*A1] != 0xff)&&
127 (letterToBits[(int)*A2] != 0xff) &&
128 IUPACidentity[(int)*A1][(int)*A2]) {
129 extraMatchesR++;
130 ++A1;
131 ++A2;
132 }
133
134 // WARN if we found extra identities
135
136 #if 0
137 if (extraMatchesL + extraMatchesR > 0) {
138 A1.setPosition(m->pos1);
139 A2.setPosition(m->pos2);
140
141 chomp(inLine);
142 fprintf(stderr, "WARNING: found "uint32FMT" extra matches to the left and "uint32FMT" extra matches to the right in %s\n",
143 extraMatchesL, extraMatchesR, inLine);
144
145 #if 0
146 for (uint32 ii=0; ii<m->len1; ii++, ++A1)
147 fprintf(stdout, "%c", *A1);
148 fprintf(stdout, "\n");
149
150 for (uint32 ii=0; ii<m->len1; ii++, ++A2)
151 fprintf(stdout, "%c", *A2);
152 fprintf(stdout, "\n");
153 #endif
154 }
155 #endif
156
157 #endif // EXTRAMATCHES
158
159
160 A1.setPosition(m->pos1);
161 A2.setPosition(m->pos2);
162 for (uint32 ii=0; ii<m->len1; ii++, ++A1, ++A2) {
163
164 // Count global matches / mismatches
165 //
166 globalSequence++;
167 if (!((letterToBits[(int)*A1] != 0xff) &&
168 (letterToBits[(int)*A2] != 0xff) &&
169 IUPACidentity[(int)*A1][(int)*A2])) {
170 globalMismatches++;
171 localMismatches++;
172 }
173
174 // Histogram of exact match block lengths
175 //
176 if ((letterToBits[(int)*A1] != 0xff) &&
177 (letterToBits[(int)*A2] != 0xff) &&
178 IUPACidentity[(int)*A1][(int)*A2]) {
179 blockMatches++;
180 } else {
181 updateExactBlockHistogram(blockHistogram, blockMatches);
182 blockMatches = 0;
183 }
184 }
185
186 // Finish off stuff
187 //
188 updateExactBlockHistogram(blockHistogram, blockMatches);
189 blockMatches = 0;
190
191 // If annotate, emit a new record.
192 }
193
194
195 // Report stuff
196 //
197 fprintf(stderr, "globalSequence = "uint32FMT"\n", globalSequence);
198 fprintf(stderr, "globalMismatches = "uint32FMT"\n", globalMismatches);
199
200 #if 0
201 FILE *O = fopen("MismatchCounter.block.histogram.out", "w");
202 for (uint32 i=0; i<8 * 1024 * 1024; i++)
203 fprintf(O, uint32FMT" "uint32FMT"\n", i, blockHistogram[i]);
204 fclose(O);
205 #endif
206
207 return(0);
208 }