| 1 | #ifndef STRANDPAIR_H |
| 2 | #define STRANDPAIR_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include <vector> |
| 9 | using namespace std; |
| 10 | |
| 11 | #include "util.h" |
| 12 | |
| 13 | // |
| 14 | // The StrandPair does the heavy chains filtering, while the TheStats |
| 15 | // collects statistics on all StrandPairs. TheStats is also |
| 16 | // responsible for reporting the options used by StrandPair. |
| 17 | // |
| 18 | |
| 19 | |
| 20 | struct Match { |
| 21 | int xlo; |
| 22 | int ylo; |
| 23 | int xhi; |
| 24 | int yhi; |
| 25 | |
| 26 | double selfS; // The intrinsic score of the Match. |
| 27 | double S; // The computed score of the Match? |
| 28 | |
| 29 | // We need two times the number of dimensions of scores. That is |
| 30 | // one score starting from each corner of the bounding box of the |
| 31 | // space. |
| 32 | // |
| 33 | double neS; |
| 34 | double swS; |
| 35 | double nwS; |
| 36 | double seS; |
| 37 | |
| 38 | int filled; // Is this the same as selfS ? |
| 39 | |
| 40 | char ori; |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | |
| 45 | class StrandPair { |
| 46 | public: |
| 47 | StrandPair(bool verbose, |
| 48 | char *assemblyid1, |
| 49 | char *assemblyid2, |
| 50 | int maxjump, |
| 51 | double minscore) { |
| 52 | beVerbose = verbose; |
| 53 | strncpy(assemblyId1, assemblyid1, 31); |
| 54 | strncpy(assemblyId2, assemblyid2, 31); |
| 55 | maxJump = maxjump; |
| 56 | minScore = minscore; |
| 57 | |
| 58 | Plen = 0; |
| 59 | Pmax = 1024; |
| 60 | P = new Match [Pmax]; |
| 61 | |
| 62 | clear(); |
| 63 | }; |
| 64 | |
| 65 | ~StrandPair(void) { |
| 66 | if (beVerbose > 1) |
| 67 | fprintf(stderr, "StrandPair::StrandPair()-- delete %s vs %s with %d hits\n", assemblyId1, assemblyId2, Plen); |
| 68 | delete [] P; |
| 69 | }; |
| 70 | |
| 71 | void addHit(char direction, |
| 72 | uint32 id1, |
| 73 | uint32 xlo, |
| 74 | uint32 xln, |
| 75 | uint32 id2, |
| 76 | uint32 ylo, |
| 77 | uint32 yln, |
| 78 | uint32 filled); |
| 79 | |
| 80 | void process(void); |
| 81 | |
| 82 | uint64 print(FILE *outF, uint64 matchid); |
| 83 | |
| 84 | void clear(void) { |
| 85 | iid1 = ~uint32ZERO; |
| 86 | iid2 = ~uint32ZERO; |
| 87 | |
| 88 | _next = 0L; |
| 89 | |
| 90 | sumlen1 = 0.0; |
| 91 | sumlen2 = 0.0; |
| 92 | maxlen1 = 0.0; |
| 93 | maxlen2 = 0.0; |
| 94 | maxScoreFwd = 0.0; // Maximum forward chain score for the strand pair. |
| 95 | maxScoreRev = 0.0; // Maximum reverse chain score for the strand pair. |
| 96 | |
| 97 | Plen = 0; |
| 98 | }; |
| 99 | |
| 100 | // The StrandPairManager (in filter-strandpair.H) is lazy and makes |
| 101 | // StrandPairs keep track of the next one. |
| 102 | // |
| 103 | StrandPair *_next; |
| 104 | StrandPair *next(void) { return(_next); }; |
| 105 | void addNext(StrandPair *n) { _next = n; }; |
| 106 | |
| 107 | uint32 sequenceIID1(void) { return(iid1); }; |
| 108 | //uint32 sequenceIID2(void) { return(iid2); }; |
| 109 | |
| 110 | double getsumlen1(void) const { return(sumlen1); }; |
| 111 | double getsumlen2(void) const { return(sumlen2); }; |
| 112 | double getmaxlen1(void) const { return(maxlen1); }; |
| 113 | double getmaxlen2(void) const { return(maxlen2); }; |
| 114 | double getmaxScoreFwd(void) const { return(maxScoreFwd); }; |
| 115 | double getmaxScoreRev(void) const { return(maxScoreRev); }; |
| 116 | |
| 117 | private: |
| 118 | // This used to use a vector<Match>, but DPTree wants a pointer to the |
| 119 | // array of matches |
| 120 | // |
| 121 | int Plen; |
| 122 | int Pmax; |
| 123 | Match *P; |
| 124 | |
| 125 | void Padd(Match *m) { |
| 126 | if (Plen >= Pmax) { |
| 127 | Pmax *= 2; |
| 128 | Match *n = new Match [Pmax]; |
| 129 | memcpy(n, P, sizeof(Match) * Plen); |
| 130 | delete [] P; |
| 131 | P = n; |
| 132 | } |
| 133 | memcpy(P+Plen, m, sizeof(Match)); |
| 134 | Plen++; |
| 135 | }; |
| 136 | |
| 137 | private: |
| 138 | uint32 iid1; |
| 139 | uint32 iid2; |
| 140 | uint32 beVerbose; |
| 141 | char assemblyId1[32]; |
| 142 | char assemblyId2[32]; |
| 143 | int maxJump; // Default maximum intra-run jump allowed in a good run. |
| 144 | double minScore; // Default minimum of bp filled in a good run. |
| 145 | |
| 146 | // The following are only known after StrandPair::print(). |
| 147 | |
| 148 | double sumlen1; |
| 149 | double sumlen2; |
| 150 | double maxlen1; |
| 151 | double maxlen2; |
| 152 | double maxScoreFwd; // Maximum forward chain score for the strand pair. |
| 153 | double maxScoreRev; // Maximum reverse chain score for the strand pair. |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | class TheStats { |
| 162 | public: |
| 163 | TheStats(bool verbose, |
| 164 | char *assemblyid1, |
| 165 | char *assemblyid2, |
| 166 | int maxjump, |
| 167 | double minscore) { |
| 168 | beVerbose = verbose; |
| 169 | strncpy(assemblyId1, assemblyid1, 31); // Note the cap 'i' |
| 170 | strncpy(assemblyId2, assemblyid2, 31); |
| 171 | maxJump = maxjump; |
| 172 | minScore = minscore; |
| 173 | |
| 174 | sumlen1 = 0.0; |
| 175 | sumlen2 = 0.0; |
| 176 | sumMaxLen1 = 0.0; |
| 177 | sumMaxLen2 = 0.0; |
| 178 | sumMaxScoreFwd = 0.0; |
| 179 | sumMaxScoreRev = 0.0; |
| 180 | }; |
| 181 | |
| 182 | void add(StrandPair *sp) { |
| 183 | sumlen1 += sp->getsumlen1(); |
| 184 | sumlen2 += sp->getsumlen2(); |
| 185 | sumMaxLen1 += sp->getmaxlen1(); |
| 186 | sumMaxLen2 += sp->getmaxlen2(); |
| 187 | sumMaxScoreFwd += sp->getmaxScoreFwd(); |
| 188 | sumMaxScoreRev += sp->getmaxScoreRev(); |
| 189 | }; |
| 190 | |
| 191 | void show(FILE *outfile) { |
| 192 | fprintf(outfile, "/assemblyId1=%s\n", assemblyId1); |
| 193 | fprintf(outfile, "/assemblyId2=%s\n", assemblyId2); |
| 194 | fprintf(outfile, "/heavyMaxJump=%d\n", maxJump); |
| 195 | fprintf(outfile, "/heavyMinFill=%f\n", minScore); |
| 196 | fprintf(outfile, "/heavySumLen1=%f\n", sumlen1); |
| 197 | fprintf(outfile, "/heavySumLen2=%f\n", sumlen2); |
| 198 | fprintf(outfile, "/heavySumMaxLen1=%f\n", sumMaxLen1); |
| 199 | fprintf(outfile, "/heavySumMaxLen2=%f\n", sumMaxLen2); |
| 200 | fprintf(outfile, "/heavySumMaxScoreFwd=%f\n", sumMaxScoreFwd); |
| 201 | fprintf(outfile, "/heavySumMaxScoreRev=%f\n", sumMaxScoreRev); |
| 202 | }; |
| 203 | |
| 204 | private: |
| 205 | // Parameters to the filter |
| 206 | int beVerbose; |
| 207 | char assemblyId1[32]; |
| 208 | char assemblyId2[32]; |
| 209 | int maxJump; |
| 210 | double minScore; |
| 211 | |
| 212 | double sumlen1; |
| 213 | double sumlen2; |
| 214 | double sumMaxLen1; |
| 215 | double sumMaxLen2; |
| 216 | double sumMaxScoreFwd; |
| 217 | double sumMaxScoreRev; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | struct Interval { |
| 225 | int lo; |
| 226 | int hi; |
| 227 | double S; |
| 228 | |
| 229 | Interval() {}; |
| 230 | // This is an explicit redefinition of the default constructor. |
| 231 | }; |
| 232 | |
| 233 | int x_compar(const void *x,const void *y); |
| 234 | int y_compar(const void *x,const void *y); |
| 235 | |
| 236 | class DPTree { |
| 237 | Interval *node; |
| 238 | Match *match; |
| 239 | int node_size; |
| 240 | int match_size; // The number of matches stored in the tree. |
| 241 | |
| 242 | // DP parameters |
| 243 | int MaxJump; |
| 244 | |
| 245 | struct kd_node { |
| 246 | bool Xy; |
| 247 | int start,stop; // The indices to define a segment of the vector. |
| 248 | int intv; // some index |
| 249 | kd_node() {}; |
| 250 | // This is an explicit redefinition of the default constructor. |
| 251 | |
| 252 | inline int nmatches() const {return stop-start;} |
| 253 | inline int midpoint() const {return (start+stop+1)/2;} |
| 254 | // This is the midpoint of the match list, not the midpoint of the region. |
| 255 | |
| 256 | inline bool lesser_is_node() const {return nmatches() > 2;} |
| 257 | inline bool greater_is_node() const {return nmatches() > 3;} |
| 258 | |
| 259 | inline bool X() const {return Xy;} |
| 260 | inline bool Y() const {return !Xy;} |
| 261 | |
| 262 | kd_node lesser_node() const { |
| 263 | kd_node ret(*this); // This is an explicit invocation of the copy constructor filled with "this". |
| 264 | // Equivalent to: kd_node ret = (*this); |
| 265 | ret.intv = ret.intv*2+1; |
| 266 | ret.stop = midpoint(); |
| 267 | ret.Xy = !ret.Xy; |
| 268 | return ret; |
| 269 | } |
| 270 | kd_node greater_node() const { |
| 271 | kd_node ret(*this); // This is an explicit invocation of the copy constructor filled with "this". |
| 272 | // Equivalent to: kd_node ret = (*this); |
| 273 | ret.intv = ret.intv*2+2; |
| 274 | ret.start = midpoint(); |
| 275 | ret.Xy = !ret.Xy; |
| 276 | return ret; |
| 277 | } |
| 278 | inline kd_node child_node(int x) const { |
| 279 | if (x < midpoint()) |
| 280 | return lesser_node(); |
| 281 | else |
| 282 | return greater_node(); |
| 283 | } |
| 284 | |
| 285 | // root is always real, and everyone else must have at least 2 points |
| 286 | inline bool is_real() const {return intv==0 || nmatches() > 1;} |
| 287 | }; |
| 288 | |
| 289 | kd_node root() const { |
| 290 | kd_node ret; |
| 291 | ret.start = 0; |
| 292 | ret.stop = match_size; |
| 293 | ret.intv = 0; |
| 294 | ret.Xy = true; |
| 295 | return ret; // Return the structure by value. |
| 296 | } |
| 297 | |
| 298 | public: |
| 299 | |
| 300 | ~DPTree() { |
| 301 | delete[] node; |
| 302 | } |
| 303 | |
| 304 | DPTree(unsigned siz, Match *p): node(NULL),match(p), |
| 305 | node_size(0),match_size(siz) { |
| 306 | node_size = 2; |
| 307 | for(unsigned sz = match_size; sz>1 ; sz/=2, node_size *= 2); |
| 308 | node = new Interval[node_size]; |
| 309 | } |
| 310 | |
| 311 | inline void setParams(int mj) { |
| 312 | MaxJump = mj; |
| 313 | } |
| 314 | |
| 315 | double treeScore() { |
| 316 | init(); |
| 317 | if (match_size > 0) |
| 318 | privScore(root(),root()); |
| 319 | |
| 320 | return node[root().intv].S; |
| 321 | } |
| 322 | |
| 323 | private: |
| 324 | |
| 325 | inline double pairScore(const Match &pl,const Match &ph) const { |
| 326 | const int dx = ph.xlo - pl.xlo; |
| 327 | const int dy = ph.ylo - pl.ylo; // causality difference |
| 328 | |
| 329 | const int ix = ph.xlo - pl.xhi; |
| 330 | const int iy = ph.ylo - pl.yhi; |
| 331 | |
| 332 | const int smaller_jump = (ix < iy)?ix:iy; // will be < 0 if they intersect |
| 333 | const int larger_jump = (ix < iy)?iy:ix; // must be < MaxJump for an interaction |
| 334 | |
| 335 | int intersection = smaller_jump * (smaller_jump < 0); |
| 336 | |
| 337 | return (dx >= 0 && dy >= 0 && larger_jump < MaxJump) |
| 338 | * (pl.S + intersection ); |
| 339 | } |
| 340 | |
| 341 | inline bool pruneScore(kd_node f, |
| 342 | const Match &p) const { |
| 343 | int d,jd; |
| 344 | |
| 345 | if (f.X()) { |
| 346 | jd = p.xlo - node[f.intv].hi; |
| 347 | d = p.xlo - node[f.intv].lo; |
| 348 | } |
| 349 | else { |
| 350 | jd = p.ylo - node[f.intv].hi; |
| 351 | d = p.ylo - node[f.intv].lo; |
| 352 | } |
| 353 | // returns true if we really need to check this score |
| 354 | return (d >= 0 && jd < MaxJump) && (node[f.intv].S >= p.S); |
| 355 | } |
| 356 | |
| 357 | double privScore(kd_node flo,kd_node fhi) { |
| 358 | // no longer double recursive -- just iterate through fhi |
| 359 | for(int x = fhi.start; x < fhi.stop; ++x) { |
| 360 | match[x].S = 0; |
| 361 | matchScore(flo,match[x]); |
| 362 | match[x].S += match[x].selfS; |
| 363 | |
| 364 | for(kd_node tmp(fhi); tmp.is_real() ; tmp = tmp.child_node(x)) { |
| 365 | if (node[tmp.intv].S < match[x].S) node[tmp.intv].S = match[x].S; |
| 366 | } |
| 367 | } |
| 368 | return node[fhi.intv].S; |
| 369 | } |
| 370 | |
| 371 | double matchScore(kd_node flo,Match &p) { |
| 372 | double score = 0; |
| 373 | |
| 374 | if ( (flo.X() && node[flo.intv].lo <= p.xlo || |
| 375 | flo.Y() && node[flo.intv].lo <= p.ylo) |
| 376 | && pruneScore(flo,p) ) { |
| 377 | |
| 378 | if (flo.greater_is_node()) |
| 379 | score = matchScore(flo.greater_node(),p); |
| 380 | else |
| 381 | score = pairScore(match[flo.stop-1],p); |
| 382 | if (p.S < score) p.S = score; |
| 383 | |
| 384 | if (flo.lesser_is_node()) |
| 385 | score = matchScore(flo.lesser_node(),p); |
| 386 | else |
| 387 | score = pairScore(match[flo.start],p); |
| 388 | |
| 389 | if (p.S < score) p.S = score; |
| 390 | |
| 391 | } |
| 392 | return p.S; |
| 393 | } |
| 394 | |
| 395 | void init() { |
| 396 | if (match_size > 0){ sort_nodes(root());} |
| 397 | |
| 398 | int minx=0,miny=0,maxx=0,maxy=0; // initial values will be overwritten |
| 399 | get_bbox(root(),minx,miny,maxx,maxy); |
| 400 | |
| 401 | for (int i=0; i < node_size; ++i) node[i].S = -1; |
| 402 | for (int i=0; i < match_size; ++i) match[i].S = -1; |
| 403 | } |
| 404 | |
| 405 | void sort_nodes(kd_node fs) { |
| 406 | if (fs.intv >= node_size) { |
| 407 | fprintf(stderr,"overflow %d %d\n",fs.intv,node_size); |
| 408 | } |
| 409 | |
| 410 | qsort( match+fs.start, fs.nmatches(), sizeof(Match), |
| 411 | (fs.X()?x_compar:y_compar) ); |
| 412 | |
| 413 | if (fs.greater_is_node()) sort_nodes(fs.greater_node()); |
| 414 | if (fs.lesser_is_node()) sort_nodes(fs.lesser_node()); |
| 415 | } |
| 416 | |
| 417 | void get_bbox(kd_node fs,int &minx,int &miny,int &maxx,int &maxy) { |
| 418 | int lminx,lminy,lmaxx,lmaxy; |
| 419 | int gminx,gminy,gmaxx,gmaxy; |
| 420 | |
| 421 | if (fs.lesser_is_node()) { |
| 422 | get_bbox(fs.lesser_node(),lminx,lminy,lmaxx,lmaxy); |
| 423 | } |
| 424 | else { |
| 425 | lminx = match[fs.start].xlo; |
| 426 | lmaxx = match[fs.start].xhi; |
| 427 | lminy = match[fs.start].ylo; |
| 428 | lmaxy = match[fs.start].yhi; |
| 429 | } |
| 430 | |
| 431 | if (fs.greater_is_node()) { |
| 432 | get_bbox(fs.greater_node(),gminx,gminy,gmaxx,gmaxy); |
| 433 | } |
| 434 | else { |
| 435 | gminx = match[fs.stop-1].xlo; |
| 436 | gmaxx = match[fs.stop-1].xhi; |
| 437 | gminy = match[fs.stop-1].ylo; |
| 438 | gmaxy = match[fs.stop-1].yhi; |
| 439 | } |
| 440 | |
| 441 | miny = (lminy < gminy)?lminy:gminy; |
| 442 | minx = (lminx < gminx)?lminx:gminx; |
| 443 | maxy = (lmaxy > gmaxy)?lmaxy:gmaxy; |
| 444 | maxx = (lmaxx > gmaxx)?lmaxx:gmaxx; |
| 445 | |
| 446 | if (fs.X()) { |
| 447 | node[fs.intv].lo = minx; |
| 448 | node[fs.intv].hi = maxx; |
| 449 | } |
| 450 | else { |
| 451 | node[fs.intv].lo = miny; |
| 452 | node[fs.intv].hi = maxy; |
| 453 | } |
| 454 | |
| 455 | } |
| 456 | }; |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | #endif // STRANDPAIR_H |