5 // Created by SarahsWork on 10/28/13.
6 // Copyright (c) 2013 Schloss Lab. All rights reserved.
9 #include "sracommand.h"
10 #include "sffinfocommand.h"
11 #include "parsefastaqcommand.h"
13 //**********************************************************************************************************************
14 vector<string> SRACommand::setParameters(){
16 CommandParameter psff("sff", "InputTypes", "", "", "sffFastQFile", "sffFastQFile", "none","sra",false,false); parameters.push_back(psff);
17 CommandParameter pgroup("group", "InputTypes", "", "", "groupOligos", "none", "none","sra",false,false); parameters.push_back(pgroup);
18 CommandParameter poligos("oligos", "InputTypes", "", "", "groupOligos", "none", "none","sra",false,false); parameters.push_back(poligos);
19 CommandParameter pfile("file", "InputTypes", "", "", "sffFastQFile", "sffFastQFile", "none","sra",false,false); parameters.push_back(pfile);
20 CommandParameter pfastq("fastq", "InputTypes", "", "", "sffFastQFile", "sffFastQFile", "none","sra",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);
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);
33 vector<string> myArray;
34 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
38 m->errorOut(e, "SRACommand", "setParameters");
42 //**********************************************************************************************************************
43 string SRACommand::getHelpString(){
45 string helpString = "";
46 helpString += "The sra command creates a sequence read archive from sff or fastq files.\n";
47 helpString += "The sra command parameters are: sff, fastqfiles, oligos, platform....\n";
48 helpString += "The sffiles parameter is used to provide a file containing a \n";
49 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";
50 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
51 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
52 helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
53 helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
55 helpString += "The new command should be in the following format: \n";
56 helpString += "new(...)\n";
60 m->errorOut(e, "SRACommand", "getHelpString");
64 //**********************************************************************************************************************
65 string SRACommand::getOutputPattern(string type) {
69 if (type == "sra") { pattern = "[filename],sra"; }
70 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
75 m->errorOut(e, "SRACommand", "getOutputPattern");
79 //**********************************************************************************************************************
80 SRACommand::SRACommand(){
82 abort = true; calledHelp = true;
84 vector<string> tempOutNames;
85 outputTypes["sra"] = tempOutNames;
88 m->errorOut(e, "SRACommand", "SRACommand");
92 //**********************************************************************************************************************
93 SRACommand::SRACommand(string option) {
95 abort = false; calledHelp = false;
97 //allow user to run help
98 if(option == "help") { help(); abort = true; calledHelp = true; }
99 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
102 //valid paramters for this command
103 vector<string> myArray = setParameters();
105 OptionParser parser(option);
106 map<string,string> parameters = parser.getParameters();
108 ValidParameters validParameter;
109 map<string,string>::iterator it;
110 //check to make sure all parameters are valid for command
111 for (it = parameters.begin(); it != parameters.end(); it++) {
112 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
116 //if the user changes the input directory command factory will send this info to us in the output parameter
117 string inputDir = validParameter.validFile(parameters, "inputdir", false);
118 if (inputDir == "not found"){ inputDir = ""; }
122 it = parameters.find("sff");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["sff"] = inputDir + it->second; }
130 it = parameters.find("fastq");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["fastq"] = inputDir + it->second; }
138 it = parameters.find("file");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["file"] = inputDir + it->second; }
146 it = parameters.find("group");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["group"] = inputDir + it->second; }
154 it = parameters.find("oligos");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["oligos"] = inputDir + it->second; }
163 //check for parameters
164 fastqfile = validParameter.validFile(parameters, "fastq", true);
165 if (fastqfile == "not open") { fastqfile = ""; abort = true; }
166 else if (fastqfile == "not found") { fastqfile = ""; }
168 sfffile = validParameter.validFile(parameters, "sff", true);
169 if (sfffile == "not open") { sfffile = ""; abort = true; }
170 else if (sfffile == "not found") { sfffile = ""; }
172 file = validParameter.validFile(parameters, "file", true);
173 if (file == "not open") { file = ""; abort = true; }
174 else if (file == "not found") { file = ""; }
176 groupfile = validParameter.validFile(parameters, "group", true);
177 if (groupfile == "not open") { groupfile = ""; abort = true; }
178 else if (groupfile == "not found") { groupfile = ""; }
179 else { m->setGroupFile(groupfile); }
181 oligosfile = validParameter.validFile(parameters, "oligos", true);
182 if (oligosfile == "not found") { oligosfile = ""; }
183 else if(oligosfile == "not open") { abort = true; }
184 else { m->setOligosFile(oligosfile); }
187 file = validParameter.validFile(parameters, "file", true);
188 if (file == "not open") { file = ""; abort = true; }
189 else if (file == "not found") { file = ""; }
191 if ((fastqfile == "") && (sfffile == "") && (sfffile == "")) {
192 m->mothurOut("[ERROR]: You must provide a file, sff file or fastq file before you can use the sra command."); m->mothurOutEndLine(); abort = true;
195 if ((groupfile != "") && (oligosfile != "")) {
196 m->mothurOut("[ERROR]: You may not use a group file and an oligos file, only one."); m->mothurOutEndLine(); abort = true;
199 if ((fastqfile != "") || (sfffile != "")) {
200 if ((groupfile == "") && (oligosfile == "")) {
201 oligosfile = m->getOligosFile();
202 if (oligosfile != "") { m->mothurOut("Using " + oligosfile + " as input file for the oligos parameter."); m->mothurOutEndLine(); }
204 groupfile = m->getGroupFile();
205 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
207 m->mothurOut("[ERROR]: You must provide groupfile or oligos file if splitting a fastq or sff file."); m->mothurOutEndLine(); abort = true;
213 //use only one Mutliple type
214 platform = validParameter.validFile(parameters, "platform", false);
215 if (platform == "not found") { platform = "454"; }
217 if ((platform == "454") || (platform == "????") || (platform == "????") || (platform == "????")) { }
218 else { m->mothurOut("Not a valid platform option. Valid platform options are 454, ...."); m->mothurOutEndLine(); abort = true; }
221 string temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
222 m->mothurConvert(temp, bdiffs);
224 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
225 m->mothurConvert(temp, pdiffs);
227 temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
228 m->mothurConvert(temp, ldiffs);
230 temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
231 m->mothurConvert(temp, sdiffs);
233 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs; temp = toString(tempTotal); }
234 m->mothurConvert(temp, tdiffs);
236 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs + ldiffs + sdiffs; }
243 catch(exception& e) {
244 m->errorOut(e, "SRACommand", "SRACommand");
248 //**********************************************************************************************************************
249 int SRACommand::execute(){
252 if (abort == true) { if (calledHelp) { return 0; } return 2; }
255 vector<string> filesBySample;
257 if (file != "") { readFile(filesBySample); }
258 else if (sfffile != "") { parseSffFile(filesBySample); }
259 else if (fastqfile != "") { parseFastqFile(filesBySample); }
264 //output files created by command
265 m->mothurOutEndLine();
266 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
267 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
268 m->mothurOutEndLine();
272 catch(exception& e) {
273 m->errorOut(e, "SRACommand", "SRACommand");
277 //**********************************************************************************************************************
278 int SRACommand::readFile(vector<string>& files){
283 catch(exception& e) {
284 m->errorOut(e, "SRACommand", "readFile");
288 //**********************************************************************************************************************
289 int SRACommand::parseSffFile(vector<string>& files){
291 //run sffinfo to parse sff file into individual sampled sff files
292 string commandString = "sff=" + sfffile;
293 if (groupfile != "") { commandString += ", group=" + groupfile; }
294 else if (oligosfile != "") {
295 commandString += ", oligos=" + oligosfile;
296 //add in pdiffs, bdiffs, ldiffs, sdiffs, tdiffs
297 if (pdiffs != 0) { commandString += ", pdiffs=" + toString(pdiffs); }
298 if (bdiffs != 0) { commandString += ", bdiffs=" + toString(bdiffs); }
299 if (ldiffs != 0) { commandString += ", ldiffs=" + toString(ldiffs); }
300 if (sdiffs != 0) { commandString += ", sdiffs=" + toString(sdiffs); }
301 if (tdiffs != 0) { commandString += ", tdiffs=" + toString(tdiffs); }
303 m->mothurOutEndLine();
304 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
305 m->mothurOut("Running command: sffinfo(" + commandString + ")"); m->mothurOutEndLine();
306 m->mothurCalling = true;
308 Command* sffinfoCommand = new SffInfoCommand(commandString);
309 sffinfoCommand->execute();
311 map<string, vector<string> > filenames = sffinfoCommand->getOutputFiles();
312 map<string, vector<string> >::iterator it = filenames.find("sff");
313 if (it != filenames.end()) { files = it->second; }
314 else { m->control_pressed = true; } // error in sffinfo
316 delete sffinfoCommand;
317 m->mothurCalling = false;
318 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
322 catch(exception& e) {
323 m->errorOut(e, "SRACommand", "readFile");
328 //**********************************************************************************************************************
329 int SRACommand::parseFastqFile(vector<string>& files){
332 //run sffinfo to parse sff file into individual sampled sff files
333 string commandString = "fastq=" + fastqfile;
334 if (groupfile != "") { commandString += ", group=" + groupfile; }
335 else if (oligosfile != "") {
336 commandString += ", oligos=" + oligosfile;
337 //add in pdiffs, bdiffs, ldiffs, sdiffs, tdiffs
338 if (pdiffs != 0) { commandString += ", pdiffs=" + toString(pdiffs); }
339 if (bdiffs != 0) { commandString += ", bdiffs=" + toString(bdiffs); }
340 if (ldiffs != 0) { commandString += ", ldiffs=" + toString(ldiffs); }
341 if (sdiffs != 0) { commandString += ", sdiffs=" + toString(sdiffs); }
342 if (tdiffs != 0) { commandString += ", tdiffs=" + toString(tdiffs); }
344 m->mothurOutEndLine();
345 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
346 m->mothurOut("Running command: fastq.info(" + commandString + ")"); m->mothurOutEndLine();
347 m->mothurCalling = true;
349 Command* fastqinfoCommand = new ParseFastaQCommand(commandString);
350 fastqinfoCommand->execute();
352 map<string, vector<string> > filenames = fastqinfoCommand->getOutputFiles();
353 map<string, vector<string> >::iterator it = filenames.find("fastq");
354 if (it != filenames.end()) { files = it->second; }
355 else { m->control_pressed = true; } // error in sffinfo
357 delete fastqinfoCommand;
358 m->mothurCalling = false;
359 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
363 catch(exception& e) {
364 m->errorOut(e, "SRACommand", "readFile");
368 //**********************************************************************************************************************