| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 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 | void |
| 27 | usage(char *name) { |
| 28 | fprintf(stderr, "usage: %s [] -m matches -l log\n", name); |
| 29 | fprintf(stderr, " When it works, fill this in...\n"); |
| 30 | } |
| 31 | |
| 32 | // Reads an atac mapping, and a list of features. Features on one |
| 33 | // axis are projected to the other axis using the atac map. |
| 34 | |
| 35 | int |
| 36 | main(int argc, char **argv) { |
| 37 | char *matchesFile = 0L; |
| 38 | char *featureFile = 0L; |
| 39 | FILE *logFile = 0L; |
| 40 | |
| 41 | int arg=1; |
| 42 | while (arg < argc) { |
| 43 | if (strcmp(argv[arg], "-m") == 0) { |
| 44 | matchesFile = argv[++arg]; |
| 45 | } else if (strcmp(argv[arg], "-f") == 0) { |
| 46 | featureFile = argv[++arg]; |
| 47 | } else if (strcmp(argv[arg], "-l") == 0) { |
| 48 | errno = 0; |
| 49 | logFile = fopen(argv[++arg], "w"); |
| 50 | if (errno) |
| 51 | fprintf(stderr, "Failed to open logfile '%s': %s\n", strerror(errno), argv[arg]), exit(1); |
| 52 | } else { |
| 53 | usage(argv[0]); |
| 54 | exit(1); |
| 55 | } |
| 56 | arg++; |
| 57 | } |
| 58 | |
| 59 | if (matchesFile == 0L) |
| 60 | usage(argv[0]), exit(1); |
| 61 | if (featureFile == 0L) |
| 62 | usage(argv[0]), exit(1); |
| 63 | if (logFile == 0L) |
| 64 | usage(argv[0]), exit(1); |
| 65 | |
| 66 | atacFile AF(matchesFile); |
| 67 | atacMatchList &ML = *AF.matches(); |
| 68 | atacMatchOrder MO(ML); |
| 69 | |
| 70 | |
| 71 | // XXXX extrabroken! |
| 72 | #warning BROKEN |
| 73 | atacFeatureList FL; |
| 74 | |
| 75 | // Project features from A to B. |
| 76 | MO.sortA(); |
| 77 | FL.sort(); |
| 78 | |
| 79 | uint32 mid = 0; |
| 80 | uint32 fid = 0; |
| 81 | uint32 pid = 0; |
| 82 | |
| 83 | while ((mid < MO.numberOfMatches()) && |
| 84 | (fid < FL.numberOfFeatures())) { |
| 85 | atacMatch *m = MO[mid]; |
| 86 | atacFeature *f = FL[fid]; |
| 87 | |
| 88 | if (m->iid1 < f->iid) { |
| 89 | mid++; |
| 90 | continue; |
| 91 | } |
| 92 | if (f->iid < m->iid1) { |
| 93 | fid++; |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | // Same sequences now! |
| 98 | |
| 99 | if (m->pos1 + m->len1 < f->pos) { |
| 100 | // match ends before the feature |
| 101 | mid++; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if (f->pos + f->len < m->pos1) { |
| 106 | // Feature begins before match |
| 107 | fid++; |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | // Feature and match now overlap! |
| 112 | |
| 113 | |
| 114 | // |
| 115 | // This does A -> B -- ONLY. |
| 116 | // |
| 117 | |
| 118 | |
| 119 | |
| 120 | // If feature is completely in match, this is easy. |
| 121 | // |
| 122 | if ((m->pos1 <= f->pos) && ((f->pos + f->len) <= (m->pos1 + m->len1))) { |
| 123 | uint32 beg; |
| 124 | |
| 125 | if (m->fwd2 == true) { |
| 126 | beg = m->pos2 + f->pos - m->pos1; |
| 127 | } else { |
| 128 | beg = m->pos2 + m->len2 - (f->pos - m->pos1) - f->len; |
| 129 | } |
| 130 | |
| 131 | if (f->len > 0) |
| 132 | fprintf(stdout, "M u Aprojected"uint32FMT" %s.%s %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %d\n", |
| 133 | pid, |
| 134 | f->featureuid, m->matchuid, |
| 135 | AF.labelA(), f->iid, f->pos, f->len, |
| 136 | AF.labelB(), m->iid2, beg, f->len, (m->fwd2) ? 1 : -1); |
| 137 | pid++; |
| 138 | fid++; |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | // If match is completely within feature, super easy! |
| 143 | // |
| 144 | if ((f->pos < m->pos1) && (m->pos1 + m->len1) < (f->pos + f->len)) { |
| 145 | if (m->len1 > 0) |
| 146 | fprintf(stdout, "M u Bprojected"uint32FMT" %s.%s %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %d\n", |
| 147 | pid, |
| 148 | f->featureuid, m->matchuid, |
| 149 | AF.labelA(), m->iid1, m->pos1, m->len1, |
| 150 | AF.labelB(), m->iid2, m->pos2, m->len2, (m->fwd2) ? 1 : -1); |
| 151 | pid++; |
| 152 | fid++; |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | // Dang, feature isn't completely in match. Guess where feature |
| 158 | // could be ending? Or just project as much as possible? |
| 159 | |
| 160 | if (f->pos < m->pos1) { |
| 161 | uint32 len = f->len - (m->pos1 - f->pos); |
| 162 | uint32 beg; |
| 163 | |
| 164 | if (m->fwd2 == true) { |
| 165 | beg = m->pos2; |
| 166 | } else { |
| 167 | beg = m->pos2 + m->len2 - len; |
| 168 | } |
| 169 | |
| 170 | if (len > 0) |
| 171 | fprintf(stdout, "M u Cprojected"uint32FMT" %s.%s %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %d\n", |
| 172 | pid, |
| 173 | f->featureuid, m->matchuid, |
| 174 | AF.labelA(), f->iid, m->pos1, len, |
| 175 | AF.labelB(), m->iid2, beg, len, (m->fwd2) ? 1 : -1); |
| 176 | pid++; |
| 177 | fid++; |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | if (m->pos1 + m->len1 < f->pos + f->len) { |
| 182 | uint32 len = m->pos1 + m->len1 - f->pos; |
| 183 | uint32 beg; |
| 184 | |
| 185 | if (m->fwd2 == true) { |
| 186 | beg = m->pos2 + m->len2 - len; |
| 187 | } else { |
| 188 | beg = m->pos2; |
| 189 | } |
| 190 | |
| 191 | if (len > 0) |
| 192 | fprintf(stdout, "M u Dprojected"uint32FMT" %s.%s %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %d\n", |
| 193 | pid, |
| 194 | f->featureuid, m->matchuid, |
| 195 | AF.labelA(), f->iid, f->pos, len, |
| 196 | AF.labelB(), m->iid2, beg, len, (m->fwd2) ? 1 : -1); |
| 197 | pid++; |
| 198 | fid++; |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | fprintf(stderr, "projectFeatures: Unhandled case?\n"); |
| 203 | m->print(stdout, "A", "B"); |
| 204 | f->print(stdout, "A"); |
| 205 | |
| 206 | assert(0); |
| 207 | } |
| 208 | } |