| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2005, 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" |
| 24 | #include "atac.H" |
| 25 | |
| 26 | |
| 27 | void |
| 28 | atacMatchOrder::mergeMatches(atacMatch *l, atacMatch *r, uint32 mergeuid) { |
| 29 | atacMatch n; |
| 30 | |
| 31 | // Create a new match record for the merged match. We could |
| 32 | // probably do this inplace in l. |
| 33 | |
| 34 | // Copy all the defaults from L first. This copies most of the stuff. |
| 35 | // |
| 36 | memcpy(&n, l, sizeof(atacMatch)); |
| 37 | |
| 38 | sprintf(n.matchuid, "merge"uint32FMT, mergeuid); |
| 39 | |
| 40 | n.len1 = (r->pos1 + r->len1) - (l->pos1); |
| 41 | n.len2 = n.len1; |
| 42 | |
| 43 | if (r->fwd2 == false) |
| 44 | n.pos2 = r->pos2; |
| 45 | |
| 46 | n.fwd2 = r->fwd2; |
| 47 | |
| 48 | // Update l with the new contents. |
| 49 | |
| 50 | memcpy(l, &n, sizeof(atacMatch)); |
| 51 | |
| 52 | // Remove the r match from our set. The hardest part is figuring |
| 53 | // out what index the r match is at. The easiest way to do that is |
| 54 | // the most inefficient (start at zero, when we find the r match, |
| 55 | // start updating). The quickest way (given we want an array) |
| 56 | // makes us trust our index. |
| 57 | // |
| 58 | _matchesLen--; |
| 59 | for (uint32 idx = index(r->matchiid); idx < _matchesLen; idx++) { |
| 60 | _matches[idx] = _matches[idx+1]; |
| 61 | _matchIIDtoIdx[_matches[idx]->matchiid] = idx; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | static |
| 68 | int |
| 69 | sortA_(const void *a, const void *b) { |
| 70 | const atacMatch *A = *(const atacMatch * const *)a; |
| 71 | const atacMatch *B = *(const atacMatch * const *)b; |
| 72 | |
| 73 | if (A->iid1 < B->iid1) return(-1); |
| 74 | if (A->iid1 > B->iid1) return(1); |
| 75 | if (A->pos1 < B->pos1) return(-1); |
| 76 | if (A->pos1 > B->pos1) return(1); |
| 77 | if (A->len1 > B->len1) return(-1); |
| 78 | if (A->len1 < B->len1) return(1); |
| 79 | if (A->iid2 < B->iid2) return(-1); |
| 80 | if (A->iid2 > B->iid2) return(1); |
| 81 | if (A->pos2 < B->pos2) return(-1); |
| 82 | if (A->pos2 > B->pos2) return(1); |
| 83 | if (A->len2 > B->len2) return(-1); |
| 84 | if (A->len2 < B->len2) return(1); |
| 85 | return(0); |
| 86 | } |
| 87 | |
| 88 | static |
| 89 | int |
| 90 | sortB_(const void *a, const void *b) { |
| 91 | const atacMatch *A = *(const atacMatch * const *)a; |
| 92 | const atacMatch *B = *(const atacMatch * const *)b; |
| 93 | |
| 94 | if (A->iid2 < B->iid2) return(-1); |
| 95 | if (A->iid2 > B->iid2) return(1); |
| 96 | if (A->pos2 < B->pos2) return(-1); |
| 97 | if (A->pos2 > B->pos2) return(1); |
| 98 | if (A->len2 > B->len2) return(-1); |
| 99 | if (A->len2 < B->len2) return(1); |
| 100 | if (A->iid1 < B->iid1) return(-1); |
| 101 | if (A->iid1 > B->iid1) return(1); |
| 102 | if (A->pos1 < B->pos1) return(-1); |
| 103 | if (A->pos1 > B->pos1) return(1); |
| 104 | if (A->len1 > B->len1) return(-1); |
| 105 | if (A->len1 < B->len1) return(1); |
| 106 | |
| 107 | return(0); |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | int |
| 112 | sortdiagonal_(const void *a, const void *b) { |
| 113 | const atacMatch *A = *(const atacMatch * const *)a; |
| 114 | const atacMatch *B = *(const atacMatch * const *)b; |
| 115 | |
| 116 | if (A->iid2 < B->iid2) return(-1); |
| 117 | if (A->iid2 > B->iid2) return(1); |
| 118 | if (A->iid1 < B->iid1) return(-1); |
| 119 | if (A->iid1 > B->iid1) return(1); |
| 120 | if (A->fwd2 < B->fwd2) return(-1); |
| 121 | if (A->fwd2 > B->fwd2) return(1); |
| 122 | |
| 123 | // We're now in the same sequence pair with the same orientation. |
| 124 | |
| 125 | // So much easier if we use signed math. |
| 126 | |
| 127 | // This works for forward matches |
| 128 | int32 dA = (int32)A->pos2 - (int32)A->pos1; |
| 129 | int32 dB = (int32)B->pos2 - (int32)B->pos1; |
| 130 | |
| 131 | if (A->fwd2 == 0) { |
| 132 | // OK, so not the greatest diagonal computation ever. We end up |
| 133 | // with a gigantic discontinuity at the origin, but we don't |
| 134 | // care, just as long as the diagonals are distinct. |
| 135 | // |
| 136 | dA = (int32)A->pos2 - (1000000000 - (int32)(A->pos2 + A->len2)); |
| 137 | dB = (int32)B->pos2 - (1000000000 - (int32)(B->pos2 + B->len2)); |
| 138 | } |
| 139 | |
| 140 | if (dA < dB) return(-1); |
| 141 | if (dA > dB) return(1); |
| 142 | |
| 143 | // This is just candy; might make things easier later |
| 144 | if (A->pos1 < B->pos1) return(-1); |
| 145 | if (A->pos1 > B->pos1) return(1); |
| 146 | if (A->len1 > B->len1) return(-1); |
| 147 | if (A->len1 < B->len1) return(1); |
| 148 | |
| 149 | return(0); |
| 150 | } |
| 151 | |
| 152 | static |
| 153 | int |
| 154 | sortmatchuid_(const void *a, const void *b) { |
| 155 | const atacMatch *A = *(const atacMatch * const *)a; |
| 156 | const atacMatch *B = *(const atacMatch * const *)b; |
| 157 | |
| 158 | int r = strcmp(A->matchuid, B->matchuid); |
| 159 | if (r < 0) return(-1); |
| 160 | if (r > 0) return(1); |
| 161 | r = strcmp(A->parentuid, B->parentuid); |
| 162 | if (r < 0) return(-1); |
| 163 | if (r > 0) return(1); |
| 164 | |
| 165 | return(0); |
| 166 | } |
| 167 | |
| 168 | static |
| 169 | int |
| 170 | sortparentuid_(const void *a, const void *b) { |
| 171 | const atacMatch *A = *(const atacMatch * const *)a; |
| 172 | const atacMatch *B = *(const atacMatch * const *)b; |
| 173 | |
| 174 | int r = strcmp(A->parentuid, B->parentuid); |
| 175 | if (r < 0) return(-1); |
| 176 | if (r > 0) return(1); |
| 177 | r = strcmp(A->matchuid, B->matchuid); |
| 178 | if (r < 0) return(-1); |
| 179 | if (r > 0) return(1); |
| 180 | |
| 181 | return(0); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void |
| 186 | atacMatchOrder::sortA(uint32 first, uint32 len) { |
| 187 | if (len == 0) len = _matchesLen; |
| 188 | qsort(_matches + first, len, sizeof(atacMatch*), sortA_); |
| 189 | updateIndex(); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | atacMatchOrder::sortB(uint32 first, uint32 len) { |
| 194 | if (len == 0) len = _matchesLen; |
| 195 | qsort(_matches + first, len, sizeof(atacMatch*), sortB_); |
| 196 | updateIndex(); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | atacMatchOrder::sortDiagonal(uint32 first, uint32 len) { |
| 201 | if (len == 0) len = _matchesLen; |
| 202 | qsort(_matches + first, len, sizeof(atacMatch*), sortdiagonal_); |
| 203 | updateIndex(); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | atacMatchOrder::sortMatchUID(uint32 first, uint32 len) { |
| 208 | if (len == 0) len = _matchesLen; |
| 209 | qsort(_matches + first, len, sizeof(atacMatch*), sortmatchuid_); |
| 210 | updateIndex(); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | atacMatchOrder::sortParentUID(uint32 first, uint32 len) { |
| 215 | if (len == 0) len = _matchesLen; |
| 216 | qsort(_matches + first, len, sizeof(atacMatch*), sortparentuid_); |
| 217 | updateIndex(); |
| 218 | } |