]> git.donarmstrong.com Git - mothur.git/blobdiff - rarefact.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / rarefact.cpp
index 76f68296b209ec1eb8e2ff5a14668b32fd451b8b..0454d9e253473b368af34f35021dabbc4a50ae1b 100644 (file)
@@ -20,8 +20,49 @@ int Rarefact::getCurve(float percentFreq = 0.01, int nIters = 1000){
                }
                
                //convert freq percentage to number
-               int increment = numSeqs * percentFreq;
+               int increment = 1;
+               if (percentFreq < 1.0) {  increment = numSeqs * percentFreq;  }
+               else { increment = percentFreq;  }      
                
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+                               if(processors == 1){
+                                       driver(rcd, increment, nIters); 
+                               }else{
+                                       vector<int> procIters;
+                                       
+                                       int numItersPerProcessor = nIters / processors;
+                                       
+                                       //divide iters between processes
+                                       for (int i = 0; i < processors; i++) {
+                                               if(i == processors - 1){
+                                                       numItersPerProcessor = nIters - i * numItersPerProcessor;
+                                               }
+                                               procIters.push_back(numItersPerProcessor);
+                                       }
+                                       
+                                       createProcesses(procIters, rcd, increment); 
+                               }
+
+               #else
+                       driver(rcd, increment, nIters); 
+               #endif
+
+               for(int i=0;i<displays.size();i++){
+                       displays[i]->close();
+               }
+               
+               delete rcd;
+               return 0;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Rarefact", "getCurve");
+               exit(1);
+       }
+}
+/***********************************************************************/
+int Rarefact::driver(RarefactionCurveData* rcd, int increment, int nIters = 1000){
+       try {
+                       
                for(int iter=0;iter<nIters;iter++){
                
                        for(int i=0;i<displays.size();i++){
@@ -45,12 +86,12 @@ int Rarefact::getCurve(float percentFreq = 0.01, int nIters = 1000){
                                lookup->set(binNumber, abundance);
                                rank->set(abundance, rank->get(abundance)+1);
 
-                               if((i == 0) || (i+1) % increment == 0){
+                               if((i == 0) || ((i+1) % increment == 0) || (ends.count(i+1) != 0)){
                                        rcd->updateRankData(rank);
                                }
                        }
        
-                       if(numSeqs % increment != 0){
+                       if((numSeqs % increment != 0) || (ends.count(numSeqs) != 0)){
                                rcd->updateRankData(rank);
                        }
 
@@ -62,18 +103,72 @@ int Rarefact::getCurve(float percentFreq = 0.01, int nIters = 1000){
                        delete rank;
                }
 
-               for(int i=0;i<displays.size();i++){
-                       displays[i]->close();
-               }
-               delete rcd;
                return 0;
        }
        catch(exception& e) {
-               m->errorOut(e, "Rarefact", "getCurve");
+               m->errorOut(e, "Rarefact", "driver");
                exit(1);
        }
 }
+/**************************************************************************************************/
 
+int Rarefact::createProcesses(vector<int>& procIters, RarefactionCurveData* rcd, int increment) {
+       try {
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+               int process = 1;
+               
+               vector<int> processIDS;
+               
+               EstOutput results;
+               
+               //loop through and create all the processes you want
+               while (process != processors) {
+                       int pid = fork();
+                       
+                       if (pid > 0) {
+                               processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
+                               process++;
+                       }else if (pid == 0){
+                               driver(rcd, increment, procIters[process]);
+                       
+                               //pass numSeqs to parent
+                               for(int i=0;i<displays.size();i++){
+                                       string tempFile = toString(getpid()) + toString(i) + ".rarefact.temp";
+                                       displays[i]->outputTempFiles(tempFile);
+                               }
+                               exit(0);
+                       }else { 
+                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
+                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                               exit(0);
+                       }
+               }
+               
+               driver(rcd, increment, procIters[0]);
+               
+               //force parent to wait until all the processes are done
+               for (int i=0;i<(processors-1);i++) { 
+                       int temp = processIDS[i];
+                       wait(&temp);
+               }
+               
+               //get data created by processes
+               for (int i=0;i<(processors-1);i++) { 
+                       for(int j=0;j<displays.size();j++){
+                               string s = toString(processIDS[i]) + toString(j) + ".rarefact.temp";
+                               displays[j]->inputTempFiles(s);
+                               m->mothurRemove(s);
+                       }
+               }
+               
+               return 0;
+#endif         
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Rarefact", "createProcesses");
+               exit(1);
+       }
+}
 /***********************************************************************/
 
 int Rarefact::getSharedCurve(float percentFreq = 0.01, int nIters = 1000){
@@ -88,10 +183,12 @@ try {
                }
                
                //if jumble is false all iters will be the same
-               if (globaldata->jumble == false)  {  nIters = 1;  }
+               if (m->jumble == false)  {  nIters = 1;  }
                
                //convert freq percentage to number
-               int increment = numSeqs * percentFreq;
+               int increment = 1;
+               if (percentFreq < 1.0) {  increment = numSeqs * percentFreq;  }
+               else { increment = percentFreq;  }
                
                for(int iter=0;iter<nIters;iter++){
                
@@ -99,7 +196,7 @@ try {
                                displays[i]->init(label);                 
                        }
                        
-                       if (globaldata->jumble == true)  {
+                       if (m->jumble == true)  {
                                //randomize the groups
                                random_shuffle(lookup.begin(), lookup.end());
                        }