]> git.donarmstrong.com Git - mothur.git/blob - sracommand.cpp
working on pam
[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","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);
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 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";
54
55                 helpString += "The new command should be in the following format: \n";
56                 helpString += "new(...)\n";
57                 return helpString;
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "SRACommand", "getHelpString");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65 string SRACommand::getOutputPattern(string type) {
66     try {
67         string pattern = "";
68         
69         if (type == "sra") {  pattern = "[filename],sra"; }
70         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
71         
72         return pattern;
73     }
74     catch(exception& e) {
75         m->errorOut(e, "SRACommand", "getOutputPattern");
76         exit(1);
77     }
78 }
79 //**********************************************************************************************************************
80 SRACommand::SRACommand(){
81         try {
82                 abort = true; calledHelp = true;
83                 setParameters();
84         vector<string> tempOutNames;
85                 outputTypes["sra"] = tempOutNames; 
86         }
87         catch(exception& e) {
88                 m->errorOut(e, "SRACommand", "SRACommand");
89                 exit(1);
90         }
91 }
92 //**********************************************************************************************************************
93 SRACommand::SRACommand(string option)  {
94         try {
95                 abort = false; calledHelp = false;
96                 
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;}
100                 
101                 else {
102                         //valid paramters for this command
103                         vector<string> myArray = setParameters();
104                         
105                         OptionParser parser(option);
106                         map<string,string> parameters = parser.getParameters();
107                         
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;  }
113                         }
114                         
115                         
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 = "";          }
119                         else {
120             
121                 string path;
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;              }
128                                 }
129                                 
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;            }
136                                 }
137                 
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;             }
144                                 }
145                 
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;            }
152                                 }
153                 
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;           }
160                                 }
161             }
162             
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 = ""; }
167                         
168                         sfffile = validParameter.validFile(parameters, "sff", true);
169                         if (sfffile == "not open") {  sfffile = "";  abort = true; }
170                         else if (sfffile == "not found") { sfffile = ""; }
171             
172             file = validParameter.validFile(parameters, "file", true);
173                         if (file == "not open") {  file = "";  abort = true; }
174                         else if (file == "not found") { file = ""; }
175             
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); }
180             
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); }
185             
186             
187             file = validParameter.validFile(parameters, "file", true);
188                         if (file == "not open") {  file = "";  abort = true; }
189                         else if (file == "not found") { file = ""; }
190                         
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;
193             }
194             
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;
197             }
198             
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(); }
203                                         else {
204                                                 groupfile = m->getGroupFile();
205                         if (groupfile != "") {  m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
206                         else {
207                             m->mothurOut("[ERROR]: You must provide groupfile or oligos file if splitting a fastq or sff file."); m->mothurOutEndLine(); abort = true;
208                         }
209                                         }
210                 }
211             }
212                                     
213             //use only one Mutliple type
214                         platform = validParameter.validFile(parameters, "platform", false);
215                         if (platform == "not found") { platform = "454"; }
216                         
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; }
219             
220             
221             string temp = validParameter.validFile(parameters, "bdiffs", false);                if (temp == "not found"){       temp = "0";             }
222                         m->mothurConvert(temp, bdiffs);
223                         
224                         temp = validParameter.validFile(parameters, "pdiffs", false);           if (temp == "not found"){       temp = "0";             }
225                         m->mothurConvert(temp, pdiffs);
226                         
227             temp = validParameter.validFile(parameters, "ldiffs", false);               if (temp == "not found") { temp = "0"; }
228                         m->mothurConvert(temp, ldiffs);
229             
230             temp = validParameter.validFile(parameters, "sdiffs", false);               if (temp == "not found") { temp = "0"; }
231                         m->mothurConvert(temp, sdiffs);
232                         
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);
235                         
236                         if(tdiffs == 0){        tdiffs = bdiffs + pdiffs + ldiffs + sdiffs;     }
237             
238
239                                 
240                 }
241                 
242         }
243         catch(exception& e) {
244                 m->errorOut(e, "SRACommand", "SRACommand");
245                 exit(1);
246         }
247 }
248 //**********************************************************************************************************************
249 int SRACommand::execute(){
250         try {
251                 
252                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
253         
254         //parse files
255         vector<string> filesBySample;
256         
257         if (file != "")             {       readFile(filesBySample);        }
258         else if (sfffile != "")     {       parseSffFile(filesBySample);    }
259         else if (fastqfile != "")   {       parseFastqFile(filesBySample);  }
260         
261         
262         
263                 
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();
269         return 0;
270                 
271     }
272         catch(exception& e) {
273                 m->errorOut(e, "SRACommand", "SRACommand");
274                 exit(1);
275         }
276 }
277 //**********************************************************************************************************************
278 int SRACommand::readFile(vector<string>& files){
279         try {
280         
281         return 0;
282     }
283         catch(exception& e) {
284                 m->errorOut(e, "SRACommand", "readFile");
285                 exit(1);
286         }
287 }
288 //**********************************************************************************************************************
289 int SRACommand::parseSffFile(vector<string>& files){
290         try {
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); }
302         }
303         m->mothurOutEndLine();
304         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
305         m->mothurOut("Running command: sffinfo(" + commandString + ")"); m->mothurOutEndLine();
306         m->mothurCalling = true;
307         
308         Command* sffinfoCommand = new SffInfoCommand(commandString);
309         sffinfoCommand->execute();
310         
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
315         
316         delete sffinfoCommand;
317         m->mothurCalling = false;
318         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
319         
320         return 0;
321     }
322         catch(exception& e) {
323                 m->errorOut(e, "SRACommand", "readFile");
324                 exit(1);
325         }
326 }
327
328 //**********************************************************************************************************************
329 int SRACommand::parseFastqFile(vector<string>& files){
330         try {
331         
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); }
343         }
344         m->mothurOutEndLine();
345         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
346         m->mothurOut("Running command: fastq.info(" + commandString + ")"); m->mothurOutEndLine();
347         m->mothurCalling = true;
348         
349         Command* fastqinfoCommand = new ParseFastaQCommand(commandString);
350         fastqinfoCommand->execute();
351         
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
356         
357         delete fastqinfoCommand;
358         m->mothurCalling = false;
359         m->mothurOut("/******************************************/"); m->mothurOutEndLine();
360         
361         return 0;
362     }
363         catch(exception& e) {
364                 m->errorOut(e, "SRACommand", "readFile");
365                 exit(1);
366         }
367 }
368 //**********************************************************************************************************************
369
370