]> git.donarmstrong.com Git - mothur.git/blob - sracommand.cpp
pcr.seqs bug fix. working on shannon range calc
[mothur.git] / sracommand.cpp
1 //
2 //  sracommand.cpp
3 //  Mothur
4 //
5 //  Created by SarahsWork on 10/28/13.
6 //  Copyright (c) 2013 Schloss Lab. All rights reserved.
7 //
8
9 #include "sracommand.h"
10 #include "sffinfocommand.h"
11 #include "parsefastaqcommand.h"
12
13 //**********************************************************************************************************************
14 vector<string> SRACommand::setParameters(){
15         try {
16         CommandParameter psff("sff", "InputTypes", "", "", "sffFastQFile", "sffFastQFile", "none","xml",false,false); parameters.push_back(psff);
17         CommandParameter pgroup("group", "InputTypes", "", "", "groupOligos", "none", "none","",false,false); parameters.push_back(pgroup);
18         CommandParameter poligos("oligos", "InputTypes", "", "", "groupOligos", "none", "none","",false,false); parameters.push_back(poligos);
19         CommandParameter pfile("file", "InputTypes", "", "", "sffFastQFile", "sffFastQFile", "none","xml",false,false); parameters.push_back(pfile);
20                 CommandParameter pfastq("fastq", "InputTypes", "", "", "sffFastQFile", "sffFastQFile", "none","xml",false,false); parameters.push_back(pfastq);
21         //choose only one multiple options
22         CommandParameter pplatform("platform", "Multiple", "454-???-???", "454", "", "", "","",false,false); parameters.push_back(pplatform);
23         CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ppdiffs);
24                 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pbdiffs);
25         CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pldiffs);
26                 CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(psdiffs);
27         CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ptdiffs);
28         
29          //every command must have inputdir and outputdir.  This allows mothur users to redirect input and output files.
30                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
31                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
32                 
33                 vector<string> myArray;
34                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
35                 return myArray;
36         }
37         catch(exception& e) {
38                 m->errorOut(e, "SRACommand", "setParameters");
39                 exit(1);
40         }
41 }
42 //**********************************************************************************************************************
43 string SRACommand::getHelpString(){
44         try {
45                 string helpString = "";
46                 helpString += "The sra command creates the necessary files for a NCBI submission. The xml file and individual sff or fastq files parsed from the original sff or fastq file.\n";
47                 helpString += "The sra command parameters are: sff, fastq, file, oligos, pdiffs, bdiffs, ldiffs, sdiffs, tdiffs, group.\n";
48         helpString += "The sff parameter is used to provide the original sff file.\n";
49                 helpString += "The fastq parameter is used to provide the original fastq file.\n";
50         helpString += "The oligos parameter is used to provide an oligos file to parse your sff or fastq file by.\n";
51         helpString += "The group parameter is used to provide the group file to parse your sff or fastq file by.\n";
52                 helpString += "The file parameter is used to provide a file containing a list of individual fastq or sff files.\n";
53         helpString += "The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs + sdiffs + ldiffs.\n";
54                 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
55                 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
56         helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
57                 helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
58
59                 helpString += "The new command should be in the following format: \n";
60                 helpString += "new(...)\n";
61                 return helpString;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "SRACommand", "getHelpString");
65                 exit(1);
66         }
67 }
68 //**********************************************************************************************************************
69 string SRACommand::getOutputPattern(string type) {
70     try {
71         string pattern = "";
72         
73         if (type == "xml") {  pattern = "[filename],xml"; }
74         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
75         
76         return pattern;
77     }
78     catch(exception& e) {
79         m->errorOut(e, "SRACommand", "getOutputPattern");
80         exit(1);
81     }
82 }
83 //**********************************************************************************************************************
84 SRACommand::SRACommand(){
85         try {
86                 abort = true; calledHelp = true;
87                 setParameters();
88         vector<string> tempOutNames;
89                 outputTypes["xml"] = tempOutNames;
90         }
91         catch(exception& e) {
92                 m->errorOut(e, "SRACommand", "SRACommand");
93                 exit(1);
94         }
95 }
96 //**********************************************************************************************************************
97 SRACommand::SRACommand(string option)  {
98         try {
99                 abort = false; calledHelp = false;
100                 
101                 //allow user to run help
102                 if(option == "help") { help(); abort = true; calledHelp = true; }
103                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
104                 
105                 else {
106                         //valid paramters for this command
107                         vector<string> myArray = setParameters();
108                         
109                         OptionParser parser(option);
110                         map<string,string> parameters = parser.getParameters();
111                         
112                         ValidParameters validParameter;
113                         map<string,string>::iterator it;
114                         //check to make sure all parameters are valid for command
115                         for (it = parameters.begin(); it != parameters.end(); it++) {
116                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
117                         }
118                         
119             vector<string> tempOutNames;
120             outputTypes["xml"] = tempOutNames;
121                         
122                         //if the user changes the input directory command factory will send this info to us in the output parameter
123                         string inputDir = validParameter.validFile(parameters, "inputdir", false);
124                         if (inputDir == "not found"){   inputDir = "";          }
125                         else {
126             
127                 string path;
128                                 it = parameters.find("sff");
129                                 //user has given a template file
130                                 if(it != parameters.end()){
131                                         path = m->hasPath(it->second);
132                                         //if the user has not given a path then, add inputdir. else leave path alone.
133                                         if (path == "") {       parameters["sff"] = inputDir + it->second;              }
134                                 }
135                                 
136                                 it = parameters.find("fastq");
137                                 //user has given a template file
138                                 if(it != parameters.end()){
139                                         path = m->hasPath(it->second);
140                                         //if the user has not given a path then, add inputdir. else leave path alone.
141                                         if (path == "") {       parameters["fastq"] = inputDir + it->second;            }
142                                 }
143                 
144                 it = parameters.find("file");
145                                 //user has given a template file
146                                 if(it != parameters.end()){
147                                         path = m->hasPath(it->second);
148                                         //if the user has not given a path then, add inputdir. else leave path alone.
149                                         if (path == "") {       parameters["file"] = inputDir + it->second;             }
150                                 }
151                 
152                 it = parameters.find("group");
153                                 //user has given a template file
154                                 if(it != parameters.end()){
155                                         path = m->hasPath(it->second);
156                                         //if the user has not given a path then, add inputdir. else leave path alone.
157                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
158                                 }
159                 
160                 it = parameters.find("oligos");
161                                 //user has given a template file
162                                 if(it != parameters.end()){
163                                         path = m->hasPath(it->second);
164                                         //if the user has not given a path then, add inputdir. else leave path alone.
165                                         if (path == "") {       parameters["oligos"] = inputDir + it->second;           }
166                                 }
167             }
168             
169                         //check for parameters
170             fastqfile = validParameter.validFile(parameters, "fastq", true);
171                         if (fastqfile == "not open") { fastqfile = "";  abort = true; }
172                         else if (fastqfile == "not found") { fastqfile = ""; }
173                         
174                         sfffile = validParameter.validFile(parameters, "sff", true);
175                         if (sfffile == "not open") {  sfffile = "";  abort = true; }
176                         else if (sfffile == "not found") { sfffile = ""; }
177             
178             file = validParameter.validFile(parameters, "file", true);
179                         if (file == "not open") {  file = "";  abort = true; }
180                         else if (file == "not found") { file = ""; }
181             
182             groupfile = validParameter.validFile(parameters, "group", true);
183                         if (groupfile == "not open") {  groupfile = "";  abort = true; }
184                         else if (groupfile == "not found") { groupfile = ""; }
185             else {  m->setGroupFile(groupfile); }
186             
187             oligosfile = validParameter.validFile(parameters, "oligos", true);
188                         if (oligosfile == "not found")      {   oligosfile = "";        }
189                         else if(oligosfile == "not open")       {       abort = true;           }
190                         else {  m->setOligosFile(oligosfile); }
191             
192             
193             file = validParameter.validFile(parameters, "file", true);
194                         if (file == "not open") {  file = "";  abort = true; }
195                         else if (file == "not found") { file = ""; }
196                         
197                         if ((fastqfile == "") && (sfffile == "") && (sfffile == "")) {
198                 m->mothurOut("[ERROR]: You must provide a file, sff file or fastq file before you can use the sra command."); m->mothurOutEndLine(); abort = true;
199             }
200             
201             if ((groupfile != "") && (oligosfile != "")) {
202                 m->mothurOut("[ERROR]: You may not use a group file and an oligos file, only one."); m->mothurOutEndLine(); abort = true;
203             }
204             
205             if ((fastqfile != "") || (sfffile != "")) {
206                 if ((groupfile == "") && (oligosfile == "")) {
207                     oligosfile = m->getOligosFile();
208                                         if (oligosfile != "") {  m->mothurOut("Using " + oligosfile + " as input file for the oligos parameter."); m->mothurOutEndLine(); }
209                                         else {
210                                                 groupfile = m->getGroupFile();
211                         if (groupfile != "") {  m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
212                         else {
213                             m->mothurOut("[ERROR]: You must provide groupfile or oligos file if splitting a fastq or sff file."); m->mothurOutEndLine(); abort = true;
214                         }
215                                         }
216                 }
217             }
218                                     
219             //use only one Mutliple type
220                         platform = validParameter.validFile(parameters, "platform", false);
221                         if (platform == "not found") { platform = "454"; }
222                         
223                         if ((platform == "454") || (platform == "????") || (platform == "????") || (platform == "????")) { }
224                         else { m->mothurOut("Not a valid platform option.  Valid platform options are 454, ...."); m->mothurOutEndLine(); abort = true; }
225             
226             
227             string temp = validParameter.validFile(parameters, "bdiffs", false);                if (temp == "not found"){       temp = "0";             }
228                         m->mothurConvert(temp, bdiffs);
229                         
230                         temp = validParameter.validFile(parameters, "pdiffs", false);           if (temp == "not found"){       temp = "0";             }
231                         m->mothurConvert(temp, pdiffs);
232                         
233             temp = validParameter.validFile(parameters, "ldiffs", false);               if (temp == "not found") { temp = "0"; }
234                         m->mothurConvert(temp, ldiffs);
235             
236             temp = validParameter.validFile(parameters, "sdiffs", false);               if (temp == "not found") { temp = "0"; }
237                         m->mothurConvert(temp, sdiffs);
238                         
239                         temp = validParameter.validFile(parameters, "tdiffs", false);           if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs;  temp = toString(tempTotal); }
240                         m->mothurConvert(temp, tdiffs);
241                         
242                         if(tdiffs == 0){        tdiffs = bdiffs + pdiffs + ldiffs + sdiffs;     }
243                                 
244                 }
245                 
246         }
247         catch(exception& e) {
248                 m->errorOut(e, "SRACommand", "SRACommand");
249                 exit(1);
250         }
251 }
252 //**********************************************************************************************************************
253 int SRACommand::execute(){
254         try {
255                 
256                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
257         
258         //parse files
259         vector<string> filesBySample;
260         isSFF = false;
261         
262         if (file != "")             {       readFile(filesBySample);        }
263         else if (sfffile != "")     {       parseSffFile(filesBySample);    }
264         else if (fastqfile != "")   {       parseFastqFile(filesBySample);  }
265         
266         //create xml file
267         
268                 
269         //output files created by command
270                 m->mothurOutEndLine();
271                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
272                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
273                 m->mothurOutEndLine();
274         return 0;
275                 
276     }
277         catch(exception& e) {
278                 m->errorOut(e, "SRACommand", "SRACommand");
279                 exit(1);
280         }
281 }
282 //**********************************************************************************************************************
283 int SRACommand::readFile(vector<string>& files){
284         try {
285         files.clear();
286         
287         ifstream in;
288         m->openInputFile(file, in);
289         
290         while(!in.eof()) {
291             
292             if (m->control_pressed) { break; }
293             
294             string filename;
295             in >> filename; m->gobble(in);
296             files.push_back(filename);
297         }
298         in.close();
299         
300         if (!m->control_pressed) {
301             if (files.size() > 0) {
302                 int pos = files[0].find(".sff");
303                 if (pos != string::npos) { isSFF = true; } //these files are sff files
304             }
305         }
306         
307         return 0;
308     }
309         catch(exception& e) {
310                 m->errorOut(e, "SRACommand", "readFile");
311                 exit(1);
312         }
313 }
314 //**********************************************************************************************************************
315 int SRACommand::parseSffFile(vector<string>& files){
316         try {
317         isSFF = true;
318         //run sffinfo to parse sff file into individual sampled sff files
319         string commandString = "sff=" + sfffile;
320         if (groupfile != "") { commandString += ", group=" + groupfile; }
321         else if (oligosfile != "") {
322             commandString += ", oligos=" + oligosfile;
323             //add in pdiffs, bdiffs, ldiffs, sdiffs, tdiffs
324             if (pdiffs != 0) { commandString += ", pdiffs=" + toString(pdiffs); }
325             if (bdiffs != 0) { commandString += ", bdiffs=" + toString(bdiffs); }
326             if (ldiffs != 0) { commandString += ", ldiffs=" + toString(ldiffs); }
327             if (sdiffs != 0) { commandString += ", sdiffs=" + toString(sdiffs); }
328             if (tdiffs != 0) { commandString += ", tdiffs=" + toString(tdiffs); }
329         }
330         m->mothurOutEndLine();
331         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
332         m->mothurOut("Running command: sffinfo(" + commandString + ")"); m->mothurOutEndLine();
333         m->mothurCalling = true;
334         
335         Command* sffinfoCommand = new SffInfoCommand(commandString);
336         sffinfoCommand->execute();
337         
338         map<string, vector<string> > filenames = sffinfoCommand->getOutputFiles();
339         map<string, vector<string> >::iterator it = filenames.find("sff");
340         if (it != filenames.end()) { files = it->second; }
341         else { m->control_pressed = true; } // error in sffinfo
342         
343         delete sffinfoCommand;
344         m->mothurCalling = false;
345         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
346         
347         return 0;
348     }
349         catch(exception& e) {
350                 m->errorOut(e, "SRACommand", "readFile");
351                 exit(1);
352         }
353 }
354
355 //**********************************************************************************************************************
356 int SRACommand::parseFastqFile(vector<string>& files){
357         try {
358         
359         //run sffinfo to parse sff file into individual sampled sff files
360         string commandString = "fastq=" + fastqfile;
361         if (groupfile != "") { commandString += ", group=" + groupfile; }
362         else if (oligosfile != "") {
363             commandString += ", oligos=" + oligosfile;
364             //add in pdiffs, bdiffs, ldiffs, sdiffs, tdiffs
365             if (pdiffs != 0) { commandString += ", pdiffs=" + toString(pdiffs); }
366             if (bdiffs != 0) { commandString += ", bdiffs=" + toString(bdiffs); }
367             if (ldiffs != 0) { commandString += ", ldiffs=" + toString(ldiffs); }
368             if (sdiffs != 0) { commandString += ", sdiffs=" + toString(sdiffs); }
369             if (tdiffs != 0) { commandString += ", tdiffs=" + toString(tdiffs); }
370         }
371         m->mothurOutEndLine();
372         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
373         m->mothurOut("Running command: fastq.info(" + commandString + ")"); m->mothurOutEndLine();
374         m->mothurCalling = true;
375         
376         Command* fastqinfoCommand = new ParseFastaQCommand(commandString);
377         fastqinfoCommand->execute();
378         
379         map<string, vector<string> > filenames = fastqinfoCommand->getOutputFiles();
380         map<string, vector<string> >::iterator it = filenames.find("fastq");
381         if (it != filenames.end()) { files = it->second; }
382         else { m->control_pressed = true; } // error in sffinfo
383         
384         delete fastqinfoCommand;
385         m->mothurCalling = false;
386         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
387         
388         return 0;
389     }
390         catch(exception& e) {
391                 m->errorOut(e, "SRACommand", "readFile");
392                 exit(1);
393         }
394 }
395 //**********************************************************************************************************************
396
397