X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=pintail.cpp;h=b9f2434d19d4cc842bfac1dce4f6de5a0d1b742d;hp=5f4abd0d15d440cbee9ab4f2f15bbb1aa524c025;hb=df7e3ff9f68ef157b0328a2d353c3258c5d45d89;hpb=3abb236c602eb168ee112f080b563ebe2c705029 diff --git a/pintail.cpp b/pintail.cpp index 5f4abd0..b9f2434 100644 --- a/pintail.cpp +++ b/pintail.cpp @@ -8,673 +8,531 @@ */ #include "pintail.h" +#include "ignoregaps.h" #include "eachgapdist.h" +//******************************************************************************************************************** +//sorts lowest to highest +inline bool compareQuanMembers(quanMember left, quanMember right){ + return (left.score < right.score); +} //*************************************************************************************************************** -Pintail::Pintail(string filename, string temp) { fastafile = filename; templateFile = temp; } -//*************************************************************************************************************** - -Pintail::~Pintail() { +Pintail::Pintail(string filename, string temp, bool f, int p, string mask, string cons, string q, int win, int inc, string o) : Chimera() { try { - for (int i = 0; i < querySeqs.size(); i++) { delete querySeqs[i]; } - for (int i = 0; i < templateSeqs.size(); i++) { delete templateSeqs[i]; } + + fastafile = filename; + templateFileName = temp; templateSeqs = readSeqs(temp); + filter = f; + processors = p; + setMask(mask); + consfile = cons; + quanfile = q; + window = win; + increment = inc; + outputDir = o; + + distcalculator = new eachGapDist(); + decalc = new DeCalculator(); + + doPrep(); } catch(exception& e) { - errorOut(e, "Pintail", "~Pintail"); + m->errorOut(e, "Pintail", "Pintail"); exit(1); } -} + +} //*************************************************************************************************************** -void Pintail::print(ostream& out) { + +Pintail::~Pintail() { try { - for (int i = 0; i < querySeqs.size(); i++) { - - out << querySeqs[i]->getName() << '\t' << "div: " << deviation[i] << "\tstDev: " << DE[i] << endl; - out << "Observed\t"; - - for (int j = 0; j < obsDistance[i].size(); j++) { out << obsDistance[i][j] << '\t'; } - out << endl; - - out << "Expected\t"; - - for (int m = 0; m < expectedDistance[i].size(); m++) { out << expectedDistance[i][m] << '\t'; } - out << endl; - - } + delete distcalculator; + delete decalc; } catch(exception& e) { - errorOut(e, "Pintail", "print"); + m->errorOut(e, "Pintail", "~Pintail"); exit(1); } } - //*************************************************************************************************************** -void Pintail::getChimeras() { +int Pintail::doPrep() { try { - //read in query sequences and subject sequences - mothurOut("Reading sequences and template file... "); cout.flush(); - querySeqs = readSeqs(fastafile); - templateSeqs = readSeqs(templateFile); - mothurOut("Done."); mothurOutEndLine(); - - int numSeqs = querySeqs.size(); - - obsDistance.resize(numSeqs); - expectedDistance.resize(numSeqs); - seqCoef.resize(numSeqs); - DE.resize(numSeqs); - Qav.resize(numSeqs); - bestfit.resize(numSeqs); - trim.resize(numSeqs); - deviation.resize(numSeqs); - windowSizes.resize(numSeqs, window); - - //break up file if needed - int linesPerProcess = processors / numSeqs; - - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - //find breakup of sequences for all times we will Parallelize - if (processors == 1) { lines.push_back(new linePair(0, numSeqs)); } - else { - //fill line pairs - for (int i = 0; i < (processors-1); i++) { - lines.push_back(new linePair((i*linesPerProcess), ((i*linesPerProcess) + linesPerProcess))); + mergedFilterString = ""; + windowSizesTemplate.resize(templateSeqs.size(), window); + quantiles.resize(100); //one for every percent mismatch + quantilesMembers.resize(100); //one for every percent mismatch + + //if the user does not enter a mask then you want to keep all the spots in the alignment + if (seqMask.length() == 0) { decalc->setAlignmentLength(templateSeqs[0]->getAligned().length()); } + else { decalc->setAlignmentLength(seqMask.length()); } + + decalc->setMask(seqMask); + + #ifdef USE_MPI + //do nothing + #else + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + //find breakup of templatefile for quantiles + if (processors == 1) { templateLines.push_back(new linePair(0, templateSeqs.size())); } + else { + for (int i = 0; i < processors; i++) { + templateLines.push_back(new linePair()); + templateLines[i]->start = int (sqrt(float(i)/float(processors)) * templateSeqs.size()); + templateLines[i]->end = int (sqrt(float(i+1)/float(processors)) * templateSeqs.size()); } - //this is necessary to get remainder of processors / numSeqs so you don't miss any lines at the end - int i = processors - 1; - lines.push_back(new linePair((i*linesPerProcess), numSeqs)); } #else - lines.push_back(new linePair(0, numSeqs)); + templateLines.push_back(new linePair(0, templateSeqs.size())); #endif + #endif - distcalculator = new eachGapDist(); - - if (processors == 1) { - mothurOut("Finding closest sequence in template to each sequence... "); cout.flush(); - bestfit = findPairs(lines[0]->start, lines[0]->end); -for (int m = 0; m < templateSeqs.size(); m++) { - if (templateSeqs[m]->getName() == "198806") { bestfit[0] = *(templateSeqs[m]); } - if (templateSeqs[m]->getName() == "198806") { bestfit[1] = *(templateSeqs[m]); } - if (templateSeqs[m]->getName() == "108139") { bestfit[2] = *(templateSeqs[m]); } -} - -for (int j = 0; j < bestfit.size(); j++) {//cout << querySeqs[j]->getName() << '\t' << "length = " << querySeqs[j]->getAligned().length() << '\t' << bestfit[j].getName() << " length = " << bestfit[j].getAligned().length() << endl; - //chops off beginning and end of sequences so they both start and end with a base - trimSeqs(querySeqs[j], bestfit[j], j); -//cout << "NEW SEQ PAIR" << querySeqs[j]->getAligned() << endl << "IN THE MIDDLE" << endl << bestfit[j].getAligned() << endl; - -} - - mothurOut("Done."); mothurOutEndLine(); - - windows = findWindows(lines[0]->start, lines[0]->end); - } else { createProcessesSpots(); } - - //find P - if (consfile == "") { probabilityProfile = calcFreq(templateSeqs); } - else { probabilityProfile = readFreq(); } + m->mothurOut("Getting conservation... "); cout.flush(); + if (consfile == "") { + m->mothurOut("Calculating probability of conservation for your template sequences. This can take a while... I will output the frequency of the highest base in each position to a .freq file so that you can input them using the conservation parameter next time you run this command. Providing the .freq file will improve speed. "); cout.flush(); + probabilityProfile = decalc->calcFreq(templateSeqs, templateFileName); + if (m->control_pressed) { return 0; } + m->mothurOut("Done."); m->mothurOutEndLine(); + }else { probabilityProfile = readFreq(); m->mothurOut("Done."); } + m->mothurOutEndLine(); //make P into Q - for (int i = 0; i < probabilityProfile.size(); i++) { probabilityProfile[i] = 1 - probabilityProfile[i]; } + for (int i = 0; i < probabilityProfile.size(); i++) { probabilityProfile[i] = 1 - probabilityProfile[i]; } // - if (processors == 1) { + bool reRead = false; + //create filter if needed for later + if (filter) { - mothurOut("Calculating observed distance... "); cout.flush(); - obsDistance = calcObserved(lines[0]->start, lines[0]->end); - mothurOut("Done."); mothurOutEndLine(); - - mothurOut("Finding variability... "); cout.flush(); - Qav = findQav(lines[0]->start, lines[0]->end); -for (int i = 0; i < Qav.size(); i++) { -cout << querySeqs[i]->getName() << " = "; -for (int u = 0; u < Qav[i].size();u++) { cout << Qav[i][u] << '\t'; } -cout << endl << endl; -} - - - mothurOut("Done."); mothurOutEndLine(); - - mothurOut("Calculating alpha... "); cout.flush(); - seqCoef = getCoef(lines[0]->start, lines[0]->end); -for (int i = 0; i < seqCoef.size(); i++) { -cout << querySeqs[i]->getName() << " coef = " << seqCoef[i] << endl; -} + //read in all query seqs + vector tempQuerySeqs = readSeqs(fastafile); + + vector temp; + //merge query seqs and template seqs + temp = templateSeqs; + for (int i = 0; i < tempQuerySeqs.size(); i++) { temp.push_back(tempQuerySeqs[i]); } + + if (seqMask != "") { + reRead = true; + //mask templates + for (int i = 0; i < temp.size(); i++) { + if (m->control_pressed) { + for (int i = 0; i < tempQuerySeqs.size(); i++) { delete tempQuerySeqs[i]; } + return 0; + } + decalc->runMask(temp[i]); + } + } - mothurOut("Done."); mothurOutEndLine(); - - mothurOut("Calculating expected distance... "); cout.flush(); - expectedDistance = calcExpected(lines[0]->start, lines[0]->end); - mothurOut("Done."); mothurOutEndLine(); + mergedFilterString = createFilter(temp, 0.5); - mothurOut("Finding deviation... "); cout.flush(); - DE = calcDE(lines[0]->start, lines[0]->end); - deviation = calcDist(lines[0]->start, lines[0]->end); - mothurOut("Done."); mothurOutEndLine(); + if (m->control_pressed) { + for (int i = 0; i < tempQuerySeqs.size(); i++) { delete tempQuerySeqs[i]; } + return 0; + } + //reread template seqs + for (int i = 0; i < tempQuerySeqs.size(); i++) { delete tempQuerySeqs[i]; } + } + + + //quantiles are used to determine whether the de values found indicate a chimera + //if you have to calculate them, its time intensive because you are finding the de and deviation values for each + //combination of sequences in the template + if (quanfile != "") { + quantiles = readQuantiles(); + }else { + if ((!filter) && (seqMask != "")) { //if you didn't filter but you want to mask. if you filtered then you did mask first above. + reRead = true; + //mask templates + for (int i = 0; i < templateSeqs.size(); i++) { + if (m->control_pressed) { return 0; } + decalc->runMask(templateSeqs[i]); + } + } + if (filter) { + reRead = true; + for (int i = 0; i < templateSeqs.size(); i++) { + if (m->control_pressed) { return 0; } + runFilter(templateSeqs[i]); + } + } - } - else { createProcesses(); } + m->mothurOut("Calculating quantiles for your template. This can take a while... I will output the quantiles to a .quan file that you can input them using the quantiles parameter next time you run this command. Providing the .quan file will dramatically improve speed. "); cout.flush(); + if (processors == 1) { + quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, 0, templateSeqs.size()); + }else { createProcessesQuan(); } - delete distcalculator; - - //free memory - for (int i = 0; i < lines.size(); i++) { delete lines[i]; } + if (m->control_pressed) { return 0; } + string noOutliers, outliers; + + if ((!filter) && (seqMask == "")) { + noOutliers = m->getRootName(m->getSimpleName(templateFileName)) + "pintail.quan"; + }else if ((!filter) && (seqMask != "")) { + noOutliers =m->getRootName(m->getSimpleName(templateFileName)) + "pintail.masked.quan"; + }else if ((filter) && (seqMask != "")) { + noOutliers = m->getRootName(m->getSimpleName(templateFileName)) + "pintail.filtered." + m->getSimpleName(m->getRootName(fastafile)) + "masked.quan"; + }else if ((filter) && (seqMask == "")) { + noOutliers = m->getRootName(m->getSimpleName(templateFileName)) + "pintail.filtered." + m->getSimpleName(m->getRootName(fastafile)) + "quan"; + } - } - catch(exception& e) { - errorOut(e, "Pintail", "getChimeras"); - exit(1); - } -} - -//*************************************************************************************************************** - -vector Pintail::readSeqs(string file) { - try { - - ifstream in; - openInputFile(file, in); - vector container; + decalc->removeObviousOutliers(quantilesMembers, templateSeqs.size()); + + if (m->control_pressed) { return 0; } - //read in seqs and store in vector - while(!in.eof()){ - Sequence* current = new Sequence(in); + string outputString = "#" + m->getVersion() + "\n"; - if (current->getAligned() == "") { current->setAligned(current->getUnaligned()); } - //takes out stuff is needed - current->setUnaligned(current->getUnaligned()); + //adjust quantiles + for (int i = 0; i < quantilesMembers.size(); i++) { + vector temp; + + if (quantilesMembers[i].size() == 0) { + //in case this is not a distance found in your template files + for (int g = 0; g < 6; g++) { + temp.push_back(0.0); + } + }else{ + + sort(quantilesMembers[i].begin(), quantilesMembers[i].end()); + + //save 10% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.10)]); + //save 25% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.25)]); + //save 50% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.5)]); + //save 75% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.75)]); + //save 95% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.95)]); + //save 99% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.99)]); + + } + + //output quan value + outputString += toString(i+1) + "\t"; + for (int u = 0; u < temp.size(); u++) { outputString += toString(temp[u]) + "\t"; } + outputString += "\n"; + + quantiles[i] = temp; + + } + + printQuanFile(noOutliers, outputString); - container.push_back(current); + //free memory + quantilesMembers.clear(); - gobble(in); + m->mothurOut("Done."); m->mothurOutEndLine(); } - in.close(); - return container; - - } - catch(exception& e) { - errorOut(e, "Pintail", "readSeqs"); - exit(1); - } -} - -//*************************************************************************************************************** -//num is query's spot in querySeqs -void Pintail::trimSeqs(Sequence* query, Sequence& subject, int num) { - try { - - string q = query->getAligned(); - string s = subject.getAligned(); - - int front = 0; - for (int i = 0; i < q.length(); i++) { - if (isalpha(q[i]) && isalpha(s[i])) { front = i; break; } + if (reRead) { + for (int i = 0; i < templateSeqs.size(); i++) { delete templateSeqs[i]; } + templateSeqs.clear(); + templateSeqs = readSeqs(templateFileName); } + - q = q.substr(front, q.length()); - s = s.substr(front, s.length()); + //free memory + for (int i = 0; i < templateLines.size(); i++) { delete templateLines[i]; } + + return 0; - int back = 0; - for (int i = q.length(); i >= 0; i--) { - if (isalpha(q[i]) && isalpha(s[i])) { back = i; break; } - } - - q = q.substr(0, back); - s = s.substr(0, back); - - trim[num][front] = back; - - //save - query->setAligned(q); - query->setUnaligned(q); - subject.setAligned(s); - subject.setUnaligned(s); } catch(exception& e) { - errorOut(e, "Pintail", "trimSeqs"); + m->errorOut(e, "Pintail", "doPrep"); exit(1); } } - //*************************************************************************************************************** - -vector Pintail::readFreq() { +Sequence Pintail::print(ostream& out, ostream& outAcc) { try { - - ifstream in; - openInputFile(consfile, in); - vector prob; + int index = ceil(deviation); - //read in probabilities and store in vector - int pos; float num; + //is your DE value higher than the 95% + string chimera; + if (index != 0) { //if index is 0 then its an exact match to a template seq + if (quantiles[index][4] == 0.0) { + chimera = "Your template does not include sequences that provide quantile values at distance " + toString(index); + }else { + if (DE > quantiles[index][4]) { chimera = "Yes"; } + else { chimera = "No"; } + } + }else{ chimera = "No"; } - while(!in.eof()){ - - in >> pos >> num; - - prob.push_back(num); - - gobble(in); + out << querySeq->getName() << '\t' << "div: " << deviation << "\tstDev: " << DE << "\tchimera flag: " << chimera << endl; + if (chimera == "Yes") { + m->mothurOut(querySeq->getName() + "\tdiv: " + toString(deviation) + "\tstDev: " + toString(DE) + "\tchimera flag: " + chimera); m->mothurOutEndLine(); + outAcc << querySeq->getName() << endl; } + out << "Observed\t"; - in.close(); - return prob; + for (int j = 0; j < obsDistance.size(); j++) { out << obsDistance[j] << '\t'; } + out << endl; - } - catch(exception& e) { - errorOut(e, "Pintail", "readFreq"); - exit(1); - } -} - -//*************************************************************************************************************** -//calculate the distances from each query sequence to all sequences in the template to find the closest sequence -vector Pintail::findPairs(int start, int end) { - try { + out << "Expected\t"; - vector seqsMatches; seqsMatches.resize(end-start); + for (int m = 0; m < expectedDistance.size(); m++) { out << expectedDistance[m] << '\t'; } + out << endl; - for(int i = start; i < end; i++){ - - float smallest = 10000.0; - Sequence query = *(querySeqs[i]); - - for(int j = 0; j < templateSeqs.size(); j++){ - - Sequence temp = *(templateSeqs[j]); - - distcalculator->calcDist(query, temp); - float dist = distcalculator->getDist(); - - if (dist < smallest) { - seqsMatches[i] = *(templateSeqs[j]); - smallest = dist; - } - } - } + return *querySeq; - return seqsMatches; - } catch(exception& e) { - errorOut(e, "Pintail", "findPairs"); + m->errorOut(e, "Pintail", "print"); exit(1); } } - +#ifdef USE_MPI //*************************************************************************************************************** -//find the window breaks for each sequence -vector< vector > Pintail::findWindows(int start, int end) { +Sequence Pintail::print(MPI_File& out, MPI_File& outAcc) { try { - vector< vector > win; win.resize(end-start); - - //for each sequence - int count = 0; - for(int i = start; i < end; i++){ - - //if window is set to default - if (windowSizes[i] == 0) { if (querySeqs[i]->getAligned().length() > 1200) { windowSizes[i] = 300; } - else{ windowSizes[i] = (querySeqs[i]->getAligned().length() / 4); } } - else if (windowSizes[i] > (querySeqs[i]->getAligned().length() / 4)) { - mothurOut("You have selected to large a window size for sequence " + querySeqs[i]->getName() + ". I will choose an appropriate window size."); mothurOutEndLine(); - windowSizes[i] = (querySeqs[i]->getAligned().length() / 4); + string outputString = ""; + int index = ceil(deviation); + + //is your DE value higher than the 95% + string chimera; + if (index != 0) { //if index is 0 then its an exact match to a template seq + if (quantiles[index][4] == 0.0) { + chimera = "Your template does not include sequences that provide quantile values at distance " + toString(index); + }else { + if (DE > quantiles[index][4]) { chimera = "Yes"; } + else { chimera = "No"; } } - - //cout << "length = " << querySeqs[i]->getAligned().length() << " window = " << windowSizes[i] << " increment = " << increment << endl; - + }else{ chimera = "No"; } - string seq = querySeqs[i]->getAligned(); - int numBases = querySeqs[i]->getUnaligned().length(); - int spot = 0; - - //find location of first base - for (int j = 0; j < seq.length(); j++) { - if (isalpha(seq[j])) { spot = j; break; } - } - - //save start of seq - win[count].push_back(spot); - + outputString += querySeq->getName() + "\tdiv: " + toString(deviation) + "\tstDev: " + toString(DE) + "\tchimera flag: " + chimera + "\n"; + if (chimera == "Yes") { + cout << querySeq->getName() << "\tdiv: " << toString(deviation) << "\tstDev: " << toString(DE) << "\tchimera flag: " << chimera << endl; + string outAccString = querySeq->getName() + "\n"; - //move ahead increment bases at a time until all bases are in a window - int countBases = 0; - int totalBases = 0; //used to eliminate window of blanks at end of sequence - for (int m = spot; m < seq.length(); m++) { + MPI_Status statusAcc; + int length = outAccString.length(); + char* buf = new char[length]; + memcpy(buf, outAccString.c_str(), length); - //count number of bases you see - if (isalpha(seq[m])) { countBases++; totalBases++; } - - //if you have seen enough bases to make a new window - if (countBases >= increment) { - win[count].push_back(m); //save spot in alignment - countBases = 0; //reset bases you've seen in this window - } - - //no need to continue if all your bases are in a window - if (totalBases == numBases) { break; } - } - - count++; - } - - - - return win; - - } - catch(exception& e) { - errorOut(e, "Pintail", "findWindows"); - exit(1); - } -} + MPI_File_write_shared(outAcc, buf, length, MPI_CHAR, &statusAcc); + delete buf; -//*************************************************************************************************************** -vector< vector > Pintail::calcObserved(int start, int end) { - try { + return *querySeq; + } + outputString += "Observed\t"; - vector< vector > temp; - temp.resize(end-start); + for (int j = 0; j < obsDistance.size(); j++) { outputString += toString(obsDistance[j]) + "\t"; } + outputString += "\n"; - int count = 0; - for(int i = start; i < end; i++){ + outputString += "Expected\t"; - Sequence* query = querySeqs[i]; - Sequence subject = bestfit[i]; + for (int m = 0; m < expectedDistance.size(); m++) { outputString += toString(expectedDistance[m]) + "\t"; } + outputString += "\n"; - int startpoint = 0; - for (int m = 0; m < windows[i].size(); m++) { - - string seqFrag = query->getAligned().substr(windows[i][startpoint], windowSizes[i]); - string seqFragsub = subject.getAligned().substr(windows[i][startpoint], windowSizes[i]); - - int diff = 0; - for (int b = 0; b < seqFrag.length(); b++) { - - //if either the query or subject is not a gap - if ((isalpha(seqFrag[b])) || (isalpha(seqFragsub[b]))) { - //and they are different - penalize - if (seqFrag[b] != seqFragsub[b]) { diff++; } - } - } - - //percentage of mismatched bases - float dist; - dist = diff / (float) seqFrag.length() * 100; - - temp[count].push_back(dist); + MPI_Status status; + int length = outputString.length(); + char* buf2 = new char[length]; + memcpy(buf2, outputString.c_str(), length); - startpoint++; - } - - count++; - } + MPI_File_write_shared(out, buf2, length, MPI_CHAR, &status); + delete buf2; - return temp; + return *querySeq; } catch(exception& e) { - errorOut(e, "Pintail", "calcObserved"); + m->errorOut(e, "Pintail", "print"); exit(1); } } +#endif //*************************************************************************************************************** -vector Pintail::calcDist(int start, int end) { +int Pintail::getChimeras(Sequence* query) { try { + querySeq = query; + trimmed.clear(); + windowSizes = window; + + //find pairs has to be done before a mask + bestfit = findPairs(query); + + if (m->control_pressed) { return 0; } + + //if they mask + if (seqMask != "") { + decalc->runMask(query); + decalc->runMask(bestfit); + } + + if (filter) { //must be done after a mask + runFilter(query); + runFilter(bestfit); + } - vector temp; - for(int i = start; i < end; i++){ - - Sequence* query = querySeqs[i]; - Sequence subject = bestfit[i]; - - string seqFrag = query->getAligned(); - string seqFragsub = subject.getAligned(); - - int diff = 0; - for (int b = 0; b < seqFrag.length(); b++) { - - //if either the query or subject is not a gap - if ((isalpha(seqFrag[b])) || (isalpha(seqFragsub[b]))) { - //and they are different - penalize - if (seqFrag[b] != seqFragsub[b]) { diff++; } - } - } - - //percentage of mismatched bases - float dist; - dist = diff / (float) seqFrag.length() * 100; + //trim seq + decalc->trimSeqs(query, bestfit, trimmed); + + //find windows + it = trimmed.begin(); + windowsForeachQuery = decalc->findWindows(query, it->first, it->second, windowSizes, increment); + + //find observed distance + obsDistance = decalc->calcObserved(query, bestfit, windowsForeachQuery, windowSizes); + + if (m->control_pressed) { return 0; } - temp.push_back(dist); - } + Qav = decalc->findQav(windowsForeachQuery, windowSizes, probabilityProfile); - return temp; - } - catch(exception& e) { - errorOut(e, "Pintail", "calcDist"); - exit(1); - } -} + if (m->control_pressed) { return 0; } -//*************************************************************************************************************** -vector< vector > Pintail::calcExpected(int start, int end) { - try { + //find alpha + seqCoef = decalc->getCoef(obsDistance, Qav); - vector< vector > temp; temp.resize(end-start); + //calculating expected distance + expectedDistance = decalc->calcExpected(Qav, seqCoef); - //for each sequence - int count = 0; - for(int i = start; i < end; i++){ - - float coef = seqCoef[i]; - - //for each window - vector queryExpected; - for (int m = 0; m < windows[i].size(); m++) { - float expected = Qav[i][m] * coef; - queryExpected.push_back(expected); -//cout << "average variabilty over window = " << averageProbability[m] << " coef = " << coef << " ei = " << expected << '\t' << "window = " << m << endl; - } - - temp[count] = queryExpected; - - count++; - - } + if (m->control_pressed) { return 0; } - return temp; - - } - catch(exception& e) { - errorOut(e, "Pintail", "calcExpected"); - exit(1); - } -} -//*************************************************************************************************************** -vector Pintail::calcDE(int start, int end) { - try { + //finding de + DE = decalc->calcDE(obsDistance, expectedDistance); - vector temp; temp.resize(end-start); - - //for each sequence - int count = 0; - for(int i = start; i < end; i++){ - - vector obs = obsDistance[i]; - vector exp = expectedDistance[i]; - -// cout << "difference between obs and exp = " << abs(obs[m] - exp[m]) << endl; - //for each window - float sum = 0.0; //sum = sum from 1 to m of (oi-ei)^2 - for (int m = 0; m < windows[i].size(); m++) { sum += ((obs[m] - exp[m]) * (obs[m] - exp[m])); } - - float de = sqrt((sum / (windows[i].size() - 1))); - - temp[count] = de; - count++; - } + if (m->control_pressed) { return 0; } + + //find distance between query and closest match + it = trimmed.begin(); + deviation = decalc->calcDist(query, bestfit, it->first, it->second); - return temp; + delete bestfit; + + return 0; } catch(exception& e) { - errorOut(e, "Pintail", "calcDE"); + m->errorOut(e, "Pintail", "getChimeras"); exit(1); } } //*************************************************************************************************************** -vector Pintail::calcFreq(vector seqs) { +vector Pintail::readFreq() { try { + //read in probabilities and store in vector + int pos; float num; vector prob; - string freqfile = getRootName(templateFile) + "probability"; - ofstream outFreq; + set h = decalc->getPos(); //positions of bases in masking sequence - openOutputFile(freqfile, outFreq); + #ifdef USE_MPI - //at each position in the sequence - for (int i = 0; i < seqs[0]->getAligned().length(); i++) { + MPI_File inMPI; + MPI_Offset size; + MPI_Status status; + + //char* inFileName = new char[consfile.length()]; + //memcpy(inFileName, consfile.c_str(), consfile.length()); - vector freq; freq.resize(4,0); - int gaps = 0; - - //find the frequency of each nucleotide - for (int j = 0; j < seqs.size(); j++) { - - char value = seqs[j]->getAligned()[i]; + char inFileName[1024]; + strcpy(inFileName, consfile.c_str()); + + MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); + MPI_File_get_size(inMPI, &size); + //delete inFileName; + + char* buffer = new char[size]; + MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status); + + string tempBuf = buffer; + delete buffer; + + if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); } + istringstream iss (tempBuf,istringstream::in); + + //read version + string line = m->getline(iss); m->gobble(iss); + + while(!iss.eof()) { + iss >> pos >> num; + + if (h.count(pos) > 0) { + float Pi; + Pi = (num - 0.25) / 0.75; - if(toupper(value) == 'A') { freq[0]++; } - else if(toupper(value) == 'T' || toupper(value) == 'U') { freq[1]++; } - else if(toupper(value) == 'G') { freq[2]++; } - else if(toupper(value) == 'C') { freq[3]++; } - else { gaps++; } + //cannot have probability less than 0. + if (Pi < 0) { Pi = 0.0; } + + //do you want this spot + prob.push_back(Pi); } - //find base with highest frequency - int highest = 0; - for (int m = 0; m < freq.size(); m++) { if (freq[m] > highest) { highest = freq[m]; } } - - float highFreq; - //if ( (seqs.size() - gaps) == 0 ) { highFreq = 1.0; } - //else { highFreq = highest / (float) (seqs.size() - gaps); } - highFreq = highest / (float) seqs.size(); -cout << i << '\t' << highFreq << endl; - - float Pi; - Pi = (highFreq - 0.25) / 0.75; - - //saves this for later - outFreq << i << '\t' << Pi << endl; - - prob.push_back(Pi); + m->gobble(iss); } + + MPI_File_close(&inMPI); - outFreq.close(); - - return prob; - - } - catch(exception& e) { - errorOut(e, "Pintail", "calcFreq"); - exit(1); - } -} -//*************************************************************************************************************** -vector< vector > Pintail::findQav(int start, int end) { - try { - vector< vector > averages; - map::iterator it; - - for(int i = start; i < end; i++){ + #else + + ifstream in; + m->openInputFile(consfile, in); - //for each window find average - vector temp; - for (int m = 0; m < windows[i].size(); m++) { - - float average = 0.0; - - it = trim[i].begin(); //trim[i] is a map of where this sequence was trimmed - - //while you are in the window for this sequence - for (int j = windows[i][m]+it->first; j < (windows[i][m]+windowSizes[i]); j++) { average += probabilityProfile[j]; } + //read version + string line = m->getline(in); m->gobble(in); - average = average / windowSizes[i]; - //cout << average << endl; - //save this windows average - temp.push_back(average); + while(!in.eof()){ + + in >> pos >> num; + + if (h.count(pos) > 0) { + float Pi; + Pi = (num - 0.25) / 0.75; + + //cannot have probability less than 0. + if (Pi < 0) { Pi = 0.0; } + + //do you want this spot + prob.push_back(Pi); } - //save this qav - averages.push_back(temp); + m->gobble(in); } + in.close(); + + #endif + + return prob; - return averages; } catch(exception& e) { - errorOut(e, "Pintail", "findQav"); + m->errorOut(e, "Pintail", "readFreq"); exit(1); } } + //*************************************************************************************************************** -vector Pintail::getCoef(int start, int end) { +//calculate the distances from each query sequence to all sequences in the template to find the closest sequence +Sequence* Pintail::findPairs(Sequence* q) { try { - vector coefs; - coefs.resize(end-start); - //find a coef for each sequence - int count = 0; - for(int i = start; i < end; i++){ + Sequence* seqsMatches; - //find average prob for this seqs windows - float probAverage = 0.0; - for (int j = 0; j < Qav[i].size(); j++) { probAverage += Qav[i][j]; } - probAverage = probAverage / (float) Qav[i].size(); - cout << "(sum of ai) / m = " << probAverage << endl; - - vector temp = obsDistance[i]; - - //find observed average - float obsAverage = 0.0; - for (int j = 0; j < temp.size(); j++) { obsAverage += temp[j]; } - obsAverage = obsAverage / (float) temp.size(); -cout << "(sum of oi) / m = " << obsAverage << endl; - float coef = obsAverage / probAverage; - - //save this sequences coefficient - coefs[count] = coef; - - count++; - } - - - return coefs; + seqsMatches = decalc->findClosest(q, templateSeqs); + return seqsMatches; + } catch(exception& e) { - errorOut(e, "Pintail", "getCoef"); + m->errorOut(e, "Pintail", "findPairs"); exit(1); } } - - -/**************************************************************************************************/ - -void Pintail::createProcessesSpots() { +//************************************************************************************************** +void Pintail::createProcessesQuan() { try { -#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - int process = 0; +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + int process = 1; vector processIDS; - vector< vector > win; win.resize(querySeqs.size()); - vector< map > t; t.resize(querySeqs.size()); - + //loop through and create all the processes you want while (process != processors) { int pid = fork(); @@ -684,147 +542,234 @@ void Pintail::createProcessesSpots() { process++; }else if (pid == 0){ - vector tempbest; - tempbest = findPairs(lines[process]->start, lines[process]->end); - int count = 0; - for (int i = lines[process]->start; i < lines[process]->end; i++) { - bestfit[i] = tempbest[count]; - - //chops off beginning and end of sequences so they both start and end with a base - trimSeqs(querySeqs[i], bestfit[i], i); - t[i] = trim[i]; - - count++; - } - - + quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, templateLines[process]->start, templateLines[process]->end); - vector< vector > temp = findWindows(lines[process]->start, lines[process]->end); - - //move into best - count = 0; - for (int i = lines[process]->start; i < lines[process]->end; i++) { - win[i] = temp[count]; - count++; + //write out data to file so parent can read it + ofstream out; + string s = toString(getpid()) + ".temp"; + m->openOutputFile(s, out); + + //output observed distances + for (int i = 0; i < quantilesMembers.size(); i++) { + out << quantilesMembers[i].size() << '\t'; + for (int j = 0; j < quantilesMembers[i].size(); j++) { + out << quantilesMembers[i][j] << '\t'; + } + out << endl; } + out.close(); + exit(0); - }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); } + }else { + m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); + for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); } + exit(0); + } } + //parent does its part + quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, templateLines[0]->start, templateLines[0]->end); + //force parent to wait until all the processes are done - for (int i=0;istart, lines[0]->end); + //get data created by processes + for (int i=0;i<(processors-1);i++) { + ifstream in; + string s = toString(processIDS[i]) + ".temp"; + m->openInputFile(s, in); + + vector< vector > quan; + quan.resize(100); + + //get quantiles + for (int h = 0; h < quan.size(); h++) { + int num; + in >> num; + + m->gobble(in); + + vector q; float w; + for (int j = 0; j < num; j++) { + in >> w; + q.push_back(w); + } + + quan[h] = q; + m->gobble(in); + } + + + //save quan in quantiles + for (int j = 0; j < quan.size(); j++) { + //put all values of q[i] into quan[i] + for (int l = 0; l < quan[j].size(); l++) { quantilesMembers[j].push_back(quan[j][l]); } + //quantilesMembers[j].insert(quantilesMembers[j].begin(), quan[j].begin(), quan[j].end()); + } + + in.close(); + m->mothurRemove(s); + } + +#else + quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, 0, templateSeqs.size()); #endif } catch(exception& e) { - errorOut(e, "Pintail", "createProcessesSpots"); + m->errorOut(e, "Pintail", "createProcessesQuan"); exit(1); } } - - -/**************************************************************************************************/ - -void Pintail::createProcesses() { +//*************************************************************************************************************** +vector< vector > Pintail::readQuantiles() { try { -#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - int process = 0; - vector processIDS; + int num; + float ten, twentyfive, fifty, seventyfive, ninetyfive, ninetynine; - vector< vector > exp; exp.resize(querySeqs.size()); - vector de; de.resize(querySeqs.size()); - vector< vector > obs; obs.resize(querySeqs.size()); + vector< vector > quan; + vector temp; temp.resize(6, 0); + //to fill 0 + quan.push_back(temp); + + #ifdef USE_MPI - //loop through and create all the processes you want - while (process != processors) { - int pid = fork(); - - if (pid > 0) { - processIDS.push_back(pid); - process++; - }else if (pid == 0){ - - vector< vector > temp; - vector tempde; - int count = 0; - - - temp = calcObserved(lines[process]->start, lines[process]->end); - count = 0; - for (int i = lines[process]->start; i < lines[process]->end; i++) { - obs[i] = temp[count]; - count++; - } + MPI_File inMPI; + MPI_Offset size; + MPI_Status status; + + //char* inFileName = new char[quanfile.length()]; + //memcpy(inFileName, quanfile.c_str(), quanfile.length()); + + char inFileName[1024]; + strcpy(inFileName, quanfile.c_str()); - temp = findQav(lines[process]->start, lines[process]->end); - count = 0; - for (int i = lines[process]->start; i < lines[process]->end; i++) { - Qav[i] = temp[count]; - count++; - } - - tempde = getCoef(lines[process]->start, lines[process]->end); - count = 0; - for (int i = lines[process]->start; i < lines[process]->end; i++) { - seqCoef[i] = tempde[count]; - count++; - } - - temp = calcExpected(lines[process]->start, lines[process]->end); - count = 0; - for (int i = lines[process]->start; i < lines[process]->end; i++) { - exp[i] = temp[count]; - count++; - } + MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); + MPI_File_get_size(inMPI, &size); + //delete inFileName; - - tempde = calcDE(lines[process]->start, lines[process]->end); - count = 0; - for (int i = lines[process]->start; i < lines[process]->end; i++) { - de[i] = tempde[count]; - count++; - } - exit(0); - }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); } + char* buffer = new char[size]; + MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status); + + string tempBuf = buffer; + if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); } + istringstream iss (tempBuf,istringstream::in); + delete buffer; + + //read version + string line = m->getline(iss); m->gobble(iss); + + while(!iss.eof()) { + iss >> num >> ten >> twentyfive >> fifty >> seventyfive >> ninetyfive >> ninetynine; + + temp.clear(); + + temp.push_back(ten); + temp.push_back(twentyfive); + temp.push_back(fifty); + temp.push_back(seventyfive); + temp.push_back(ninetyfive); + temp.push_back(ninetynine); + + quan.push_back(temp); + + m->gobble(iss); } + + MPI_File_close(&inMPI); - //force parent to wait until all the processes are done - for (int i=0;iopenInputFile(quanfile, in); + + //read version + string line = m->getline(in); m->gobble(in); + + while(!in.eof()){ + + in >> num >> ten >> twentyfive >> fifty >> seventyfive >> ninetyfive >> ninetynine; + + temp.clear(); + + temp.push_back(ten); + temp.push_back(twentyfive); + temp.push_back(fifty); + temp.push_back(seventyfive); + temp.push_back(ninetyfive); + temp.push_back(ninetynine); + + quan.push_back(temp); + + m->gobble(in); } + in.close(); + #endif + + return quan; - obsDistance = obs; - expectedDistance = exp; - DE = de; + } + catch(exception& e) { + m->errorOut(e, "Pintail", "readQuantiles"); + exit(1); + } +} +//***************************************************************************************************************/ + +void Pintail::printQuanFile(string file, string outputString) { + try { + + #ifdef USE_MPI -#else - bestfit = findPairs(lines[0]->start, lines[0]->end); - obsDistance = calcObserved(lines[0]->start, lines[0]->end); - Qav = findQav(lines[0]->start, lines[0]->end); - seqCoef = getCoef(lines[0]->start, lines[0]->end); - expectedDistance = calcExpected(lines[0]->start, lines[0]->end); - DE = calcDE(lines[0]->start, lines[0]->end); + MPI_File outQuan; + MPI_Status status; + + int pid; + MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are -#endif + int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; + + //char* FileName = new char[file.length()]; + //memcpy(FileName, file.c_str(), file.length()); + + char FileName[1024]; + strcpy(FileName, file.c_str()); + + if (pid == 0) { + MPI_File_open(MPI_COMM_SELF, FileName, outMode, MPI_INFO_NULL, &outQuan); //comm, filename, mode, info, filepointer + + int length = outputString.length(); + char* buf = new char[length]; + memcpy(buf, outputString.c_str(), length); + + MPI_File_write(outQuan, buf, length, MPI_CHAR, &status); + delete buf; + + MPI_File_close(&outQuan); + } + + //delete FileName; + #else + ofstream outQuan; + m->openOutputFile(file, outQuan); + + outQuan << outputString; + + outQuan.close(); + #endif } catch(exception& e) { - errorOut(e, "Pintail", "createProcesses"); + m->errorOut(e, "Pintail", "printQuanFile"); exit(1); } } -//*************************************************************************************************************** +//***************************************************************************************************************/ +