]> git.donarmstrong.com Git - mothur.git/blob - sffmultiplecommand.cpp
1675d24a3ee8b9b80e6be67dd0b627846220aef1
[mothur.git] / sffmultiplecommand.cpp
1 //
2 //  sffmultiplecommand.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 8/14/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "sffmultiplecommand.h"
10
11
12 //**********************************************************************************************************************
13 vector<string> SffMultipleCommand::setParameters(){     
14         try {           
15                 CommandParameter pfile("file", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfile);
16         
17         //sffinfo
18                 CommandParameter ptrim("trim", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(ptrim);
19         
20         //trim.flows
21                 CommandParameter pmaxhomop("maxhomop", "Number", "", "9", "", "", "",false,false); parameters.push_back(pmaxhomop);
22                 CommandParameter pmaxflows("maxflows", "Number", "", "450", "", "", "",false,false); parameters.push_back(pmaxflows);
23                 CommandParameter pminflows("minflows", "Number", "", "450", "", "", "",false,false); parameters.push_back(pminflows);
24                 CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ppdiffs);
25                 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pbdiffs);
26         CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pldiffs);
27                 CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(psdiffs);
28         CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ptdiffs);
29                 CommandParameter psignal("signal", "Number", "", "0.50", "", "", "",false,false); parameters.push_back(psignal);
30                 CommandParameter pnoise("noise", "Number", "", "0.70", "", "", "",false,false); parameters.push_back(pnoise);
31                 CommandParameter pallfiles("allfiles", "Boolean", "", "t", "", "", "",false,false); parameters.push_back(pallfiles);
32                 CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder);
33
34         CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
35                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
36                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
37                 
38                 vector<string> myArray;
39                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "SffMultipleCommand", "setParameters");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 string SffMultipleCommand::getHelpString(){     
49         try {
50                 string helpString = "";
51                 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";
52                 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";
53                 helpString += "The file parameter allows you to enter the a file containing the list of sff files and optional oligos files.\n";
54         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";
55                 helpString += "Example sff.multiple(file=mySffOligosFile.txt, trim=F).\n";
56                 helpString += "Note: No spaces between parameter labels (i.e. file), '=' and parameters (i.e.mySffOligosFile.txt).\n";
57                 return helpString;
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "SffMultipleCommand", "getHelpString");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65 string SffMultipleCommand::getOutputFileNameTag(string type, string inputName=""){      
66         try {
67         string outputFileName = "";
68                 map<string, vector<string> >::iterator it;
69         
70         //is this a type this command creates
71         it = outputTypes.find(type);
72         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
73         else {
74             //if (type == "fasta")            {   outputFileName =  "fasta";   }
75             //else if (type == "flow")    {   outputFileName =  "flow";   }
76            // else if (type == "sfftxt")        {   outputFileName =  "sff.txt";   }
77             //else if (type == "qfile")       {   outputFileName =  "qual";   }
78             //else { 
79                 m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  
80         //}
81         }
82         return outputFileName;
83         }
84         catch(exception& e) {
85                 m->errorOut(e, "SffMultipleCommand", "getOutputFileNameTag");
86                 exit(1);
87         }
88 }
89
90
91 //**********************************************************************************************************************
92 SffMultipleCommand::SffMultipleCommand(){       
93         try {
94                 abort = true; calledHelp = true; 
95                 setParameters();
96                 vector<string> tempOutNames;
97                 outputTypes["fasta"] = tempOutNames;
98                 outputTypes["flow"] = tempOutNames;
99                 outputTypes["qfile"] = tempOutNames;
100         }
101         catch(exception& e) {
102                 m->errorOut(e, "SffMultipleCommand", "SffMultipleCommand");
103                 exit(1);
104         }
105 }
106 //**********************************************************************************************************************
107
108 SffMultipleCommand::SffMultipleCommand(string option)  {
109         try {
110                 abort = false; calledHelp = false;   
111                 
112                 //allow user to run help
113                 if(option == "help") { help(); abort = true; calledHelp = true; }
114                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
115                 
116                 else {
117                         //valid paramters for this command
118                         vector<string> myArray = setParameters();
119                         
120                         OptionParser parser(option);
121                         map<string, string> parameters = parser.getParameters();
122                         
123                         ValidParameters validParameter;
124             map<string,string>::iterator it;
125             
126                         //check to make sure all parameters are valid for command
127                         for (it = parameters.begin(); it != parameters.end(); it++) { 
128                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
129                         }
130                         
131                         //initialize outputTypes
132                         vector<string> tempOutNames;
133                         outputTypes["fasta"] = tempOutNames;
134                         outputTypes["flow"] = tempOutNames;
135                         outputTypes["qfile"] = tempOutNames;
136                         
137                         //if the user changes the output directory command factory will send this info to us in the output parameter 
138                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
139                         
140                         //if the user changes the input directory command factory will send this info to us in the output parameter 
141                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
142                         if (inputDir == "not found"){   inputDir = "";          }
143                         else {
144                                 string path;
145                 it = parameters.find("file");
146                                 //user has given a template file
147                                 if(it != parameters.end()){ 
148                                         path = m->hasPath(it->second);
149                                         //if the user has not given a path then, add inputdir. else leave path alone.
150                                         if (path == "") {       parameters["file"] = inputDir + it->second;             }
151                                 }
152                         }
153             
154                         filename = validParameter.validFile(parameters, "file", true);
155             if (filename == "not open") { filename = ""; abort = true; }
156             else if (filename == "not found") { filename = "";  }
157                         
158                         string temp;
159                         temp = validParameter.validFile(parameters, "trim", false);                                     if (temp == "not found"){       temp = "T";                             }
160                         trim = m->isTrue(temp); 
161                 }
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "SffMultipleCommand", "SffMultipleCommand");
165                 exit(1);
166         }
167 }
168 //**********************************************************************************************************************
169 int SffMultipleCommand::execute(){
170         try {
171                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
172                 
173                 vector<string> sffFiles, oligosFiles;
174         readFile(sffFiles, oligosFiles);
175         
176         if (m->control_pressed) { return 0; }
177         
178                 
179                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);        } return 0; }
180                 
181                 //set fasta file as new current fastafile
182                 string current = "";
183                 itTypes = outputTypes.find("fasta");
184                 if (itTypes != outputTypes.end()) {
185                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
186                 }
187                 
188                 itTypes = outputTypes.find("qfile");
189                 if (itTypes != outputTypes.end()) {
190                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
191                 }
192                 
193                 itTypes = outputTypes.find("flow");
194                 if (itTypes != outputTypes.end()) {
195                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
196                 }
197                 
198                 //report output filenames
199                 m->mothurOutEndLine();
200                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
201                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
202                 m->mothurOutEndLine();
203         
204                 return 0;
205         }
206         catch(exception& e) {
207                 m->errorOut(e, "SffMultipleCommand", "execute");
208                 exit(1);
209         }
210 }
211 //**********************************************************************************************************************
212 int SffMultipleCommand::readFile(vector<string>& sffFiles, vector<string>& oligosFiles){
213         try {
214         
215         ifstream in;
216         m->openInputFile(filename, in);
217         
218         string oligos, sff;
219         while (!in.eof()) {
220             
221             if (m->control_pressed) { break; }
222             
223             in >> sff;
224             
225             //ignore file pairing
226             if(sff[0] == '#'){ while (!in.eof())        {       char c = in.get();  if (c == 10 || c == 13){    break;  }       } m->gobble(in); }
227             else { //check for oligos file
228                 oligos = "";
229             
230                 // get rest of line in case there is a oligos filename
231                 while (!in.eof())       {       
232                     char c = in.get(); 
233                     if (c == 10 || c == 13){    break;  }
234                     else if (c == 32 || c == 9){;} //space or tab
235                     else {      oligos += c;  }
236                 } 
237             }
238             m->gobble(in);
239             
240             sffFiles.push_back(sff);
241             oligosFiles.push_back(oligos); //will push a blank if there is not an oligos for this sff file
242         }
243         in.close();
244         
245         return 0;
246     }
247         catch(exception& e) {
248                 m->errorOut(e, "SffMultipleCommand", "readFile");
249                 exit(1);
250         }
251 }
252 //**********************************************************************************************************************
253
254
255
256