| 1 | // This file is part of A2Amapper.cxx . . $ `<file>/atac-driver/libatac/atacMatchOrder.C`/ |
| 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"cxx . . $ `<file>/libbio/bio++.H`/ |
| 24 | #include "atac.H"cxx . . $ `<file>/atac-driver/libatac/atac.H`/ |
| 25 | |
| 26 | |
| 27 | void |
| 28 | atacMatchOrdercxx . . $ atacMatchOrder#::mergeMatchescxx . . $ atacMatchOrder#mergeMatches(72ed48fc4fc66ee8).(atacMatchcxx . . $ atacMatch# *l, atacMatchcxx . . $ atacMatch# *r, uint32cxx . . $ uint32# mergeuid) { |
| 29 | atacMatchcxx . . $ atacMatch# ncxx . . $ atacMatch#atacMatch(49f6e7a06ebc5aa8).; |
| 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(atacMatchcxx . . $ atacMatch#)); |
| 37 | |
| 38 | sprintf(n.matchuidcxx . . $ atacMatch#matchuid., "merge"uint32FMTcxx . . $ `libutil/util.h:65:10`!, mergeuid); |
| 39 | |
| 40 | n.len1cxx . . $ atacMatch#len1. = (r->pos1cxx . . $ atacMatch#pos1. + r->len1cxx . . $ atacMatch#len1.) - (l->pos1cxx . . $ atacMatch#pos1.); |
| 41 | n.len2cxx . . $ atacMatch#len2. = n.len1cxx . . $ atacMatch#len1.; |
| 42 | |
| 43 | if (r->fwd2cxx . . $ atacMatch#fwd2. == false) |
| 44 | n.pos2cxx . . $ atacMatch#pos2. = r->pos2cxx . . $ atacMatch#pos2.; |
| 45 | |
| 46 | n.fwd2cxx . . $ atacMatch#fwd2. = r->fwd2cxx . . $ atacMatch#fwd2.; |
| 47 | |
| 48 | // Update l with the new contents. |
| 49 | |
| 50 | memcpy(l, &n, sizeof(atacMatchcxx . . $ 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 | _matchesLencxx . . $ atacMatchOrder#_matchesLen.--; |
| 59 | for (uint32cxx . . $ uint32# idx = indexcxx . . $ atacMatchOrder#index(2141b656ce2cc063).(r->matchiidcxx . . $ atacMatch#matchiid.); idx < _matchesLencxx . . $ atacMatchOrder#_matchesLen.; idx++) { |
| 60 | _matchescxx . . $ atacMatchOrder#_matches.[idx] = _matchescxx . . $ atacMatchOrder#_matches.[idx+1]; |
| 61 | _matchIIDtoIdxcxx . . $ atacMatchOrder#_matchIIDtoIdx.[_matchescxx . . $ atacMatchOrder#_matches.[idx]->matchiidcxx . . $ atacMatch#matchiid.] = idx; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | static |
| 68 | int |
| 69 | sortA_cxx . . $ sortA_(9148c8c3ff42c9d8).(const void *a, const void *b) { |
| 70 | const atacMatchcxx . . $ atacMatch# *A = *(const atacMatchcxx . . $ atacMatch# * const *)a; |
| 71 | const atacMatchcxx . . $ atacMatch# *B = *(const atacMatchcxx . . $ atacMatch# * const *)b; |
| 72 | |
| 73 | if (A->iid1cxx . . $ atacMatch#iid1. < B->iid1cxx . . $ atacMatch#iid1.) return(-1); |
| 74 | if (A->iid1cxx . . $ atacMatch#iid1. > B->iid1cxx . . $ atacMatch#iid1.) return(1); |
| 75 | if (A->pos1cxx . . $ atacMatch#pos1. < B->pos1cxx . . $ atacMatch#pos1.) return(-1); |
| 76 | if (A->pos1cxx . . $ atacMatch#pos1. > B->pos1cxx . . $ atacMatch#pos1.) return(1); |
| 77 | if (A->len1cxx . . $ atacMatch#len1. > B->len1cxx . . $ atacMatch#len1.) return(-1); |
| 78 | if (A->len1cxx . . $ atacMatch#len1. < B->len1cxx . . $ atacMatch#len1.) return(1); |
| 79 | if (A->iid2cxx . . $ atacMatch#iid2. < B->iid2cxx . . $ atacMatch#iid2.) return(-1); |
| 80 | if (A->iid2cxx . . $ atacMatch#iid2. > B->iid2cxx . . $ atacMatch#iid2.) return(1); |
| 81 | if (A->pos2cxx . . $ atacMatch#pos2. < B->pos2cxx . . $ atacMatch#pos2.) return(-1); |
| 82 | if (A->pos2cxx . . $ atacMatch#pos2. > B->pos2cxx . . $ atacMatch#pos2.) return(1); |
| 83 | if (A->len2cxx . . $ atacMatch#len2. > B->len2cxx . . $ atacMatch#len2.) return(-1); |
| 84 | if (A->len2cxx . . $ atacMatch#len2. < B->len2cxx . . $ atacMatch#len2.) return(1); |
| 85 | return(0); |
| 86 | } |
| 87 | |
| 88 | static |
| 89 | int |
| 90 | sortB_cxx . . $ sortB_(9148c8c3ff42c9d8).(const void *a, const void *b) { |
| 91 | const atacMatchcxx . . $ atacMatch# *A = *(const atacMatchcxx . . $ atacMatch# * const *)a; |
| 92 | const atacMatchcxx . . $ atacMatch# *B = *(const atacMatchcxx . . $ atacMatch# * const *)b; |
| 93 | |
| 94 | if (A->iid2cxx . . $ atacMatch#iid2. < B->iid2cxx . . $ atacMatch#iid2.) return(-1); |
| 95 | if (A->iid2cxx . . $ atacMatch#iid2. > B->iid2cxx . . $ atacMatch#iid2.) return(1); |
| 96 | if (A->pos2cxx . . $ atacMatch#pos2. < B->pos2cxx . . $ atacMatch#pos2.) return(-1); |
| 97 | if (A->pos2cxx . . $ atacMatch#pos2. > B->pos2cxx . . $ atacMatch#pos2.) return(1); |
| 98 | if (A->len2cxx . . $ atacMatch#len2. > B->len2cxx . . $ atacMatch#len2.) return(-1); |
| 99 | if (A->len2cxx . . $ atacMatch#len2. < B->len2cxx . . $ atacMatch#len2.) return(1); |
| 100 | if (A->iid1cxx . . $ atacMatch#iid1. < B->iid1cxx . . $ atacMatch#iid1.) return(-1); |
| 101 | if (A->iid1cxx . . $ atacMatch#iid1. > B->iid1cxx . . $ atacMatch#iid1.) return(1); |
| 102 | if (A->pos1cxx . . $ atacMatch#pos1. < B->pos1cxx . . $ atacMatch#pos1.) return(-1); |
| 103 | if (A->pos1cxx . . $ atacMatch#pos1. > B->pos1cxx . . $ atacMatch#pos1.) return(1); |
| 104 | if (A->len1cxx . . $ atacMatch#len1. > B->len1cxx . . $ atacMatch#len1.) return(-1); |
| 105 | if (A->len1cxx . . $ atacMatch#len1. < B->len1cxx . . $ atacMatch#len1.) return(1); |
| 106 | |
| 107 | return(0); |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | int |
| 112 | sortdiagonal_cxx . . $ sortdiagonal_(9148c8c3ff42c9d8).(const void *a, const void *b) { |
| 113 | const atacMatchcxx . . $ atacMatch# *A = *(const atacMatchcxx . . $ atacMatch# * const *)a; |
| 114 | const atacMatchcxx . . $ atacMatch# *B = *(const atacMatchcxx . . $ atacMatch# * const *)b; |
| 115 | |
| 116 | if (A->iid2cxx . . $ atacMatch#iid2. < B->iid2cxx . . $ atacMatch#iid2.) return(-1); |
| 117 | if (A->iid2cxx . . $ atacMatch#iid2. > B->iid2cxx . . $ atacMatch#iid2.) return(1); |
| 118 | if (A->iid1cxx . . $ atacMatch#iid1. < B->iid1cxx . . $ atacMatch#iid1.) return(-1); |
| 119 | if (A->iid1cxx . . $ atacMatch#iid1. > B->iid1cxx . . $ atacMatch#iid1.) return(1); |
| 120 | if (A->fwd2cxx . . $ atacMatch#fwd2. < B->fwd2cxx . . $ atacMatch#fwd2.) return(-1); |
| 121 | if (A->fwd2cxx . . $ atacMatch#fwd2. > B->fwd2cxx . . $ atacMatch#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 | int32cxx . . $ int32# dA = (int32cxx . . $ int32#)A->pos2cxx . . $ atacMatch#pos2. - (int32cxx . . $ int32#)A->pos1cxx . . $ atacMatch#pos1.; |
| 129 | int32cxx . . $ int32# dB = (int32cxx . . $ int32#)B->pos2cxx . . $ atacMatch#pos2. - (int32cxx . . $ int32#)B->pos1cxx . . $ atacMatch#pos1.; |
| 130 | |
| 131 | if (A->fwd2cxx . . $ atacMatch#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 = (int32cxx . . $ int32#)A->pos2cxx . . $ atacMatch#pos2. - (1000000000 - (int32cxx . . $ int32#)(A->pos2cxx . . $ atacMatch#pos2. + A->len2cxx . . $ atacMatch#len2.)); |
| 137 | dB = (int32cxx . . $ int32#)B->pos2cxx . . $ atacMatch#pos2. - (1000000000 - (int32cxx . . $ int32#)(B->pos2cxx . . $ atacMatch#pos2. + B->len2cxx . . $ atacMatch#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->pos1cxx . . $ atacMatch#pos1. < B->pos1cxx . . $ atacMatch#pos1.) return(-1); |
| 145 | if (A->pos1cxx . . $ atacMatch#pos1. > B->pos1cxx . . $ atacMatch#pos1.) return(1); |
| 146 | if (A->len1cxx . . $ atacMatch#len1. > B->len1cxx . . $ atacMatch#len1.) return(-1); |
| 147 | if (A->len1cxx . . $ atacMatch#len1. < B->len1cxx . . $ atacMatch#len1.) return(1); |
| 148 | |
| 149 | return(0); |
| 150 | } |
| 151 | |
| 152 | static |
| 153 | int |
| 154 | sortmatchuid_cxx . . $ sortmatchuid_(9148c8c3ff42c9d8).(const void *a, const void *b) { |
| 155 | const atacMatchcxx . . $ atacMatch# *A = *(const atacMatchcxx . . $ atacMatch# * const *)a; |
| 156 | const atacMatchcxx . . $ atacMatch# *B = *(const atacMatchcxx . . $ atacMatch# * const *)b; |
| 157 | |
| 158 | int r = strcmp(A->matchuidcxx . . $ atacMatch#matchuid., B->matchuidcxx . . $ atacMatch#matchuid.); |
| 159 | if (r < 0) return(-1); |
| 160 | if (r > 0) return(1); |
| 161 | r = strcmp(A->parentuidcxx . . $ atacMatch#parentuid., B->parentuidcxx . . $ atacMatch#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_cxx . . $ sortparentuid_(9148c8c3ff42c9d8).(const void *a, const void *b) { |
| 171 | const atacMatchcxx . . $ atacMatch# *A = *(const atacMatchcxx . . $ atacMatch# * const *)a; |
| 172 | const atacMatchcxx . . $ atacMatch# *B = *(const atacMatchcxx . . $ atacMatch# * const *)b; |
| 173 | |
| 174 | int r = strcmp(A->parentuidcxx . . $ atacMatch#parentuid., B->parentuidcxx . . $ atacMatch#parentuid.); |
| 175 | if (r < 0) return(-1); |
| 176 | if (r > 0) return(1); |
| 177 | r = strcmp(A->matchuidcxx . . $ atacMatch#matchuid., B->matchuidcxx . . $ atacMatch#matchuid.); |
| 178 | if (r < 0) return(-1); |
| 179 | if (r > 0) return(1); |
| 180 | |
| 181 | return(0); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void |
| 186 | atacMatchOrdercxx . . $ atacMatchOrder#::sortAcxx . . $ atacMatchOrder#sortA(b4d55b1286ff73e8).(uint32cxx . . $ uint32# first, uint32cxx . . $ uint32# len) { |
| 187 | if (len == 0) len = _matchesLencxx . . $ atacMatchOrder#_matchesLen.; |
| 188 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(_matchescxx . . $ atacMatchOrder#_matches. + first, len, sizeof(atacMatchcxx . . $ atacMatch#*), sortA_cxx . . $ sortA_(9148c8c3ff42c9d8).); |
| 189 | updateIndexcxx . . $ atacMatchOrder#updateIndex(49f6e7a06ebc5aa8).(); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | atacMatchOrdercxx . . $ atacMatchOrder#::sortBcxx . . $ atacMatchOrder#sortB(b4d55b1286ff73e8).(uint32cxx . . $ uint32# first, uint32cxx . . $ uint32# len) { |
| 194 | if (len == 0) len = _matchesLencxx . . $ atacMatchOrder#_matchesLen.; |
| 195 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(_matchescxx . . $ atacMatchOrder#_matches. + first, len, sizeof(atacMatchcxx . . $ atacMatch#*), sortB_cxx . . $ sortB_(9148c8c3ff42c9d8).); |
| 196 | updateIndexcxx . . $ atacMatchOrder#updateIndex(49f6e7a06ebc5aa8).(); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | atacMatchOrdercxx . . $ atacMatchOrder#::sortDiagonalcxx . . $ atacMatchOrder#sortDiagonal(b4d55b1286ff73e8).(uint32cxx . . $ uint32# first, uint32cxx . . $ uint32# len) { |
| 201 | if (len == 0) len = _matchesLencxx . . $ atacMatchOrder#_matchesLen.; |
| 202 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(_matchescxx . . $ atacMatchOrder#_matches. + first, len, sizeof(atacMatchcxx . . $ atacMatch#*), sortdiagonal_cxx . . $ sortdiagonal_(9148c8c3ff42c9d8).); |
| 203 | updateIndexcxx . . $ atacMatchOrder#updateIndex(49f6e7a06ebc5aa8).(); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | atacMatchOrdercxx . . $ atacMatchOrder#::sortMatchUIDcxx . . $ atacMatchOrder#sortMatchUID(b4d55b1286ff73e8).(uint32cxx . . $ uint32# first, uint32cxx . . $ uint32# len) { |
| 208 | if (len == 0) len = _matchesLencxx . . $ atacMatchOrder#_matchesLen.; |
| 209 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(_matchescxx . . $ atacMatchOrder#_matches. + first, len, sizeof(atacMatchcxx . . $ atacMatch#*), sortmatchuid_cxx . . $ sortmatchuid_(9148c8c3ff42c9d8).); |
| 210 | updateIndexcxx . . $ atacMatchOrder#updateIndex(49f6e7a06ebc5aa8).(); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | atacMatchOrdercxx . . $ atacMatchOrder#::sortParentUIDcxx . . $ atacMatchOrder#sortParentUID(b4d55b1286ff73e8).(uint32cxx . . $ uint32# first, uint32cxx . . $ uint32# len) { |
| 215 | if (len == 0) len = _matchesLencxx . . $ atacMatchOrder#_matchesLen.; |
| 216 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(_matchescxx . . $ atacMatchOrder#_matches. + first, len, sizeof(atacMatchcxx . . $ atacMatch#*), sortparentuid_cxx . . $ sortparentuid_(9148c8c3ff42c9d8).); |
| 217 | updateIndexcxx . . $ atacMatchOrder#updateIndex(49f6e7a06ebc5aa8).(); |
| 218 | } |