]> git.donarmstrong.com Git - mothur.git/blobdiff - chimeraccodecommand.cpp
added [ERROR] flag if command aborts
[mothur.git] / chimeraccodecommand.cpp
index 753bc9ad9f43dc1008689ac877aa5c16ca11d0a5..8a4ccdedea59eb676ecf4f2161e9c1719db8f72a 100644 (file)
 #include "chimeraccodecommand.h"
 #include "ccode.h"
 
+//**********************************************************************************************************************
+vector<string> ChimeraCcodeCommand::getValidParameters(){      
+       try {
+               string AlignArray[] =  {"fasta", "filter", "processors", "window", "template", "mask", "numwanted", "outputdir","inputdir" };
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChimeraCcodeCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+ChimeraCcodeCommand::ChimeraCcodeCommand(){    
+       try {
+               abort = true; calledHelp = true;
+               vector<string> tempOutNames;
+               outputTypes["chimera"] = tempOutNames;
+               outputTypes["mapinfo"] = tempOutNames;
+               outputTypes["accnos"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChimeraCcodeCommand", "ChimeraCcodeCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> ChimeraCcodeCommand::getRequiredParameters(){   
+       try {
+               string AlignArray[] =  {"template","fasta"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChimeraCcodeCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> ChimeraCcodeCommand::getRequiredFiles(){        
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChimeraCcodeCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //***************************************************************************************************************
-
 ChimeraCcodeCommand::ChimeraCcodeCommand(string option)  {
        try {
-               abort = false;
+               abort = false; calledHelp = false;   
                
                //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
-                       string Array[] =  {"fasta", "filter", "processors", "window", "template", "mask", "numwanted", "outputdir","inputdir", };
+                       string Array[] =  {"fasta", "filter", "processors", "window", "template", "mask", "numwanted", "outputdir","inputdir" };
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -35,6 +83,11 @@ ChimeraCcodeCommand::ChimeraCcodeCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       vector<string> tempOutNames;
+                       outputTypes["chimera"] = tempOutNames;
+                       outputTypes["mapinfo"] = tempOutNames;
+                       outputTypes["accnos"] = tempOutNames;
+                       
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
                        if (inputDir == "not found"){   inputDir = "";          }
@@ -73,10 +126,25 @@ ChimeraCcodeCommand::ChimeraCcodeCommand(string option)  {
                                                if (m->getDefaultPath() != "") { //default path is set
                                                        string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
                                                        m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
-                                                       ableToOpen = m->openInputFile(tryPath, in, "noerror");
+                                                       ifstream in2;
+                                                       ableToOpen = m->openInputFile(tryPath, in2, "noerror");
+                                                       in2.close();
                                                        fastaFileNames[i] = tryPath;
                                                }
                                        }
+                                       
+                                       //if you can't open it, try default location
+                                       if (ableToOpen == 1) {
+                                               if (m->getOutputDir() != "") { //default path is set
+                                                       string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
+                                                       m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
+                                                       ifstream in2;
+                                                       ableToOpen = m->openInputFile(tryPath, in2, "noerror");
+                                                       in2.close();
+                                                       fastaFileNames[i] = tryPath;
+                                               }
+                                       }
+                                       
                                        in.close();
                                        
                                        if (ableToOpen == 1) { 
@@ -92,10 +160,7 @@ ChimeraCcodeCommand::ChimeraCcodeCommand(string option)  {
                        }
                        
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
-                       outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
-                               outputDir = ""; 
-                               outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
-                       }
+                       outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
 
                        templatefile = validParameter.validFile(parameters, "template", true);
                        if (templatefile == "not open") { abort = true; }
@@ -175,7 +240,7 @@ ChimeraCcodeCommand::~ChimeraCcodeCommand(){        /*      do nothing      */      }
 int ChimeraCcodeCommand::execute(){
        try{
                
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                for (int s = 0; s < fastaFileNames.size(); s++) {
                                
@@ -192,6 +257,7 @@ int ChimeraCcodeCommand::execute(){
                        if (chimera->getUnaligned()) { m->mothurOut("Your template sequences are different lengths, please correct."); m->mothurOutEndLine(); delete chimera; return 0; }
                        templateSeqsLength = chimera->getLength();
                        
+                       if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[s]);  }//if user entered a file with a path then preserve it
                        string outputFileName, accnosFileName;
                        if (maskfile != "") {
                                outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + maskfile + ".ccode.chimeras";
@@ -203,11 +269,11 @@ int ChimeraCcodeCommand::execute(){
 
                        string mapInfo = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "mapinfo";
                        
-                       if (m->control_pressed) { delete chimera;  for (int j = 0; j < outputNames.size(); j++) {       remove(outputNames[j].c_str()); }  return 0;    }
+                       if (m->control_pressed) { delete chimera;  for (int j = 0; j < outputNames.size(); j++) {       remove(outputNames[j].c_str()); } outputTypes.clear(); return 0;        }
                        
                #ifdef USE_MPI
                
-                               int pid, end, numSeqsPerProcessor; 
+                               int pid, numSeqsPerProcessor; 
                                int tag = 2001;
                                vector<unsigned long int> MPIPos;
                                                                
@@ -235,7 +301,7 @@ int ChimeraCcodeCommand::execute(){
                                MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
                                MPI_File_open(MPI_COMM_WORLD, outAccnosFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
 
-                               if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);   MPI_File_close(&outMPIAccnos);  for (int j = 0; j < outputNames.size(); j++) {   remove(outputNames[j].c_str()); }  delete chimera; return 0;  }
+                               if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);   MPI_File_close(&outMPIAccnos);  for (int j = 0; j < outputNames.size(); j++) {   remove(outputNames[j].c_str()); } outputTypes.clear();  delete chimera; return 0;  }
                        
                                if (pid == 0) { //you are the root process 
                                        string outTemp = "For full window mapping info refer to " + mapInfo + "\n\n";
@@ -265,7 +331,7 @@ int ChimeraCcodeCommand::execute(){
                                        //align your part
                                        driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
                                        
-                                       if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);   MPI_File_close(&outMPIAccnos);  remove(outputFileName.c_str());  remove(accnosFileName.c_str());  for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }  delete chimera; return 0;  }
+                                       if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);   MPI_File_close(&outMPIAccnos);  remove(outputFileName.c_str());  remove(accnosFileName.c_str());  for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } outputTypes.clear();  delete chimera; return 0;  }
 
                                }else{ //you are a child process
                                        MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
@@ -281,7 +347,7 @@ int ChimeraCcodeCommand::execute(){
                                        //align your part
                                        driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, outMPIAccnos, MPIPos);
                                        
-                                       if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);   MPI_File_close(&outMPIAccnos);  for (int j = 0; j < outputNames.size(); j++) {   remove(outputNames[j].c_str()); }  delete chimera; return 0;  }
+                                       if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);   MPI_File_close(&outMPIAccnos);  for (int j = 0; j < outputNames.size(); j++) {   remove(outputNames[j].c_str()); }  outputTypes.clear(); delete chimera; return 0;  }
                                }
                                
                                //close files 
@@ -312,7 +378,7 @@ int ChimeraCcodeCommand::execute(){
                                                                                
                                        numSeqs = driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
                                        
-                                       if (m->control_pressed) { remove(outputFileName.c_str()); remove(tempHeader.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear(); delete chimera; return 0; }
+                                       if (m->control_pressed) { remove(outputFileName.c_str()); remove(tempHeader.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) {  delete lines[i];  } outputTypes.clear();  lines.clear(); delete chimera; return 0; }
                                        
                                }else{
                                        processIDS.resize(0);
@@ -337,7 +403,7 @@ int ChimeraCcodeCommand::execute(){
                                        if (m->control_pressed) { 
                                                remove(outputFileName.c_str()); 
                                                remove(accnosFileName.c_str());
-                                               for (int j = 0; j < outputNames.size(); j++) {  remove(outputNames[j].c_str()); } 
+                                               for (int j = 0; j < outputNames.size(); j++) {  remove(outputNames[j].c_str()); } outputTypes.clear();
                                                for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear();
                                                delete chimera;
                                                return 0;
@@ -348,7 +414,7 @@ int ChimeraCcodeCommand::execute(){
                        #else
                                numSeqs = driver(lines[0], outputFileName, fastaFileNames[s], accnosFileName);
                                
-                               if (m->control_pressed) { remove(outputFileName.c_str()); remove(tempHeader.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear(); delete chimera; return 0; }
+                               if (m->control_pressed) { remove(outputFileName.c_str()); remove(tempHeader.c_str()); remove(accnosFileName.c_str()); for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); } for (int i = 0; i < lines.size(); i++) {  delete lines[i];  } outputTypes.clear();  lines.clear(); delete chimera; return 0; }
                                
                        #endif
        
@@ -360,10 +426,10 @@ int ChimeraCcodeCommand::execute(){
                
                        delete chimera;
                        
-                       outputNames.push_back(outputFileName);
-                       outputNames.push_back(mapInfo);
-                       outputNames.push_back(accnosFileName); 
-                       
+                       outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
+                       outputNames.push_back(mapInfo); outputTypes["mapinfo"].push_back(mapInfo);
+                       outputNames.push_back(accnosFileName); outputTypes["accnos"].push_back(accnosFileName);
+                        
                        for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear();
                        
                        m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
@@ -485,7 +551,7 @@ int ChimeraCcodeCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File
                                        if (m->control_pressed) {       delete candidateSeq; return 1;  }
                
                                        //print results
-                                       bool isChimeric = chimera->print(outMPI, outAccMPI);
+                                       chimera->print(outMPI, outAccMPI);
                                }
                        }
                        delete candidateSeq;
@@ -532,7 +598,11 @@ int ChimeraCcodeCommand::createProcesses(string outputFileName, string filename,
                                out.close();
 
                                exit(0);
-                       }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); 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);
+                       }
                }
                
                //force parent to wait until all the processes are done