| 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 <errno.h> |
| 22 | #include "heavychains.H" |
| 23 | |
| 24 | |
| 25 | |
| 26 | // The following would need to parameterized for a general kD tree. |
| 27 | // Could we use one function with a static variable to remember the |
| 28 | // sorting direction? |
| 29 | // |
| 30 | int x_compar(const void *x,const void *y) { |
| 31 | const Match &p1=*((const Match*)x); |
| 32 | const Match &p2=*((const Match*)y); |
| 33 | if (p1.xhi < p2.xhi) return -1; |
| 34 | if (p1.xhi > p2.xhi) return 1; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | int y_compar(const void *x,const void *y) { |
| 39 | const Match &p1=*((const Match*)x); |
| 40 | const Match &p2=*((const Match*)y); |
| 41 | if (p1.yhi < p2.yhi) return -1; |
| 42 | if (p1.yhi > p2.yhi) return 1; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | void StrandPair::addHit(char direction, |
| 49 | uint32 id1, |
| 50 | uint32 xlo, |
| 51 | uint32 xln, |
| 52 | uint32 id2, |
| 53 | uint32 ylo, |
| 54 | uint32 yln, |
| 55 | uint32 filled) { |
| 56 | Match tmp; |
| 57 | |
| 58 | tmp.xlo = xlo; |
| 59 | tmp.ylo = ylo; |
| 60 | |
| 61 | tmp.xhi = xlo + xln; |
| 62 | tmp.yhi = ylo + yln; |
| 63 | |
| 64 | // Use the match lengths to initialize the self scores. |
| 65 | tmp.selfS = xln; |
| 66 | if (yln < xln) |
| 67 | tmp.selfS = yln; |
| 68 | |
| 69 | tmp.S = 0.0; |
| 70 | tmp.neS = 0; |
| 71 | tmp.nwS = 0; |
| 72 | tmp.seS = 0; |
| 73 | tmp.swS = 0; |
| 74 | |
| 75 | tmp.filled = filled; |
| 76 | tmp.ori = direction; |
| 77 | |
| 78 | iid1 = id1; |
| 79 | iid2 = id2; |
| 80 | |
| 81 | if (beVerbose > 1) |
| 82 | fprintf(stderr, "heavychains: add %8d %8d %8d -- %8d %8d %8d\n", id1, tmp.xlo, tmp.xhi, id2, tmp.ylo, tmp.yhi); |
| 83 | |
| 84 | Padd(&tmp); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | // new strand pair: begin processing data for the strand pair |
| 90 | // |
| 91 | void StrandPair::process(void) { |
| 92 | int swapi; |
| 93 | |
| 94 | if (Plen > 0) { |
| 95 | if (beVerbose > 0) |
| 96 | fprintf(stderr,"HeavyChains: filtering strands "uint32FMT" "uint32FMT" "uint32FMT"\n", iid1, iid2, Plen); |
| 97 | |
| 98 | DPTree *dp = NULL; |
| 99 | dp = new DPTree(Plen, P); |
| 100 | dp->setParams(maxJump); |
| 101 | |
| 102 | for(int quadrant=0; quadrant < 4; ++quadrant) { |
| 103 | if (beVerbose > 1) |
| 104 | fprintf(stderr,"HeavyChains: arranging process quadrant %d\n", quadrant); |
| 105 | |
| 106 | if ((quadrant == 0) || (quadrant == 2)) { |
| 107 | for (int i=0; i<Plen; ++i) { |
| 108 | swapi = -P[i].xlo; |
| 109 | P[i].xlo = -P[i].xhi; |
| 110 | P[i].xhi = swapi; |
| 111 | } |
| 112 | } else { |
| 113 | for (int i=0; i<Plen; ++i) { |
| 114 | swapi = -P[i].ylo; |
| 115 | P[i].ylo = -P[i].yhi; |
| 116 | P[i].yhi = swapi; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (beVerbose > 1) |
| 121 | fprintf(stderr,"HeavyChains: scoring quadrant\n"); |
| 122 | |
| 123 | dp->treeScore(); |
| 124 | |
| 125 | if (beVerbose>1) |
| 126 | fprintf(stderr,"HeavyChains: recording scores\n"); |
| 127 | |
| 128 | switch(quadrant) { |
| 129 | case 0: for (int i=0; i < Plen; ++i) P[i].nwS = P[i].S; break; |
| 130 | case 1: for (int i=0; i < Plen; ++i) P[i].swS = P[i].S; break; |
| 131 | case 2: for (int i=0; i < Plen; ++i) P[i].seS = P[i].S; break; |
| 132 | case 3: for (int i=0; i < Plen; ++i) P[i].neS = P[i].S; break; |
| 133 | } |
| 134 | |
| 135 | if (beVerbose > 1) |
| 136 | fprintf(stderr,"HeavyChains: done quadrant\n"); |
| 137 | } |
| 138 | |
| 139 | // All output information is now in the match records of P. |
| 140 | delete dp; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | uint64 |
| 147 | StrandPair::print(FILE *outF, |
| 148 | uint64 matchid) { |
| 149 | |
| 150 | for (int i=0; i<Plen; ++i) { |
| 151 | |
| 152 | // symmetrize the forward and backward scores |
| 153 | double inc = P[i].neS + P[i].swS - P[i].selfS; // forward complement orientations |
| 154 | double dec = P[i].seS + P[i].nwS - P[i].selfS; // reverse complement orientations |
| 155 | |
| 156 | // Each score already contains the self score |
| 157 | |
| 158 | if ((inc >= minScore) || (dec >= minScore)) { |
| 159 | int len1 = (P[i].xhi-P[i].xlo); |
| 160 | int len2 = (P[i].yhi-P[i].ylo); |
| 161 | matchid++; |
| 162 | |
| 163 | if (beVerbose > 1) |
| 164 | fprintf(stderr, "heavychains: out "uint32FMTW(8)" %8d %8d -- "uint32FMTW(8)" %8d %8d\n", |
| 165 | iid1, P[i].xlo, P[i].xhi, |
| 166 | iid2, P[i].ylo, P[i].yhi); |
| 167 | |
| 168 | errno = 0; |
| 169 | fprintf(outF, "M x H"uint64FMT" . %s:"uint32FMT" %d %d %d %s:"uint32FMT" %d %d %d > /hf=%.1f /hr=%.1f\n", |
| 170 | matchid, |
| 171 | assemblyId1, iid1, P[i].xlo, len1, 1, |
| 172 | assemblyId2, iid2, P[i].ylo, len2, (P[i].ori == 'f'? 1 : -1), |
| 173 | inc, dec); |
| 174 | if (errno) |
| 175 | fprintf(stderr, "StrandPair::print()-- write failed: %s\n", strerror(errno)); |
| 176 | |
| 177 | sumlen1 += len1; |
| 178 | sumlen2 += len2; |
| 179 | maxlen1 = (maxlen1 > len1) ? maxlen1 : len1; |
| 180 | maxlen2 = (maxlen2 > len2) ? maxlen2 : len2; |
| 181 | maxScoreFwd = (maxScoreFwd > inc) ? maxScoreFwd : inc; |
| 182 | maxScoreRev = (maxScoreRev > dec) ? maxScoreRev : dec; |
| 183 | } |
| 184 | |
| 185 | if (beVerbose > 0) |
| 186 | fprintf(stderr, "HeavyChains: finished strands "uint32FMTW(8)" "uint32FMTW(8)" maxlen1=%f maxlen2=%f maxScoreFwd=%f maxScoreRef=%f\n", |
| 187 | iid1, iid2, maxlen1, maxlen2, maxScoreFwd, maxScoreRev); |
| 188 | } |
| 189 | |
| 190 | return(matchid); |
| 191 | } |