]> git.donarmstrong.com Git - mothur.git/blobdiff - pintail.cpp
working on chimeras
[mothur.git] / pintail.cpp
index 6ccccde7f1d73154faee33a1003e13633217e0d6..b7e5b5fb137fa45b977766c713efafb9bee03cef 100644 (file)
 
 //***************************************************************************************************************
 
-Pintail::Pintail(string name) {
-       try {
-               fastafile = name;
-       }
-       catch(exception& e) {
-               errorOut(e, "Pintail", "Pintail");
-               exit(1);
-       }
-}
+Pintail::Pintail(string filename, string temp) {  fastafile = filename;  templateFile = temp;  }
 //***************************************************************************************************************
 
 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];      }
+               
+               if (processors != 1) {   for (int i = 0; i < bestfit.size(); i++)       {  delete bestfit[i];   }  }
        }
        catch(exception& e) {
                errorOut(e, "Pintail", "~Pintail");
@@ -37,23 +31,30 @@ Pintail::~Pintail() {
 void Pintail::print(ostream& out) {
        try {
                
-               for (itCoef = DE.begin(); itCoef != DE.end(); itCoef++) {
-               
-                       out << itCoef->first->getName() << '\t' << itCoef->second << endl;
+               for (int i = 0; i < querySeqs.size(); i++) {
+                       
+                       int index = ceil(deviation[i]);
+                       
+                       //is your DE value higher than the 95%
+                       string chimera;
+                       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";
                        
-                       itObsDist = obsDistance.find(itCoef->first);
-                       for (int i = 0; i < itObsDist->second.size(); i++) {  out << itObsDist->second[i] << '\t';  }
+                       for (int j = 0; j < obsDistance[i].size(); j++) {  out << obsDistance[i][j] << '\t';  }
                        out << endl;
                        
                        out << "Expected\t";
                        
-                       itExpDist = expectedDistance.find(itCoef->first);
-                       for (int i = 0; i < itExpDist->second.size(); i++) {  out << itExpDist->second[i] << '\t';  }
+                       for (int m = 0; m < expectedDistance[i].size(); m++) {  out << expectedDistance[i][m] << '\t';  }
                        out << endl;
                        
                }
-
        }
        catch(exception& e) {
                errorOut(e, "Pintail", "print");
@@ -65,8 +66,6 @@ void Pintail::print(ostream& out) {
 void Pintail::getChimeras() {
        try {
                
-               distCalculator = new ignoreGaps();
-               
                //read in query sequences and subject sequences
                mothurOut("Reading sequences and template file... "); cout.flush();
                querySeqs = readSeqs(fastafile);
@@ -75,364 +74,380 @@ void Pintail::getChimeras() {
                
                int numSeqs = querySeqs.size();
                
-               //if window is set to default
-               if (window == 0) {  if (querySeqs[0]->getAligned().length() > 800) {  setWindow(200); }
-                                                       else{  setWindow((querySeqs[0]->getAligned().length() / 4)); }  } 
-       
-               //calculate number of iters
-               iters = (querySeqs[0]->getAligned().length() - window + 1) / increment;
-               
-               int linesPerProcess = processors / numSeqs;
-               
-               //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)));
+               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);
+               windowSizesTemplate.resize(templateSeqs.size(), window);
+               windowsForeachQuery.resize(numSeqs);
+               h.resize(numSeqs);
+               quantiles.resize(100);  //one for every percent mismatch
+               
+               //break up file if needed
+               int linesPerProcess = numSeqs / processors ;
+               
+               #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)));
+                               }
+                               //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));
                        }
-                       //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));
-               }
+                       
+                       //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
                
-               //map query sequences to their most similiar sequences in the template - Parallelized
-               mothurOut("Finding closest sequence in template to each sequence... "); cout.flush();
-               if (processors == 1) {   findPairs(lines[0]->start, lines[0]->end);  } 
-               else {          createProcessesPairs();         }
-               mothurOut("Done."); mothurOutEndLine();
-       //      itBest = bestfit.begin();
-       //      itBest++; itBest++;
-//cout << itBest->first->getName() << '\t' << itBest->second->getName()        << endl;        
-               //find Oqs for each sequence - the observed distance in each window - Parallelized
-               mothurOut("Calculating observed percentage differences for each sequence... "); cout.flush();
-               if (processors == 1) {   calcObserved(lines[0]->start, lines[0]->end);  } 
-               else {          createProcessesObserved();              }
-               mothurOut("Done."); mothurOutEndLine();
+               distcalculator = new ignoreGaps();
+               decalc = new DeCalculator();
                
-               //find P
-               mothurOut("Calculating expected percentage differences for each sequence... "); cout.flush();
-               vector<float> probabilityProfile = calcFreq(templateSeqs);
+               decalc->setMask(seqMask);
                
-               //make P into Q
-               for (int i = 0; i < probabilityProfile.size(); i++)  {  probabilityProfile[i] = 1 - probabilityProfile[i];      }
-//cout << "after p into Q" << endl;            
-               //find Qav
-               averageProbability = findQav(probabilityProfile);
-//cout << "after Qav" << endl;         
-               //find Coefficient - maps a sequence to its coefficient
-               seqCoef = getCoef(averageProbability);
-//cout << "after coef" << endl;                
-               //find Eqs for each sequence - the expected distance in each window - Parallelized
-               if (processors == 1) {   calcExpected(lines[0]->start, lines[0]->end);  } 
-               else {          createProcessesExpected();              }
-               mothurOut("Done."); mothurOutEndLine();
+               //find pairs
+               if (processors == 1) { 
+                       mothurOut("Finding closest sequence in template to each sequence... "); cout.flush();
+                       bestfit = findPairs(lines[0]->start, lines[0]->end);
+                       mothurOut("Done."); mothurOutEndLine();
+               }else {         createProcessesPairs();         }
                
-               //find deviation - Parallelized
-               mothurOut("Finding deviation from expected... "); cout.flush();
-               if (processors == 1) {   calcDE(lines[0]->start, lines[0]->end);  } 
-               else {          createProcessesDE();            }
-               mothurOut("Done."); mothurOutEndLine();
                
+               for (int j = 0; j < bestfit.size(); j++) { 
+                               //chops off beginning and end of sequences so they both start and end with a base
+                               ofstream out;
+                               string s = querySeqs[j]->getName();
                                
-               //free memory
-               for (int i = 0; i < lines.size(); i++)                  {       delete lines[i];                }
-               delete distCalculator;  
-
-       }
-       catch(exception& e) {
-               errorOut(e, "Pintail", "getChimeras");
-               exit(1);
-       }
-}
-
-//***************************************************************************************************************
-
-vector<Sequence*> Pintail::readSeqs(string file) {
-       try {
-       
-               ifstream in;
-               openInputFile(file, in);
-               vector<Sequence*> container;
-               
-               //read in seqs and store in vector
-               while(!in.eof()){
-                       Sequence* current = new Sequence(in);
-                       
-                       if (current->getAligned() == "") { current->setAligned(current->getUnaligned()); }
-                       
-                       container.push_back(current);
-                       
-                       gobble(in);
+                               openOutputFile(s, out);
+                               out << ">" << querySeqs[j]->getName() << endl;
+                               out << querySeqs[j]->getAligned() << endl;
+                               out.close();
+                               
+                               string t =querySeqs[j]->getName() + ".ref";
+                               openOutputFile(t, out);
+                               out << ">" << bestfit[j]->getName() << endl;
+                               out << bestfit[j]->getAligned() << endl;
+                               out.close();
                }
+
                
-               in.close();
-               return container;
-               
-       }
-       catch(exception& e) {
-               errorOut(e, "Pintail", "readSeqs");
-               exit(1);
-       }
-}
+               //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();                      }
 
-//***************************************************************************************************************
-//calculate the distances from each query sequence to all sequences in the template to find the closest sequence
-void Pintail::findPairs(int start, int end) {
-       try {
+               //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();
                
-               for(int i = start; i < end; i++){
+               //mask querys
+               for (int i = 0; i < querySeqs.size(); i++) {
+               //cout << querySeqs[i]->getName() << " before mask = " << querySeqs[i]->getAligned() << endl << endl;
+                       decalc->runMask(querySeqs[i]);
+               //cout << querySeqs[i]->getName() << " after mask = " << querySeqs[i]->getAligned() << endl << endl;
+               }
                
-                       float smallest = 10000.0;
-                       Sequence query = *(querySeqs[i]);
+               //mask templates
+               for (int i = 0; i < templateSeqs.size(); i++) {
+                       decalc->runMask(templateSeqs[i]);
+               }
                
-                       for(int j = 0; j < templateSeqs.size(); j++){
-                               
-                               Sequence temp = *(templateSeqs[j]);
-                               
-                               distCalculator->calcDist(query, temp);
-                               float dist = distCalculator->getDist();
+//for (int i = 0; i < lines.size(); i++) { cout << "line pair " << i << " = " << lines[i]->start << '\t' << lines[i]->end << endl;  }
                                
-                               if (dist < smallest) { 
-                               
-                                       bestfit[querySeqs[i]] = templateSeqs[j];
-                                       smallest = dist;
-                               }
+               if (processors == 1) { 
+       
+                       for (int j = 0; j < bestfit.size(); j++) { 
+                       cout << querySeqs[j]->getName() << " after mask = " << querySeqs[j]->getAligned() << endl << endl;
+                       cout << bestfit[j]->getName() << " after mask = " << bestfit[j]->getAligned() << endl << endl;
+                               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();
+cout << i << '\t' << "trimmed = " << it->first << '\t' << it->second << endl;
+                               vector<int> win = decalc->findWindows(querySeqs[i], it->first, it->second, windowSizes[i], increment);
+                               windowsForeachQuery[i] = win;
+                       }
+                       mothurOut("Done."); mothurOutEndLine();
                
-       }
-       catch(exception& e) {
-               errorOut(e, "Pintail", "findPairs");
-               exit(1);
-       }
-}
-//***************************************************************************************************************
-void Pintail::calcObserved(int start, int end) {
-       try {
+               }else {         createProcessesSpots();         }
 
-               for(int i = start; i < end; i++){
                
-                       itBest = bestfit.find(querySeqs[i]);
-                       Sequence* query;
-                       Sequence* subject;
-                       
-                       if (itBest != bestfit.end()) {
-                               query = itBest->first;
-                               subject = itBest->second;
-                       }else{ mothurOut("Error in calcObserved"); mothurOutEndLine(); }
+               if (processors == 1) { 
+                                               
+                       mothurOut("Calculating observed distance... "); cout.flush();
+                       for (int i = lines[0]->start; i < lines[0]->end; i++) {
+       cout << querySeqs[i]->getName() << '\t' << bestfit[i]->getName() << " windows = " << windowsForeachQuery[i].size() << " size = " << windowSizes[i] << endl;
+                               vector<float> obsi = decalc->calcObserved(querySeqs[i], bestfit[i], windowsForeachQuery[i], windowSizes[i]);
+       
+       for (int j = 0; j < obsi.size(); j++) {
+               cout << obsi[j] << '\t';
+       }
+       cout << endl;
+                               obsDistance[i] = obsi;
+                       }
+                       mothurOut("Done."); mothurOutEndLine();
                        
-                       vector<Sequence*> queryFragment;
-                       vector<Sequence*> subjectFragment;
                        
-                       int startpoint = 0; 
-                       for (int m = 0; m < iters; m++) {
-                               string seqFrag = query->getAligned().substr(startpoint, window);
-                               Sequence* temp = new Sequence(query->getName(), seqFrag);
-                               queryFragment.push_back(temp);
-                               
-                               string seqFragsub = subject->getAligned().substr(startpoint, window);
-                               Sequence* tempsub = new Sequence(subject->getName(), seqFragsub);
-                               subjectFragment.push_back(tempsub);
-                               
-                               startpoint += increment;
+                       mothurOut("Finding variability... "); cout.flush();
+                       for (int i = lines[0]->start; i < lines[0]->end; i++) {
+                               vector<float> q = decalc->findQav(windowsForeachQuery[i], windowSizes[i], probabilityProfile);
+
+                               Qav[i] = q;
+cout << i+1 << endl;
+for (int j = 0; j < Qav[i].size(); j++) {
+       cout << Qav[i][j] << '\t';
+}
+cout << endl << endl;
+
                        }
+                       mothurOut("Done."); mothurOutEndLine();
                        
                        
-                       for(int j = 0; j < iters; j++){
-                       
-                               distCalculator->calcDist(*(queryFragment[j]), *(subjectFragment[j]));
-                               float dist = distCalculator->getDist();
-                               
-                               //convert to similiarity
-                               //dist = 1 - dist;
-                               
-                               //save oi
-                               obsDistance[query].push_back(dist);
+                       mothurOut("Calculating alpha... "); cout.flush();
+                       for (int i = lines[0]->start; i < lines[0]->end; i++) {
+                               float alpha = decalc->getCoef(obsDistance[i], Qav[i]);
+cout << i+1 << "\tcoef = " << alpha << endl;
+                               seqCoef[i] = alpha;
                        }
+                       mothurOut("Done."); mothurOutEndLine();
                
-                       //free memory
-                       for (int i = 0; i < queryFragment.size(); i++)  {  delete queryFragment[i];             }
-                       for (int i = 0; i < subjectFragment.size(); i++) {  delete subjectFragment[i];  }
-               }
-               
-       }
-       catch(exception& e) {
-               errorOut(e, "Pintail", "calcObserved");
-               exit(1);
-       }
-}
-
-//***************************************************************************************************************
-void Pintail::calcExpected(int start, int end) {
-       try {
                
-       
-               //for each sequence
-               for(int i = start; i < end; i++){
+                       mothurOut("Calculating expected distance... "); cout.flush();
+                       for (int i = lines[0]->start; i < lines[0]->end; i++) {
+                               vector<float> 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;
+cout << querySeqs[i]->getName() << '\t' << "de value = " << de << endl;                                
+                               it = trimmed[i].begin();
+                               float dist = decalc->calcDist(querySeqs[i], bestfit[i], it->first, it->second); 
+cout << querySeqs[i]->getName() << '\t' << "dist value = " << dist << endl;
+                               deviation[i] = dist;
+                       }
+                       mothurOut("Done."); mothurOutEndLine();
                        
-                       itCoef = seqCoef.find(querySeqs[i]);
-                       float coef = itCoef->second;
+               } 
+               else {          createProcesses();              }
+               
+               
+               //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 {
                        
-                       //for each window
-                       vector<float> queryExpected;
-                       for (int m = 0; m < iters; m++) {               
-                               float expected = averageProbability[m] * coef;
-                               queryExpected.push_back(expected);              
+                       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) { 
+                               quantiles = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, 0, templateSeqs.size());
+                       }else {         createProcessesQuan();          }
+                       
+                       ofstream out4;
+                       string o = getRootName(templateFile) + "quan";
+                       
+                       openOutputFile(o, out4);
+                       
+                       //adjust quantiles
+                       for (int i = 0; i < quantiles.size(); i++) {
+                               if (quantiles[i].size() == 0) {
+                                       //in case this is not a distance found in your template files
+                                       for (int g = 0; g < 6; g++) {
+                                               quantiles[i].push_back(0.0);
+                                       }
+                               }else{
+                                       
+                                       sort(quantiles[i].begin(), quantiles[i].end());
+                                       
+                                       vector<float> temp;
+                                       //save 10%
+                                       temp.push_back(quantiles[i][int(quantiles[i].size() * 0.10)]);
+                                       //save 25%
+                                       temp.push_back(quantiles[i][int(quantiles[i].size() * 0.25)]);
+                                       //save 50%
+                                       temp.push_back(quantiles[i][int(quantiles[i].size() * 0.5)]);
+                                       //save 75%
+                                       temp.push_back(quantiles[i][int(quantiles[i].size() * 0.75)]);
+                                       //save 95%
+                                       temp.push_back(quantiles[i][int(quantiles[i].size() * 0.95)]);
+                                       //save 99%
+                                       temp.push_back(quantiles[i][int(quantiles[i].size() * 0.99)]);
+                                       
+                                       quantiles[i] = temp;
+                               }
+                               
+                               //output quan value
+                               out4 << i+1 << '\t';                            
+                               for (int u = 0; u < quantiles[i].size(); u++) {   out4 << quantiles[i][u] << '\t'; }
+                               out4 << endl;
+
                        }
                        
-                       expectedDistance[querySeqs[i]] = queryExpected;
-                       
+                       mothurOut("Done."); mothurOutEndLine();
                }
-                               
-       }
-       catch(exception& e) {
-               errorOut(e, "Pintail", "calcExpected");
-               exit(1);
-       }
-}
-//***************************************************************************************************************
-void Pintail::calcDE(int start, int end) {
-       try {
-               
        
-               //for each sequence
-               for(int i = start; i < end; i++){
-                       
-                       itObsDist = obsDistance.find(querySeqs[i]);
-                       vector<float> obs = itObsDist->second;
-                       
-                       itExpDist = expectedDistance.find(querySeqs[i]);
-                       vector<float> exp = itExpDist->second;
-                       
-                       //for each window
-                       float sum = 0.0;  //sum = sum from 1 to m of (oi-ei)^2
-                       for (int m = 0; m < iters; m++) {               sum += ((obs[m] - exp[m]) * (obs[m] - exp[m]));         }
-                       
-                       float de = sqrt((sum / (iters - 1)));
+               //free memory
+               for (int i = 0; i < lines.size(); i++)                                  {       delete lines[i];                                }
+               for (int i = 0; i < templateLines.size(); i++)                  {       delete templateLines[i];                }
                        
-                       DE[querySeqs[i]] = de;
-               }
-                               
+               delete distcalculator;
+               delete decalc;
        }
        catch(exception& e) {
-               errorOut(e, "Pintail", "calcDE");
+               errorOut(e, "Pintail", "getChimeras");
                exit(1);
        }
 }
 
 //***************************************************************************************************************
 
-vector<float> Pintail::calcFreq(vector<Sequence*> seqs) {
+vector<float> Pintail::readFreq() {
        try {
-
+       
+               ifstream in;
+               openInputFile(consfile, in);
+               
                vector<float> prob;
+               set<int> h = decalc->getPos();  //positions of bases in masking sequence
                
-               //at each position in the sequence
-               for (int i = 0; i < seqs[0]->getAligned().length(); i++) {
+               //read in probabilities and store in vector
+               int pos; float num; 
                
-                       vector<int> freq;   freq.resize(4,0);
+               while(!in.eof()){
                        
-                       //find the frequency of each nucleotide
-                       for (int j = 0; j < seqs.size(); j++) {
-                               
-                               char value = seqs[j]->getAligned()[i];
+                       in >> pos >> num;
                        
-                               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]++;      }
-                       }
-                       
-                       //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 = highest / (float) seqs.size(); 
-                       float Pi;
-                       //if this is not a gap column
-                       if (highFreq != 0) { Pi =  (highFreq - 0.25) / 0.75;  }
-                       else { Pi = 0; }
+                       if (h.count(pos-1) > 0) {
+                               float Pi;
+                               Pi =  (num - 0.25) / 0.75; 
                        
-                       prob.push_back(Pi);
+                               //cannot have probability less than 0.
+                               if (Pi < 0) { Pi = 0.0; }
+
+                               //do you want this spot
+                               prob.push_back(Pi);  
+                       }
                        
+                       gobble(in);
                }
                
+               in.close();
                return prob;
-                               
+               
        }
        catch(exception& e) {
-               errorOut(e, "Pintail", "calcFreq");
+               errorOut(e, "Pintail", "readFreq");
                exit(1);
        }
 }
+
 //***************************************************************************************************************
-vector<float> Pintail::findQav(vector<float> prob) {
+
+vector< vector<float> > Pintail::readQuantiles() {
        try {
-               vector<float> averages;
+       
+               ifstream in;
+               openInputFile(quanfile, in);
                
-               //for each window find average
-               int startpoint = 0;
-               for (int m = 0; m < iters; m++) {
+               vector< vector<float> > quan;
+       
+               int num; float ten, twentyfive, fifty, seventyfive, ninetyfive, ninetynine; 
+               
+               while(!in.eof()){
                        
-                       float average = 0.0;
-                       for (int i = startpoint; i < (startpoint+window); i++) {   average += prob[i];  }
+                       in >> num >> ten >> twentyfive >> fifty >> seventyfive >> ninetyfive >> ninetynine; 
                        
-                       average = average / window;
+                       vector <float> temp;
                        
-                       //save this windows average
-                       averages.push_back(average);
-               
-                       startpoint += increment;
+                       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);  
+                       
+                       gobble(in);
                }
                
-               return averages;
+               in.close();
+               return quan;
+               
        }
        catch(exception& e) {
-               errorOut(e, "Pintail", "findQav");
+               errorOut(e, "Pintail", "readQuantiles");
                exit(1);
        }
 }
 //***************************************************************************************************************
-map<Sequence*, float> Pintail::getCoef(vector<float> prob) {
+//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) {
        try {
-               map<Sequence*, float> coefs;
                
-               //find average prob
-               float probAverage = 0.0;
-               for (int i = 0; i < prob.size(); i++) {   probAverage += prob[i];       }
-               probAverage = probAverage / (float) prob.size();
+               vector<Sequence*> seqsMatches;  
                
-               //find a coef for each sequence
-               map<Sequence*, vector<float> >::iterator it;
-               for (it = obsDistance.begin(); it != obsDistance.end(); it++) {
-                       
-                       vector<float> temp = it->second;
-                       Sequence* tempSeq = it->first;
-                       
-                       //find observed average 
-                       float obsAverage = 0.0;
-                       for (int i = 0; i < temp.size(); i++) {   obsAverage += temp[i];        }
-                       obsAverage = obsAverage / (float) temp.size();
+               for(int i = start; i < end; i++){
+               
+                       float smallest = 10000.0;
+                       Sequence query = *(querySeqs[i]);
+                       Sequence* match;
                        
-                       float coef = obsAverage / probAverage;
+                       for(int j = 0; j < templateSeqs.size(); j++){
+                               
+                               Sequence temp = *(templateSeqs[j]);
+                               
+                               distcalculator->calcDist(query, temp);
+                               float dist = distcalculator->getDist();
+                               
+                               if (dist < smallest) { 
+                                       match = templateSeqs[j];
+                                       smallest = dist;
+                               }
+                       }
                        
-                       //save this sequences coefficient
-                       coefs[tempSeq] = coef;
+                       seqsMatches.push_back(match);
                }
                
-                                               
-               return coefs;
+               return seqsMatches;
+       
        }
        catch(exception& e) {
-               errorOut(e, "Pintail", "getCoef");
+               errorOut(e, "Pintail", "findPairs");
                exit(1);
        }
 }
 
 /**************************************************************************************************/
 
-void Pintail::createProcessesPairs() {
+void Pintail::createProcessesSpots() {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                int process = 0;
@@ -446,7 +461,51 @@ void Pintail::createProcessesPairs() {
                                processIDS.push_back(pid);  
                                process++;
                        }else if (pid == 0){
-                               findPairs(lines[process]->start, lines[process]->end);
+                               
+                               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<int, int> trim;
+                                       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();
+                               
+                               //write out data to file so parent can read it
+                               ofstream out;
+                               string s = toString(getpid()) + ".temp";
+                               openOutputFile(s, out);
+                               
+                               //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;
+                               }
+                       
+                               //output windowSizes
+                               for (int i = lines[process]->start; i < lines[process]->end; i++) {
+                                       out << windowSizes[i] << '\t';
+                               }
+                               out << endl;
+                               
+                               //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;
+                               }
+                               out.close();
+                               
                                exit(0);
                        }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
                }
@@ -457,20 +516,85 @@ void Pintail::createProcessesPairs() {
                        wait(&temp);
                }
                
+               //get data created by processes
+               for (int i=0;i<processors;i++) { 
+                       ifstream in;
+                       string s = toString(processIDS[i]) + ".temp";
+                       openInputFile(s, in);
+                       
+                       int size = lines[i]->end - lines[i]->start;
+                                       
+                       int count = lines[i]->start;
+                       for (int m = 0; m < size; m++) {
+                               int num;
+                               in >> num;
+                       
+                               vector<int> win;  int w;
+                               for (int j = 0; j < num; j++) {
+                                       in >> w;
+                                       win.push_back(w);
+                               }
+                       
+                               windowsForeachQuery[count] = win;
+                               count++;
+                               gobble(in);
+                       }
+               
+                       gobble(in);
+                       count = lines[i]->start;
+                       for (int m = 0; m < size; m++) {
+                               int num;
+                               in >> num;
+                               
+                               windowSizes[count] = num;
+                               count++;
+                       }
+                       
+                       gobble(in);
+                       
+                       count = lines[i]->start;
+                       for (int m = 0; m < size; m++) {
+                               int front, back;
+                               in >> front >> back;
+                               
+                               map<int, int> t;
+                               
+                               t[front] = back;
+                               
+                               trimmed[count] = t;
+                               count++;
+                               
+                               gobble(in);
+                       }
+
+                       
+                       in.close();
+                       remove(s.c_str());
+               }
+                       
+       
 #else
-               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
+                       decalc->trimSeqs(querySeqs[j], bestfit[j], trimmed[j]);  
+               }
+
+               for (int i = lines[0]->start; i < lines[0]->end; i++) {
+                               it = trimmed[i].begin();
+                               map<int, int> win = decalc->findWindows(querySeqs[i], it->first, it->second, windowSizes[i], increment);
+                               windows[i] = win;
+               }
 
 #endif         
        }
        catch(exception& e) {
-               errorOut(e, "Pintail", "createProcessesPairs");
+               errorOut(e, "Pintail", "createProcessesSpots");
                exit(1);
        }
 }
-
 /**************************************************************************************************/
 
-void Pintail::createProcessesObserved() {
+void Pintail::createProcessesPairs() {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                int process = 0;
@@ -484,7 +608,25 @@ void Pintail::createProcessesObserved() {
                                processIDS.push_back(pid);  
                                process++;
                        }else if (pid == 0){
-                               calcObserved(lines[process]->start, lines[process]->end);
+                               
+                               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();
+                               
+                               //write out data to file so parent can read it
+                               ofstream out;
+                               string s = toString(getpid()) + ".temp";
+                               openOutputFile(s, out);
+                               
+                               //output range and size
+                               out << bestfit.size() << endl;
+                               
+                               //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); }
                }
@@ -495,20 +637,42 @@ void Pintail::createProcessesObserved() {
                        wait(&temp);
                }
                
+               //get data created by processes
+               for (int i=0;i<processors;i++) { 
+                       ifstream in;
+                       string s = toString(processIDS[i]) + ".temp";
+                       openInputFile(s, in);
+                       
+                       int size;
+                       in >> size;  gobble(in);
+                               
+                       //get pairs
+                       int count = lines[i]->start;
+                       for (int m = 0; m < size; m++) {
+                               Sequence* temp = new Sequence(in);
+                               bestfit[count] = temp;
+                       
+                               count++;
+                               gobble(in);
+                       }
+                       
+                       in.close();
+                       remove(s.c_str());
+               }
+                       
+       
 #else
-               calcObserved(lines[0]->start, lines[0]->end);
-
+               bestfit = findPairs(lines[0]->start, lines[0]->end);
 #endif         
        }
        catch(exception& e) {
-               errorOut(e, "Pintail", "createProcessesObserved");
+               errorOut(e, "Pintail", "createProcessesPairs");
                exit(1);
        }
 }
+/**************************************************************************************************/
 
-//***************************************************************************************************************
-
-void Pintail::createProcessesExpected() {
+void Pintail::createProcesses() {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                int process = 0;
@@ -522,7 +686,75 @@ void Pintail::createProcessesExpected() {
                                processIDS.push_back(pid);  
                                process++;
                        }else if (pid == 0){
-                               calcExpected(lines[process]->start, lines[process]->end);
+                               
+                               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++) {
+                                       
+                                       vector<float> obsi = decalc->calcObserved(querySeqs[i], bestfit[i], windowsForeachQuery[i], windowSizes[i]);
+                                       obsDistance[i] = obsi;
+                               
+                                       //calc Qav
+                                       vector<float> q = decalc->findQav(windowsForeachQuery[i], windowSizes[i], probabilityProfile);
+                                       
+                                       //get alpha
+                                       float alpha = decalc->getCoef(obsDistance[i], q);
+                                       
+                                       //find expected
+                                       vector<float> exp = decalc->calcExpected(q, alpha);
+                                       expectedDistance[i] = exp;
+                                       
+                                       //get de and deviation
+                                       float dei = decalc->calcDE(obsi, exp);
+                                       DE[i] = dei;
+                                       
+                                       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;
+                               }
+                               
+                               
+                               //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;    
+                               
+                               //output de values
+                               for (int i = lines[process]->start; i < lines[process]->end; i++) {
+                                       out << deviation[i] << '\t';
+                               }
+                               out << endl;
+                               
+                               out.close();
+
                                exit(0);
                        }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
                }
@@ -533,25 +765,143 @@ void Pintail::createProcessesExpected() {
                        wait(&temp);
                }
                
+               //get data created by processes
+               for (int i=0;i<processors;i++) { 
+                       ifstream in;
+                       string s = toString(processIDS[i]) + ".temp";
+                       openInputFile(s, in);
+                       
+                       int size;
+                       in >> size;  gobble(in);
+                       
+                       //get observed distances
+                       int count = lines[i]->start;
+                       for (int m = 0; m < size; m++) {
+                               int num;
+                               in >> num;
+                       
+                               vector<float> 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<float> 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
-               calcExpected(lines[0]->start, lines[0]->end);
+                       mothurOut("Calculating observed distance... "); cout.flush();
+                       for (int i = lines[0]->start; i < lines[0]->end; i++) {
+                               vector<float> obsi = decalc->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 = decalc->findQav(windows[i], windowSizes[i], probabilityProfile, h[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 = 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<float> 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", "createProcessesExpected");
+               errorOut(e, "Pintail", "createProcesses");
                exit(1);
        }
 }
 
+
 /**************************************************************************************************/
 
-void Pintail::createProcessesDE() {
+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();
@@ -560,7 +910,26 @@ void Pintail::createProcessesDE() {
                                processIDS.push_back(pid);  
                                process++;
                        }else if (pid == 0){
-                               calcDE(lines[process]->start, lines[process]->end);
+                               
+                               quantiles = 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);
+                               
+                                                               
+                               //output observed distances
+                               for (int i = 0; i < quantiles.size(); i++) {
+                                       out << quantiles[i].size() << '\t';
+                                       for (int j = 0; j < quantiles[i].size(); j++) {
+                                               out << quantiles[i][j] << '\t';
+                                       }
+                                       out << endl;
+                               }
+                               
+                               out.close();
+                               
                                exit(0);
                        }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
                }
@@ -571,17 +940,51 @@ void Pintail::createProcessesDE() {
                        wait(&temp);
                }
                
+               //get data created by processes
+               for (int i=0;i<processors;i++) { 
+                       ifstream in;
+                       string s = toString(processIDS[i]) + ".temp";
+                       openInputFile(s, in);
+                       
+                       vector< vector<float> > quan; quan.resize(100);
+                       
+                       //get quantiles
+                       for (int m = 0; m < quan.size(); m++) {
+                               int num;
+                               in >> num;
+                       
+                               vector<float> q;  float w;
+                               for (int j = 0; j < num; j++) {
+                                       in >> w;
+                                       q.push_back(w);
+                               }
+                       
+                               quan[m] = q;
+                               gobble(in);
+                       }
+                       
+                       
+                       //save quan in quantiles
+                       for (int i = 0; i < quan.size(); i++) {
+                               //put all values of q[i] into quan[i]
+                               quantiles[i].insert(quantiles[i].begin(), quan[i].begin(), quan[i].end());
+                       }
+                                       
+                       in.close();
+                       remove(s.c_str());
+               }
+               
 #else
-               calcDE(lines[0]->start, lines[0]->end);
-
+               quantiles = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, 0, templateSeqs.size());
 #endif         
        }
        catch(exception& e) {
-               errorOut(e, "Pintail", "createProcessesDE");
+               errorOut(e, "Pintail", "createProcessesQuan");
                exit(1);
        }
 }
 
+
 //***************************************************************************************************************