]> git.donarmstrong.com Git - mothur.git/blobdiff - catchallcommand.cpp
fixed bug with dist.shared subsampling. added mode parameter to dist.shared so...
[mothur.git] / catchallcommand.cpp
index 29f2220dcc512d7a5db58bfee3c3767418ff7aa7..329b56a2c24dcf024ca66cab0f160b76438c0922 100644 (file)
@@ -119,10 +119,12 @@ CatchAllCommand::CatchAllCommand(string option)  {
                        sabundfile = validParameter.validFile(parameters, "sabund", true);
                        if (sabundfile == "not open") { sabundfile = ""; abort = true; }
                        else if (sabundfile == "not found") { sabundfile = "";  }
+                       else { m->setSabundFile(sabundfile); }
                        
                        sharedfile = validParameter.validFile(parameters, "shared", true);
                        if (sharedfile == "not open") { sharedfile = ""; abort = true; }
                        else if (sharedfile == "not found") { sharedfile = "";   }
+                       else { m->setSharedFile(sharedfile); }
                        
                        string label = validParameter.validFile(parameters, "label", false);                    
                        if (label == "not found") { label = ""; }
@@ -172,19 +174,52 @@ int CatchAllCommand::execute() {
                path = path.substr(0, (path.find_last_of("othur")-5));
                path = m->getFullPathName(path);
                
+        if (m->debug) { m->mothurOut("[DEBUG]: mothur's path = " + path + "\n"); }
+       
                savedOutputDir = outputDir;
-               string catchAllCommandExe = ""; 
-               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-                       catchAllCommandExe += "mono " + path + "CatchAllcmdL.exe ";
+               string catchAllCommandExe = "";
+        string catchAllTest = "";
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                        if (outputDir == "") { outputDir = "./"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd.
+            catchAllTest = path + "CatchAllcmdL.exe";
                #else
-                       catchAllCommandExe += "\"" + path + "CatchAllcmdW.exe\"" + " ";
                        if (outputDir == "") { outputDir = ".\\"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd.
+            catchAllTest = path + "CatchAllcmdW.exe";
                #endif
                
+        //test to make sure formatdb exists
+               ifstream in;
+               catchAllTest = m->getFullPathName(catchAllTest);
+               int ableToOpen = m->openInputFile(catchAllTest, in, "no error"); in.close();
+               if(ableToOpen == 1) {   
+            m->mothurOut(catchAllTest + " file does not exist. Checking path... \n");
+            //check to see if uchime is in the path??
+            
+            string programName = "CatchAllcmdW.exe";
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+            programName = "CatchAllcmdL.exe";
+#endif
+            string cLocation = m->findProgramPath(programName);
+            
+            ifstream in2;
+            ableToOpen = m->openInputFile(cLocation, in2, "no error"); in2.close();
+
+            if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + cLocation + " file does not exist. mothur requires the catchall executable."); m->mothurOutEndLine();  return 0; } 
+            else {  m->mothurOut("Found catchall in your path, using " + cLocation + "\n"); catchAllTest = cLocation; }
+        }
+        catchAllTest = m->getFullPathName(catchAllTest);
+        
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+        catchAllCommandExe += "mono " + catchAllTest + " ";
+#else
+        catchAllCommandExe += "\"" + catchAllTest + "\" ";
+#endif
+        
                //prepare full output directory
                outputDir = m->getFullPathName(outputDir);
                
+        if (m->debug) { m->mothurOut("[DEBUG]: catchall location = " + catchAllCommandExe + "\n[DEBUG]: outputDir = " + outputDir + "\n"); }
+        
                vector<string> inputFileNames;
                if (sharedfile != "") { inputFileNames = parseSharedFile(sharedfile);   }
                else {  inputFileNames.push_back(sabundfile);  }                
@@ -194,8 +229,8 @@ int CatchAllCommand::execute() {
                                m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
                        }
                        
-                       InputData* input = new InputData(inputFileNames[p], "sabund");
-                       SAbundVector* sabund = input->getSAbundVector();
+                       InputData input(inputFileNames[p], "sabund");
+                       SAbundVector* sabund = input.getSAbundVector();
                        string lastLabel = sabund->getLabel();
                                                        
                        set<string> processedLabels;
@@ -204,14 +239,18 @@ int CatchAllCommand::execute() {
                        string summaryfilename = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "catchall.summary";
                        summaryfilename = m->getFullPathName(summaryfilename);
                        
+            if (m->debug) { m->mothurOut("[DEBUG]: Input File = " + inputFileNames[p] + ".\n[DEBUG]: inputdata address = " + toString(&input) + ".\n[DEBUG]: sabund address = " + toString(&sabund) + ".\n"); } 
+            
                        ofstream out;
                        m->openOutputFile(summaryfilename, out);        
                        
                        out << "label\tmodel\testimate\tlci\tuci" << endl;
+            
+            if (m->debug) { string open = "no"; if (out.is_open()) { open = "yes"; } m->mothurOut("[DEBUG]: output stream is open = " + open + ".\n"); }
                        
                        //for each label the user selected
                        while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
-
+                               
                                                
                                if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
                                                m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
@@ -219,10 +258,10 @@ int CatchAllCommand::execute() {
                                                //create catchall input file from mothur's inputfile
                                                string filename = process(sabund, inputFileNames[p]);
                                                string outputPath = m->getPathName(filename);
-                                       
+                                                                                       
                                                //create system command
                                                string catchAllCommand = "";
-                                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                                                        catchAllCommand += catchAllCommandExe + filename + " " + outputPath + " 1";
                                                #else
                                                        if (outputPath.length() > 0) { outputPath = outputPath.substr(0, outputPath.length()-1); }
@@ -230,11 +269,16 @@ int CatchAllCommand::execute() {
                                                        //wrap entire string in ""
                                                        catchAllCommand = "\"" + catchAllCommand + "\"";
                                                #endif
-                                       
+                        
+                        if (m->debug) {  m->mothurOut("[DEBUG]: catchall command = " + catchAllCommand + ". About to call system.\n"); }
+                    
                                                //run catchall
                                                system(catchAllCommand.c_str());
-                                       
-                                               remove(filename.c_str());
+                    
+                        if (m->debug) { m->mothurOut("[DEBUG]: back from system call. Keeping file: " + filename + ".\n"); }
+                    
+                        if (!m->debug) { m->mothurRemove(filename); }
+
                                        
                                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
                                                if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
@@ -243,10 +287,14 @@ int CatchAllCommand::execute() {
                                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
                                                outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
                                                outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
-                                       
+                        
+                        if (m->debug) { m->mothurOut("[DEBUG]: About to create summary file for: " + filename + ".\n[DEBUG]: sabund label = " + sabund->getLabel() + ".\n"); }
+                    
                                                createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
+                    
+                        if (m->debug) { m->mothurOut("[DEBUG]: Done creating summary file.\n"); }
                                                                                        
-                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str());    }  delete input;  delete sabund;  return 0; }
+                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);   }  delete sabund;  return 0; }
 
                                                processedLabels.insert(sabund->getLabel());
                                                userLabels.erase(sabund->getLabel());
