| 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 | static |
| 28 | bool |
| 29 | isHeader(char *inLine) { |
| 30 | return((inLine[0] == '!') || |
| 31 | (inLine[0] == '#') || |
| 32 | (inLine[0] == '/')); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | atacFileStream::atacFileStream(char const *filename) { |
| 37 | |
| 38 | if (filename == 0L) |
| 39 | return; |
| 40 | |
| 41 | _inFile = stdin; |
| 42 | |
| 43 | if ((filename != 0L) && (strcmp(filename, "-") != 0)) { |
| 44 | errno = 0; |
| 45 | _inFile = fopen(filename, "r"); |
| 46 | if (errno) |
| 47 | fprintf(stderr, "atacFileStream::atacFileStream()-- failed to open %s: %s\n", filename, strerror(errno)), exit(1); |
| 48 | } |
| 49 | |
| 50 | _theMatchIID = 0; |
| 51 | _theFeatureIID = 0; |
| 52 | |
| 53 | readHeader(_inLine, _inFile); |
| 54 | } |
| 55 | |
| 56 | atacFileStream::~atacFileStream() { |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | atacMatch* |
| 61 | atacFileStream::nextMatch(char type) { |
| 62 | atacMatch *ret = 0L; |
| 63 | |
| 64 | while ((ret == 0L) && |
| 65 | (feof(_inFile) == false)) { |
| 66 | |
| 67 | if (_inLine[0] == 'M') { |
| 68 | _theMatch.decode(_inLine); |
| 69 | |
| 70 | if (_theMatch.type[0] == type) { |
| 71 | _theMatch.matchiid = _theMatchIID++; |
| 72 | ret = &_theMatch; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fgets(_inLine, 1024, _inFile); |
| 77 | } |
| 78 | |
| 79 | return(ret); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | atacFeature* |
| 84 | atacFileStream::nextFeature(char type[4]) { |
| 85 | atacFeature *ret = 0L; |
| 86 | |
| 87 | while ((ret == 0L) && |
| 88 | (feof(_inFile) == false)) { |
| 89 | |
| 90 | if (_inLine[0] == 'F') { |
| 91 | _theFeature.decode(_inLine); |
| 92 | |
| 93 | // Return the feature if it is the correct type. 0 is the |
| 94 | // wildcard. |
| 95 | // |
| 96 | if (((_theFeature.type[0] == 0) || (type[0] == 0) || (_theFeature.type[0] == type[0])) && |
| 97 | ((_theFeature.type[1] == 0) || (type[1] == 0) || (_theFeature.type[1] == type[1])) && |
| 98 | ((_theFeature.type[2] == 0) || (type[2] == 0) || (_theFeature.type[2] == type[2])) && |
| 99 | ((_theFeature.type[3] == 0) || (type[3] == 0) || (_theFeature.type[3] == type[3]))) { |
| 100 | _theFeature.featureiid = _theFeatureIID++; |
| 101 | ret = &_theFeature; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fgets(_inLine, 1024, _inFile); |
| 106 | } |
| 107 | |
| 108 | return(ret); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | atacFile::atacFile(char const *filename) { |
| 115 | |
| 116 | if (filename == 0L) |
| 117 | return; |
| 118 | |
| 119 | FILE *inFile = stdin; |
| 120 | char inLine[1024]; |
| 121 | |
| 122 | if ((filename != 0L) && (strcmp(filename, "-") != 0)) { |
| 123 | errno = 0; |
| 124 | inFile = fopen(filename, "r"); |
| 125 | if (errno) |
| 126 | fprintf(stderr, "atacFile::atacFile()-- failed to load %s: %s\n", filename, strerror(errno)), exit(1); |
| 127 | } |
| 128 | |
| 129 | // Read the preamble, look for our data sources. This leaves us with |
| 130 | // the first match in the inLine, and fills in fileA and fileB. |
| 131 | // |
| 132 | readHeader(inLine, inFile); |
| 133 | |
| 134 | while (!feof(inFile)) { |
| 135 | switch(inLine[0]) { |
| 136 | case 'M': |
| 137 | { |
| 138 | atacMatch m(inLine); |
| 139 | |
| 140 | if (m.sanity(fastaA(), fastaB(), inLine)) { |
| 141 | if ((m.type[0] == 'u') || |
| 142 | (m.type[0] == 'x')) { |
| 143 | _matches.add(m); |
| 144 | } else if (m.type[0] == 'r') { |
| 145 | _runs.add(m); |
| 146 | } else if (m.type[0] == 'c') { |
| 147 | _clumps.add(m); |
| 148 | } else { |
| 149 | chomp(inLine); |
| 150 | fprintf(stderr, "atacFile::atacFile()-- Unknown match record type '%c' -- '%s'.\n", m.type[0], inLine); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | break; |
| 155 | |
| 156 | case 'F': |
| 157 | { |
| 158 | chomp(inLine); |
| 159 | fprintf(stderr, "atacFile::atacFile()-- Unknown feature record -- '%s'.\n", inLine); |
| 160 | } |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | { |
| 165 | chomp(inLine); |
| 166 | fprintf(stderr, "atacFile::atacFile()-- Unknown record -- '%s'.\n", inLine); |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | fgets(inLine, 1024, inFile); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | atacFile::~atacFile() { |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | atacFileBase::atacFileBase() { |
| 186 | _fileA[0] = 0; |
| 187 | _fileB[0] = 0; |
| 188 | |
| 189 | _labelA[0] = 0; |
| 190 | _labelB[0] = 0; |
| 191 | |
| 192 | _seqA = 0L; |
| 193 | _seqB = 0L; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | atacFileBase::~atacFileBase() { |
| 198 | delete _seqA; |
| 199 | delete _seqB; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void |
| 204 | atacFileBase::readHeader(char *inLine, FILE *in) { |
| 205 | |
| 206 | fgets(inLine, 1024, in); |
| 207 | while (!feof(in) && isHeader(inLine)) { |
| 208 | chomp(inLine); |
| 209 | |
| 210 | if (inLine[0] == '/') { |
| 211 | char *key = inLine + 1; |
| 212 | char *val = inLine + 1; |
| 213 | |
| 214 | while (isspace(*key)) key++; // Skip whitespace between "/" and the key |
| 215 | while (*val != '=') val++; // Move to the "=" |
| 216 | *val++ = 0; // Terminate the key |
| 217 | while (isspace(*val)) val++; // Skip whitespace between "=" and the val |
| 218 | |
| 219 | chomp(key); |
| 220 | chomp(val); |
| 221 | |
| 222 | //fprintf(stderr, "key='%s' val='%s'\n", key, val); |
| 223 | |
| 224 | string K = key; |
| 225 | string V = val; |
| 226 | _params[K] = V; |
| 227 | |
| 228 | // Save ones we use |
| 229 | |
| 230 | if (strncmp(key, "assemblyFile1", 14) == 0) |
| 231 | strcpy(_fileA, val); |
| 232 | |
| 233 | if (strncmp(key, "assemblyFile2", 14) == 0) |
| 234 | strcpy(_fileB, val); |
| 235 | |
| 236 | if (strncmp(key, "assemblyId1", 12) == 0) |
| 237 | strcpy(_labelA, val); |
| 238 | |
| 239 | if (strncmp(key, "assemblyId2", 12) == 0) |
| 240 | strcpy(_labelB, val); |
| 241 | } |
| 242 | |
| 243 | // Otherwise, it's a comment or the header |
| 244 | |
| 245 | fgets(inLine, 1024, in); |
| 246 | } |
| 247 | |
| 248 | //fprintf(stderr, "assemblyFile1 = '%s'\n", _fileA); |
| 249 | //fprintf(stderr, "assemblyFile2 = '%s'\n", _fileB); |
| 250 | //fprintf(stderr, "assemblyId1 = '%s'\n", _labelA); |
| 251 | //fprintf(stderr, "assemblyId2 = '%s'\n", _labelB); |
| 252 | |
| 253 | // Open some seqCache for each of the files |
| 254 | // |
| 255 | if (_fileA && _fileA[0]) { |
| 256 | if (fileExists(_fileA)) { |
| 257 | _seqA = new seqCache(_fileA); |
| 258 | } else { |
| 259 | fprintf(stderr, "atacFile::readHeader()-- can't find '%s', no sequence read.\n", _fileA); |
| 260 | } |
| 261 | } |
| 262 | if (_fileB && _fileB[0]) { |
| 263 | if (fileExists(_fileA)) { |
| 264 | _seqB = new seqCache(_fileB); |
| 265 | } else { |
| 266 | fprintf(stderr, "atacFile::readHeader()-- can't find '%s', no sequence read.\n", _fileB); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | void |
| 274 | atacFileBase::writeHeader(FILE *out) { |
| 275 | |
| 276 | if (out == 0L) |
| 277 | out = stdout; |
| 278 | |
| 279 | fprintf(out, "!format atac 1.0\n"); |
| 280 | fprintf(out, "#\n"); |
| 281 | fprintf(out, "# Legend:\n"); |
| 282 | fprintf(out, "#\n"); |
| 283 | fprintf(out, "# Field 0: the row class\n"); |
| 284 | fprintf(out, "# Field 1: the match type u=ungapped, x=exact, ....\n"); |
| 285 | fprintf(out, "# Field 2: the match instance index\n"); |
| 286 | fprintf(out, "# Field 3: the parent index\n"); |
| 287 | fprintf(out, "# Field 4: the FASTA sequence id in the first assembly\n"); |
| 288 | fprintf(out, "# Field 5: the offset from the start of the sequence for the match\n"); |
| 289 | fprintf(out, "# Field 6: the length of the match in the first assembly\n"); |
| 290 | fprintf(out, "# Field 7: the orientation of the match sequence in the first assembly.\n"); |
| 291 | fprintf(out, "# Field 8: the FASTA sequence id for the second assembly\n"); |
| 292 | fprintf(out, "# Field 9: the offset from the start of the sequence for the match\n"); |
| 293 | fprintf(out, "# Field 10: the length of the match in the second assembly\n"); |
| 294 | fprintf(out, "# Field 11: the orientation of the match sequence in the second assembly.\n"); |
| 295 | fprintf(out, "#\n"); |
| 296 | |
| 297 | map<string,string>::iterator it; |
| 298 | for (it=_params.begin(); it != _params.end(); it++) |
| 299 | fprintf(out, "/%s=%s\n", it->first.c_str(), it->second.c_str()); |
| 300 | } |