| 1 | // This file is part of A2Amapper.cxx . . $ `<file>/atac-driver/gapShifter/correctGaps.C`/ |
| 2 | // Copyright (c) 2006 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"cxx . . $ `<file>/libbio/bio++.H`/ |
| 24 | #include "atac.H"cxx . . $ `<file>/atac-driver/libatac/atac.H`/ |
| 25 | |
| 26 | |
| 27 | void |
| 28 | usagecxx . . $ usage(fda57c8427c60377).(char *name) { |
| 29 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "usage: %s [] -m matches -l log\n", name); |
| 30 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " When it works, fill this in...\n"); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | int |
| 35 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) { |
| 36 | char *matchesFile = 0L; |
| 37 | FILE *logFile = 0L; |
| 38 | |
| 39 | int arg=1; |
| 40 | while (arg < argc) { |
| 41 | if (strcmp(argv[arg], "-m") == 0) { |
| 42 | matchesFile = argv[++arg]; |
| 43 | } else if (strcmp(argv[arg], "-l") == 0) { |
| 44 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 45 | logFile = fopen(argv[++arg], "w"); |
| 46 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) |
| 47 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to open logfile '%s': %s\n", strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!), argv[arg]), exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 48 | } else { |
| 49 | usagecxx . . $ usage(fda57c8427c60377).(argv[0]); |
| 50 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 51 | } |
| 52 | arg++; |
| 53 | } |
| 54 | |
| 55 | if (matchesFile == 0L) |
| 56 | usagecxx . . $ usage(fda57c8427c60377).(argv[0]), exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 57 | if (logFile == 0L) |
| 58 | usagecxx . . $ usage(fda57c8427c60377).(argv[0]), exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 59 | |
| 60 | atacFilecxx . . $ atacFile# AFcxx . . $ atacFile#atacFile(85c52e162fed56f9).(matchesFile); |
| 61 | atacMatchListcxx . . $ atacMatchList# &ML = *AF.matchescxx . . $ atacFile#matches(33e88ebc28d05bce).(); |
| 62 | atacMatchOrdercxx . . $ atacMatchOrder# MOcxx . . $ atacMatchOrder#atacMatchOrder(f3ef1525738b52af).(ML); |
| 63 | |
| 64 | // Sort by either axis. |
| 65 | MO.sortAcxx . . $ atacMatchOrder#sortA(b4d55b1286ff73e8).(); |
| 66 | |
| 67 | // We need to compute the identity of the gap; our metric (thanks to Nelson) is |
| 68 | // if ("long" and "not low identity") or ("short"), close the gap |
| 69 | |
| 70 | // We could use the seqCache, but with only a handful of gaps, we |
| 71 | // just let the OS cache stuff. |
| 72 | |
| 73 | seqCachecxx . . $ seqCache# *C1 = new seqCachecxx . . $ seqCache#seqCache(cc76bd1196c143a3).(AF.assemblyFileAcxx . . $ atacFileBase#assemblyFileA(41208ce6ca65dd58).(), 2, false); |
| 74 | seqCachecxx . . $ seqCache# *C2 = new seqCachecxx . . $ seqCache#seqCache(cc76bd1196c143a3).(AF.assemblyFileBcxx . . $ atacFileBase#assemblyFileB(41208ce6ca65dd58).(), 1024, false); |
| 75 | |
| 76 | seqInCorecxx . . $ seqInCore# *S1 = 0L; |
| 77 | seqInCorecxx . . $ seqInCore# *S2 = 0L; |
| 78 | |
| 79 | for (uint32cxx . . $ uint32# iter=0; iter<10; iter++) { |
| 80 | uint32cxx . . $ uint32# gapsize = 1000; |
| 81 | uint32cxx . . $ uint32# fgaps = 0; |
| 82 | uint32cxx . . $ uint32# rgaps = 0; |
| 83 | |
| 84 | int mergeuid = 1; |
| 85 | |
| 86 | for (uint32cxx . . $ uint32# i=1; i<MO.numMatchescxx . . $ atacMatchOrder#numMatches(db52760e79bf9054).(); i++) { |
| 87 | atacMatchcxx . . $ atacMatch# *l = MO[cxx . . $ atacMatchOrder#`operator[]`(4b949e4d42dbfd78).i-1]; |
| 88 | atacMatchcxx . . $ atacMatch# *r = MO[cxx . . $ atacMatchOrder#`operator[]`(4b949e4d42dbfd78).i]; |
| 89 | |
| 90 | bool joinMatches = false; |
| 91 | |
| 92 | uint32cxx . . $ uint32# gap1 = 0; |
| 93 | uint32cxx . . $ uint32# gap2 = 0; |
| 94 | |
| 95 | if ((l->iid1cxx . . $ atacMatch#iid1. == r->iid1cxx . . $ atacMatch#iid1.) && // Matches are between the same sequences |
| 96 | (l->iid2cxx . . $ atacMatch#iid2. == r->iid2cxx . . $ atacMatch#iid2.) && |
| 97 | (l->fwd2cxx . . $ atacMatch#fwd2. == r->fwd2cxx . . $ atacMatch#fwd2.)) { // Matches are the same orientation |
| 98 | |
| 99 | |
| 100 | if (l->fwd2cxx . . $ atacMatch#fwd2. == true) { |
| 101 | if ((l->pos1cxx . . $ atacMatch#pos1. + l->len1cxx . . $ atacMatch#len1. <= r->pos1cxx . . $ atacMatch#pos1.) && // Matches are ordered correctly (should be, from the sort) |
| 102 | (l->pos2cxx . . $ atacMatch#pos2. + l->len2cxx . . $ atacMatch#len2. <= r->pos2cxx . . $ atacMatch#pos2.)) { |
| 103 | |
| 104 | gap1 = r->pos1cxx . . $ atacMatch#pos1. - (l->pos1cxx . . $ atacMatch#pos1. + l->len1cxx . . $ atacMatch#len1.); |
| 105 | gap2 = r->pos2cxx . . $ atacMatch#pos2. - (l->pos2cxx . . $ atacMatch#pos2. + l->len2cxx . . $ atacMatch#len2.); |
| 106 | |
| 107 | if ((gap1 == gap2) && |
| 108 | (gap1 <= gapsize)) { |
| 109 | S1 = C1->getSequenceInCorecxx . . $ seqCache#getSequenceInCore(715c05706546271f).(l->iid1cxx . . $ atacMatch#iid1.); |
| 110 | S2 = C2->getSequenceInCorecxx . . $ seqCache#getSequenceInCore(715c05706546271f).(l->iid2cxx . . $ atacMatch#iid2.); |
| 111 | |
| 112 | char *s1 = S1->sequencecxx . . $ seqInCore#sequence(e7e5aa338b9d5147).() + l->pos1cxx . . $ atacMatch#pos1. + l->len1cxx . . $ atacMatch#len1.; |
| 113 | char *s2 = S2->sequencecxx . . $ seqInCore#sequence(e7e5aa338b9d5147).() + l->pos2cxx . . $ atacMatch#pos2. + l->len1cxx . . $ atacMatch#len1.; |
| 114 | |
| 115 | uint32cxx . . $ uint32# identities = 0; |
| 116 | uint32cxx . . $ uint32# n1 = 0; |
| 117 | uint32cxx . . $ uint32# n2 = 0; |
| 118 | for (uint32cxx . . $ uint32# p=0; p<gap1; p++) { |
| 119 | if (toUpper[(int)s1[p]] == toUpper[(int)s2[p]]) |
| 120 | identities++; |
| 121 | if (toUpper[(int)s1[p]] == 'N') |
| 122 | n1++; |
| 123 | if (toUpper[(int)s2[p]] == 'N') |
| 124 | n2++; |
| 125 | } |
| 126 | |
| 127 | if ((100*n1 < 20*gap1) && // gap is not N and |
| 128 | (100*n2 < 20*gap1) && // gap is not N and |
| 129 | ((100*identities < 80*gap1) || // (gap is high identity |
| 130 | ((gap1 < 11) && (2*gap1 <= l->len1cxx . . $ atacMatch#len1.) && (2*gap1 <= r->len1cxx . . $ atacMatch#len1.)) || // (gap is short, and the flanks are big |
| 131 | ((gap1 < 11) && (100*identities < 90*gap1)))) { // (gap is short and high quality |
| 132 | |
| 133 | // ALSO need to check that the gap is not actually |
| 134 | // mapped in sequence 2. Not really, just make sure |
| 135 | // these two matches are in the same run. |
| 136 | // |
| 137 | if (strcmp(l->parentuidcxx . . $ atacMatch#parentuid., r->parentuidcxx . . $ atacMatch#parentuid.) != 0) { |
| 138 | fprintf(logFile, "HEY! F gap of size "uint32FMTcxx . . $ `libutil/util.h:65:10`!" not in a run?\n", gap1); |
| 139 | l->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(logFile, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 140 | r->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(logFile, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 141 | } else { |
| 142 | fgaps++; |
| 143 | |
| 144 | joinMatches = true; |
| 145 | |
| 146 | //fprintf(logFile, "potential f gap of size L "uint32FMTW(4)" (n1="uint32FMTW(4)" n2="uint32FMTW(4)" ident="uint32FMTW(4)"/"uint32FMTW(4)")!\n", |
| 147 | // gap1, n1, n2, identities, gap1); |
| 148 | //l->print(logFile, AF.labelA(), AF.labelB()); |
| 149 | //r->print(logFile, AF.labelA(), AF.labelB()); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } // was a forward match |
| 155 | |
| 156 | |
| 157 | |
| 158 | if (l->fwd2cxx . . $ atacMatch#fwd2. == false) { |
| 159 | if ((l->pos1cxx . . $ atacMatch#pos1. + l->len1cxx . . $ atacMatch#len1. <= r->pos1cxx . . $ atacMatch#pos1.) && // Matches are ordered correctly (should be, from the sort) |
| 160 | (r->pos2cxx . . $ atacMatch#pos2. + r->len2cxx . . $ atacMatch#len2. <= l->pos2cxx . . $ atacMatch#pos2.)) { |
| 161 | |
| 162 | gap1 = r->pos1cxx . . $ atacMatch#pos1. - (l->pos1cxx . . $ atacMatch#pos1. + l->len1cxx . . $ atacMatch#len1.); |
| 163 | gap2 = l->pos2cxx . . $ atacMatch#pos2. - (r->pos2cxx . . $ atacMatch#pos2. + r->len2cxx . . $ atacMatch#len2.); |
| 164 | |
| 165 | if ((gap1 == gap2) && |
| 166 | (gap1 <= gapsize)) { |
| 167 | |
| 168 | S1 = C1->getSequenceInCorecxx . . $ seqCache#getSequenceInCore(715c05706546271f).(l->iid1cxx . . $ atacMatch#iid1.); |
| 169 | S2 = C2->getSequenceInCorecxx . . $ seqCache#getSequenceInCore(715c05706546271f).(l->iid2cxx . . $ atacMatch#iid2.); |
| 170 | |
| 171 | char *s1 = S1->sequencecxx . . $ seqInCore#sequence(e7e5aa338b9d5147).() + l->pos1cxx . . $ atacMatch#pos1. + l->len1cxx . . $ atacMatch#len1.; |
| 172 | char *s2 = S2->sequencecxx . . $ seqInCore#sequence(e7e5aa338b9d5147).() + r->pos2cxx . . $ atacMatch#pos2. + r->len2cxx . . $ atacMatch#len2.; |
| 173 | |
| 174 | uint32cxx . . $ uint32# identities = 0; |
| 175 | uint32cxx . . $ uint32# n1 = 0; |
| 176 | uint32cxx . . $ uint32# n2 = 0; |
| 177 | for (uint32cxx . . $ uint32# p=0, q=gap1-1; p<gap1; p++, q--) { |
| 178 | if (toUpper[(int)s1[p]] == toUpper[complementSymbol[(int)s2[q]]]) |
| 179 | identities++; |
| 180 | if (toUpper[(int)s1[p]] == 'N') |
| 181 | n1++; |
| 182 | if (toUpper[(int)s2[q]] == 'N') |
| 183 | n2++; |
| 184 | } |
| 185 | |
| 186 | // Gap is short, flanks are big |
| 187 | // Gap is short, flanks are short and gap is good |
| 188 | |
| 189 | if ((100*n1 < 20*gap1) && // gap is not N and |
| 190 | (100*n2 < 20*gap1) && // gap is not N and |
| 191 | ((100*identities < 80*gap1) || // (gap is high identity |
| 192 | ((gap1 < 11) && (2*gap1 <= l->len1cxx . . $ atacMatch#len1.) && (2*gap1 <= r->len1cxx . . $ atacMatch#len1.)) || // (gap is short, and the flanks are big |
| 193 | ((gap1 < 11) && (100*identities < 90*gap1)))) { // (gap is short and high quality |
| 194 | |
| 195 | // ALSO need to check that the gap is not actually |
| 196 | // mapped in sequence 2. Not really, just make sure |
| 197 | // these two matches are in the same run. |
| 198 | // |
| 199 | if (strcmp(l->parentuidcxx . . $ atacMatch#parentuid., r->parentuidcxx . . $ atacMatch#parentuid.) != 0) { |
| 200 | fprintf(logFile, "HEY! R gap of size "uint32FMTcxx . . $ `libutil/util.h:65:10`!" not in a run?\n", gap1); |
| 201 | l->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(logFile, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 202 | r->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(logFile, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 203 | } else { |
| 204 | rgaps++; |
| 205 | |
| 206 | joinMatches = true; |
| 207 | |
| 208 | //fprintf(logFile, "potential r gap of size L "uint32FMTW(4)" (n1="uint32FMTW(4)" n2="uint32FMTW(4)" ident="uint32FMTW(4)"/"uint32FMTW(4)")!\n", |
| 209 | // gap1, n1, n2, identities, gap1); |
| 210 | //l->print(logFile, AF.labelA(), AF.labelB()); |
| 211 | //r->print(logFile, AF.labelA(), AF.labelB()); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (joinMatches) { |
| 222 | fprintf(logFile, "CLOSE "uint32FMTcxx . . $ `libutil/util.h:65:10`!"----------------------------------------\n", gap1); |
| 223 | l->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(logFile, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 224 | r->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(logFile, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 225 | |
| 226 | MO.mergeMatchescxx . . $ atacMatchOrder#mergeMatches(72ed48fc4fc66ee8).(l, r, mergeuid); |
| 227 | |
| 228 | l->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(logFile, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 229 | |
| 230 | mergeuid++; |
| 231 | i--; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | fprintf(logFile, "At gapSize="uint32FMTcxx . . $ `libutil/util.h:65:10`!" closed "uint32FMTcxx . . $ `libutil/util.h:65:10`!" f-gaps and "uint32FMTcxx . . $ `libutil/util.h:65:10`!" r-gaps.\n", gapsize, fgaps, rgaps); |
| 236 | |
| 237 | if (fgaps + rgaps == 0) |
| 238 | iter = 10; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | #if 0 |
| 243 | // This analyzes an atac mapping, looking for a signature that indicates a bad |
| 244 | // alignment. If we have an alignment of: |
| 245 | // XXXXXXaC-YYYYYY |
| 246 | // XXXXXX-CtYYYYYY |
| 247 | // this will generate three matches, instead of one match with mismatches in it. |
| 248 | // We scan the FORWARD matches for this pattern, and report any we find. |
| 249 | // |
| 250 | // We only found 3 on huref4 vs b35. Further development here was stopped. |
| 251 | |
| 252 | for (uint32 i=2; i<ML.numMatches(); i++) { |
| 253 | atacMatch *l = ML[i-2]; |
| 254 | atacMatch *m = ML[i-1]; |
| 255 | atacMatch *r = ML[i]; |
| 256 | |
| 257 | if (m->len1 < 3) { // The match in the middle is small |
| 258 | |
| 259 | if ((l->iid1 == r->iid1) && // Matches are between the same sequences |
| 260 | (l->iid2 == r->iid2) && |
| 261 | (l->fwd2 == r->fwd2)) { // Matches are the same orientation |
| 262 | |
| 263 | if (l->fwd2 == true) { |
| 264 | if ((l->pos1 + l->len1 <= r->pos1) && // Matches are ordered correctly (should be, from the sort) |
| 265 | (l->pos2 + l->len2 <= r->pos2)) { |
| 266 | |
| 267 | uint32 gapl1 = m->pos1 - (l->pos1 + l->len1); |
| 268 | uint32 gapl2 = m->pos2 - (l->pos2 + l->len2); |
| 269 | uint32 gapr1 = r->pos1 - (m->pos1 + m->len1); |
| 270 | uint32 gapr2 = r->pos2 - (m->pos2 + m->len2); |
| 271 | |
| 272 | if ((gapl1 + gapr1 == gapl2 + gapr2) && (gapl1 + gapr1 < 5)) { |
| 273 | fprintf(logFile, "potential f fix of size L "uint32FMT" "uint32FMT" and R "uint32FMT" "uint32FMT"!\n", |
| 274 | gapl1, gapl2, gapr1, gapr2); |
| 275 | l->print(logFile, "A", "B"); |
| 276 | m->print(logFile, "A", "B"); |
| 277 | r->print(logFile, "A", "B"); |
| 278 | } |
| 279 | } else { |
| 280 | fprintf(logFile, "sort is forward broken.\n"); |
| 281 | } |
| 282 | } // was a forward match |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | |
| 289 | // Write the new output to stdout -- we preserve runs here, but |
| 290 | // discard everything else. |
| 291 | // |
| 292 | AF.writeHeadercxx . . $ atacFileBase#writeHeader(8d4262d45fc644ab).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!); |
| 293 | |
| 294 | for (uint32cxx . . $ uint32# i=0; i<MO.numMatchescxx . . $ atacMatchOrder#numMatches(db52760e79bf9054).(); i++) |
| 295 | MO[cxx . . $ atacMatchOrder#`operator[]`(4b949e4d42dbfd78).i]->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 296 | |
| 297 | for (uint32cxx . . $ uint32# i=0; i<AF.runscxx . . $ atacFile#runs(33e88ebc28d05bce).()->numberOfMatchescxx . . $ atacMatchList#numberOfMatches(db52760e79bf9054).(); i++) |
| 298 | AF.runscxx . . $ atacFile#runs(33e88ebc28d05bce).()->getMatchcxx . . $ atacMatchList#getMatch(4b949e4d42dbfd78).(i)->printcxx . . $ atacMatch#print(f61d327ebbdda89f).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, AF.labelAcxx . . $ atacFileBase#labelA(41208ce6ca65dd58).(), AF.labelBcxx . . $ atacFileBase#labelB(41208ce6ca65dd58).()); |
| 299 | |
| 300 | return(0); |
| 301 | } |