@@ -256,7 +304,7 @@ int CatchAllCommand::execute() {
                                                string saveLabel = sabund->getLabel();
                                                
                                                delete sabund;          
-                                               sabund = (input->getSAbundVector(lastLabel));
+                                               sabund = (input.getSAbundVector(lastLabel));
                                                
                                                m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
                                                
@@ -264,21 +312,26 @@ int CatchAllCommand::execute() {
                                                //create catchall input file from mothur's inputfile
                                                string filename = process(sabund, inputFileNames[p]);
                                                string outputPath = m->getPathName(filename);
-                                               
+                                                                                       
                                                //create system command
                                                string catchAllCommand = "";
-                                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                                                        catchAllCommand += catchAllCommandExe + filename + " " + outputPath + " 1";
                                                #else
                                                        if (outputPath.length() > 0) { outputPath = outputPath.substr(0, outputPath.length()-1); }
                                                        catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + "\" 1";
                                                        catchAllCommand = "\"" + catchAllCommand + "\"";
                                                #endif
-
+                        
+                        if (m->debug) {  m->mothurOut("[DEBUG]: catchall command = " + catchAllCommand + ". About to call system.\n"); }
+                    
                                                //run catchall
                                                system(catchAllCommand.c_str());
-                                       
-                                               remove(filename.c_str());
+                    
+                        if (m->debug) { m->mothurOut("[DEBUG]: back from system call. Keeping file: " + filename + ".\n"); }
+                    
+                        if (!m->debug) { m->mothurRemove(filename); }
+
                                        
                                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
                                                if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
@@ -287,10 +340,15 @@ int CatchAllCommand::execute() {
                                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
                                                outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
                                                outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
-                                       
+                        
+                    
+                        if (m->debug) { m->mothurOut("[DEBUG]: About to create summary file for: " + filename + ".\n[DEBUG]: sabund label = " + sabund->getLabel() + ".\n"); }
+                    
                                                createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
+                    
+                        if (m->debug) { m->mothurOut("[DEBUG]: Done creating summary file.\n"); }
                                        
-                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str());    }  delete input;  delete sabund;  return 0; }
+                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);   }   delete sabund;  return 0; }
 
                                                processedLabels.insert(sabund->getLabel());
                                                userLabels.erase(sabund->getLabel());
