X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=pintail.cpp;h=7ae3ff6dbb96224197dff97db4b435880738d87b;hb=98ea55ba2d46a429f031086b4b3272780d0ec894;hp=5f4abd0d15d440cbee9ab4f2f15bbb1aa524c025;hpb=3abb236c602eb168ee112f080b563ebe2c705029;p=mothur.git diff --git a/pintail.cpp b/pintail.cpp index 5f4abd0..7ae3ff6 100644 --- a/pintail.cpp +++ b/pintail.cpp @@ -8,8 +8,14 @@ */ #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; } @@ -19,6 +25,7 @@ Pintail::~Pintail() { try { for (int i = 0; i < querySeqs.size(); i++) { delete querySeqs[i]; } for (int i = 0; i < templateSeqs.size(); i++) { delete templateSeqs[i]; } + for (int i = 0; i < bestfit.size(); i++) { delete bestfit[i]; } } catch(exception& e) { errorOut(e, "Pintail", "~Pintail"); @@ -29,9 +36,25 @@ Pintail::~Pintail() { void Pintail::print(ostream& out) { try { + mothurOutEndLine(); + for (int i = 0; i < querySeqs.size(); i++) { - out << querySeqs[i]->getName() << '\t' << "div: " << deviation[i] << "\tstDev: " << DE[i] << endl; + int index = ceil(deviation[i]); + + //is your DE value higher than the 95% + string chimera; + if (quantiles[index][4] == 0.0) { + chimera = "Your template does not include sequences that provide quantile values at distance " + toString(index); + }else { + if (DE[i] > quantiles[index][4]) { chimera = "Yes"; } + else { chimera = "No"; } + } + + out << querySeqs[i]->getName() << '\t' << "div: " << deviation[i] << "\tstDev: " << DE[i] << "\tchimera flag: " << chimera << endl; + if (chimera == "Yes") { + mothurOut(querySeqs[i]->getName() + "\tdiv: " + toString(deviation[i]) + "\tstDev: " + toString(DE[i]) + "\tchimera flag: " + chimera); mothurOutEndLine(); + } out << "Observed\t"; for (int j = 0; j < obsDistance[i].size(); j++) { out << obsDistance[i][j] << '\t'; } @@ -68,12 +91,17 @@ void Pintail::getChimeras() { DE.resize(numSeqs); Qav.resize(numSeqs); bestfit.resize(numSeqs); - trim.resize(numSeqs); deviation.resize(numSeqs); + trimmed.resize(numSeqs); windowSizes.resize(numSeqs, window); + windowSizesTemplate.resize(templateSeqs.size(), window); + windowsForeachQuery.resize(numSeqs); + h.resize(numSeqs); + quantiles.resize(100); //one for every percent mismatch + quantilesMembers.resize(100); //one for every percent mismatch //break up file if needed - int linesPerProcess = processors / numSeqs; + int linesPerProcess = numSeqs / processors ; #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) //find breakup of sequences for all times we will Parallelize @@ -87,158 +115,276 @@ void Pintail::getChimeras() { int i = processors - 1; lines.push_back(new linePair((i*linesPerProcess), numSeqs)); } + + //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()); + } + } #else lines.push_back(new linePair(0, numSeqs)); + templateLines.push_back(new linePair(0, templateSeqs.size())); #endif distcalculator = new eachGapDist(); + decalc = new DeCalculator(); + + //if the user does enter a mask then you want to keep all the spots in the alignment + if (seqMask.length() == 0) { decalc->setAlignmentLength(querySeqs[0]->getAligned().length()); } + else { decalc->setAlignmentLength(seqMask.length()); } + decalc->setMask(seqMask); + + //find pairs 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(); + }else { createProcessesPairs(); } + +//string o = "closestmatch.eachgap.fasta"; +//ofstream out7; +//openOutputFile(o, out7); +//for (int i = 0; i < bestfit.size(); i++) { + //out7 << ">" << querySeqs[i]->getName() << "-"<< bestfit[i]->getName() << endl; + //out7 << bestfit[i]->getAligned() << endl; +//} +//out7.close(); + //find P + mothurOut("Getting conservation... "); cout.flush(); + if (consfile == "") { + 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, templateFile); mothurOut("Done."); mothurOutEndLine(); + }else { probabilityProfile = readFreq(); } - windows = findWindows(lines[0]->start, lines[0]->end); - } else { createProcessesSpots(); } + //make P into Q + for (int i = 0; i < probabilityProfile.size(); i++) { probabilityProfile[i] = 1 - probabilityProfile[i]; } //cout << i << '\t' << probabilityProfile[i] << endl; + mothurOut("Done."); mothurOutEndLine(); + + //mask sequences if the user wants to + if (seqMask != "") { + //mask querys + for (int i = 0; i < querySeqs.size(); i++) { + decalc->runMask(querySeqs[i]); + } + + //mask templates + for (int i = 0; i < templateSeqs.size(); i++) { + decalc->runMask(templateSeqs[i]); + } + + for (int i = 0; i < bestfit.size(); i++) { + decalc->runMask(bestfit[i]); + } - //find P - if (consfile == "") { probabilityProfile = calcFreq(templateSeqs); } - else { probabilityProfile = readFreq(); } + } - //make P into Q - for (int i = 0; i < probabilityProfile.size(); i++) { probabilityProfile[i] = 1 - probabilityProfile[i]; } + if (filter) { + vector temp = templateSeqs; + for (int i = 0; i < querySeqs.size(); i++) { temp.push_back(querySeqs[i]); } + + createFilter(temp); + + runFilter(querySeqs); + runFilter(templateSeqs); + runFilter(bestfit); + } + + + if (processors == 1) { + + for (int j = 0; j < bestfit.size(); j++) { + decalc->trimSeqs(querySeqs[j], bestfit[j], trimmed[j]); + } + + mothurOut("Finding window breaks... "); cout.flush(); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + it = trimmed[i].begin(); + vector win = decalc->findWindows(querySeqs[i], it->first, it->second, windowSizes[i], increment); + windowsForeachQuery[i] = win; + } + mothurOut("Done."); mothurOutEndLine(); + + }else { createProcessesSpots(); } if (processors == 1) { mothurOut("Calculating observed distance... "); cout.flush(); - obsDistance = calcObserved(lines[0]->start, lines[0]->end); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + vector obsi = decalc->calcObserved(querySeqs[i], bestfit[i], windowsForeachQuery[i], windowSizes[i]); + + obsDistance[i] = obsi; + } 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; -} - + for (int i = lines[0]->start; i < lines[0]->end; i++) { + vector q = decalc->findQav(windowsForeachQuery[i], windowSizes[i], probabilityProfile); + Qav[i] = q; + } 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; -} - + for (int i = lines[0]->start; i < lines[0]->end; i++) { + float alpha = decalc->getCoef(obsDistance[i], Qav[i]); + seqCoef[i] = alpha; + } mothurOut("Done."); mothurOutEndLine(); - + + mothurOut("Calculating expected distance... "); cout.flush(); - expectedDistance = calcExpected(lines[0]->start, lines[0]->end); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + vector exp = decalc->calcExpected(Qav[i], seqCoef[i]); + expectedDistance[i] = exp; + } mothurOut("Done."); mothurOutEndLine(); - mothurOut("Finding deviation... "); cout.flush(); - DE = calcDE(lines[0]->start, lines[0]->end); - deviation = calcDist(lines[0]->start, lines[0]->end); - mothurOut("Done."); mothurOutEndLine(); + mothurOut("Finding deviation... "); cout.flush(); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + float de = decalc->calcDE(obsDistance[i], expectedDistance[i]); + DE[i] = de; + it = trimmed[i].begin(); + float dist = decalc->calcDist(querySeqs[i], bestfit[i], it->first, it->second); + deviation[i] = dist; + } + mothurOut("Done."); mothurOutEndLine(); } else { createProcesses(); } - delete distcalculator; - - //free memory - for (int i = 0; i < lines.size(); i++) { delete lines[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 { - - } - catch(exception& e) { - errorOut(e, "Pintail", "getChimeras"); - exit(1); - } -} - -//*************************************************************************************************************** - -vector Pintail::readSeqs(string file) { - try { - - ifstream in; - openInputFile(file, in); - vector container; + 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(); } - //read in seqs and store in vector - while(!in.eof()){ - Sequence* current = new Sequence(in); - if (current->getAligned() == "") { current->setAligned(current->getUnaligned()); } - //takes out stuff is needed - current->setUnaligned(current->getUnaligned()); + ofstream out4, out5; + string noOutliers, outliers; - container.push_back(current); + if ((!filter) && (seqMask == "")) { + noOutliers = getRootName(templateFile) + "pintail.quan"; + }else if ((filter) && (seqMask == "")) { + noOutliers = getRootName(templateFile) + "pintail.filtered.quan"; + }else if ((!filter) && (seqMask != "")) { + noOutliers = getRootName(templateFile) + "pintail.masked.quan"; + }else if ((filter) && (seqMask != "")) { + noOutliers = getRootName(templateFile) + "pintail.filtered.masked.quan"; + } + + //outliers = getRootName(templateFile) + "pintail.quanYESOUTLIERS"; - gobble(in); - } - - in.close(); - return container; - - } - catch(exception& e) { - errorOut(e, "Pintail", "readSeqs"); - exit(1); - } -} + /*openOutputFile(outliers, out4); + + //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(), compareQuanMembers); + + //save 10% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.10)].score); + //save 25% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.25)].score); + //save 50% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.5)].score); + //save 75% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.75)].score); + //save 95% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.95)].score); + //save 99% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.99)].score); + + } + + //output quan value + out4 << i+1 << '\t'; + for (int u = 0; u < temp.size(); u++) { out4 << temp[u] << '\t'; } + out4 << endl; + + quantiles[i] = temp; + + } + + out4.close();*/ + + decalc->removeObviousOutliers(quantilesMembers, templateSeqs.size()); + + openOutputFile(noOutliers, out5); + + //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(), compareQuanMembers); + + //save 10% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.10)].score); + //save 25% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.25)].score); + //save 50% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.5)].score); + //save 75% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.75)].score); + //save 95% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.95)].score); + //save 99% + temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.99)].score); + + } + + //output quan value + out5 << i+1 << '\t'; + for (int u = 0; u < temp.size(); u++) { out5 << temp[u] << '\t'; } + out5 << endl; + + quantiles[i] = temp; + + } -//*************************************************************************************************************** -//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; } - } - - q = q.substr(front, q.length()); - s = s.substr(front, s.length()); - - int back = 0; - for (int i = q.length(); i >= 0; i--) { - if (isalpha(q[i]) && isalpha(s[i])) { back = i; break; } + mothurOut("Done."); mothurOutEndLine(); + } - - 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); + + //free memory + for (int i = 0; i < lines.size(); i++) { delete lines[i]; } + for (int i = 0; i < templateLines.size(); i++) { delete templateLines[i]; } + + delete distcalculator; + delete decalc; } catch(exception& e) { - errorOut(e, "Pintail", "trimSeqs"); + errorOut(e, "Pintail", "getChimeras"); exit(1); } } @@ -252,15 +398,25 @@ vector Pintail::readFreq() { openInputFile(consfile, in); vector prob; + set h = decalc->getPos(); //positions of bases in masking sequence //read in probabilities and store in vector - int pos; float num; + int pos; float num; while(!in.eof()){ in >> pos >> num; - prob.push_back(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); + } gobble(in); } @@ -277,16 +433,17 @@ vector Pintail::readFreq() { //*************************************************************************************************************** //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) { +vector Pintail::findPairs(int start, int end) { try { - vector seqsMatches; seqsMatches.resize(end-start); + vector seqsMatches; for(int i = start; i < end; i++){ float smallest = 10000.0; Sequence query = *(querySeqs[i]); - + Sequence* match; + for(int j = 0; j < templateSeqs.size(); j++){ Sequence temp = *(templateSeqs[j]); @@ -295,10 +452,15 @@ vector Pintail::findPairs(int start, int end) { float dist = distcalculator->getDist(); if (dist < smallest) { - seqsMatches[i] = *(templateSeqs[j]); + match = templateSeqs[j]; smallest = dist; } } + + //make copy so trimSeqs doesn't overtrim + Sequence* copy = new Sequence(match->getName(), match->getAligned()); + + seqsMatches.push_back(copy); } return seqsMatches; @@ -310,370 +472,238 @@ vector Pintail::findPairs(int start, int end) { } } -//*************************************************************************************************************** -//find the window breaks for each sequence -vector< vector > Pintail::findWindows(int start, int end) { +/**************************************************************************************************/ + +void Pintail::createProcessesSpots() { try { +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + int process = 0; + vector processIDS; - vector< vector > win; win.resize(end-start); - - //for each sequence - int count = 0; - for(int i = start; i < end; i++){ + //loop through and create all the processes you want + while (process != processors) { + int pid = fork(); - //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); - } - - //cout << "length = " << querySeqs[i]->getAligned().length() << " window = " << windowSizes[i] << " increment = " << increment << endl; + if (pid > 0) { + processIDS.push_back(pid); + process++; + }else if (pid == 0){ + + for (int j = lines[process]->start; j < lines[process]->end; j++) { + //chops off beginning and end of sequences so they both start and end with a base + map trim; - 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); - - - //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++) { + decalc->trimSeqs(querySeqs[j], bestfit[j], trim); + trimmed[j] = trim; + + } + + mothurOut("Finding window breaks for sequences " + toString(lines[process]->start) + " to " + toString(lines[process]->end)); mothurOutEndLine(); + for (int i = lines[process]->start; i < lines[process]->end; i++) { + it = trimmed[i].begin(); + windowsForeachQuery[i] = decalc->findWindows(querySeqs[i], it->first, it->second, windowSizes[i], increment); + } + mothurOut("Done finding window breaks for sequences " + toString(lines[process]->start) + " to " + toString(lines[process]->end)); mothurOutEndLine(); - //count number of bases you see - if (isalpha(seq[m])) { countBases++; totalBases++; } + //write out data to file so parent can read it + ofstream out; + string s = toString(getpid()) + ".temp"; + openOutputFile(s, out); - //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 + //output windowsForeachQuery + for (int i = lines[process]->start; i < lines[process]->end; i++) { + out << windowsForeachQuery[i].size() << '\t'; + for (int j = 0; j < windowsForeachQuery[i].size(); j++) { + out << windowsForeachQuery[i][j] << '\t'; + } + out << endl; } - - //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); - } -} - -//*************************************************************************************************************** -vector< vector > Pintail::calcObserved(int start, int end) { - try { - - vector< vector > temp; - temp.resize(end-start); - - int count = 0; - for(int i = start; i < end; i++){ - - Sequence* query = querySeqs[i]; - Sequence subject = bestfit[i]; - - 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); - - startpoint++; - } - - count++; - } - - return temp; - } - catch(exception& e) { - errorOut(e, "Pintail", "calcObserved"); - exit(1); - } -} -//*************************************************************************************************************** -vector Pintail::calcDist(int start, int end) { - try { - - vector temp; + //output windowSizes + for (int i = lines[process]->start; i < lines[process]->end; i++) { + out << windowSizes[i] << '\t'; + } + out << endl; - 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++; } + //output trimmed values + for (int i = lines[process]->start; i < lines[process]->end; i++) { + it = trimmed[i].begin(); + out << it->first << '\t' << it->second << endl; } - } - - //percentage of mismatched bases - float dist; - dist = diff / (float) seqFrag.length() * 100; + out.close(); - temp.push_back(dist); + exit(0); + }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); } } - return temp; - } - catch(exception& e) { - errorOut(e, "Pintail", "calcDist"); - exit(1); - } -} - -//*************************************************************************************************************** -vector< vector > Pintail::calcExpected(int start, int end) { - try { - - vector< vector > temp; temp.resize(end-start); + //force parent to wait until all the processes are done + for (int i=0;i 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; - } + //get data created by processes + for (int i=0;iend - lines[i]->start; + + int count = lines[i]->start; + for (int m = 0; m < size; m++) { + int num; + in >> num; - count++; + vector win; int w; + for (int j = 0; j < num; j++) { + in >> w; + win.push_back(w); + } - } + windowsForeachQuery[count] = win; + count++; + gobble(in); + } - return temp; + gobble(in); + count = lines[i]->start; + for (int m = 0; m < size; m++) { + int num; + in >> num; - } - catch(exception& e) { - errorOut(e, "Pintail", "calcExpected"); - exit(1); - } -} -//*************************************************************************************************************** -vector Pintail::calcDE(int start, int end) { - try { - - 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]; + windowSizes[count] = num; + count++; + } -// 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])); } + gobble(in); - float de = sqrt((sum / (windows[i].size() - 1))); + count = lines[i]->start; + for (int m = 0; m < size; m++) { + int front, back; + in >> front >> back; - temp[count] = de; - count++; - } - - return temp; - } - catch(exception& e) { - errorOut(e, "Pintail", "calcDE"); - exit(1); - } -} - -//*************************************************************************************************************** - -vector Pintail::calcFreq(vector seqs) { - try { - - vector prob; - string freqfile = getRootName(templateFile) + "probability"; - ofstream outFreq; - - openOutputFile(freqfile, outFreq); - - //at each position in the sequence - for (int i = 0; i < seqs[0]->getAligned().length(); i++) { - - vector freq; freq.resize(4,0); - int gaps = 0; - - //find the frequency of each nucleotide - for (int j = 0; j < seqs.size(); j++) { + map t; - char value = seqs[j]->getAligned()[i]; - - 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++; } + t[front] = back; + + trimmed[count] = t; + count++; + + gobble(in); } + - //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; + in.close(); + remove(s.c_str()); + } - //saves this for later - outFreq << i << '\t' << Pi << endl; - - prob.push_back(Pi); + +#else + for (int j = 0; j < bestfit.size(); j++) { + //chops off beginning and end of sequences so they both start and end with a base + decalc->trimSeqs(querySeqs[j], bestfit[j], trimmed[j]); } - - outFreq.close(); - - return prob; - + + for (int i = lines[0]->start; i < lines[0]->end; i++) { + it = trimmed[i].begin(); + vector win = decalc->findWindows(querySeqs[i], it->first, it->second, windowSizes[i], increment); + windowsForeachQuery[i] = win; + } + +#endif } catch(exception& e) { - errorOut(e, "Pintail", "calcFreq"); + errorOut(e, "Pintail", "createProcessesSpots"); exit(1); } } -//*************************************************************************************************************** -vector< vector > Pintail::findQav(int start, int end) { +/**************************************************************************************************/ + +void Pintail::createProcessesPairs() { try { - vector< vector > averages; - map::iterator it; - - for(int i = start; i < end; i++){ +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + int process = 0; + vector processIDS; - //for each window find average - vector temp; - for (int m = 0; m < windows[i].size(); m++) { + //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){ - float average = 0.0; + mothurOut("Finding pairs for sequences " + toString(lines[process]->start) + " to " + toString(lines[process]->end)); mothurOutEndLine(); + bestfit = findPairs(lines[process]->start, lines[process]->end); + mothurOut("Done finding pairs for sequences " + toString(lines[process]->start) + " to " + toString(lines[process]->end)); mothurOutEndLine(); - it = trim[i].begin(); //trim[i] is a map of where this sequence was trimmed + //write out data to file so parent can read it + ofstream out; + string s = toString(getpid()) + ".temp"; + openOutputFile(s, out); - //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]; } + //output range and size + out << bestfit.size() << endl; - average = average / windowSizes[i]; - //cout << average << endl; - //save this windows average - temp.push_back(average); - } - - //save this qav - averages.push_back(temp); + //output pairs + for (int i = 0; i < bestfit.size(); i++) { + out << ">" << bestfit[i]->getName() << endl << bestfit[i]->getAligned() << endl; + } + out.close(); + + exit(0); + }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); } } - return averages; - } - catch(exception& e) { - errorOut(e, "Pintail", "findQav"); - exit(1); - } -} -//*************************************************************************************************************** -vector Pintail::getCoef(int start, int end) { - try { - vector coefs; - coefs.resize(end-start); - - //find a coef for each sequence - int count = 0; - for(int i = start; i < end; i++){ + //force parent to wait until all the processes are done + for (int i=0;i 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) { @@ -684,30 +714,74 @@ void Pintail::createProcessesSpots() { process++; }else if (pid == 0){ - vector tempbest; - tempbest = findPairs(lines[process]->start, lines[process]->end); - int count = 0; + mothurOut("Calculating observed, expected and de values for sequences " + toString(lines[process]->start) + " to " + toString(lines[process]->end)); mothurOutEndLine(); 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]; + vector obsi = decalc->calcObserved(querySeqs[i], bestfit[i], windowsForeachQuery[i], windowSizes[i]); + obsDistance[i] = obsi; + + //calc Qav + vector q = decalc->findQav(windowsForeachQuery[i], windowSizes[i], probabilityProfile); + + //get alpha + float alpha = decalc->getCoef(obsDistance[i], q); + + //find expected + vector exp = decalc->calcExpected(q, alpha); + expectedDistance[i] = exp; + + //get de and deviation + float dei = decalc->calcDE(obsi, exp); + DE[i] = dei; - count++; + it = trimmed[i].begin(); + float dist = decalc->calcDist(querySeqs[i], bestfit[i], it->first, it->second); + deviation[i] = dist; } + mothurOut("Done calculating observed, expected and de values for sequences " + toString(lines[process]->start) + " to " + toString(lines[process]->end)); mothurOutEndLine(); + //write out data to file so parent can read it + ofstream out; + string s = toString(getpid()) + ".temp"; + openOutputFile(s, out); + + int size = lines[process]->end - lines[process]->start; + out << size << endl; + + //output observed distances + for (int i = lines[process]->start; i < lines[process]->end; i++) { + out << obsDistance[i].size() << '\t'; + for (int j = 0; j < obsDistance[i].size(); j++) { + out << obsDistance[i][j] << '\t'; + } + out << endl; + } - vector< vector > temp = findWindows(lines[process]->start, lines[process]->end); + //output expected distances + for (int i = lines[process]->start; i < lines[process]->end; i++) { + out << expectedDistance[i].size() << '\t'; + for (int j = 0; j < expectedDistance[i].size(); j++) { + out << expectedDistance[i][j] << '\t'; + } + out << endl; + } + + + //output de values + for (int i = lines[process]->start; i < lines[process]->end; i++) { + out << DE[i] << '\t'; + } + out << endl; - //move into best - count = 0; + //output de values for (int i = lines[process]->start; i < lines[process]->end; i++) { - win[i] = temp[count]; - count++; + out << deviation[i] << '\t'; } + out << endl; + out.close(); + exit(0); }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); } } @@ -718,15 +792,130 @@ void Pintail::createProcessesSpots() { wait(&temp); } - windows = win; - trim = t; + //get data created by processes + for (int i=0;i> size; gobble(in); + + //get observed distances + int count = lines[i]->start; + for (int m = 0; m < size; m++) { + int num; + in >> num; + + vector obs; float w; + for (int j = 0; j < num; j++) { + in >> w; + obs.push_back(w); + } + + obsDistance[count] = obs; + count++; + gobble(in); + } + + gobble(in); + + //get expected distances + count = lines[i]->start; + for (int m = 0; m < size; m++) { + int num; + in >> num; + + vector exp; float w; + for (int j = 0; j < num; j++) { + in >> w; + exp.push_back(w); + } + + expectedDistance[count] = exp; + count++; + gobble(in); + } + + gobble(in); + + count = lines[i]->start; + for (int m = 0; m < size; m++) { + float num; + in >> num; + + DE[count] = num; + count++; + } + + gobble(in); + + count = lines[i]->start; + for (int m = 0; m < size; m++) { + float num; + in >> num; + + deviation[count] = num; + count++; + } + + in.close(); + remove(s.c_str()); + } + + #else - windows = findWindows(lines[0]->start, lines[0]->end); + mothurOut("Calculating observed distance... "); cout.flush(); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + vector obsi = decalc->calcObserved(querySeqs[i], bestfit[i], windowsForeachQuery[i], windowSizes[i]); + obsDistance[i] = obsi; + } + mothurOut("Done."); mothurOutEndLine(); + + + + mothurOut("Finding variability... "); cout.flush(); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + vector q = decalc->findQav(windowsForeachQuery[i], windowSizes[i], probabilityProfile); + Qav[i] = q; + } + mothurOut("Done."); mothurOutEndLine(); + + + + mothurOut("Calculating alpha... "); cout.flush(); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + float alpha = decalc->getCoef(obsDistance[i], Qav[i]); + seqCoef.push_back(alpha); + } + mothurOut("Done."); mothurOutEndLine(); + + + + mothurOut("Calculating expected distance... "); cout.flush(); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + vector exp = decalc->calcExpected(Qav[i], seqCoef[i]); + expectedDistance[i] = exp; + } + mothurOut("Done."); mothurOutEndLine(); + + + + mothurOut("Finding deviation... "); cout.flush(); + for (int i = lines[0]->start; i < lines[0]->end; i++) { + float de = decalc->calcDE(obsDistance[i], expectedDistance[i]); + DE[i] = de; + + it = trimmed[i].begin(); + float dist = decalc->calcDist(querySeqs[i], bestfit[i], it->first, it->second); + deviation[i] = dist; + } + mothurOut("Done."); mothurOutEndLine(); #endif } catch(exception& e) { - errorOut(e, "Pintail", "createProcessesSpots"); + errorOut(e, "Pintail", "createProcesses"); exit(1); } } @@ -734,17 +923,12 @@ void Pintail::createProcessesSpots() { /**************************************************************************************************/ -void Pintail::createProcesses() { +void Pintail::createProcessesQuan() { try { #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) int process = 0; vector processIDS; - - vector< vector > exp; exp.resize(querySeqs.size()); - vector de; de.resize(querySeqs.size()); - vector< vector > obs; obs.resize(querySeqs.size()); - - + //loop through and create all the processes you want while (process != processors) { int pid = fork(); @@ -754,47 +938,25 @@ void Pintail::createProcesses() { process++; }else if (pid == 0){ - vector< vector > temp; - vector tempde; - int count = 0; + quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, templateLines[process]->start, templateLines[process]->end); + //write out data to file so parent can read it + ofstream out; + string s = toString(getpid()) + ".temp"; + openOutputFile(s, out); - 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++; - } - - 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++; + + //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].score << '\t' << quantilesMembers[i][j].member1 << '\t' << quantilesMembers[i][j].member2 << '\t'; + } + out << endl; } - 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++; - } - + out.close(); - 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); } } @@ -804,27 +966,59 @@ void Pintail::createProcesses() { int temp = processIDS[i]; wait(&temp); } - - obsDistance = obs; - expectedDistance = exp; - DE = de; + + //get data created by processes + for (int i=0;i > quan; + quan.resize(100); + + //get quantiles + for (int m = 0; m < quan.size(); m++) { + int num; + in >> num; + + gobble(in); + + vector q; float w; int b, n; + for (int j = 0; j < num; j++) { + in >> w >> b >> n; + //cout << w << '\t' << b << '\t' n << endl; + quanMember newMember(w, b, n); + q.push_back(newMember); + } +//cout << "here" << endl; + quan[m] = q; +//cout << "now here" << endl; + 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(); + remove(s.c_str()); + } #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); - + quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, 0, templateSeqs.size()); #endif } catch(exception& e) { - errorOut(e, "Pintail", "createProcesses"); + errorOut(e, "Pintail", "createProcessesQuan"); exit(1); } } + //***************************************************************************************************************