| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2004 Applera Corporation |
| 3 | // Copyright (c) 2005 The J. Craig Venter Institute |
| 4 | // Author: Clark Mobarry |
| 5 | // Author: Brian Walenz |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU General Public License as published by |
| 9 | // the Free Software Foundation; either version 2 of the License, or |
| 10 | // (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received (LICENSE.txt) a copy of the GNU General Public |
| 18 | // License along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include "util++.H" |
| 25 | #include "heavychains.H" |
| 26 | |
| 27 | |
| 28 | extern "C" { |
| 29 | void *construct(char *options); |
| 30 | void destruct(void *handle); |
| 31 | void addHit(void *handle, |
| 32 | char orientation, |
| 33 | uint32 id1, |
| 34 | uint32 pos1, |
| 35 | uint32 len1, |
| 36 | uint32 id2, |
| 37 | uint32 pos2, |
| 38 | uint32 len2, |
| 39 | uint32 filled); |
| 40 | void filter(void *handle); |
| 41 | uint64 output(void *handle, FILE *file, uint64 matchid); |
| 42 | |
| 43 | void *constructStats(char *options); |
| 44 | void destructStats(void *handle); |
| 45 | void addStats(void *handle, void *sp); |
| 46 | void showStats(void *handle, FILE *file); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | // HeavyChains is implemented in the StrandPair class. It takes all |
| 52 | // hits for a single pair of sequences and....does something. Seatac |
| 53 | // gives the filterObj interface (aka, the interface in this file) |
| 54 | // all hits for a single sequence to the whole genome (or part of). |
| 55 | // So, the StrandPairManager acts as the, uhhh, manager for a bunch |
| 56 | // of StrandPairs, ensuring that each StrandPair is in fact a pair. |
| 57 | // |
| 58 | // It is interface compatible with a StrandPair. |
| 59 | // |
| 60 | class StrandPairManager { |
| 61 | private: |
| 62 | int beVerbose; |
| 63 | char assemblyId1[32]; |
| 64 | char assemblyId2[32]; |
| 65 | int maxJump; // Default maximum intra-run jump allowed in a good run. |
| 66 | double minScore; // Default minimum of bp filled in a good run. |
| 67 | |
| 68 | bool isForward; |
| 69 | |
| 70 | StrandPair *P; |
| 71 | StrandPair *Proot; |
| 72 | public: |
| 73 | StrandPairManager(bool verbose, |
| 74 | char *assemblyid1, |
| 75 | char *assemblyid2, |
| 76 | int maxjump, |
| 77 | double minscore) { |
| 78 | beVerbose = verbose; |
| 79 | strncpy(assemblyId1, assemblyid1, 31); |
| 80 | strncpy(assemblyId2, assemblyid2, 31); |
| 81 | maxJump = maxjump; |
| 82 | minScore = minscore; |
| 83 | |
| 84 | isForward = true; |
| 85 | |
| 86 | Proot = 0L; |
| 87 | P = 0L; |
| 88 | }; |
| 89 | |
| 90 | ~StrandPairManager(void) { |
| 91 | P = Proot; |
| 92 | while (Proot) { |
| 93 | Proot = Proot->next(); |
| 94 | delete P; |
| 95 | P = Proot; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | void addHit(char direction, |
| 100 | uint32 id1, |
| 101 | uint32 xlo, |
| 102 | uint32 xln, |
| 103 | uint32 id2, |
| 104 | uint32 ylo, |
| 105 | uint32 yln, |
| 106 | uint32 filled) { |
| 107 | |
| 108 | // We're given hits for exactly one id2 and all id1, forward hits |
| 109 | // followed by reverse hits. Which means that id1 makes two |
| 110 | // passes through, both passes are increasing (enforced by the |
| 111 | // seqStream used in seatac). |
| 112 | // |
| 113 | // A linked list of strand pairs is kept (the links are built |
| 114 | // into StrandPair for convenience), each strand pair knows it's |
| 115 | // pair of ids. |
| 116 | // |
| 117 | |
| 118 | // No root? Make one and add the hit. |
| 119 | // |
| 120 | if (Proot == 0L) { |
| 121 | P = Proot = new StrandPair(beVerbose, assemblyId1, assemblyId2, maxJump, minScore); |
| 122 | P->addHit(direction, id1, xlo, xln, id2, ylo, yln, filled); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Reset to the start if we just switched from forward to |
| 127 | // reverse. This is also the only time that the sequence id can |
| 128 | // decrease, and we might have to make a new root. |
| 129 | // |
| 130 | if (isForward && (direction == 'r')) { |
| 131 | isForward = false; |
| 132 | |
| 133 | if (id1 < Proot->sequenceIID1()) { |
| 134 | StrandPair *N = new StrandPair(beVerbose, assemblyId1, assemblyId2, maxJump, minScore); |
| 135 | N->addHit(direction, id1, xlo, xln, id2, ylo, yln, filled); |
| 136 | N->addNext(Proot); |
| 137 | P = Proot = N; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | P = Proot; |
| 142 | } |
| 143 | |
| 144 | // Verify that id1 didn't decrease. |
| 145 | // |
| 146 | if (id1 < P->sequenceIID1()) { |
| 147 | fprintf(stderr, "Why did the sequence id just decrease? This should not have happened.\n"); |
| 148 | fprintf(stderr, "Crash. %s at line %d\n", __FILE__, __LINE__ - 2); |
| 149 | exit(1); |
| 150 | } |
| 151 | |
| 152 | // Move to the node just before, or exactly at, the one we want |
| 153 | // to add to. Remember, id1 never decreases. |
| 154 | // |
| 155 | while ((P->next()) && (P->next()->sequenceIID1() <= id1)) |
| 156 | P = P->next(); |
| 157 | |
| 158 | // If we're not at the correct node, insert one after the |
| 159 | // current, and make it the correct one. |
| 160 | // |
| 161 | if (P->sequenceIID1() != id1) { |
| 162 | StrandPair *NP = new StrandPair(beVerbose, assemblyId1, assemblyId2, maxJump, minScore); |
| 163 | NP->addNext(P->next()); |
| 164 | P->addNext(NP); |
| 165 | P = NP; // Hooray! |
| 166 | } |
| 167 | |
| 168 | // And now we can just add the hit. |
| 169 | // |
| 170 | P->addHit(direction, id1, xlo, xln, id2, ylo, yln, filled); |
| 171 | }; |
| 172 | |
| 173 | void process(void) { |
| 174 | for (StrandPair *SP=Proot; SP; SP=SP->next()) |
| 175 | SP->process(); |
| 176 | }; |
| 177 | |
| 178 | uint64 print(FILE *outF, uint64 matchid) { |
| 179 | for (StrandPair *SP=Proot; SP; SP=SP->next()) |
| 180 | matchid = SP->print(outF, matchid); |
| 181 | return(matchid); |
| 182 | }; |
| 183 | |
| 184 | void addStats(TheStats *ST) { |
| 185 | for (StrandPair *SP=Proot; SP; SP=SP->next()) |
| 186 | ST->add(SP); |
| 187 | }; |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | void* |
| 198 | construct(char *options) { |
| 199 | int beVerbose = 0; |
| 200 | char assemblyIdD[4] = { 'U', 'N', 'K', 0 }; |
| 201 | char *assemblyId1 = assemblyIdD; |
| 202 | char *assemblyId2 = assemblyIdD; |
| 203 | double minScore = 100.0; // Default minimum of bp filled in a good run. |
| 204 | int maxJump = 100000; // Default maximum intra-run jump allowed in a good run. |
| 205 | |
| 206 | // Parse the options to find the parameters |
| 207 | // |
| 208 | splitToWords W(options); |
| 209 | |
| 210 | uint32 arg = 0; |
| 211 | while (arg < W.numWords()) { |
| 212 | if (strcmp(W.getWord(arg), "-v") == 0) { |
| 213 | beVerbose++; |
| 214 | } else if (strcmp(W.getWord(arg), "-s") == 0) { |
| 215 | minScore = atof(W.getWord(++arg)); |
| 216 | } else if (strcmp(W.getWord(arg), "-j") == 0) { |
| 217 | maxJump = atoi(W.getWord(++arg)); |
| 218 | } else if (strcmp(W.getWord(arg), "-1") == 0) { |
| 219 | assemblyId1 = W.getWord(++arg); |
| 220 | } else if (strcmp(W.getWord(arg), "-2") == 0) { |
| 221 | assemblyId2 = W.getWord(++arg); |
| 222 | } |
| 223 | |
| 224 | arg++; |
| 225 | } |
| 226 | |
| 227 | return((void *)(new StrandPairManager(beVerbose, assemblyId1, assemblyId2, maxJump, minScore))); |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | destruct(void *handle) { |
| 232 | delete (StrandPairManager *)handle; |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | addHit(void *handle, |
| 237 | char orientation, |
| 238 | uint32 id1, |
| 239 | uint32 pos1, |
| 240 | uint32 len1, |
| 241 | uint32 id2, |
| 242 | uint32 pos2, |
| 243 | uint32 len2, |
| 244 | uint32 filled) { |
| 245 | ((StrandPairManager *)handle)->addHit(orientation, id1, pos1, len1, id2, pos2, len2, filled); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | filter(void *handle) { |
| 250 | ((StrandPairManager *)handle)->process(); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | uint64 |
| 255 | output(void *handle, FILE *file, uint64 matchid) { |
| 256 | return(((StrandPairManager *)handle)->print(file, matchid)); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | void* |
| 265 | constructStats(char *options) { |
| 266 | int beVerbose = 0; |
| 267 | char assemblyIdD[4] = { 'U', 'N', 'K', 0 }; |
| 268 | char *assemblyId1 = assemblyIdD; |
| 269 | char *assemblyId2 = assemblyIdD; |
| 270 | double minScore = 100.0; // Default minimum of bp filled in a good run. |
| 271 | int maxJump = 100000; // Default maximum intra-run jump allowed in a good run. |
| 272 | |
| 273 | // Parse the options to find the parameters |
| 274 | // |
| 275 | splitToWords W(options); |
| 276 | |
| 277 | uint32 arg = 0; |
| 278 | while (arg < W.numWords()) { |
| 279 | if (strcmp(W.getWord(arg), "-v") == 0) { |
| 280 | beVerbose++; |
| 281 | } else if (strcmp(W.getWord(arg), "-s") == 0) { |
| 282 | minScore = atof(W.getWord(++arg)); |
| 283 | } else if (strcmp(W.getWord(arg), "-j") == 0) { |
| 284 | maxJump = atoi(W.getWord(++arg)); |
| 285 | } else if (strcmp(W.getWord(arg), "-1") == 0) { |
| 286 | assemblyId1 = W.getWord(++arg); |
| 287 | } else if (strcmp(W.getWord(arg), "-2") == 0) { |
| 288 | assemblyId2 = W.getWord(++arg); |
| 289 | } |
| 290 | |
| 291 | arg++; |
| 292 | } |
| 293 | |
| 294 | return((void *)(new TheStats(beVerbose, assemblyId1, assemblyId2, maxJump, minScore))); |
| 295 | } |
| 296 | |
| 297 | void |
| 298 | destructStats(void *handle) { |
| 299 | delete (TheStats *)handle; |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | addStats(void *handle, void *sp) { |
| 304 | |
| 305 | // We aren't getting a single StrandPair anymore, we're getting a StrandPairManager now. |
| 306 | // |
| 307 | //((TheStats *)handle)->add((StrandPair *)sp); |
| 308 | // |
| 309 | ((StrandPairManager *)sp)->addStats((TheStats *)handle); |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | showStats(void *handle, FILE *file) { |
| 314 | ((TheStats *)handle)->show(file); |
| 315 | } |