@@ -303,7 +361,7 @@ int CatchAllCommand::execute() {
                                lastLabel = sabund->getLabel(); 
                                
                                delete sabund;          
-                               sabund = (input->getSAbundVector());
+                               sabund = (input.getSAbundVector());
                        }
                        
                        //output error messages about any remaining user labels
@@ -322,7 +380,7 @@ int CatchAllCommand::execute() {
                        //run last label if you need to
                        if (needToRun == true)  {
                                if (sabund != NULL) {   delete sabund;  }
-                               sabund = (input->getSAbundVector(lastLabel));
+                               sabund = (input.getSAbundVector(lastLabel));
                                
                                m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
                                
@@ -332,18 +390,22 @@ int CatchAllCommand::execute() {
                                
                                //create system command
                                string catchAllCommand = "";
-                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                                        catchAllCommand += catchAllCommandExe + filename + " " + outputPath + " 1";
                                #else
                                        if (outputPath.length() > 0) { outputPath = outputPath.substr(0, outputPath.length()-1); }
                                        catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + "\" 1";
                                        catchAllCommand = "\"" + catchAllCommand + "\"";
                                #endif
+                
+                if (m->debug) {  m->mothurOut("[DEBUG]: catchall command = " + catchAllCommand + ". About to call system.\n"); }
                                
                                //run catchall
                                system(catchAllCommand.c_str());
+                
+                if (m->debug) { m->mothurOut("[DEBUG]: back from system call. Keeping file: " + filename + ".\n"); }
                                
-                               remove(filename.c_str());
+                               if (!m->debug) { m->mothurRemove(filename); }
                                
                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
                                if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
@@ -352,16 +414,18 @@ int CatchAllCommand::execute() {
                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
                                outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
                                outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");        
-                               
+                               if (m->debug) { m->mothurOut("[DEBUG]: About to create summary file for: " + filename + ".\n[DEBUG]: sabund label = " + sabund->getLabel() + ".\n"); }
+                
                                createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
+                
+                if (m->debug) { m->mothurOut("[DEBUG]: Done creating summary file.\n"); }
                                
                                delete sabund;
                        }
                        
                        out.close();
-                       delete input; 
                        
-                       if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } return 0; }
+                       if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);        } return 0; }
                                
                }
                
@@ -400,14 +464,19 @@ string CatchAllCommand::process(SAbundVector* sabund, string file1) {
                ofstream out;
                m->openOutputFile(filename, out);
                
+        if (m->debug) { m->mothurOut("[DEBUG]: Creating " + filename + " file for catchall, shown below.\n\n"); }
+        
                for (int i = 1; i <= sabund->getMaxRank(); i++) {
                        int temp = sabund->get(i);
                        
                        if (temp != 0) {
                                out << i << "," << temp << endl;
+                if (m->debug) { m->mothurOut(toString(i) + "," + toString(temp) + "\n"); }
                        }
                }
                out.close();
+        
+        if (m->debug) { m->mothurOut("[DEBUG]: Done creating " + filename + " file for catchall, shown above.\n\n"); }
                
                return filename;
        
@@ -469,7 +538,7 @@ string CatchAllCommand::combineSummmary(vector<string>& outputNames) {
                        numLines = thisFilesLines.size();
                        
                        temp.close();
-                       remove(outputNames[i].c_str());
+                       m->mothurRemove(outputNames[i]);
                }
                
                //for each label
@@ -570,14 +639,14 @@ vector<string> CatchAllCommand::parseSharedFile(string filename) {
                vector<string> filenames;
                
                //read first line
-               InputData* input = new InputData(filename, "sharedfile");
-               vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
+               InputData input(filename, "sharedfile");
+               vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
                
                string sharedFileRoot = outputDir + m->getRootName(m->getSimpleName(filename));
                
                //clears file before we start to write to it below
                for (int i=0; i<lookup.size(); i++) {
-                       remove((sharedFileRoot + lookup[i]->getGroup() + ".sabund").c_str());
+                       m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
                        filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
                        groups.push_back(lookup[i]->getGroup());
                }
@@ -593,11 +662,9 @@ vector<string> CatchAllCommand::parseSharedFile(string filename) {
                        }
                        
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
-                       lookup = input->getSharedRAbundVectors();
+                       lookup = input.getSharedRAbundVectors();
                }
                
-               delete input;
-               
                return filenames;
        }
        catch(exception& e) {