]> git.donarmstrong.com Git - mothur.git/blobdiff - subsamplecommand.cpp
fixed unifrac bug with multiple processors if numComps was less than processors....
[mothur.git] / subsamplecommand.cpp
index 2a3d138d77497d545a04c259f55dae41d3191413..223c9f5edf68c6eaa23f668e48ac0917854f6067 100644 (file)
@@ -25,8 +25,7 @@ vector<string> SubSampleCommand::getValidParameters(){
 //**********************************************************************************************************************
 SubSampleCommand::SubSampleCommand(){  
        try {
-               abort = true;
-               //initialize outputTypes
+               abort = true; calledHelp = true; 
                vector<string> tempOutNames;
                outputTypes["shared"] = tempOutNames;
                outputTypes["list"] = tempOutNames;
@@ -68,12 +67,12 @@ vector<string> SubSampleCommand::getRequiredFiles(){
 SubSampleCommand::SubSampleCommand(string option) {
        try {
                globaldata = GlobalData::getInstance();
-               abort = false;
+               abort = false; calledHelp = false;   
                allLines = 1;
                labels.clear();
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
                        //valid paramters for this command
@@ -279,7 +278,7 @@ SubSampleCommand::~SubSampleCommand(){}
 int SubSampleCommand::execute(){
        try {
        
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                if (sharedfile != "")   {   getSubSampleShared();       }
                if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str()); return 0; } }
@@ -414,7 +413,7 @@ int SubSampleCommand::getSubSampleFasta() {
                                        bool done = false;
                                        int myrand;
                                        while (!done) {
-                                               myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1));
+                                               myrand = int((float)(thisSize) * (float)(rand()) / ((float)RAND_MAX+1.0));
                                                
                                                if (subset.count(names[myrand]) == 0)  { 
                                                        
@@ -427,6 +426,7 @@ int SubSampleCommand::getSubSampleFasta() {
                                }
                        }
                }else {
+                       
                        //randomly select a subset of those names to include in the subsample
                        for (int j = 0; j < size; j++) {
                                
@@ -436,7 +436,7 @@ int SubSampleCommand::getSubSampleFasta() {
                                bool done = false;
                                int myrand;
                                while (!done) {
-                                       myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1));
+                                       myrand = int((float)(thisSize) * (float)(rand()) / ((float)RAND_MAX+1.0));
                                        
                                        if (subset.count(names[myrand]) == 0)  { 
                                                
@@ -763,7 +763,7 @@ int SubSampleCommand::processShared(vector<SharedRAbundVector*>& thislookup, ofs
                                        if (m->control_pressed) { delete order; return 0; }
                                        
                                        //get random number to sample from order between 0 and thisSize-1.
-                                       int myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1));
+                                       int myrand = int((float)(thisSize) * (float)(rand()) / ((float)RAND_MAX+1.0));
                                        
                                        int bin = order->get(myrand);
                                        
@@ -960,7 +960,7 @@ int SubSampleCommand::getSubSampleList() {
                                        bool done = false;
                                        int myrand;
                                        while (!done) {
-                                               myrand = (int)((float)(rand()) / (RAND_MAX / (names.size()-1) + 1));
+                                               myrand = int((float)(names.size()) * (float)(rand()) / ((float)RAND_MAX+1.0));
                                                
                                                if (subset.count(names[myrand]) == 0) { //you are not already added
                                                        if (groupMap->getGroup(names[myrand]) == Groups[i])  { subset.insert(names[myrand]); break;     }
@@ -977,7 +977,7 @@ int SubSampleCommand::getSubSampleList() {
                                bool done = false;
                                int myrand;
                                while (!done) {
-                                       myrand = (int)((float)(rand()) / (RAND_MAX / (names.size()-1) + 1));
+                                       myrand = int((float)(names.size()) * (float)(rand()) / ((float)RAND_MAX+1.0));
                                        
                                        if (subset.count(names[myrand]) == 0)  { subset.insert(names[myrand]); break;   }
                                }
@@ -1263,7 +1263,7 @@ int SubSampleCommand::processRabund(RAbundVector*& rabund, ofstream& out) {
                                if (m->control_pressed) { delete order; return 0; }
                                
                                //get random number to sample from order between 0 and thisSize-1.
-                               int myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1));
+                               int myrand = int((float)(thisSize) * (float)(rand()) / ((float)RAND_MAX+1.0));
                                
                                int bin = order->get(myrand);
                                
@@ -1423,7 +1423,7 @@ int SubSampleCommand::processSabund(SAbundVector*& sabund, ofstream& out) {
                                if (m->control_pressed) { delete order; return 0; }
                                
                                //get random number to sample from order between 0 and thisSize-1.
-                               int myrand = (int)((float)(rand()) / (RAND_MAX / (thisSize-1) + 1));
+                               int myrand = int((float)(thisSize) * (float)(rand()) / ((float)RAND_MAX+1.0));
                                
                                int bin = order->get(myrand);