| 1 | #include "searchGENOME.H" |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <math.h> |
| 6 | |
| 7 | configuration::configuration(void) { |
| 8 | |
| 9 | _beVerbose = false; |
| 10 | |
| 11 | _merSize = 20; |
| 12 | _merSkip = 0; |
| 13 | _numSearchThreads = 4; |
| 14 | |
| 15 | _doReverse = true; |
| 16 | _doForward = true; |
| 17 | |
| 18 | _maxDiagonal = 25; |
| 19 | _maxGap = 0; |
| 20 | _qsOverlap = 15; |
| 21 | _dsOverlap = 15; |
| 22 | |
| 23 | // Alternate match extension scheme |
| 24 | _extendWeight = 0; |
| 25 | _extendMinimum = 0; |
| 26 | _extendAlternate = false; |
| 27 | |
| 28 | _maxIntronLength = 1000000000; |
| 29 | |
| 30 | _smallSequenceCutoff = 0; |
| 31 | |
| 32 | _minLengthSingle = 0; |
| 33 | _minCoverageSingle = 0.0; |
| 34 | _minLengthMultiple = 0; |
| 35 | _minCoverageMultiple = 0.0; |
| 36 | |
| 37 | _dbFileName = 0L; |
| 38 | _qsFileName = 0L; |
| 39 | _maskFileName = 0L; |
| 40 | _onlyFileName = 0L; |
| 41 | _outputFileName = 0L; |
| 42 | _queryMatchFileName = 0L; |
| 43 | |
| 44 | _outputFile = STDOUT_FILENO; |
| 45 | _matchCountsFile = -1; |
| 46 | |
| 47 | _tableTemporaryFileName = 0L; |
| 48 | _tableFileName = 0L; |
| 49 | _tableBuildOnly = false; |
| 50 | |
| 51 | _binaryOutput = false; |
| 52 | |
| 53 | _qsFASTA = 0L; |
| 54 | _maskDB = 0L; |
| 55 | _onlyDB = 0L; |
| 56 | _positions = 0L; |
| 57 | |
| 58 | _numberOfQueries = 0; |
| 59 | |
| 60 | _startTime = getTime(); |
| 61 | _initTime = _startTime; |
| 62 | _buildTime = _startTime; |
| 63 | _searchTime = _startTime; |
| 64 | |
| 65 | _loaderQueue = 16 * 1024; |
| 66 | _loaderSleep.tv_sec = 1; |
| 67 | _loaderSleep.tv_nsec = 0; |
| 68 | _loaderWarnings = false; |
| 69 | |
| 70 | _searchSleep.tv_sec = 0; |
| 71 | _searchSleep.tv_nsec = 10000000; |
| 72 | |
| 73 | _writerQueue = 32 * 1024; |
| 74 | _writerSleep.tv_sec = 1; |
| 75 | _writerSleep.tv_nsec = 0; |
| 76 | _writerWarnings = false; |
| 77 | } |
| 78 | |
| 79 | configuration::~configuration() { |
| 80 | |
| 81 | if (_beVerbose) { |
| 82 | uint32 nq = _qsFASTA->getNumberOfSequences(); |
| 83 | double tm = _searchTime - _buildTime; |
| 84 | fprintf(stderr, "\n"uint32FMTW(7)" sequences in %5.2f seconds, %8.3f per second.\n", nq, tm, nq/tm); |
| 85 | } |
| 86 | |
| 87 | errno = 0; |
| 88 | close(_outputFile); |
| 89 | close(_matchCountsFile); |
| 90 | if (errno) |
| 91 | fprintf(stderr, "Couldn't close to the output file '%s': %s\n", config._outputFileName, strerror(errno)); |
| 92 | |
| 93 | delete _qsFASTA; |
| 94 | delete _maskDB; |
| 95 | delete _onlyDB; |
| 96 | delete _positions; |
| 97 | } |
| 98 | |
| 99 | static char const *usageString = |
| 100 | "usage: %s [options]\n" |
| 101 | "\n" |
| 102 | "Algorithm Options:\n" |
| 103 | " -mersize k Use k-mers\n" |
| 104 | " -merskip j Skip j mers between each mer inserted into table\n" |
| 105 | " -forward Search only the normal query sequences\n" |
| 106 | " -reverse Search only the reverse-complemented query sequences\n" |
| 107 | " -maxdiagonal d\n" |
| 108 | " -maxgap g\n" |
| 109 | " -qoverlap q\n" |
| 110 | " -doverlap d\n" |
| 111 | " -maxintron m\n" |
| 112 | " -smallsequence\n" |
| 113 | " -singlelength l\n" |
| 114 | " -singlecoverage c\n" |
| 115 | " -multiplelength l\n" |
| 116 | " -multiplecoverage c\n" |
| 117 | " -extendweight w\n" |
| 118 | " -extendminimum m\n" |
| 119 | "\n" |
| 120 | "Process Options\n" |
| 121 | " -numthreads n Use n search threads\n" |
| 122 | "\n" |
| 123 | " -loaderqueue h Size of the loader queue\n" |
| 124 | " -loadersleep t Time the loader will sleep when its output queue is full\n" |
| 125 | " -loaderwarnings Enable warning messages for the loader\n" |
| 126 | "\n" |
| 127 | " -searchsleep t Time the searcher will sleep when it has no input\n" |
| 128 | "\n" |
| 129 | " -writerqueue h Size of the output queue\n" |
| 130 | " -writersleep t Time the writer will sleep when it has nothing to write\n" |
| 131 | " -writerwarnings Enable warning messages for the writer\n" |
| 132 | "\n" |
| 133 | "\n" |
| 134 | " -buildtables datfile If 'datfile' doesn't exist, build the tables, write\n" |
| 135 | " them to 'datfile' and exit.\n" |
| 136 | " -usetables datfile Load the tables from 'datfile' file and do the compute.\n" |
| 137 | " If 'datfile' doesn't exist, an implicit -buildtables is\n" |
| 138 | " performed.\n" |
| 139 | "Input Options:\n" |
| 140 | " -mask f Ignore all mers listed in file f\n" |
| 141 | " -only f Use only the mers listed in file f\n" |
| 142 | " -cdna c.fasta Query sequences (the cDNA, the stream)\n" |
| 143 | " -stream An alias for -cdna\n" |
| 144 | " -genomic g.fasta Database sequences (the genome, the table)\n" |
| 145 | " -table An alias for -genomic)\n" |
| 146 | " -use #,#,#,# using only those sequences specified\n" |
| 147 | " -use file using only those sequences listed in the file\n" |
| 148 | "\n" |
| 149 | "Output Options\n" |
| 150 | " -verbose Entertain the user\n" |
| 151 | " -binary Write the hits in a binary format\n" |
| 152 | " -output f Write output to file f\n" |
| 153 | " -count f Write counts of hits to file f\n"; |
| 154 | |
| 155 | void |
| 156 | configuration::usage(char *name) { |
| 157 | fprintf(stderr, usageString, name); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | void |
| 163 | configuration::read(int argc, char **argv) { |
| 164 | int arg = 1; |
| 165 | |
| 166 | if (argc < 2) { |
| 167 | usage(argv[0]); |
| 168 | exit(1); |
| 169 | } |
| 170 | |
| 171 | while (arg < argc) { |
| 172 | if (strcmp(argv[arg], "-mersize") == 0) { |
| 173 | arg++; |
| 174 | _merSize = atoi(argv[arg]); |
| 175 | } else if (strcmp(argv[arg], "-merskip") == 0) { |
| 176 | arg++; |
| 177 | _merSkip = atoi(argv[arg]); |
| 178 | } else if (strcmp(argv[arg], "-numthreads") == 0) { |
| 179 | arg++; |
| 180 | _numSearchThreads = atoi(argv[arg]); |
| 181 | } else if (strcmp(argv[arg], "-mask") == 0) { |
| 182 | arg++; |
| 183 | _maskFileName = argv[arg]; |
| 184 | } else if (strcmp(argv[arg], "-only") == 0) { |
| 185 | arg++; |
| 186 | _onlyFileName = argv[arg]; |
| 187 | } else if (strcmp(argv[arg], "-cdna") == 0) { |
| 188 | arg++; |
| 189 | _qsFileName = argv[arg]; |
| 190 | } else if (strcmp(argv[arg], "-stream") == 0) { |
| 191 | arg++; |
| 192 | _qsFileName = argv[arg]; |
| 193 | } else if (strcmp(argv[arg], "-genomic") == 0) { |
| 194 | arg++; |
| 195 | _dbFileName = argv[arg]; |
| 196 | } else if (strcmp(argv[arg], "-table") == 0) { |
| 197 | arg++; |
| 198 | _dbFileName = argv[arg]; |
| 199 | } else if (strcmp(argv[arg], "-buildtemporary") == 0) { |
| 200 | arg++; |
| 201 | _tableTemporaryFileName = argv[arg]; |
| 202 | } else if (strcmp(argv[arg], "-buildtables") == 0) { |
| 203 | arg++; |
| 204 | _tableFileName = argv[arg]; |
| 205 | _tableBuildOnly = true; |
| 206 | } else if (strcmp(argv[arg], "-usetables") == 0) { |
| 207 | arg++; |
| 208 | _tableFileName = argv[arg]; |
| 209 | _tableBuildOnly = false; |
| 210 | } else if (strcmp(argv[arg], "-positions") == 0) { |
| 211 | arg++; |
| 212 | _tableFileName = argv[arg]; |
| 213 | _tableBuildOnly = false; |
| 214 | } else if (strcmp(argv[arg], "-forward") == 0) { |
| 215 | _doForward = true; |
| 216 | _doReverse = false; |
| 217 | } else if (strcmp(argv[arg], "-reverse") == 0) { |
| 218 | _doReverse = true; |
| 219 | _doForward = false; |
| 220 | } else if (strcmp(argv[arg], "-verbose") == 0) { |
| 221 | _beVerbose = true; |
| 222 | } else if (strcmp(argv[arg], "-binary") == 0) { |
| 223 | _binaryOutput = true; |
| 224 | } else if (strcmp(argv[arg], "-output") == 0) { |
| 225 | arg++; |
| 226 | _outputFileName = argv[arg]; |
| 227 | } else if (strcmp(argv[arg], "-count") == 0) { |
| 228 | arg++; |
| 229 | _queryMatchFileName = argv[arg]; |
| 230 | } else if (strcmp(argv[arg], "-maxdiagonal") == 0) { |
| 231 | arg++; |
| 232 | _maxDiagonal = atoi(argv[arg]); |
| 233 | } else if (strcmp(argv[arg], "-maxgap") == 0) { |
| 234 | arg++; |
| 235 | _maxGap = atoi(argv[arg]); |
| 236 | } else if (strcmp(argv[arg], "-qoverlap") == 0) { |
| 237 | arg++; |
| 238 | _qsOverlap = atoi(argv[arg]); |
| 239 | } else if (strcmp(argv[arg], "-doverlap") == 0) { |
| 240 | arg++; |
| 241 | _dsOverlap = atoi(argv[arg]); |
| 242 | } else if (strcmp(argv[arg], "-maxintron") == 0) { |
| 243 | arg++; |
| 244 | _maxIntronLength = atoi(argv[arg]); |
| 245 | } else if (strcmp(argv[arg], "-smallsequence") == 0) { |
| 246 | arg++; |
| 247 | _smallSequenceCutoff = atoi(argv[arg]); |
| 248 | } else if (strcmp(argv[arg], "-singlelength") == 0) { |
| 249 | arg++; |
| 250 | _minLengthSingle = atoi(argv[arg]); |
| 251 | } else if (strcmp(argv[arg], "-multiplelength") == 0) { |
| 252 | arg++; |
| 253 | _minLengthMultiple = atoi(argv[arg]); |
| 254 | } else if (strcmp(argv[arg], "-singlecoverage") == 0) { |
| 255 | arg++; |
| 256 | _minCoverageSingle = atof(argv[arg]); |
| 257 | } else if (strcmp(argv[arg], "-multiplecoverage") == 0) { |
| 258 | arg++; |
| 259 | _minCoverageMultiple = atof(argv[arg]); |
| 260 | } else if (strncmp(argv[arg], "-extendweight", 7) == 0) { |
| 261 | arg++; |
| 262 | _extendWeight = atoi(argv[arg]); |
| 263 | _extendAlternate = true; |
| 264 | } else if (strncmp(argv[arg], "-extendminimum", 7) == 0) { |
| 265 | arg++; |
| 266 | _extendMinimum = atoi(argv[arg]); |
| 267 | _extendAlternate = true; |
| 268 | |
| 269 | } else if (strncmp(argv[arg], "-loaderqueue", 8) == 0) { |
| 270 | _loaderQueue = atoi(argv[++arg]); |
| 271 | } else if (strncmp(argv[arg], "-loadersleep", 8) == 0) { |
| 272 | setTime(&_loaderSleep, atof(argv[++arg])); |
| 273 | } else if (strncmp(argv[arg], "-loaderwarnings", 8) == 0) { |
| 274 | _loaderWarnings = true; |
| 275 | |
| 276 | } else if (strncmp(argv[arg], "-searchsleep", 8) == 0) { |
| 277 | setTime(&_searchSleep, atof(argv[++arg])); |
| 278 | |
| 279 | } else if (strncmp(argv[arg], "-writerqueue", 8) == 0) { |
| 280 | _writerQueue = atoi(argv[++arg]); |
| 281 | } else if (strncmp(argv[arg], "-writersleep", 8) == 0) { |
| 282 | setTime(&_writerSleep, atof(argv[++arg])); |
| 283 | } else if (strncmp(argv[arg], "-writerwarnings", 8) == 0) { |
| 284 | _writerWarnings = true; |
| 285 | |
| 286 | } else { |
| 287 | fprintf(stderr, "Unknown option '%s'\n", argv[arg]); |
| 288 | } |
| 289 | arg++; |
| 290 | } |
| 291 | |
| 292 | // |
| 293 | // Make sure some constraints are met |
| 294 | // |
| 295 | |
| 296 | if (_maskFileName && _onlyFileName) { |
| 297 | fprintf(stderr, "ERROR: At most one of -mask and -only may be used.\n"); |
| 298 | exit(1); |
| 299 | } |
| 300 | |
| 301 | // |
| 302 | // Check that the mers are at least adjacent |
| 303 | // |
| 304 | if (_merSkip >= _merSize) { |
| 305 | fprintf(stderr, "ERROR: Mers are not adjacent; make sure merskip <= mersize.\n"); |
| 306 | exit(1); |
| 307 | } |
| 308 | |
| 309 | // Fail if we don't get reasonable signal criteria |
| 310 | // |
| 311 | if (((_minLengthSingle == 0) && (_minCoverageSingle == 0.0)) || |
| 312 | ((_minLengthMultiple == 0) && (_minCoverageMultiple == 0.0))) |
| 313 | fprintf(stderr, "WARNING: Minimum match lengths not specified. All matches will be reported.\n"); |
| 314 | |
| 315 | |
| 316 | // Open output file |
| 317 | // |
| 318 | if (_outputFileName) { |
| 319 | errno = 0; |
| 320 | _outputFile = open(_outputFileName, |
| 321 | O_WRONLY | O_LARGEFILE | O_CREAT | O_TRUNC, |
| 322 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 323 | if (errno) { |
| 324 | fprintf(stderr, "Couldn't open the output file '%s'?\n%s\n", _outputFileName, strerror(errno)); |
| 325 | exit(1); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | if (_queryMatchFileName) { |
| 331 | errno = 0; |
| 332 | _matchCountsFile = open(_queryMatchFileName, |
| 333 | O_WRONLY | O_LARGEFILE | O_CREAT | O_TRUNC, |
| 334 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 335 | if (errno) { |
| 336 | fprintf(stderr, "Couldn't open the match counts file '%s'?\n%s\n", _queryMatchFileName, strerror(errno)); |
| 337 | exit(1); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | // Gotta go somewhere! |
| 343 | // |
| 344 | _startTime = getTime(); |
| 345 | } |