]> git.donarmstrong.com Git - mothur.git/blobdiff - trimseqscommand.cpp
added logfile feature
[mothur.git] / trimseqscommand.cpp
index 132cc6b767956e2c5b522adb4facb9904fc30a78..ef0c06649af1afb64458bf153457bdadda566999 100644 (file)
@@ -37,7 +37,7 @@ TrimSeqsCommand::TrimSeqsCommand(string option){
                        
                        //check for required parameters
                        fastaFile = validParameter.validFile(parameters, "fasta", true);
-                       if (fastaFile == "not found") { cout << "fasta is a required parameter for the screen.seqs command." << endl; abort = true; }
+                       if (fastaFile == "not found") { mothurOut("fasta is a required parameter for the screen.seqs command."); mothurOutEndLine(); abort = true; }
                        else if (fastaFile == "not open") { abort = true; }     
                
                
@@ -78,56 +78,51 @@ TrimSeqsCommand::TrimSeqsCommand(string option){
                        
                        temp = validParameter.validFile(parameters, "allfiles", false);         if (temp == "not found") { temp = "F"; }
                        allFiles = isTrue(temp);
-
+                       
                        if(allFiles && oligoFile == ""){
-                               cout << "You selected allfiles, but didn't enter an oligos file.  Ignoring the allfiles request." << endl;
+                               mothurOut("You selected allfiles, but didn't enter an oligos file.  Ignoring the allfiles request."); mothurOutEndLine();
+                       }
+                       if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
+                               mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); mothurOutEndLine();
+                               qAverage=0;
+                               qThreshold=0;
                        }
-                       
                        if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){               
-                               cout << "You didn't set any options... quiting command." << endl;
+                               mothurOut("You didn't set any options... quiting command."); mothurOutEndLine();
                                abort = true;
                        }
