1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
* classify.cpp
* Mothur
*
* Created by westcott on 11/3/09.
* Copyright 2009 Schloss Lab. All rights reserved.
*
*/
#include "classify.h"
#include "sequence.hpp"
#include "kmerdb.hpp"
#include "suffixdb.hpp"
#include "distancedb.hpp"
/**************************************************************************************************/
void Classify::generateDatabaseAndNames(string tfile, string tempFile, string method, int kmerSize, float gapOpen, float gapExtend, float match, float misMatch, string version) {
try {
Utils util;
int numSeqs = 0; maxLevel = 0;
taxFile = tfile;
templateFile = tempFile;
long start = time(nullptr); m->mothurOut("Generating search database... "); cout.flush();
//need to know number of template seqs for suffixdb
if (method == "suffix") {
ifstream inFASTA; util.openInputFile(tempFile, inFASTA);
util.getNumSeqs(inFASTA, numSeqs);
inFASTA.close();
}
bool needToGenerate = true;
string dBName;
if(method == "kmer") {
database = new KmerDB(tempFile, kmerSize);
dBName = tempFile.substr(0,tempFile.find_last_of(".")+1) + char('0'+ kmerSize) + "mer";
ifstream kmerFileTest(dBName.c_str());
if(kmerFileTest){
string line = util.getline(kmerFileTest);
bool GoodFile = util.checkReleaseVersion(line, version); kmerFileTest.close();
int shortcutTimeStamp = util.getTimeStamp(dBName);
int referenceTimeStamp = util.getTimeStamp(tempFile);
//if the shortcut file is older then the reference file, remake shortcut file
if (shortcutTimeStamp < referenceTimeStamp) { GoodFile = false; }
if (GoodFile) { needToGenerate = false; }
}
}
else if(method == "suffix") { database = new SuffixDB(numSeqs); }
else if(method == "distance") { database = new DistanceDB(); }
else {
m->mothurOut(method + " is not a valid search option. I will run the command using kmer, ksize=8.\n");
database = new KmerDB(tempFile, 8);
}
if (!m->getControl_pressed()) {
if (needToGenerate) {
ifstream fastaFile; util.openInputFile(tempFile, fastaFile);
while (!fastaFile.eof()) {
Sequence temp(fastaFile); gobble(fastaFile);
names.push_back(temp.getName());
database->addSequence(temp);
}
fastaFile.close();
if ((method == "kmer") && (!shortcuts)) {;} //don't print
else {database->generateDB(); }
}else if ((method == "kmer") && (!needToGenerate)) {
ifstream FileTest(dBName.c_str());
database->readDB(FileTest);
ifstream fastaFile; util.openInputFile(tempFile, fastaFile);
while (!fastaFile.eof()) {
Sequence temp(fastaFile); gobble(fastaFile);
names.push_back(temp.getName());
}
fastaFile.close();
}
database->setNumSeqs(names.size());
m->mothurOut("DONE.\nIt took " + toString(time(nullptr) - start) + " seconds generate search database.\n");
readTaxonomy(taxFile);
//sanity check
bool okay = phyloTree->ErrorCheck(names);
if (!okay) { m->setControl_pressed(true); }
}
}
catch(exception& e) {
m->errorOut(e, "Classify", "generateDatabaseAndNames");
exit(1);
}
}
/**************************************************************************************************/
Classify::Classify() { m = MothurOut::getInstance(); database = nullptr; phyloTree=nullptr; maxLevel = 0; }
/**************************************************************************************************/
int Classify::readTaxonomy(string file) {
try {
phyloTree = new PhyloTree();
string name, taxInfo;
m->mothurOut("\nReading in the " + file + " taxonomy...\t"); cout.flush();
if (m->getDebug()) { m->mothurOut("[DEBUG]: Taxonomies read in...\n"); }
taxonomy.clear();
Utils util; util.readTax(file, taxonomy, true);
for (map<string, string>::iterator itTax = taxonomy.begin(); itTax != taxonomy.end(); itTax++) {
phyloTree->addSeqToTree(itTax->first, itTax->second);
if (m->getControl_pressed()) { break; }
}
phyloTree->assignHeirarchyIDs(0);
maxLevel = phyloTree->getMaxLevel();
phyloTree->setUp(file);
m->mothurOut("DONE.\n"); cout.flush();
return phyloTree->getNumSeqs();
}
catch(exception& e) {
m->errorOut(e, "Classify", "readTaxonomy");
exit(1);
}
}
/**************************************************************************************************/
vector<string> Classify::parseTax(string tax) {
try {
vector<string> taxons;
Utils util; util.splitAtChar(tax, taxons, ';');
return taxons;
}
catch(exception& e) {
m->errorOut(e, "Classify", "parseTax");
exit(1);
}
}
/**************************************************************************************************/
double Classify::getLogExpSum(vector<double> probabilities, int& maxIndex){
try {
// http://jblevins.org/notes/log-sum-exp
double maxProb = probabilities[0];
maxIndex = 0;
int numProbs = (int)probabilities.size();
for(int i=1;i<numProbs;i++){
if(probabilities[i] >= maxProb){
maxProb = probabilities[i];
maxIndex = i;
}
}
double probSum = 0.0000;
for(int i=0;i<numProbs;i++){
probSum += exp(probabilities[i] - maxProb);
}
probSum = log(probSum) + maxProb;
return probSum;
}
catch(exception& e) {
m->errorOut(e, "Classify", "getLogExpSum");
exit(1);
}
}
/**************************************************************************************************/
bool Classify::checkReleaseDate(vector<ifstream*>& files, string version) {
try {
Utils util;
bool good = true;
vector<string> versionVector;
util.splitAtChar(version, versionVector, '.');
for (int i = 0; i < files.size(); i++) {
string line = util.getline(*files[i]);
if (line[0] != '#') { good = false; break; } //shortcut files from before we added this check
else {
line = line.substr(1);
vector<string> linesVector; util.splitAtChar(line, linesVector, '.');
if (versionVector.size() != linesVector.size()) { good = false; break; }
else {
for (int j = 0; j < versionVector.size(); j++) {
int num1, num2;
convert(versionVector[j], num1);
convert(linesVector[j], num2);
//if mothurs version is newer than this files version, then we want to remake it
if (num1 > num2) { good = false; break; }
}
}
if (!good) { break; }
}
}
if (!good) { for (int i = 0; i < files.size(); i++) { files[i]->close(); } }
else { for (int i = 0; i < files.size(); i++) { files[i]->seekg(0); } }
return good;
}
catch(exception& e) {
m->errorOut(e, "Classify", "checkReleaseDate");
exit(1);
}
}
/**************************************************************************************************/
|