| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2005 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 <vector> |
| 24 | #include <algorithm> |
| 25 | using namespace std; |
| 26 | |
| 27 | #include "atac.H" |
| 28 | #include "match.H" |
| 29 | #include "bio++.H" |
| 30 | #include "seqCache.H" |
| 31 | |
| 32 | |
| 33 | uint32 minEndRunLen = 10; // -E /matchExtenderMinEndRunLen |
| 34 | uint32 maxMMBlock = 3; // -B /matchExtenderMaxMMBlock |
| 35 | uint32 minBlockSep = 20; // -S /matchExtenderMinBlockSep |
| 36 | double minIdentity = 0.95; // -I /matchExtenderMinIdentity |
| 37 | uint32 maxNbrSep = 100; // -P /matchExtenderMaxNbrSep |
| 38 | uint32 maxNbrPathMM = 5; // -D /matchExtenderMaxNbrPathMM |
| 39 | |
| 40 | |
| 41 | bool trim_to_pct(vector<match_s *>& matches, uint32 midx, double pct); |
| 42 | void extend_match_backward(vector<match_s *>& matches, uint32 midx, uint32 min_start_pos); |
| 43 | bool can_reach_nearby_match(match_s *src, match_s *dest); |
| 44 | bool extend_match_forward(vector<match_s *>& matches, uint32 midx, match_s *target); |
| 45 | uint32 extend_matches_on_diagonal(vector<match_s *>& matches, uint32 diag_start); |
| 46 | |
| 47 | |
| 48 | class MatchCompare { |
| 49 | public: |
| 50 | int operator()(const match_s *m1, const match_s *m2) { |
| 51 | return(*m1 < *m2); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | |
| 57 | // Read matches until the iid differs. Leave the next match in inLine. |
| 58 | // |
| 59 | bool |
| 60 | readMatches(atacFileStreamMerge &AF, |
| 61 | atacMatch *&m, |
| 62 | seqCache *C1, |
| 63 | seqCache *C2, |
| 64 | vector<match_s *> &fwdMatches, |
| 65 | vector<match_s *> &revMatches) { |
| 66 | |
| 67 | fwdMatches.clear(); |
| 68 | revMatches.clear(); |
| 69 | |
| 70 | // If M is null, we're here for the first time, so get the next |
| 71 | // (first) match from the file. M is also null if we're at the end |
| 72 | // of the file, so if after getting a match (that's done at the end |
| 73 | // of this routine) we're still null, we're all done. |
| 74 | // |
| 75 | if (m == 0L) |
| 76 | m = AF.nextMatch('x'); |
| 77 | if (m == 0L) |
| 78 | return(false); |
| 79 | |
| 80 | uint32 iid1 = m->iid1; |
| 81 | uint32 iid2 = m->iid2; |
| 82 | |
| 83 | seqInCore *seq1 = C1->getSequenceInCore(iid1); |
| 84 | seqInCore *seq2 = C2->getSequenceInCore(iid2); |
| 85 | |
| 86 | while (m) { |
| 87 | if ((m->iid1 == iid1) && (m->iid2 == iid2)) { |
| 88 | |
| 89 | if (m->fwd1 == m->fwd2) |
| 90 | fwdMatches.push_back(new match_s(m->matchuid, |
| 91 | seq1, m->iid1, m->pos1, m->len1, m->fwd1, |
| 92 | seq2, m->iid2, m->pos2, m->len2, m->fwd2)); |
| 93 | else |
| 94 | revMatches.push_back(new match_s(m->matchuid, |
| 95 | seq1, m->iid1, m->pos1, m->len1, m->fwd1, |
| 96 | seq2, m->iid2, m->pos2, m->len2, m->fwd2)); |
| 97 | } else { |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | m = AF.nextMatch('x'); |
| 102 | } |
| 103 | |
| 104 | if (fwdMatches.size() > 0) |
| 105 | sort(fwdMatches.begin(), fwdMatches.end(), MatchCompare()); |
| 106 | |
| 107 | if (revMatches.size() > 0) |
| 108 | sort(revMatches.begin(), revMatches.end(), MatchCompare()); |
| 109 | |
| 110 | return(true); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | int |
| 117 | main(int argc, char *argv[]) { |
| 118 | bool fail = false; |
| 119 | |
| 120 | atacMatch *m = 0L; |
| 121 | atacFileStreamMerge AF; |
| 122 | |
| 123 | int arg=1; |
| 124 | while (arg < argc) { |
| 125 | if (strcmp(argv[arg], "-e") == 0) { |
| 126 | minEndRunLen = strtouint32(argv[++arg], 0L); |
| 127 | } else if (strcmp(argv[arg], "-b") == 0) { |
| 128 | maxMMBlock = strtouint32(argv[++arg], 0L); |
| 129 | } else if (strcmp(argv[arg], "-s") == 0) { |
| 130 | minBlockSep = strtouint32(argv[++arg], 0L); |
| 131 | } else if (strcmp(argv[arg], "-i") == 0) { |
| 132 | minIdentity = atof(argv[++arg]); |
| 133 | } else if (strcmp(argv[arg], "-p") == 0) { |
| 134 | maxNbrSep = strtouint32(argv[++arg], 0L); |
| 135 | } else if (strcmp(argv[arg], "-d") == 0) { |
| 136 | maxNbrPathMM = strtouint32(argv[++arg], 0L); |
| 137 | } else { |
| 138 | //fprintf(stderr, "unknown option %s\n", argv[arg]); |
| 139 | //fail = true; |
| 140 | AF.addFile(argv[arg]); |
| 141 | } |
| 142 | |
| 143 | arg++; |
| 144 | } |
| 145 | |
| 146 | if (fail) { |
| 147 | fprintf(stderr, "usage: %s [options] header.atac matches.atac ... > matches.atac\n", argv[0]); |
| 148 | fprintf(stderr, " -e <int> matchExtenderMinEndRunLen, 10\n"); |
| 149 | fprintf(stderr, " -b <int> matchExtenderMaxMMBlock, 3\n"); |
| 150 | fprintf(stderr, " -s <int> matchExtenderMinBlockSep, 20\n"); |
| 151 | fprintf(stderr, " -i <float> matchExtenderMinIdentity, 0.95\n"); |
| 152 | fprintf(stderr, " -p <int> matchExtenderMaxNbrSep, 100\n"); |
| 153 | fprintf(stderr, " -d <int> matchExtenderMaxNbrPathMM, 5\n"); |
| 154 | exit(1); |
| 155 | } |
| 156 | |
| 157 | AF.writeHeader(stdout); |
| 158 | |
| 159 | seqCache *C1 = new seqCache(AF.assemblyFileA(), 1, false); |
| 160 | seqCache *C2 = new seqCache(AF.assemblyFileB(), 1, false); |
| 161 | |
| 162 | C1->loadAllSequences(); |
| 163 | |
| 164 | vector<match_s *> fwdMatches; |
| 165 | vector<match_s *> revMatches; |
| 166 | |
| 167 | while (readMatches(AF, m, C1, C2, fwdMatches, revMatches)) { |
| 168 | |
| 169 | uint32 diag_start = 0; |
| 170 | while (diag_start < fwdMatches.size()) { |
| 171 | //fprintf(stderr, "fwd: M u %s . %s %d %d 1 %s %d %d 1\n", |
| 172 | // fwdMatches[diag_start]->_matchId, |
| 173 | // fwdMatches[diag_start]->_id1, fwdMatches[diag_start]->_acc1->getRangeBegin(), fwdMatches[diag_start]->_acc1->getRangeLength(), |
| 174 | // fwdMatches[diag_start]->_id2, fwdMatches[diag_start]->_acc2->getRangeBegin(), fwdMatches[diag_start]->_acc2->getRangeLength()); |
| 175 | diag_start = extend_matches_on_diagonal(fwdMatches, diag_start); |
| 176 | } |
| 177 | |
| 178 | diag_start = 0; |
| 179 | while (diag_start < revMatches.size()) { |
| 180 | //fprintf(stderr, "rev: M u %s . %s %d %d 1 %s %d %d 1\n", |
| 181 | // revMatches[diag_start]->_matchId, |
| 182 | // revMatches[diag_start]->_id1, revMatches[diag_start]->_acc1->getRangeBegin(), revMatches[diag_start]->_acc1->getRangeLength(), |
| 183 | // revMatches[diag_start]->_id2, revMatches[diag_start]->_acc2->getRangeBegin(), revMatches[diag_start]->_acc2->getRangeLength()); |
| 184 | diag_start = extend_matches_on_diagonal(revMatches, diag_start); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | // Dump and destroy all the matches |
| 189 | // |
| 190 | for (uint32 i=0; i<fwdMatches.size(); i++) { |
| 191 | if (!fwdMatches[i]->isDeleted()) |
| 192 | fprintf(stdout, "M u %s . %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" 1\n", |
| 193 | fwdMatches[i]->_matchId, |
| 194 | AF.labelA(), fwdMatches[i]->_iid1, fwdMatches[i]->_acc1->getRangeBegin(), fwdMatches[i]->_acc1->getRangeLength(), |
| 195 | AF.labelB(), fwdMatches[i]->_iid2, fwdMatches[i]->_acc2->getRangeBegin(), fwdMatches[i]->_acc2->getRangeLength()); |
| 196 | delete fwdMatches[i]; |
| 197 | } |
| 198 | |
| 199 | for (uint32 i=0; i<revMatches.size(); i++) { |
| 200 | if (!revMatches[i]->isDeleted()) |
| 201 | fprintf(stdout, "M u %s . %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" -1\n", |
| 202 | revMatches[i]->_matchId, |
| 203 | AF.labelA(), revMatches[i]->_iid1, revMatches[i]->_acc1->getRangeBegin(), revMatches[i]->_acc1->getRangeLength(), |
| 204 | AF.labelB(), revMatches[i]->_iid2, revMatches[i]->_acc2->getRangeBegin(), revMatches[i]->_acc2->getRangeLength()); |
| 205 | delete revMatches[i]; |
| 206 | } |
| 207 | |
| 208 | fwdMatches.clear(); |
| 209 | revMatches.clear(); |
| 210 | } |
| 211 | |
| 212 | delete C1; |
| 213 | delete C2; |
| 214 | } |