]> git.donarmstrong.com Git - mothur.git/blobdiff - sffmultiplecommand.cpp
added code to format fast files for uchime. started work on sff.multiple command
[mothur.git] / sffmultiplecommand.cpp
diff --git a/sffmultiplecommand.cpp b/sffmultiplecommand.cpp
new file mode 100644 (file)
index 0000000..1675d24
--- /dev/null
@@ -0,0 +1,256 @@
+//
+//  sffmultiplecommand.cpp
+//  Mothur
+//
+//  Created by Sarah Westcott on 8/14/12.
+//  Copyright (c) 2012 Schloss Lab. All rights reserved.
+//
+
+#include "sffmultiplecommand.h"
+
+
+//**********************************************************************************************************************
+vector<string> SffMultipleCommand::setParameters(){    
+       try {           
+               CommandParameter pfile("file", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfile);
+        
+        //sffinfo
+               CommandParameter ptrim("trim", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(ptrim);
+        
+        //trim.flows
+               CommandParameter pmaxhomop("maxhomop", "Number", "", "9", "", "", "",false,false); parameters.push_back(pmaxhomop);
+               CommandParameter pmaxflows("maxflows", "Number", "", "450", "", "", "",false,false); parameters.push_back(pmaxflows);
+               CommandParameter pminflows("minflows", "Number", "", "450", "", "", "",false,false); parameters.push_back(pminflows);
+               CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ppdiffs);
+               CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pbdiffs);
+        CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pldiffs);
+               CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(psdiffs);
+        CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ptdiffs);
+               CommandParameter psignal("signal", "Number", "", "0.50", "", "", "",false,false); parameters.push_back(psignal);
+               CommandParameter pnoise("noise", "Number", "", "0.70", "", "", "",false,false); parameters.push_back(pnoise);
+               CommandParameter pallfiles("allfiles", "Boolean", "", "t", "", "", "",false,false); parameters.push_back(pallfiles);
+               CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder);
+
+        CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
+               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               
+               vector<string> myArray;
+               for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SffMultipleCommand", "setParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string SffMultipleCommand::getHelpString(){    
+       try {
+               string helpString = "";
+               helpString += "The sff.multiple command reads a file containing sff filenames and optional oligos filenames. It runs the files through sffinfo, trim.flows, shhh.flows and trim.seqs combining the results.\n";
+               helpString += "The sff.multiple command parameters are file, trim, maxhomop, maxflows, minflows, pdiffs, bdiffs, ldiffs, sdiffs, tdiffs, signal, noise, allfiles, order. file is required. \n";
+               helpString += "The file parameter allows you to enter the a file containing the list of sff files and optional oligos files.\n";
+        helpString += "The trim parameter allows you to indicate if you would like a sequences and quality scores generated by sffinfo trimmed to the clipQualLeft and clipQualRight values.  Default=True. \n";
+               helpString += "Example sff.multiple(file=mySffOligosFile.txt, trim=F).\n";
+               helpString += "Note: No spaces between parameter labels (i.e. file), '=' and parameters (i.e.mySffOligosFile.txt).\n";
+               return helpString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SffMultipleCommand", "getHelpString");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string SffMultipleCommand::getOutputFileNameTag(string type, string inputName=""){     
+       try {
+        string outputFileName = "";
+               map<string, vector<string> >::iterator it;
+        
+        //is this a type this command creates
+        it = outputTypes.find(type);
+        if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
+        else {
+            //if (type == "fasta")            {   outputFileName =  "fasta";   }
+            //else if (type == "flow")    {   outputFileName =  "flow";   }
+           // else if (type == "sfftxt")        {   outputFileName =  "sff.txt";   }
+            //else if (type == "qfile")       {   outputFileName =  "qual";   }
+            //else { 
+                m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  
+        //}
+        }
+        return outputFileName;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SffMultipleCommand", "getOutputFileNameTag");
+               exit(1);
+       }
+}
+
+
+//**********************************************************************************************************************
+SffMultipleCommand::SffMultipleCommand(){      
+       try {
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["flow"] = tempOutNames;
+               outputTypes["qfile"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SffMultipleCommand", "SffMultipleCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+
+SffMultipleCommand::SffMultipleCommand(string option)  {
+       try {
+               abort = false; calledHelp = false;   
+               
+               //allow user to run help
+               if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
+               
+               else {
+                       //valid paramters for this command
+                       vector<string> myArray = setParameters();
+                       
+                       OptionParser parser(option);
+                       map<string, string> parameters = parser.getParameters();
+                       
+                       ValidParameters validParameter;
+            map<string,string>::iterator it;
+            
+                       //check to make sure all parameters are valid for command
+                       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["flow"] = tempOutNames;
+                       outputTypes["qfile"] = 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 = "";         }
+                       
+                       //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 = "";          }
+                       else {
+                               string path;
+                it = parameters.find("file");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["file"] = inputDir + it->second;             }
+                               }
+                       }
+            
+                       filename = validParameter.validFile(parameters, "file", true);
+            if (filename == "not open") { filename = ""; abort = true; }
+            else if (filename == "not found") { filename = "";  }
+                       
+                       string temp;
+                       temp = validParameter.validFile(parameters, "trim", false);                                     if (temp == "not found"){       temp = "T";                             }
+                       trim = m->isTrue(temp); 
+               }
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SffMultipleCommand", "SffMultipleCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int SffMultipleCommand::execute(){
+       try {
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
+               
+               vector<string> sffFiles, oligosFiles;
+        readFile(sffFiles, oligosFiles);
+        
+        if (m->control_pressed) { return 0; }
+        
+               
+               if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);        } return 0; }
+               
+               //set fasta file as new current fastafile
+               string current = "";
+               itTypes = outputTypes.find("fasta");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+               }
+               
+               itTypes = outputTypes.find("qfile");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
+               }
+               
+               itTypes = outputTypes.find("flow");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
+               }
+               
+               //report output filenames
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
+               m->mothurOutEndLine();
+        
+               return 0;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SffMultipleCommand", "execute");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int SffMultipleCommand::readFile(vector<string>& sffFiles, vector<string>& oligosFiles){
+       try {
+        
+        ifstream in;
+        m->openInputFile(filename, in);
+        
+        string oligos, sff;
+        while (!in.eof()) {
+            
+            if (m->control_pressed) { break; }
+            
+            in >> sff;
+            
+            //ignore file pairing
+            if(sff[0] == '#'){ while (!in.eof())       {       char c = in.get();  if (c == 10 || c == 13){    break;  }       } m->gobble(in); }
+            else { //check for oligos file
+                oligos = "";
+            
+                // get rest of line in case there is a oligos filename
+                while (!in.eof())      {       
+                    char c = in.get(); 
+                    if (c == 10 || c == 13){   break;  }
+                    else if (c == 32 || c == 9){;} //space or tab
+                    else {     oligos += c;  }
+                } 
+            }
+            m->gobble(in);
+            
+            sffFiles.push_back(sff);
+            oligosFiles.push_back(oligos); //will push a blank if there is not an oligos for this sff file
+        }
+        in.close();
+        
+        return 0;
+    }
+       catch(exception& e) {
+               m->errorOut(e, "SffMultipleCommand", "readFile");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+
+
+
+