#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(string filename, string o) {
+ fastafile = filename; outputDir = o;
+ distcalculator = new eachGapDist();
+ decalc = new DeCalculator();
+}
//***************************************************************************************************************
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]; }
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "~Pintail");
- exit(1);
- }
-}
-//***************************************************************************************************************
-void Pintail::print(ostream& out) {
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");
+ errorOut(e, "Pintail", "~Pintail");
exit(1);
}
}
-
//***************************************************************************************************************
-void Pintail::getChimeras() {
+void 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();
+ mergedFilterString = "";
+ windowSizesTemplate.resize(templateSeqs.size(), window);
+ quantiles.resize(100); //one for every percent mismatch
+ quantilesMembers.resize(100); //one for every percent mismatch
- int numSeqs = querySeqs.size();
+ //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()); }
- obsDistance.resize(numSeqs);
- expectedDistance.resize(numSeqs);
- seqCoef.resize(numSeqs);
- DE.resize(numSeqs);
- Qav.resize(numSeqs);
- bestfit.resize(numSeqs);
- deviation.resize(numSeqs);
- trimmed.resize(numSeqs);
- windowSizes.resize(numSeqs, window);
- windows.resize(numSeqs);
-
- //break up file if needed
- int linesPerProcess = processors / numSeqs;
+ decalc->setMask(seqMask);
#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)));
+ //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
-
- distcalculator = new ignoreGaps();
-
- 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() == "159481") { bestfit[17] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "100137") { bestfit[16] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "112956") { bestfit[15] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "102326") { bestfit[14] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "66229") { bestfit[13] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "206276") { bestfit[12] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "63607") { bestfit[11] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "7056") { bestfit[10] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "7088") { bestfit[9] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "17553") { bestfit[8] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "131723") { bestfit[7] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "69013") { bestfit[6] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "24543") { bestfit[5] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "27824") { bestfit[4] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "1456") { bestfit[3] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "1456") { bestfit[2] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "141312") { bestfit[1] = *(templateSeqs[m]); }
- if (templateSeqs[m]->getName() == "141312") { bestfit[0] = *(templateSeqs[m]); }
-
-
-}*/
-
- for (int j = 0; j < bestfit.size(); j++) {
- //chops off beginning and end of sequences so they both start and end with a base
- trimSeqs(querySeqs[j], bestfit[j], j);
- }
+
+ 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, outputDir + getSimpleName(templateFileName));
mothurOut("Done."); mothurOutEndLine();
-
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- it = trimmed[i].begin();
- vector<int> win = findWindows(querySeqs[i], it->first, it->second, windowSizes[i]);
- windows[i] = win;
- }
-
-
- } else { createProcessesSpots(); }
-
- //find P
- if (consfile == "") { probabilityProfile = calcFreq(templateSeqs); }
- else { probabilityProfile = readFreq(); }
+ }else { probabilityProfile = readFreq(); mothurOut("Done."); }
+ 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]; } //cout << i << '\t' << probabilityProfile[i] << endl;
- if (processors == 1) {
+ bool reRead = false;
+ //create filter if needed for later
+ if (filter) {
- mothurOut("Calculating observed distance... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- vector<float> obsi = calcObserved(querySeqs[i], bestfit[i], windows[i], windowSizes[i]);
- obsDistance[i] = obsi;
- }
- mothurOut("Done."); mothurOutEndLine();
-
+ //read in all query seqs
+ ifstream in;
+ openInputFile(fastafile, in);
-
- mothurOut("Finding variability... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- vector<float> q = findQav(windows[i], windowSizes[i]);
- Qav[i] = q;
+ vector<Sequence*> tempQuerySeqs;
+ while(!in.eof()){
+ Sequence* s = new Sequence(in);
+ gobble(in);
+
+ if (s->getName() != "") { tempQuerySeqs.push_back(s); }
}
- mothurOut("Done."); mothurOutEndLine();
+ in.close();
-
-
- mothurOut("Calculating alpha... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- float alpha = getCoef(obsDistance[i], Qav[i]);
- seqCoef.push_back(alpha);
+ vector<Sequence*> 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++) {
+ decalc->runMask(temp[i]);
+ }
}
- mothurOut("Done."); mothurOutEndLine();
-
+
+ mergedFilterString = createFilter(temp, 0.5);
+
+ //reread template seqs
+ for (int i = 0; i < tempQuerySeqs.size(); i++) { delete tempQuerySeqs[i]; }
+ }
- mothurOut("Calculating expected distance... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- vector<float> exp = calcExpected(Qav[i], seqCoef[i]);
- expectedDistance[i] = exp;
+ //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++) {
+ decalc->runMask(templateSeqs[i]);
+ }
+ }
+
+ if (filter) {
+ reRead = true;
+ for (int i = 0; i < templateSeqs.size(); i++) {
+ runFilter(templateSeqs[i]);
+ }
}
- mothurOut("Done."); mothurOutEndLine();
+ 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(); }
+
+ ofstream out4, out5;
+ string noOutliers, outliers;
- mothurOut("Finding deviation... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- float de = calcDE(obsDistance[i], expectedDistance[i]);
- DE[i] = de;
+ if ((!filter) && (seqMask == "")) {
+ noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.quan";
+ }else if ((!filter) && (seqMask != "")) {
+ noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.masked.quan";
+ }else if ((filter) && (seqMask != "")) {
+ noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.filtered." + getSimpleName(getRootName(fastafile)) + "masked.quan";
+ }else if ((filter) && (seqMask == "")) {
+ noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.filtered." + getSimpleName(getRootName(fastafile)) + "quan";
+ }
+
+
+
+
+ decalc->removeObviousOutliers(quantilesMembers, templateSeqs.size());
+
+ openOutputFile(noOutliers, out5);
+ //adjust quantiles
+ for (int i = 0; i < quantilesMembers.size(); i++) {
+ vector<float> 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;
- it = trimmed[i].begin();
- float dist = calcDist(querySeqs[i], bestfit[i], it->first, it->second);
- deviation[i] = dist;
}
+
mothurOut("Done."); mothurOutEndLine();
-
- }
- else { createProcesses(); }
-
- delete distcalculator;
+ }
- //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 != "") { readQuantiles(); }
- else {
- if (processors == 1) {
- quantiles = getQuantiles(lines[0]->start, lines[0]->end);
- }else { createProcessesQuan(); }
+ if (reRead) {
+ for (int i = 0; i < templateSeqs.size(); i++) { delete templateSeqs[i]; }
+ templateSeqs.clear();
+ templateSeqs = readSeqs(templateFileName);
}
-
+
//free memory
- for (int i = 0; i < lines.size(); i++) { delete lines[i]; }
-
-
+ for (int i = 0; i < templateLines.size(); i++) { delete templateLines[i]; }
+
}
catch(exception& e) {
- errorOut(e, "Pintail", "getChimeras");
+ errorOut(e, "Pintail", "doPrep");
exit(1);
}
}
-
//***************************************************************************************************************
-
-vector<Sequence*> Pintail::readSeqs(string file) {
+void Pintail::print(ostream& out, ostream& outAcc) {
try {
-
- ifstream in;
- openInputFile(file, in);
- vector<Sequence*> container;
+ 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"; }
+ }
+ }else{ chimera = "No"; }
- //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());
-
- container.push_back(current);
-
- gobble(in);
+ out << querySeq->getName() << '\t' << "div: " << deviation << "\tstDev: " << DE << "\tchimera flag: " << chimera << endl;
+ if (chimera == "Yes") {
+ mothurOut(querySeq->getName() + "\tdiv: " + toString(deviation) + "\tstDev: " + toString(DE) + "\tchimera flag: " + chimera); mothurOutEndLine();
+ outAcc << querySeq->getName() << endl;
}
+ out << "Observed\t";
+
+ for (int j = 0; j < obsDistance.size(); j++) { out << obsDistance[j] << '\t'; }
+ out << endl;
+
+ out << "Expected\t";
+
+ for (int m = 0; m < expectedDistance.size(); m++) { out << expectedDistance[m] << '\t'; }
+ out << endl;
- in.close();
- return container;
}
catch(exception& e) {
- errorOut(e, "Pintail", "readSeqs");
+ errorOut(e, "Pintail", "print");
exit(1);
}
}
//***************************************************************************************************************
-//num is query's spot in querySeqs
-map<int, int> Pintail::trimSeqs(Sequence* query, Sequence subject, int num) {
+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 they mask
+ if (seqMask != "") {
+ decalc->runMask(query);
+ decalc->runMask(bestfit);
+ }
+
+ if (filter) { //must be done after a mask
+ runFilter(query);
+ runFilter(bestfit);
+ }
- string q = query->getAligned();
- string s = subject.getAligned();
+
+ //trim seq
+ decalc->trimSeqs(query, bestfit, trimmed);
- int front = 0;
- for (int i = 0; i < q.length(); i++) {
- if (isalpha(q[i]) && isalpha(s[i])) { front = i; break; }
- }
+ //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);
+
+ Qav = decalc->findQav(windowsForeachQuery, windowSizes, probabilityProfile);
+
+ //find alpha
+ seqCoef = decalc->getCoef(obsDistance, Qav);
- int back = 0;
- for (int i = q.length(); i >= 0; i--) {
- if (isalpha(q[i]) && isalpha(s[i])) { back = i; break; }
- }
+ //calculating expected distance
+ expectedDistance = decalc->calcExpected(Qav, seqCoef);
- //if num = -1 then you are calling this from quantiles
- if (num != -1) {
- trimmed[num][front] = back;
- return trimmed[num];
- }
+ //finding de
+ DE = decalc->calcDE(obsDistance, expectedDistance);
- map<int, int> temp;
- temp[front] = back;
- return temp;
+ //find distance between query and closest match
+ it = trimmed.begin();
+ deviation = decalc->calcDist(query, bestfit, it->first, it->second);
+ delete bestfit;
+
+ return 0;
}
catch(exception& e) {
- errorOut(e, "Pintail", "trimSeqs");
+ errorOut(e, "Pintail", "getChimeras");
exit(1);
}
}
openInputFile(consfile, in);
vector<float> prob;
+ set<int> h = decalc->getPos(); //positions of bases in masking sequence
//read in probabilities and store in vector
int pos; float num;
in >> pos >> num;
- //do you want this spot
- prob.push_back(num);
+ if (h.count(pos) > 0) {
+ float Pi;
+ Pi = (num - 0.25) / 0.75;
- gobble(in);
- }
-
- in.close();
- return prob;
-
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "readFreq");
- exit(1);
- }
-}
-
-//***************************************************************************************************************
+ //cannot have probability less than 0.
+ if (Pi < 0) { Pi = 0.0; }
-vector< vector<float> > Pintail::readQuantiles() {
- try {
-
- ifstream in;
- openInputFile(quanfile, in);
-
- vector< vector<float> > quan;
-
- //read in probabilities and store in vector
- int num; float ten, twentyfive, fifty, seventyfive, ninetyfive, ninetynine;
-
- while(!in.eof()){
-
- in >> num >> ten >> twentyfive >> fifty >> seventyfive >> ninetyfive >> ninetynine;
-
- vector <float> temp;
-
- temp.push_back(ten);
- temp.push_back(twentyfive);
- temp.push_back(fifty);
- temp.push_back(seventyfive);
- temp.push_back(ninetyfive);
- temp.push_back(ninetynine);
-
- //do you want this spot
- quan.push_back(temp);
+ //do you want this spot
+ prob.push_back(Pi);
+ }
gobble(in);
}
in.close();
- return quan;
+ return prob;
}
catch(exception& e) {
- errorOut(e, "Pintail", "readQuantiles");
+ 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<Sequence> Pintail::findPairs(int start, int end) {
+Sequence* Pintail::findPairs(Sequence* q) {
try {
- vector<Sequence> seqsMatches; seqsMatches.resize(end-start);
-
- 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;
- }
- }
- }
+ Sequence* seqsMatches;
+ seqsMatches = decalc->findClosest(q, templateSeqs);
return seqsMatches;
}
exit(1);
}
}
-
-//***************************************************************************************************************
-//find the window breaks for each sequence - this is so you can move ahead by bases.
-vector<int> Pintail::findWindows(Sequence* query, int front, int back, int& size) {
- try {
-
- vector<int> win;
-
- int cutoff = back - front; //back - front
-
- //if window is set to default
- if (size == 0) { if (cutoff > 1200) { size = 300; }
- else{ size = (cutoff / 4); } }
- else if (size > (cutoff / 4)) {
- mothurOut("You have selected to large a window size for sequence " + query->getName() + ". I will choose an appropriate window size."); mothurOutEndLine();
- size = (cutoff / 4);
- }
-
- string seq = query->getAligned().substr(front, cutoff);
-
- //count bases
- int numBases = 0;
- for (int l = 0; l < seq.length(); l++) { if (isalpha(seq[l])) { numBases++; } }
-
- //save start of seq
- win.push_back(front);
-
- //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
-
- seq = query->getAligned();
- for (int m = front; m < (back - size) ; m++) {
-
- //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.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; }
- }
-
- return win;
-
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "findWindows");
- exit(1);
- }
-}
-
-//***************************************************************************************************************
-vector<float> Pintail::calcObserved(Sequence* query, Sequence subject, vector<int> window, int size) {
- try {
-
- vector<float> temp;
-
- int startpoint = 0;
- for (int m = 0; m < windows.size(); m++) {
-
- string seqFrag = query->getAligned().substr(window[startpoint], size);
- string seqFragsub = subject.getAligned().substr(window[startpoint], size);
-
- int diff = 0;
- for (int b = 0; b < seqFrag.length(); b++) {
- if (seqFrag[b] != seqFragsub[b]) { diff++; }
- }
-
- //percentage of mismatched bases
- float dist;
- dist = diff / (float) seqFrag.length() * 100;
-
- temp.push_back(dist);
-
- startpoint++;
- }
-
- return temp;
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "calcObserved");
- exit(1);
- }
-}
-//***************************************************************************************************************
-float Pintail::calcDist(Sequence* query, Sequence subject, int front, int back) {
- try {
-
- //so you only look at the trimmed part of the sequence
- int cutoff = back - front;
-
- //from first startpoint with length back-front
- string seqFrag = query->getAligned().substr(front, cutoff);
- string seqFragsub = subject.getAligned().substr(front, cutoff);
-
- int diff = 0;
- for (int b = 0; b < seqFrag.length(); b++) {
- if (seqFrag[b] != seqFragsub[b]) { diff++; }
- }
-
- //percentage of mismatched bases
- float dist = diff / (float) seqFrag.length() * 100;
-
- return dist;
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "calcDist");
- exit(1);
- }
-}
-
-//***************************************************************************************************************
-vector<float> Pintail::calcExpected(vector<float> qav, float coef) {
- try {
-
- //for each window
- vector<float> queryExpected;
-
- for (int m = 0; m < qav.size(); m++) {
-
- float expected = qav[m] * coef;
-
- queryExpected.push_back(expected);
- }
-
- return queryExpected;
-
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "calcExpected");
- exit(1);
- }
-}
-//***************************************************************************************************************
-float Pintail::calcDE(vector<float> obs, vector<float> exp) {
- try {
-
- //for each window
- float sum = 0.0; //sum = sum from 1 to m of (oi-ei)^2
- for (int m = 0; m < obsDistance.size(); m++) { sum += ((obs[m] - exp[m]) * (obs[m] - exp[m])); }
-
- float de = sqrt((sum / (obsDistance.size() - 1)));
-
- return de;
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "calcDE");
- exit(1);
- }
-}
-
-//***************************************************************************************************************
-
-vector<float> Pintail::calcFreq(vector<Sequence*> seqs) {
- try {
-
- vector<float> 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<int> 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];
-
- 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++; }
- }
-
- //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;
-
- //cannot have probability less than 0.
- if (Pi < 0) { Pi = 0.0; }
-
- //saves this for later
- outFreq << i+1 << '\t' << Pi << endl;
-
- prob.push_back(Pi);
- }
-
- outFreq.close();
-
- return prob;
-
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "calcFreq");
- exit(1);
- }
-}
-//***************************************************************************************************************
-vector<float> Pintail::findQav(vector<int> window, int size) {
- try {
- vector<float> averages;
-
- //for each window find average
- for (int m = 0; m < windows.size(); m++) {
-
- float average = 0.0;
-
- //while you are in the window for this sequence
- for (int j = window[m]; j < (window[m]+size); j++) { average += probabilityProfile[j]; }
-
- average = average / size;
-
- //save this windows average
- averages.push_back(average);
- }
-
- return averages;
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "findQav");
- exit(1);
- }
-}
-
-//***************************************************************************************************************
-vector< vector<float> > Pintail::getQuantiles(int start, int end) {
- try {
- vector< vector<float> > quan;
-
- //for each sequence
- for(int i = start; i < end; i++){
-
- Sequence* query = templateSeqs[i];
-
- //compare to every other sequence in template
- for(int j = 0; j < i; j++){
-
- Sequence subject = *(templateSeqs[j]);
-
- map<int, int> trim = trimSeqs(query, subject, -1);
-
-
-
-
-
- }
-
-
-
- }
- return quan;
-
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "findQav");
- exit(1);
- }
-}
-
-//***************************************************************************************************************
-float Pintail::getCoef(vector<float> obs, vector<float> qav) {
- try {
-
- //find average prob for this seqs windows
- float probAverage = 0.0;
- for (int j = 0; j < qav.size(); j++) { probAverage += qav[j]; }
- probAverage = probAverage / (float) Qav.size();
-
- //find observed average
- float obsAverage = 0.0;
- for (int j = 0; j < obs.size(); j++) { obsAverage += obs[j]; }
- obsAverage = obsAverage / (float) obs.size();
-
-
- float coef = obsAverage / probAverage;
-
- return coef;
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "getCoef");
- exit(1);
- }
-}
/**************************************************************************************************/
-
-void Pintail::createProcessesSpots() {
+void Pintail::createProcessesQuan() {
try {
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
int process = 0;
vector<int> processIDS;
- vector< vector<int> > win; win.resize(querySeqs.size());
-
+
//loop through and create all the processes you want
while (process != processors) {
int pid = fork();
process++;
}else if (pid == 0){
- vector<Sequence> 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);
- count++;
- }
+ 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);
- for (int i = lines[process]->start; i < lines[process]->end; i++) {
- vector<int> temp = findWindows(querySeqs[i], it->first, it->second, windowSizes[i]);
- win[i] = temp;
+
+ //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;
}
+ out.close();
+
exit(0);
}else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
}
int temp = processIDS[i];
wait(&temp);
}
-
- windows = win;
-#else
- bestfit = findPairs(lines[0]->start, lines[0]->end);
- for (int j = 0; j < bestfit.size(); j++) {
- //chops off beginning and end of sequences so they both start and end with a base
- trimSeqs(querySeqs[j], bestfit[j], j);
- }
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- it = trimmed[i].begin();
- map<int, int> win = findWindows(querySeqs[i], it->first, it->second, windowSizes[i]);
- windows[i] = win;
- }
-
-#endif
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "createProcessesSpots");
- exit(1);
- }
-}
-
-
-/**************************************************************************************************/
-
-void Pintail::createProcesses() {
- try {
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- int process = 0;
- vector<int> processIDS;
-
- vector< vector<float> > exp; exp.resize(querySeqs.size());
- vector<float> de; de.resize(querySeqs.size());
- vector< vector<float> > obs; obs.resize(querySeqs.size());
- vector<float> dev; dev.resize(querySeqs.size());
-
-
- //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){
-
- //calc obs
- for (int i = lines[process]->start; i < lines[process]->end; i++) {
- vector<float> obsi = calcObserved(querySeqs[i], bestfit[i], windows[i], windowSizes[i]);
- obs[i] = obsi;
-
- //calc Qav
- vector<float> q = findQav(windows[i], windowSizes[i]);
-
- //get alpha
- float alpha = getCoef(obsDistance[i], q);
-
- //find expected
- vector<float> exp = calcExpected(q, alpha);
- expectedDistance[i] = exp;
-
- //get de and deviation
- float dei = calcDE(obsi, exp);
- de[i] = dei;
-
- it = trimmed[i].begin();
- float dist = calcDist(querySeqs[i], bestfit[i], it->first, it->second);
- dev[i] = dist;
- }
-
- exit(0);
- }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
- }
-
- //force parent to wait until all the processes are done
+ //get data created by processes
for (int i=0;i<processors;i++) {
- int temp = processIDS[i];
- wait(&temp);
- }
-
- obsDistance = obs;
- expectedDistance = exp;
- DE = de;
- deviation = dev;
-
-#else
- mothurOut("Calculating observed distance... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- vector<float> obsi = calcObserved(querySeqs[i], bestfit[i], windows[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<float> q = findQav(windows[i], windowSizes[i]);
- Qav[i] = q;
- }
- mothurOut("Done."); mothurOutEndLine();
-
-
-
- mothurOut("Calculating alpha... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- float alpha = 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<float> exp = calcExpected(Qav[i], seqCoef[i]);
- expectedDistance[i] = exp;
+ ifstream in;
+ string s = toString(processIDS[i]) + ".temp";
+ openInputFile(s, in);
+
+ vector< vector<quanMember> > quan;
+ quan.resize(100);
+
+ //get quantiles
+ for (int m = 0; m < quan.size(); m++) {
+ int num;
+ in >> num;
+
+ gobble(in);
+
+ vector<quanMember> 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);
}
- mothurOut("Done."); mothurOutEndLine();
-
-
- mothurOut("Finding deviation... "); cout.flush();
- for (int i = lines[0]->start; i < lines[0]->end; i++) {
- float de = calcDE(obsDistance[i], expectedDistance[i]);
- DE[i] = de;
-
- it = trimmed[i].begin();
- float dist = calcDist(querySeqs[i], bestfit[i], it->first, it->second);
- deviation[i] = dist;
+
+ //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());
}
- mothurOut("Done."); mothurOutEndLine();
-
-#endif
- }
- catch(exception& e) {
- errorOut(e, "Pintail", "createProcesses");
- exit(1);
- }
-}
-
-
-/**************************************************************************************************/
-
-void Pintail::createProcessesQuan() {
- try {
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- int process = 0;
- vector<int> processIDS;
-
- //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){
-
-
- exit(0);
- }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
- }
-
- //force parent to wait until all the processes are done
- for (int i=0;i<processors;i++) {
- int temp = processIDS[i];
- wait(&temp);
+
+ in.close();
+ remove(s.c_str());
}
#else
-
+ quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, 0, templateSeqs.size());
#endif
}
catch(exception& e) {