5 * Created by westcott on 11/3/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
11 #include "sequence.hpp"
13 #include "suffixdb.hpp"
14 #include "blastdb.hpp"
15 #include "distancedb.hpp"
16 #include "referencedb.h"
18 /**************************************************************************************************/
19 void Classify::generateDatabaseAndNames(string tfile, string tempFile, string method, int kmerSize, float gapOpen, float gapExtend, float match, float misMatch) {
21 ReferenceDB* rdb = ReferenceDB::getInstance();
23 if (tfile == "saved") { tfile = rdb->getSavedTaxonomy(); }
26 readTaxonomy(taxFile);
29 if (tempFile == "saved") {
30 int start = time(NULL);
31 m->mothurOutEndLine(); m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory."); m->mothurOutEndLine();
33 numSeqs = rdb->referenceSeqs.size();
34 templateFile = rdb->getSavedReference();
35 tempFile = rdb->getSavedReference();
37 bool needToGenerate = true;
39 if(method == "kmer") {
40 database = new KmerDB(tempFile, kmerSize);
42 kmerDBName = tempFile.substr(0,tempFile.find_last_of(".")+1) + char('0'+ kmerSize) + "mer";
43 ifstream kmerFileTest(kmerDBName.c_str());
45 bool GoodFile = m->checkReleaseVersion(kmerFileTest, m->getVersion());
46 if (GoodFile) { needToGenerate = false; }
49 else if(method == "suffix") { database = new SuffixDB(numSeqs); }
50 else if(method == "blast") { database = new BlastDB(tempFile.substr(0,tempFile.find_last_of(".")+1), gapOpen, gapExtend, match, misMatch, "", threadID); }
51 else if(method == "distance") { database = new DistanceDB(); }
53 m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8.");
54 m->mothurOutEndLine();
55 database = new KmerDB(tempFile, 8);
59 for (int k = 0; k < rdb->referenceSeqs.size(); k++) {
60 Sequence temp(rdb->referenceSeqs[k].getName(), rdb->referenceSeqs[k].getAligned());
61 names.push_back(temp.getName());
62 database->addSequence(temp);
64 if ((method == "kmer") && (!shortcuts)) {;} //don't print
65 else {database->generateDB(); }
66 }else if ((method == "kmer") && (!needToGenerate)) {
67 ifstream kmerFileTest(kmerDBName.c_str());
68 database->readKmerDB(kmerFileTest);
70 for (int k = 0; k < rdb->referenceSeqs.size(); k++) {
71 names.push_back(rdb->referenceSeqs[k].getName());
75 database->setNumSeqs(numSeqs);
78 bool okay = phyloTree->ErrorCheck(names);
80 if (!okay) { m->control_pressed = true; }
82 m->mothurOut("It took " + toString(time(NULL) - start) + " to load " + toString(rdb->referenceSeqs.size()) + " sequences and generate the search databases.");m->mothurOutEndLine();
86 templateFile = tempFile;
88 int start = time(NULL);
90 m->mothurOut("Generating search database... "); cout.flush();
93 vector<unsigned long long> positions;
98 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
99 MPI_Comm_size(MPI_COMM_WORLD, &processors);
101 //char* inFileName = new char[tempFile.length()];
102 //memcpy(inFileName, tempFile.c_str(), tempFile.length());
104 char inFileName[1024];
105 strcpy(inFileName, tempFile.c_str());
107 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
110 if (pid == 0) { //only one process needs to scan file
111 positions = m->setFilePosFasta(tempFile, numSeqs); //fills MPIPos, returns numSeqs
113 //send file positions to all processes
114 for(int i = 1; i < processors; i++) {
115 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
116 MPI_Send(&positions[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
119 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
120 positions.resize(numSeqs+1);
121 MPI_Recv(&positions[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
125 if(method == "kmer") { database = new KmerDB(tempFile, kmerSize); }
126 else if(method == "suffix") { database = new SuffixDB(numSeqs); }
127 else if(method == "blast") { database = new BlastDB(tempFile.substr(0,tempFile.find_last_of(".")+1), gapOpen, gapExtend, match, misMatch, "", pid); }
128 else if(method == "distance") { database = new DistanceDB(); }
130 m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8."); m->mothurOutEndLine();
131 database = new KmerDB(tempFile, 8);
135 for(int i=0;i<numSeqs;i++){
137 int length = positions[i+1] - positions[i];
138 char* buf4 = new char[length];
139 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
141 string tempBuf = buf4;
142 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
144 istringstream iss (tempBuf,istringstream::in);
147 if (temp.getName() != "") {
148 if (rdb->save) { rdb->referenceSeqs.push_back(temp); }
149 names.push_back(temp.getName());
150 database->addSequence(temp);
154 database->generateDB();
155 MPI_File_close(&inMPI);
156 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
159 //need to know number of template seqs for suffixdb
160 if (method == "suffix") {
162 m->openInputFile(tempFile, inFASTA);
163 m->getNumSeqs(inFASTA, numSeqs);
167 bool needToGenerate = true;
169 if(method == "kmer") {
170 database = new KmerDB(tempFile, kmerSize);
172 kmerDBName = tempFile.substr(0,tempFile.find_last_of(".")+1) + char('0'+ kmerSize) + "mer";
173 ifstream kmerFileTest(kmerDBName.c_str());
175 bool GoodFile = m->checkReleaseVersion(kmerFileTest, m->getVersion());
176 if (GoodFile) { needToGenerate = false; }
179 else if(method == "suffix") { database = new SuffixDB(numSeqs); }
180 else if(method == "blast") { database = new BlastDB(tempFile.substr(0,tempFile.find_last_of(".")+1), gapOpen, gapExtend, match, misMatch, "", threadID); }
181 else if(method == "distance") { database = new DistanceDB(); }
183 m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8.");
184 m->mothurOutEndLine();
185 database = new KmerDB(tempFile, 8);
188 if (needToGenerate) {
190 m->openInputFile(tempFile, fastaFile);
192 while (!fastaFile.eof()) {
193 Sequence temp(fastaFile);
194 m->gobble(fastaFile);
196 if (rdb->save) { rdb->referenceSeqs.push_back(temp); }
198 names.push_back(temp.getName());
200 database->addSequence(temp);
204 if ((method == "kmer") && (!shortcuts)) {;} //don't print
205 else {database->generateDB(); }
207 }else if ((method == "kmer") && (!needToGenerate)) {
208 ifstream kmerFileTest(kmerDBName.c_str());
209 database->readKmerDB(kmerFileTest);
212 m->openInputFile(tempFile, fastaFile);
214 while (!fastaFile.eof()) {
215 Sequence temp(fastaFile);
216 m->gobble(fastaFile);
218 if (rdb->save) { rdb->referenceSeqs.push_back(temp); }
219 names.push_back(temp.getName());
225 database->setNumSeqs(names.size());
228 bool okay = phyloTree->ErrorCheck(names);
230 if (!okay) { m->control_pressed = true; }
232 m->mothurOut("DONE."); m->mothurOutEndLine();
233 m->mothurOut("It took " + toString(time(NULL) - start) + " seconds generate search database. "); m->mothurOutEndLine();
237 catch(exception& e) {
238 m->errorOut(e, "Classify", "generateDatabaseAndNames");
242 /**************************************************************************************************/
243 Classify::Classify() { m = MothurOut::getInstance(); database = NULL; flipped=false; }
244 /**************************************************************************************************/
246 int Classify::readTaxonomy(string file) {
249 phyloTree = new PhyloTree();
250 string name, taxInfo;
252 m->mothurOutEndLine();
253 m->mothurOut("Reading in the " + file + " taxonomy...\t"); cout.flush();
254 if (m->debug) { m->mothurOut("[DEBUG]: Taxonomies read in...\n"); }
257 int pid, num, processors;
258 vector<unsigned long long> positions;
263 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
264 MPI_Comm_size(MPI_COMM_WORLD, &processors);
266 char inFileName[1024];
267 strcpy(inFileName, file.c_str());
269 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
273 positions = m->setFilePosEachLine(file, num);
275 //send file positions to all processes
276 for(int i = 1; i < processors; i++) {
277 MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
278 MPI_Send(&positions[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
281 MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
282 positions.resize(num+1);
283 MPI_Recv(&positions[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
287 for(int i=0;i<num;i++){
289 int length = positions[i+1] - positions[i];
290 char* buf4 = new char[length];
292 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
294 string tempBuf = buf4;
295 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
298 istringstream iss (tempBuf,istringstream::in);
299 iss >> name; m->gobble(iss);
301 if (m->debug) { m->mothurOut("[DEBUG]: name = " + name + " tax = " + taxInfo + "\n"); }
302 taxonomy[name] = taxInfo;
303 phyloTree->addSeqToTree(name, taxInfo);
306 MPI_File_close(&inMPI);
307 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
311 m->readTax(file, taxonomy);
312 for (map<string, string>::iterator itTax = taxonomy.begin(); itTax != taxonomy.end(); itTax++) { phyloTree->addSeqToTree(itTax->first, itTax->second); }
314 phyloTree->assignHeirarchyIDs(0);
316 phyloTree->setUp(file);
318 m->mothurOut("DONE.");
319 m->mothurOutEndLine(); cout.flush();
321 return phyloTree->getNumSeqs();
324 catch(exception& e) {
325 m->errorOut(e, "Classify", "readTaxonomy");
329 /**************************************************************************************************/
331 vector<string> Classify::parseTax(string tax) {
333 vector<string> taxons;
335 tax = tax.substr(0, tax.length()-1); //get rid of last ';'
339 while (tax.find_first_of(';') != -1) {
340 individual = tax.substr(0,tax.find_first_of(';'));
341 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
342 taxons.push_back(individual);
346 taxons.push_back(tax);
350 catch(exception& e) {
351 m->errorOut(e, "Classify", "parseTax");
355 /**************************************************************************************************/
357 double Classify::getLogExpSum(vector<double> probabilities, int& maxIndex){
359 // http://jblevins.org/notes/log-sum-exp
361 double maxProb = probabilities[0];
364 int numProbs = (int)probabilities.size();
366 for(int i=1;i<numProbs;i++){
367 if(probabilities[i] >= maxProb){
368 maxProb = probabilities[i];
373 double probSum = 0.0000;
375 for(int i=0;i<numProbs;i++){
376 probSum += exp(probabilities[i] - maxProb);
379 probSum = log(probSum) + maxProb;
383 catch(exception& e) {
384 m->errorOut(e, "Classify", "getLogExpSum");
389 /**************************************************************************************************/