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","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);
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 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";
59 helpString += "The new command should be in the following format: \n";
60 helpString += "new(...)\n";
64 m->errorOut(e, "SRACommand", "getHelpString");
68 //**********************************************************************************************************************
69 string SRACommand::getOutputPattern(string type) {
73 if (type == "xml") { pattern = "[filename],xml"; }
74 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
79 m->errorOut(e, "SRACommand", "getOutputPattern");
83 //**********************************************************************************************************************
84 SRACommand::SRACommand(){
86 abort = true; calledHelp = true;
88 vector<string> tempOutNames;
89 outputTypes["xml"] = tempOutNames;
92 m->errorOut(e, "SRACommand", "SRACommand");
96 //**********************************************************************************************************************
97 SRACommand::SRACommand(string option) {
99 abort = false; calledHelp = false;
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;}
106 //valid paramters for this command
107 vector<string> myArray = setParameters();
109 OptionParser parser(option);
110 map<string,string> parameters = parser.getParameters();
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; }
119 vector<string> tempOutNames;
120 outputTypes["xml"] = tempOutNames;
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 = ""; }
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; }
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; }
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; }
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; }
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; }
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 = ""; }
174 sfffile = validParameter.validFile(parameters, "sff", true);
175 if (sfffile == "not open") { sfffile = ""; abort = true; }
176 else if (sfffile == "not found") { sfffile = ""; }
178 file = validParameter.validFile(parameters, "file", true);
179 if (file == "not open") { file = ""; abort = true; }
180 else if (file == "not found") { file = ""; }
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); }
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); }
193 file = validParameter.validFile(parameters, "file", true);
194 if (file == "not open") { file = ""; abort = true; }
195 else if (file == "not found") { file = ""; }
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;
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;
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(); }
210 groupfile = m->getGroupFile();
211 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
213 m->mothurOut("[ERROR]: You must provide groupfile or oligos file if splitting a fastq or sff file."); m->mothurOutEndLine(); abort = true;
219 //use only one Mutliple type
220 platform = validParameter.validFile(parameters, "platform", false);
221 if (platform == "not found") { platform = "454"; }
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; }
227 string temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
228 m->mothurConvert(temp, bdiffs);
230 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
231 m->mothurConvert(temp, pdiffs);
233 temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
234 m->mothurConvert(temp, ldiffs);
236 temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
237 m->mothurConvert(temp, sdiffs);
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);
242 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs + ldiffs + sdiffs; }
247 catch(exception& e) {
248 m->errorOut(e, "SRACommand", "SRACommand");
252 //**********************************************************************************************************************
253 int SRACommand::execute(){
256 if (abort == true) { if (calledHelp) { return 0; } return 2; }
259 vector<string> filesBySample;
262 if (file != "") { readFile(filesBySample); }
263 else if (sfffile != "") { parseSffFile(filesBySample); }
264 else if (fastqfile != "") { parseFastqFile(filesBySample); }
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();
277 catch(exception& e) {
278 m->errorOut(e, "SRACommand", "SRACommand");
282 //**********************************************************************************************************************
283 int SRACommand::readFile(vector<string>& files){
288 m->openInputFile(file, in);
292 if (m->control_pressed) { break; }
295 in >> filename; m->gobble(in);
296 files.push_back(filename);
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
309 catch(exception& e) {
310 m->errorOut(e, "SRACommand", "readFile");
314 //**********************************************************************************************************************
315 int SRACommand::parseSffFile(vector<string>& files){
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); }
330 m->mothurOutEndLine();
331 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
332 m->mothurOut("Running command: sffinfo(" + commandString + ")"); m->mothurOutEndLine();
333 m->mothurCalling = true;
335 Command* sffinfoCommand = new SffInfoCommand(commandString);
336 sffinfoCommand->execute();
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
343 delete sffinfoCommand;
344 m->mothurCalling = false;
345 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
349 catch(exception& e) {
350 m->errorOut(e, "SRACommand", "readFile");
355 //**********************************************************************************************************************
356 int SRACommand::parseFastqFile(vector<string>& files){
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); }
371 m->mothurOutEndLine();
372 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
373 m->mothurOut("Running command: fastq.info(" + commandString + ")"); m->mothurOutEndLine();
374 m->mothurCalling = true;
376 Command* fastqinfoCommand = new ParseFastaQCommand(commandString);
377 fastqinfoCommand->execute();
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
384 delete fastqinfoCommand;
385 m->mothurCalling = false;
386 m->mothurOut("/******************************************/"); m->mothurOutEndLine();
390 catch(exception& e) {
391 m->errorOut(e, "SRACommand", "readFile");
395 //**********************************************************************************************************************