-                       
                }
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function TrimSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TrimSeqsCommand class function TrimSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 //**********************************************************************************************************************
 
 void TrimSeqsCommand::help(){
        try {
-               cout << "The trim.seqs command reads a fastaFile and creates ....." << "\n";
-               cout << "The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength and maxlength." << "\n";
-               cout << "The fasta parameter is required." << "\n";
-               cout << "The flip parameter .... The default is 0." << "\n";
-               cout << "The oligos parameter .... The default is ""." << "\n";
-               cout << "The maxambig parameter .... The default is -1." << "\n";
-               cout << "The maxhomop parameter .... The default is 0." << "\n";
-               cout << "The minlength parameter .... The default is 0." << "\n";
-               cout << "The maxlength parameter .... The default is 0." << "\n";
-               cout << "The trim.seqs command should be in the following format: " << "\n";
-               cout << "trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig,  " << "\n";
-               cout << "maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength)  " << "\n";    
-               cout << "Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...)." << "\n";
-               cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta)." << "\n" << "\n";
+               mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
+               mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength and maxlength.\n");
+               mothurOut("The fasta parameter is required.\n");
+               mothurOut("The flip parameter .... The default is 0.\n");
+               mothurOut("The oligos parameter .... The default is "".\n");
+               mothurOut("The maxambig parameter .... The default is -1.\n");
+               mothurOut("The maxhomop parameter .... The default is 0.\n");
+               mothurOut("The minlength parameter .... The default is 0.\n");
+               mothurOut("The maxlength parameter .... The default is 0.\n");
+               mothurOut("The trim.seqs command should be in the following format: \n");
+               mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig,  \n");
+               mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength)  \n");       
+               mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
+               mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TrimSeqsCommand", "help");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TrimSeqsCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 
@@ -141,21 +136,21 @@ int TrimSeqsCommand::execute(){
        try{
        
                if (abort == true) { return 0; }
-       
-               vector<ofstream*> groupFileNames;
-               vector<ofstream*> fastaFileNames;
-               if(oligoFile != "")     {       getOligos(fastaFileNames, groupFileNames);      }
 
                ifstream inFASTA;
                openInputFile(fastaFile, inFASTA);
-
+               
                ofstream outFASTA;
                string trimSeqFile = getRootName(fastaFile) + "trim.fasta";
                openOutputFile(trimSeqFile, outFASTA);
                
                ofstream outGroups;
-               string groupFile = getRootName(fastaFile) + "groups"; 
-               openOutputFile(groupFile, outGroups);
+               vector<ofstream*> fastaFileNames;
+               if(oligoFile != ""){
+                       string groupFile = getRootName(fastaFile) + "groups"; 
+                       openOutputFile(groupFile, outGroups);
+                       getOligos(fastaFileNames);
+               }
                
                ofstream scrapFASTA;
                string scrapSeqFile = getRootName(fastaFile) + "scrap.fasta";
@@ -176,6 +171,7 @@ int TrimSeqsCommand::execute(){
                                if(qThreshold != 0)             {       success = stripQualThreshold(currSeq, qFile);   }
                                else if(qAverage != 0)  {       success = cullQualAverage(currSeq, qFile);              }
                                if(!success)                    {       trashCode += 'q';                                                               }
+                               qFile.close();
                        }
                        if(barcodes.size() != 0){
                                success = stripBarcode(currSeq, group);
@@ -210,7 +206,6 @@ int TrimSeqsCommand::execute(){
                                        outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
                                        
                                        if(allFiles){
-                                               *groupFileNames[group] << currSeq.getName() << '\t' << groupVector[group] << endl;                                      
                                                currSeq.printSequence(*fastaFileNames[group]);                                  
                                        }
                                }
@@ -227,29 +222,40 @@ int TrimSeqsCommand::execute(){
                scrapFASTA.close();
                outGroups.close();
                
-               for(int i=0;i<groupFileNames.size();i++){
-                       groupFileNames[i]->close();
-                       delete groupFileNames[i];
-
+               for(int i=0;i<fastaFileNames.size();i++){
                        fastaFileNames[i]->close();
                        delete fastaFileNames[i];
+               }               
+               
+               for(int i=0;i<fastaFileNames.size();i++){
+                       string seqName;
+                       openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
+                       ofstream outGroups;
+                       openOutputFile(getRootName(fastaFile) + groupVector[i] + ".groups", outGroups);
+                       
+                       while(!inFASTA.eof()){
+                               if(inFASTA.get() == '>'){
+                                       inFASTA >> seqName;
+                                       outGroups << seqName << '\t' << groupVector[i] << endl;
+                               }
+                               while (!inFASTA.eof())  {       char c = inFASTA.get(); if (c == 10 || c == 13){        break;  }       }
+                       }
+                       outGroups.close();
+                       inFASTA.close();
                }
                
+               
                return 0;               
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the TrimSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TrimSeqsCommand", "execute");
                exit(1);
        }
 }
 
 //***************************************************************************************************************
 
-void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec, vector<ofstream*>& outGroupsVec){
+void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec){
        
        ifstream inOligos;
        openInputFile(oligoFile, inOligos);
@@ -258,10 +264,10 @@ void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec, vector<ofstream*
        
        string type, oligo, group;
        int index=0;
-       
+
        while(!inOligos.eof()){
                inOligos >> type;
-               
+
                if(type[0] == '#'){
                        while (!inOligos.eof()) {       char c = inOligos.get(); if (c == 10 || c == 13){       break;  }       } // get rest of line if there's any crap there
                }
@@ -286,12 +292,13 @@ void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec, vector<ofstream*
                                        
                                if(allFiles){
                                        outFASTAVec.push_back(new ofstream((getRootName(fastaFile) + group + ".fasta").c_str(), ios::ate));
-                                       outGroupsVec.push_back(new ofstream((getRootName(fastaFile) + group + ".groups").c_str(), ios::ate));
                                }
                        }
                }
        }
        
+       inOligos.close();
+       
        numFPrimers = forPrimer.size();
        numRPrimers = revPrimer.size();
 }
@@ -457,7 +464,7 @@ bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
        string name;
        
        qFile >> name;
-       if(name.substr(1) != seq.getName())     {       cout << "sequence name mismatch btwn fasta and qual file" << endl;      }
+       if(name.substr(1) != seq.getName())     {       mothurOut("sequence name mismatch btwn fasta and qual file"); mothurOutEndLine();       }
        while (!qFile.eof())    {       char c = qFile.get(); if (c == 10 || c == 13){  break;  }       }
        
        int score;
@@ -490,7 +497,7 @@ bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
        string name;
        
        qFile >> name;
-       if(name.substr(1) != seq.getName())     {       cout << "sequence name mismatch btwn fasta and qual file" << endl;      }
+       if(name.substr(1) != seq.getName())     {       mothurOut("sequence name mismatch btwn fasta and qual file"); mothurOutEndLine();       }
        while (!qFile.eof())    {       char c = qFile.get(); if (c == 10 || c == 13){  break;  }       }
        
        float score;