]> git.donarmstrong.com Git - mothur.git/blobdiff - aligncommand.cpp
adding current file class
[mothur.git] / aligncommand.cpp
index ccf5eebe68f42426f16f2465b68832beb413d2eb..cfedd3b3ca44effd5d1029936c8636db63d568bd 100644 (file)
 
 
 //**********************************************************************************************************************
-
+vector<string> AlignCommand::getValidParameters(){     
+       try {
+               string AlignArray[] =  {"template","candidate","search","ksize","align","match","mismatch","gapopen","gapextend", "processors","flip","threshold","outputdir","inputdir"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "AlignCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> AlignCommand::getRequiredParameters(){  
+       try {
+               string AlignArray[] =  {"template","candidate"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "AlignCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> AlignCommand::getRequiredFiles(){       
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "AlignCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+AlignCommand::AlignCommand(){  
+       try {
+               abort = true; calledHelp = true; 
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["alignreport"] = tempOutNames;
+               outputTypes["accnos"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "AlignCommand", "AlignCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
 AlignCommand::AlignCommand(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 {
                        
@@ -52,7 +99,13 @@ AlignCommand::AlignCommand(string option)  {
                        for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
-
+                       
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       outputTypes["alignreport"] = tempOutNames;
+                       outputTypes["accnos"] = tempOutNames;
+                       
                        //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 = "";         }
                        
@@ -84,8 +137,11 @@ AlignCommand::AlignCommand(string option)  {
                        }else if (templateFileName == "not open") { abort = true; }     
                        
                        candidateFileName = validParameter.validFile(parameters, "candidate", false);
-                       if (candidateFileName == "not found") { m->mothurOut("candidate is a required parameter for the align.seqs command."); m->mothurOutEndLine(); abort = true;  }
-                       else { 
+                       if (candidateFileName == "not found") { 
+                               //check currentFiles for a fasta file
+                               if (currentFiles->getFastaFile() != "") {  candidateFileName = currentFiles->getFastaFile(); m->mothurOut("Using " + candidateFileName + " as candidate file."); m->mothurOutEndLine();
+                               }else { m->mothurOut("candidate is a required parameter for the align.seqs command."); m->mothurOutEndLine(); abort = true;  }
+                       }else { 
                                m->splitAtDash(candidateFileName, candidateFileNames);
                                
                                //go through files and make sure they are good, if not, then disregard them
@@ -100,19 +156,34 @@ AlignCommand::AlignCommand(string option)  {
        
                                        int ableToOpen;
                                        ifstream in;
-
                                        ableToOpen = m->openInputFile(candidateFileNames[i], in, "noerror");
-                               
+                                       in.close();     
+                                       
                                        //if you can't open it, try default location
                                        if (ableToOpen == 1) {
                                                if (m->getDefaultPath() != "") { //default path is set
                                                        string tryPath = m->getDefaultPath() + m->getSimpleName(candidateFileNames[i]);
                                                        m->mothurOut("Unable to open " + candidateFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
-                                                       ableToOpen = m->openInputFile(tryPath, in, "noerror");
+                                                       ifstream in2;
+                                                       ableToOpen = m->openInputFile(tryPath, in2, "noerror");
+                                                       in2.close();
+                                                       candidateFileNames[i] = tryPath;
+                                               }
+                                       }
+                                       
+                                       //if you can't open it, try output location
+                                       if (ableToOpen == 1) {
+                                               if (m->getOutputDir() != "") { //default path is set
+                                                       string tryPath = m->getOutputDir() + m->getSimpleName(candidateFileNames[i]);
+                                                       m->mothurOut("Unable to open " + candidateFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
+                                                       ifstream in2;
+                                                       ableToOpen = m->openInputFile(tryPath, in2, "noerror");
+                                                       in2.close();
                                                        candidateFileNames[i] = tryPath;
                                                }
                                        }
-                                       in.close();                                     
+                                       
+                                                                       
 
                                        if (ableToOpen == 1) { 
                                                m->mothurOut("Unable to open " + candidateFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
@@ -165,7 +236,6 @@ AlignCommand::AlignCommand(string option)  {
                exit(1);
        }
 }
-
 //**********************************************************************************************************************
 
 AlignCommand::~AlignCommand(){ 
@@ -211,7 +281,7 @@ void AlignCommand::help(){
 
 int AlignCommand::execute(){
        try {
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
 
                templateDB = new AlignmentDB(templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch);
                int longestBase = templateDB->getLongestBase();
@@ -225,10 +295,9 @@ int AlignCommand::execute(){
                        m->mothurOutEndLine();
                        alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
                }
-               vector<string> outputNames;
                
                for (int s = 0; s < candidateFileNames.size(); s++) {
-                       if (m->control_pressed) { return 0; }
+                       if (m->control_pressed) { outputTypes.clear(); return 0; }
                        
                        m->mothurOut("Aligning sequences from " + candidateFileNames[s] + " ..." ); m->mothurOutEndLine();
                        
@@ -243,7 +312,7 @@ int AlignCommand::execute(){
                        int start = time(NULL);
                
 #ifdef USE_MPI 
-                               int pid, end, numSeqsPerProcessor; 
+                               int pid, numSeqsPerProcessor; 
                                int tag = 2001;
                                vector<unsigned long int> MPIPos;
                                MPIWroteAccnos = false;
@@ -277,7 +346,7 @@ int AlignCommand::execute(){
                                MPI_File_open(MPI_COMM_WORLD, outReportFilename, outMode, MPI_INFO_NULL, &outMPIReport);
                                MPI_File_open(MPI_COMM_WORLD, outAccnosFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
                                
-                               if (m->control_pressed) { MPI_File_close(&inMPI);  MPI_File_close(&outMPIAlign);  MPI_File_close(&outMPIReport);  MPI_File_close(&outMPIAccnos); return 0; }
+                               if (m->control_pressed) { MPI_File_close(&inMPI);  MPI_File_close(&outMPIAlign);  MPI_File_close(&outMPIReport);  MPI_File_close(&outMPIAccnos); outputTypes.clear(); return 0; }
                                
                                if (pid == 0) { //you are the root process 
                                        
@@ -297,7 +366,7 @@ int AlignCommand::execute(){
                                        //align your part
                                        driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIAlign, outMPIReport, outMPIAccnos, MPIPos);
                                        
-                                       if (m->control_pressed) { MPI_File_close(&inMPI);  MPI_File_close(&outMPIAlign);  MPI_File_close(&outMPIReport);  MPI_File_close(&outMPIAccnos); return 0; }
+                                       if (m->control_pressed) { MPI_File_close(&inMPI);  MPI_File_close(&outMPIAlign);  MPI_File_close(&outMPIReport);  MPI_File_close(&outMPIAccnos); outputTypes.clear(); return 0; }
 
                                        for (int i = 1; i < processors; i++) {
                                                bool tempResult;
@@ -319,7 +388,7 @@ int AlignCommand::execute(){
                                        //align your part
                                        driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIAlign, outMPIReport, outMPIAccnos, MPIPos);
                                        
-                                       if (m->control_pressed) { MPI_File_close(&inMPI);  MPI_File_close(&outMPIAlign);  MPI_File_close(&outMPIReport);  MPI_File_close(&outMPIAccnos); return 0; }
+                                       if (m->control_pressed) { MPI_File_close(&inMPI);  MPI_File_close(&outMPIAlign);  MPI_File_close(&outMPIReport);  MPI_File_close(&outMPIAccnos); outputTypes.clear(); return 0; }
 
                                        MPI_Send(&MPIWroteAccnos, 1, MPI_INT, 0, tag, MPI_COMM_WORLD); 
                                }
@@ -356,64 +425,14 @@ int AlignCommand::execute(){
        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                        if(processors == 1){
                                numFastaSeqs = driver(lines[0], alignFileName, reportFileName, accnosFileName, candidateFileNames[s]);
-                               
-                               if (m->control_pressed) { remove(accnosFileName.c_str()); remove(alignFileName.c_str()); remove(reportFileName.c_str()); return 0; }
-                               
-                               //delete accnos file if its blank else report to user
-                               if (m->isBlank(accnosFileName)) {  remove(accnosFileName.c_str());  hasAccnos = false; }
-                               else { 
-                                       m->mothurOut("Some of you sequences generated alignments that eliminated too many bases, a list is provided in " + accnosFileName + ".");
-                                       if (!flip) {
-                                               m->mothurOut(" If you set the flip parameter to true mothur will try aligning the reverse compliment as well."); 
-                                       }else{  m->mothurOut(" If the reverse compliment proved to be better it was reported.");  }
-                                       m->mothurOutEndLine();
-                               }
                        }else{
-                               processIDS.resize(0);
-                               
                                numFastaSeqs = createProcesses(alignFileName, reportFileName, accnosFileName, candidateFileNames[s]); 
-                               
-                               rename((alignFileName + toString(processIDS[0]) + ".temp").c_str(), alignFileName.c_str());
-                               rename((reportFileName + toString(processIDS[0]) + ".temp").c_str(), reportFileName.c_str());
-                               
-                               //append alignment and report files
-                               for(int i=1;i<processors;i++){
-                                       appendAlignFiles((alignFileName + toString(processIDS[i]) + ".temp"), alignFileName);
-                                       remove((alignFileName + toString(processIDS[i]) + ".temp").c_str());
-                                       
-                                       appendReportFiles((reportFileName + toString(processIDS[i]) + ".temp"), reportFileName);
-                                       remove((reportFileName + toString(processIDS[i]) + ".temp").c_str());
-                               }
-                               
-                               vector<string> nonBlankAccnosFiles;
-                               //delete blank accnos files generated with multiple processes
-                               for(int i=0;i<processors;i++){  
-                                       if (!(m->isBlank(accnosFileName + toString(processIDS[i]) + ".temp"))) {
-                                               nonBlankAccnosFiles.push_back(accnosFileName + toString(processIDS[i]) + ".temp");
-                                       }else { remove((accnosFileName + toString(processIDS[i]) + ".temp").c_str());  }
-                               }
-                               
-                               //append accnos files
-                               if (nonBlankAccnosFiles.size() != 0) { 
-                                       rename(nonBlankAccnosFiles[0].c_str(), accnosFileName.c_str());
-                                       
-                                       for (int h=1; h < nonBlankAccnosFiles.size(); h++) {
-                                               appendAlignFiles(nonBlankAccnosFiles[h], accnosFileName);
-                                               remove(nonBlankAccnosFiles[h].c_str());
-                                       }
-                                       m->mothurOut("Some of you sequences generated alignments that eliminated too many bases, a list is provided in " + accnosFileName + ".");
-                                       if (!flip) {
-                                               m->mothurOut(" If you set the flip parameter to true mothur will try aligning the reverse compliment as well."); 
-                                       }else{  m->mothurOut(" If the reverse compliment proved to be better it was reported.");  }
-                                       m->mothurOutEndLine();
-                               }else{ hasAccnos = false;  }
-                               
-                               if (m->control_pressed) { remove(accnosFileName.c_str()); remove(alignFileName.c_str()); remove(reportFileName.c_str()); return 0; }
                        }
        #else
                        numFastaSeqs = driver(lines[0], alignFileName, reportFileName, accnosFileName, candidateFileNames[s]);
+       #endif
                        
-                       if (m->control_pressed) { remove(accnosFileName.c_str()); remove(alignFileName.c_str()); remove(reportFileName.c_str()); return 0; }
+                       if (m->control_pressed) { remove(accnosFileName.c_str()); remove(alignFileName.c_str()); remove(reportFileName.c_str()); outputTypes.clear();  return 0; }
                        
                        //delete accnos file if its blank else report to user
                        if (m->isBlank(accnosFileName)) {  remove(accnosFileName.c_str());  hasAccnos = false; }
@@ -424,8 +443,6 @@ int AlignCommand::execute(){
                                }else{  m->mothurOut(" If the reverse compliment proved to be better it was reported.");  }
                                m->mothurOutEndLine();
                        }
-                       
-       #endif
 
 #endif         
 
@@ -436,9 +453,9 @@ int AlignCommand::execute(){
                        if (pid == 0) { //only one process should output to screen
                #endif
 
-                       outputNames.push_back(alignFileName);
-                       outputNames.push_back(reportFileName);
-                       if (hasAccnos)  {       outputNames.push_back(accnosFileName);          }
+                       outputNames.push_back(alignFileName); outputTypes["fasta"].push_back(alignFileName);
+                       outputNames.push_back(reportFileName); outputTypes["alignreport"].push_back(reportFileName);
+                       if (hasAccnos)  {       outputNames.push_back(accnosFileName);  outputTypes["accnos"].push_back(accnosFileName);  }
                        
                #ifdef USE_MPI
                        }
@@ -449,7 +466,14 @@ int AlignCommand::execute(){
                        m->mothurOutEndLine();
                }
                
-               
+               //set align file as new current fastafile
+               string currentFasta = "";
+               itTypes = outputTypes.find("fasta");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { currentFasta = (itTypes->second)[0]; }
+               }
+               currentFiles->setFastaFile(currentFasta);
+               cout << "current fasta = " << currentFiles->getFastaFile() << endl;
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -488,7 +512,8 @@ int AlignCommand::driver(linePair* filePos, string alignFName, string reportFNam
                        if (m->control_pressed) {  return 0; }
                        
                        Sequence* candidateSeq = new Sequence(inFASTA);  m->gobble(inFASTA);
-                       
+                       report.setCandidate(candidateSeq);
+
                        int origNumBases = candidateSeq->getNumBases();
                        string originalUnaligned = candidateSeq->getUnaligned();
                        int numBasesNeeded = origNumBases * threshold;
@@ -500,10 +525,11 @@ int AlignCommand::driver(linePair* filePos, string alignFName, string reportFNam
                                                                
                                Sequence temp = templateDB->findClosestSequence(candidateSeq);
                                Sequence* templateSeq = &temp;
-                               
+                       
                                float searchScore = templateDB->getSearchScore();
                                                                
                                Nast* nast = new Nast(alignment, candidateSeq, templateSeq);
+               
                                Sequence* copy;
                                
                                Nast* nast2;
@@ -518,6 +544,7 @@ int AlignCommand::driver(linePair* filePos, string alignFName, string reportFNam
                                        string wasBetter =  "";
                                        //if the user wants you to try the reverse
                                        if (flip) {
+                               
                                                //get reverse compliment
                                                copy = new Sequence(candidateSeq->getName(), originalUnaligned);
                                                copy->reverseComplement();
@@ -549,7 +576,6 @@ int AlignCommand::driver(linePair* filePos, string alignFName, string reportFNam
                                        accnosFile << candidateSeq->getName() << wasBetter << endl;
                                }
                                
-                               report.setCandidate(candidateSeq);
                                report.setTemplate(templateSeq);
                                report.setSearchParameters(search, searchScore);
                                report.setAlignmentParameters(align, alignment);
@@ -637,7 +663,8 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
                        istringstream iss (tempBuf,istringstream::in);
 
                        Sequence* candidateSeq = new Sequence(iss);  
-       
+                       report.setCandidate(candidateSeq);
+
                        int origNumBases = candidateSeq->getNumBases();
                        string originalUnaligned = candidateSeq->getUnaligned();
                        int numBasesNeeded = origNumBases * threshold;
@@ -686,6 +713,7 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
                                                        delete nast;
                                                        nast = nast2;
                                                        needToDeleteCopy = true;
+                                                       wasBetter = "\treverse complement produced a better alignment, so mothur used the reverse complement.";
                                                }else{  
                                                        wasBetter = "\treverse complement did NOT produce a better alignment, please check sequence.";
                                                        delete nast2;
@@ -707,7 +735,6 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
                                        MPIWroteAccnos = true;
                                }
                                
-                               report.setCandidate(candidateSeq);
                                report.setTemplate(templateSeq);
                                report.setSearchParameters(search, searchScore);
                                report.setAlignmentParameters(align, alignment);
@@ -758,7 +785,8 @@ int AlignCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& align
 int AlignCommand::createProcesses(string alignFileName, string reportFileName, string accnosFName, string filename) {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-               int process = 0;
+               processIDS.resize(0);
+               int process = 1;
                int num = 0;
                //              processIDS.resize(0);
                
@@ -780,21 +808,57 @@ int AlignCommand::createProcesses(string alignFileName, string reportFileName, s
                                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);
+                       }
                }
                
+               //do my part
+               num = driver(lines[0], alignFileName, reportFileName, accnosFName, filename);
+               
                //force parent to wait until all the processes are done
                for (int i=0;i<processors;i++) { 
                        int temp = processIDS[i];
                        wait(&temp);
                }
                
+               vector<string> nonBlankAccnosFiles;
+               if (!(m->isBlank(accnosFName))) { nonBlankAccnosFiles.push_back(accnosFName); }
+               else { remove(accnosFName.c_str()); } //remove so other files can be renamed to it
+                       
                for (int i = 0; i < processIDS.size(); i++) {
                        ifstream in;
                        string tempFile =  alignFileName + toString(processIDS[i]) + ".num.temp";
                        m->openInputFile(tempFile, in);
                        if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
                        in.close(); remove(tempFile.c_str());
+                       
+                       appendAlignFiles((alignFileName + toString(processIDS[i]) + ".temp"), alignFileName);
+                       remove((alignFileName + toString(processIDS[i]) + ".temp").c_str());
+                       
+                       appendReportFiles((reportFileName + toString(processIDS[i]) + ".temp"), reportFileName);
+                       remove((reportFileName + toString(processIDS[i]) + ".temp").c_str());
+                       
+                       if (!(m->isBlank(accnosFName + toString(processIDS[i]) + ".temp"))) {
+                               nonBlankAccnosFiles.push_back(accnosFName + toString(processIDS[i]) + ".temp");
+                       }else { remove((accnosFName + toString(processIDS[i]) + ".temp").c_str());  }
+                       
+               }
+               
+               //append accnos files
+               if (nonBlankAccnosFiles.size() != 0) { 
+                       rename(nonBlankAccnosFiles[0].c_str(), accnosFName.c_str());
+                       
+                       for (int h=1; h < nonBlankAccnosFiles.size(); h++) {
+                               appendAlignFiles(nonBlankAccnosFiles[h], accnosFName);
+                               remove(nonBlankAccnosFiles[h].c_str());
+                       }
+               }else { //recreate the accnosfile if needed
+                       ofstream out;
+                       m->openOutputFile(accnosFName, out);
+                       out.close();
                }
                
                return num;