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"
17 /**************************************************************************************************/
18 void Classify::generateDatabaseAndNames(string tfile, string tempFile, string method, int kmerSize, float gapOpen, float gapExtend, float match, float misMatch) {
21 readTaxonomy(taxFile);
23 templateFile = tempFile;
25 int start = time(NULL);
28 m->mothurOut("Generating search database... "); cout.flush();
31 vector<long> positions;
36 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
37 MPI_Comm_size(MPI_COMM_WORLD, &processors);
39 //char* inFileName = new char[tempFile.length()];
40 //memcpy(inFileName, tempFile.c_str(), tempFile.length());
42 char inFileName[1024];
43 strcpy(inFileName, tempFile.c_str());
45 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
48 if (pid == 0) { //only one process needs to scan file
49 positions = setFilePosFasta(tempFile, numSeqs); //fills MPIPos, returns numSeqs
51 //send file positions to all processes
52 for(int i = 1; i < processors; i++) {
53 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
54 MPI_Send(&positions[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
57 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
58 positions.resize(numSeqs+1);
59 MPI_Recv(&positions[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
63 if(method == "kmer") { database = new KmerDB(tempFile, kmerSize); }
64 else if(method == "suffix") { database = new SuffixDB(numSeqs); }
65 else if(method == "blast") { database = new BlastDB(gapOpen, gapExtend, match, misMatch); }
66 else if(method == "distance") { database = new DistanceDB(); }
68 m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8."); m->mothurOutEndLine();
69 database = new KmerDB(tempFile, 8);
73 for(int i=0;i<numSeqs;i++){
75 int length = positions[i+1] - positions[i];
76 char* buf4 = new char[length];
77 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
79 string tempBuf = buf4;
80 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
82 istringstream iss (tempBuf,istringstream::in);
85 if (temp.getName() != "") {
86 names.push_back(temp.getName());
87 database->addSequence(temp);
91 database->generateDB();
92 MPI_File_close(&inMPI);
93 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
96 //need to know number of template seqs for suffixdb
97 if (method == "suffix") {
99 openInputFile(tempFile, inFASTA);
100 numSeqs = count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
104 bool needToGenerate = true;
106 if(method == "kmer") {
107 database = new KmerDB(tempFile, kmerSize);
109 kmerDBName = tempFile.substr(0,tempFile.find_last_of(".")+1) + char('0'+ kmerSize) + "mer";
110 ifstream kmerFileTest(kmerDBName.c_str());
111 if(kmerFileTest){ needToGenerate = false; }
113 else if(method == "suffix") { database = new SuffixDB(numSeqs); }
114 else if(method == "blast") { database = new BlastDB(gapOpen, gapExtend, match, misMatch); }
115 else if(method == "distance") { database = new DistanceDB(); }
117 m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8.");
118 m->mothurOutEndLine();
119 database = new KmerDB(tempFile, 8);
122 if (needToGenerate) {
124 openInputFile(tempFile, fastaFile);
126 while (!fastaFile.eof()) {
127 Sequence temp(fastaFile);
130 names.push_back(temp.getName());
132 database->addSequence(temp);
136 database->generateDB();
138 }else if ((method == "kmer") && (!needToGenerate)) {
139 ifstream kmerFileTest(kmerDBName.c_str());
140 database->readKmerDB(kmerFileTest);
143 openInputFile(tempFile, fastaFile);
145 while (!fastaFile.eof()) {
146 Sequence temp(fastaFile);
149 names.push_back(temp.getName());
154 database->setNumSeqs(names.size());
156 m->mothurOut("DONE."); m->mothurOutEndLine();
157 m->mothurOut("It took " + toString(time(NULL) - start) + " seconds generate search database. "); m->mothurOutEndLine();
160 catch(exception& e) {
161 m->errorOut(e, "Classify", "generateDatabaseAndNames");
165 /**************************************************************************************************/
166 Classify::Classify() { m = MothurOut::getInstance(); database = NULL; }
167 /**************************************************************************************************/
169 int Classify::readTaxonomy(string file) {
172 phyloTree = new PhyloTree();
173 string name, taxInfo;
175 m->mothurOutEndLine();
176 m->mothurOut("Reading in the " + file + " taxonomy...\t"); cout.flush();
179 int pid, num, processors;
180 vector<long> positions;
185 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
186 MPI_Comm_size(MPI_COMM_WORLD, &processors);
188 //char* inFileName = new char[file.length()];
189 //memcpy(inFileName, file.c_str(), file.length());
191 char inFileName[1024];
192 strcpy(inFileName, file.c_str());
194 MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
198 positions = setFilePosEachLine(file, num);
200 //send file positions to all processes
201 for(int i = 1; i < processors; i++) {
202 MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
203 MPI_Send(&positions[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
206 MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
207 positions.resize(num+1);
208 MPI_Recv(&positions[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
212 for(int i=0;i<num;i++){
214 int length = positions[i+1] - positions[i];
215 char* buf4 = new char[length];
217 MPI_File_read_at(inMPI, positions[i], buf4, length, MPI_CHAR, &status);
219 string tempBuf = buf4;
220 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
223 istringstream iss (tempBuf,istringstream::in);
224 iss >> name >> taxInfo;
225 taxonomy[name] = taxInfo;
226 phyloTree->addSeqToTree(name, taxInfo);
229 MPI_File_close(&inMPI);
230 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
233 openInputFile(file, inTax);
235 //read template seqs and save
236 while (!inTax.eof()) {
237 inTax >> name >> taxInfo;
239 taxonomy[name] = taxInfo;
241 phyloTree->addSeqToTree(name, taxInfo);
248 phyloTree->assignHeirarchyIDs(0);
250 phyloTree->setUp(file);
252 m->mothurOut("DONE.");
253 m->mothurOutEndLine(); cout.flush();
255 return phyloTree->getNumSeqs();
258 catch(exception& e) {
259 m->errorOut(e, "Classify", "readTaxonomy");
263 /**************************************************************************************************/
265 vector<string> Classify::parseTax(string tax) {
267 vector<string> taxons;
269 tax = tax.substr(0, tax.length()-1); //get rid of last ';'
273 while (tax.find_first_of(';') != -1) {
274 individual = tax.substr(0,tax.find_first_of(';'));
275 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
276 taxons.push_back(individual);
280 taxons.push_back(tax);
284 catch(exception& e) {
285 m->errorOut(e, "Classify", "parseTax");
289 /**************************************************************************************************/