| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2007 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 | class afsm { |
| 28 | public: |
| 29 | atacFileStream *_theFile; |
| 30 | atacMatch *_theMatch; |
| 31 | atacFeature *_theFeature; |
| 32 | |
| 33 | bool _endOfFile; |
| 34 | |
| 35 | afsm() { |
| 36 | _theFile = 0L; |
| 37 | _theMatch = 0L; |
| 38 | _theFeature = 0L; |
| 39 | _endOfFile = false; |
| 40 | }; |
| 41 | |
| 42 | ~afsm() { |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | |
| 48 | atacFileStreamMerge::atacFileStreamMerge(void) { |
| 49 | _filesLen = 0; |
| 50 | _filesMax = 4; |
| 51 | _files = new afsm [_filesMax]; |
| 52 | _theMatchIID = 0; |
| 53 | _theFeatureIID = 0; |
| 54 | } |
| 55 | |
| 56 | atacFileStreamMerge::~atacFileStreamMerge(void) { |
| 57 | for (uint32 i=0; i<_filesLen; i++) |
| 58 | delete _files[i]._theFile; |
| 59 | delete [] _files; |
| 60 | |
| 61 | // But wait! Unless we munge our copies of data, we'll free things |
| 62 | // again when atacFileBase destructs! |
| 63 | // |
| 64 | _seqA = 0L; |
| 65 | _seqB = 0L; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | void |
| 71 | atacFileStreamMerge::writeHeader(FILE *out) { |
| 72 | if (_files[0]._theFile != 0L) |
| 73 | _files[0]._theFile->writeHeader(out); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void |
| 78 | atacFileStreamMerge::addFile(char const *filename) { |
| 79 | |
| 80 | if (filename == 0L) |
| 81 | return; |
| 82 | |
| 83 | if (_filesLen >= _filesMax) { |
| 84 | _filesMax *= 2; |
| 85 | afsm *F = new afsm [_filesMax]; |
| 86 | memcpy(F, _files, sizeof(afsm) * _filesLen); |
| 87 | delete [] _files; |
| 88 | _files = F; |
| 89 | } |
| 90 | |
| 91 | _files[_filesLen]._theFile = new atacFileStream(filename); |
| 92 | |
| 93 | // Duplicate a bunch of stuff to our file. |
| 94 | // |
| 95 | if (_filesLen == 0) { |
| 96 | strcpy(_fileA, _files[_filesLen]._theFile->assemblyFileA()); |
| 97 | strcpy(_fileB, _files[_filesLen]._theFile->assemblyFileB()); |
| 98 | |
| 99 | strcpy(_labelA, _files[_filesLen]._theFile->labelA()); |
| 100 | strcpy(_labelB, _files[_filesLen]._theFile->labelB()); |
| 101 | |
| 102 | //_params = _files[_filesLen]._theFile->_params; |
| 103 | |
| 104 | _seqA = _files[_filesLen]._theFile->fastaA(); |
| 105 | _seqB = _files[_filesLen]._theFile->fastaB(); |
| 106 | } |
| 107 | |
| 108 | _filesLen++; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | atacMatch* |
| 114 | atacFileStreamMerge::nextMatch(char type) { |
| 115 | atacMatch *theMatch; |
| 116 | uint32 theMatchIdx; |
| 117 | |
| 118 | // Make sure everyone has a match |
| 119 | // |
| 120 | for (uint32 i=0; i<_filesLen; i++) { |
| 121 | if (_files[i]._endOfFile == false) { |
| 122 | if (_files[i]._theMatch == 0L) |
| 123 | _files[i]._theMatch = _files[i]._theFile->nextMatch(type); |
| 124 | if (_files[i]._theMatch == 0L) |
| 125 | _files[i]._endOfFile = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Pick the smallest. |
| 130 | // |
| 131 | theMatch = _files[0]._theMatch; |
| 132 | theMatchIdx = 0; |
| 133 | |
| 134 | |
| 135 | // need to set matchIID |
| 136 | // should probably also make a new match UID, or better, fix seatac to make UIDs |
| 137 | |
| 138 | |
| 139 | for (uint32 i=1; i<_filesLen; i++) { |
| 140 | if (_files[i]._theMatch) { |
| 141 | if (theMatch == 0L) { |
| 142 | theMatch = _files[i]._theMatch; |
| 143 | theMatchIdx = i; |
| 144 | } |
| 145 | |
| 146 | if (theMatch) { |
| 147 | if (_files[i]._theMatch->iid1 < theMatch->iid1) { |
| 148 | theMatch = _files[i]._theMatch; |
| 149 | theMatchIdx = i; |
| 150 | } |
| 151 | |
| 152 | if ((_files[i]._theMatch->iid1 <= theMatch->iid1) && |
| 153 | (_files[i]._theMatch->iid2 <= theMatch->iid2)) { |
| 154 | theMatch = _files[i]._theMatch; |
| 155 | theMatchIdx = i; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Mark it as used |
| 162 | // |
| 163 | _files[theMatchIdx]._theMatch = 0L; |
| 164 | |
| 165 | return(theMatch); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | atacFeature* |
| 170 | atacFileStreamMerge::nextFeature(char type[4]) { |
| 171 | return(0L); |
| 172 | } |