| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <new> |
| 4 | #include "seatac.H" |
| 5 | |
| 6 | |
| 7 | // Shared data |
| 8 | // |
| 9 | configuration config; |
| 10 | seqCache *qsFASTA = 0L; |
| 11 | positionDB *positions = 0L; |
| 12 | volatile uint32 numberOfQueries = 0; |
| 13 | filterObj **output = 0L; |
| 14 | pthread_mutex_t inputTailMutex; |
| 15 | seqInCore **input = 0L; |
| 16 | volatile uint32 inputHead = 0; |
| 17 | volatile uint32 inputTail = 0; |
| 18 | volatile uint32 outputPos = 0; |
| 19 | char *threadStats[MAX_THREADS] = { 0L }; |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | #ifdef _AIX |
| 26 | static |
| 27 | void |
| 28 | aix_new_handler() { |
| 29 | fprintf(stderr, "aix_new_handler()-- Memory allocation failed.\n"); |
| 30 | throw std::bad_alloc(); |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | |
| 35 | int |
| 36 | main(int argc, char **argv) { |
| 37 | |
| 38 | #ifdef _AIX |
| 39 | // By default, AIX Visual Age C++ new() returns 0L; this turns on |
| 40 | // exceptions. |
| 41 | // |
| 42 | std::set_new_handler(aix_new_handler); |
| 43 | #endif |
| 44 | |
| 45 | // Read the configuration from the command line |
| 46 | // |
| 47 | if (argc < 2) { |
| 48 | config.usage(argv[0]); |
| 49 | exit(1); |
| 50 | } |
| 51 | config.read(argc, argv); |
| 52 | |
| 53 | config._startTime = getTime(); |
| 54 | |
| 55 | // Open and init the query sequence |
| 56 | // |
| 57 | qsFASTA = new seqCache(config._qsFileName); |
| 58 | |
| 59 | numberOfQueries = qsFASTA->getNumberOfSequences(); |
| 60 | output = new filterObj * [numberOfQueries]; |
| 61 | input = new seqInCore * [numberOfQueries]; |
| 62 | inputHead = 0; |
| 63 | inputTail = 0; |
| 64 | |
| 65 | for (uint32 i=numberOfQueries; i--; ) { |
| 66 | output[i] = 0L; |
| 67 | input[i] = 0L; |
| 68 | } |
| 69 | |
| 70 | config._initTime = getTime(); |
| 71 | |
| 72 | config._genome = new seqStream(config._dbFileName); |
| 73 | |
| 74 | // Create the chunk, returning a positionDB. Threads will use both |
| 75 | // chain and postions to build hitMatrices. |
| 76 | // |
| 77 | if ((config._tableFileName) && (fileExists(config._tableFileName))) { |
| 78 | if (config._tableBuildOnly) { |
| 79 | fprintf(stderr, "All done. Table '%s' already build.\n", config._tableFileName); |
| 80 | exit(0); |
| 81 | } else { |
| 82 | fprintf(stderr, "Loading positionDB state from '%s'\n", config._tableFileName); |
| 83 | positions = new positionDB(config._tableFileName, config._merSize, config._merSkip, 0); |
| 84 | } |
| 85 | } else { |
| 86 | |
| 87 | existDB *maskDB = 0L; |
| 88 | if (config._maskFileName) { |
| 89 | if (config._beVerbose) |
| 90 | fprintf(stderr, "Building maskDB from '%s'\n", config._maskFileName); |
| 91 | maskDB = new existDB(config._maskFileName, config._merSize, existDBcanonical | existDBcompressHash | existDBcompressBuckets, 0, ~uint32ZERO); |
| 92 | } |
| 93 | |
| 94 | existDB *onlyDB = 0L; |
| 95 | if (config._onlyFileName) { |
| 96 | if (config._beVerbose) |
| 97 | fprintf(stderr, "Building onlyDB from '%s'\n", config._onlyFileName); |
| 98 | onlyDB = new existDB(config._onlyFileName, config._merSize, existDBcanonical | existDBcompressHash | existDBcompressBuckets, 0, ~uint32ZERO); |
| 99 | } |
| 100 | |
| 101 | merStream *MS = new merStream(new kMerBuilder(config._merSize), |
| 102 | config._genome, |
| 103 | true, false); |
| 104 | |
| 105 | positions = new positionDB(MS, config._merSize, config._merSkip, maskDB, onlyDB, 0L, 0, 0, 0, 0, config._beVerbose); |
| 106 | |
| 107 | delete MS; |
| 108 | |
| 109 | delete maskDB; |
| 110 | delete onlyDB; |
| 111 | |
| 112 | if (config._tableFileName) { |
| 113 | if (config._beVerbose) |
| 114 | fprintf(stderr, "Dumping positions table to '%s'\n", config._tableFileName); |
| 115 | |
| 116 | positions->saveState(config._tableFileName); |
| 117 | |
| 118 | if (config._tableBuildOnly) |
| 119 | exit(0); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | config._buildTime = getTime(); |
| 124 | |
| 125 | |
| 126 | // |
| 127 | // Initialize threads |
| 128 | // |
| 129 | pthread_attr_t threadAttr; |
| 130 | pthread_t threadID; |
| 131 | |
| 132 | pthread_mutex_init(&inputTailMutex, NULL); |
| 133 | |
| 134 | pthread_attr_init(&threadAttr); |
| 135 | pthread_attr_setscope(&threadAttr, PTHREAD_SCOPE_SYSTEM); |
| 136 | pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); |
| 137 | pthread_attr_setschedpolicy(&threadAttr, SCHED_OTHER); |
| 138 | |
| 139 | // Start the deadlock detection threads |
| 140 | // |
| 141 | #ifdef __alpha |
| 142 | fprintf(stderr, "Deadlock detection enabled!\n"); |
| 143 | pthread_create(&threadID, &threadAttr, deadlockDetector, 0L); |
| 144 | pthread_create(&threadID, &threadAttr, deadlockChecker, 0L); |
| 145 | #endif |
| 146 | |
| 147 | // Start the loader thread |
| 148 | // |
| 149 | pthread_create(&threadID, &threadAttr, loaderThread, 0L); |
| 150 | |
| 151 | // Start the search threads |
| 152 | // |
| 153 | for (uint32 i=0; i<config._numSearchThreads; i++) { |
| 154 | threadStats[i] = 0L; |
| 155 | pthread_create(&threadID, &threadAttr, searchThread, (void *)(unsigned long)i); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | // Open output file |
| 160 | // |
| 161 | FILE *resultFILE = stdout; |
| 162 | |
| 163 | if (config._outputFileName) { |
| 164 | errno = 0; |
| 165 | int rf = open(config._outputFileName, |
| 166 | O_WRONLY | O_LARGEFILE | O_CREAT | O_TRUNC, |
| 167 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 168 | if (errno) { |
| 169 | fprintf(stderr, "Couldn't open the output file '%s'?\n%s\n", config._outputFileName, strerror(errno)); |
| 170 | exit(1); |
| 171 | } |
| 172 | |
| 173 | resultFILE = fdopen(rf, "w"); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | // Dump our information to the output file. |
| 178 | // |
| 179 | config.writeATACheader(resultFILE); |
| 180 | |
| 181 | |
| 182 | |
| 183 | // Initialize the statistics collection object |
| 184 | // |
| 185 | statObj *stats = new statObj(config._filterObj, config._filteropts); |
| 186 | |
| 187 | // Wait for threads to produce output |
| 188 | // |
| 189 | outputPos = 0; |
| 190 | |
| 191 | // The match id of each output record. |
| 192 | // |
| 193 | uint64 matchID = 0; |
| 194 | |
| 195 | double zeroTime = getTime() - 0.00000001; |
| 196 | |
| 197 | while (outputPos < numberOfQueries) { |
| 198 | if (output[outputPos]) { |
| 199 | if (config._beVerbose && ((outputPos & 0x1ff) == 0x1ff)) { |
| 200 | fprintf(stderr, |
| 201 | "O:"uint32FMTW(7)" S:"uint32FMTW(7)" I:"uint32FMTW(7)" T:"uint32FMTW(7)" (%5.1f%%; %8.3f/sec) Finish in %5.2f seconds.\r", |
| 202 | outputPos, |
| 203 | inputTail, |
| 204 | inputHead, |
| 205 | numberOfQueries, |
| 206 | 100.0 * outputPos / numberOfQueries, |
| 207 | outputPos / (getTime() - zeroTime), |
| 208 | (numberOfQueries - outputPos) / (outputPos / (getTime() - zeroTime))); |
| 209 | fflush(stderr); |
| 210 | } |
| 211 | |
| 212 | errno = 0; |
| 213 | matchID = output[outputPos]->output(resultFILE, matchID); |
| 214 | if (errno) { |
| 215 | fprintf(stderr, "Couldn't write to the output file '%s'.\n%d: %s\n", |
| 216 | config._outputFileName, errno, strerror(errno)); |
| 217 | exit(1); |
| 218 | } |
| 219 | |
| 220 | // Add this set of results to the statistics collector |
| 221 | // |
| 222 | stats->add(output[outputPos]); |
| 223 | |
| 224 | //stats->show(stderr); |
| 225 | |
| 226 | delete input[outputPos]; |
| 227 | delete output[outputPos]; |
| 228 | |
| 229 | input[outputPos] = 0L; |
| 230 | output[outputPos] = 0L; |
| 231 | |
| 232 | outputPos++; |
| 233 | } else { |
| 234 | nanosleep(&config._writerSleep, 0L); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (config._beVerbose) { |
| 239 | fprintf(stderr, "\n"uint32FMTW(7)" sequences (%5.1f%%; %8.3f/sec) %5.2f seconds.\n", |
| 240 | numberOfQueries, |
| 241 | 100.0 * outputPos / numberOfQueries, |
| 242 | outputPos / (getTime() - zeroTime), |
| 243 | getTime() - zeroTime); |
| 244 | } |
| 245 | |
| 246 | // Print statistics |
| 247 | // |
| 248 | stats->show(resultFILE); |
| 249 | delete stats; |
| 250 | |
| 251 | errno = 0; |
| 252 | fclose(resultFILE); |
| 253 | if (errno) |
| 254 | fprintf(stderr, "Couldn't close to the output file '%s'.\n%s\n", config._outputFileName, strerror(errno)); |
| 255 | |
| 256 | config._searchTime = getTime(); |
| 257 | |
| 258 | // Clean up |
| 259 | // |
| 260 | delete positions; |
| 261 | |
| 262 | pthread_attr_destroy(&threadAttr); |
| 263 | pthread_mutex_destroy(&inputTailMutex); |
| 264 | |
| 265 | delete [] input; |
| 266 | delete [] output; |
| 267 | |
| 268 | return(0); |
| 269 | } |