]> git.donarmstrong.com Git - mothur.git/blob - reversecommand.cpp
changed random forest output filename
[mothur.git] / reversecommand.cpp
1 /*
2  *  reversecommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 6/6/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "reversecommand.h"
11 #include "sequence.hpp"
12 #include "qualityscores.h"
13
14 //**********************************************************************************************************************
15 vector<string> ReverseSeqsCommand::setParameters(){     
16         try {
17                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "fastaQual", "none","fasta",false,false,true); parameters.push_back(pfasta);
18                 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "fastaQual", "none","qfile",false,false,true); parameters.push_back(pqfile);
19                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "ReverseSeqsCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string ReverseSeqsCommand::getHelpString(){     
33         try {
34                 string helpString = "";
35                 helpString += "The reverse.seqs command reads a fastafile and outputs a fasta file containing the reverse compliment.\n";
36                 helpString += "The reverse.seqs command parameters fasta or qfile are required.\n";
37                 helpString += "The reverse.seqs command should be in the following format: \n";
38                 helpString += "reverse.seqs(fasta=yourFastaFile) \n";   
39                 return helpString;
40         }
41         catch(exception& e) {
42                 m->errorOut(e, "ReverseSeqsCommand", "getHelpString");
43                 exit(1);
44         }
45 }
46 //**********************************************************************************************************************
47 string ReverseSeqsCommand::getOutputPattern(string type) {
48     try {
49         string pattern = "";
50         
51         if (type == "fasta") {  pattern = "[filename],rc,[extension]"; } 
52         else if (type == "qfile") {  pattern = "[filename],rc,[extension]"; } 
53         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
54         
55         return pattern;
56     }
57     catch(exception& e) {
58         m->errorOut(e, "ReverseSeqsCommand", "getOutputPattern");
59         exit(1);
60     }
61 }
62 //**********************************************************************************************************************
63 ReverseSeqsCommand::ReverseSeqsCommand(){       
64         try {
65                 abort = true; calledHelp = true; 
66                 setParameters();
67                 vector<string> tempOutNames;
68                 outputTypes["fasta"] = tempOutNames;
69                 outputTypes["qfile"] = tempOutNames;
70         }
71         catch(exception& e) {
72                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
73                 exit(1);
74         }
75 }
76 //***************************************************************************************************************
77
78 ReverseSeqsCommand::ReverseSeqsCommand(string option)  {
79         try {
80                 abort = false; calledHelp = false;   
81                 
82                 //allow user to run help
83                 if(option == "help") { help(); abort = true; calledHelp = true; }
84                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85                 
86                 else {
87                         vector<string> myArray = setParameters();
88                         
89                         OptionParser parser(option);
90                         map<string,string> parameters = parser.getParameters();
91                         
92                         ValidParameters validParameter;
93                         map<string,string>::iterator it;
94                         
95                         //check to make sure all parameters are valid for command
96                         for (it = parameters.begin(); it != parameters.end(); it++) { 
97                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["fasta"] = tempOutNames;
103                         outputTypes["qfile"] = tempOutNames;
104
105                         //if the user changes the input directory command factory will send this info to us in the output parameter 
106                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
107                         if (inputDir == "not found"){   inputDir = "";          }
108                         else {
109                                 string path;
110                                 it = parameters.find("fasta");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
116                                 }
117                                 
118                                 it = parameters.find("qfile");
119                                 //user has given a template file
120                                 if(it != parameters.end()){ 
121                                         path = m->hasPath(it->second);
122                                         //if the user has not given a path then, add inputdir. else leave path alone.
123                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
124                                 }
125                         }
126
127                         //check for required parameters
128                         fastaFileName = validParameter.validFile(parameters, "fasta", true);
129                         if (fastaFileName == "not open") { abort = true; }
130                         else if (fastaFileName == "not found") { fastaFileName = "";}// m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true;  }    
131                         else { m->setFastaFile(fastaFileName); }
132                         
133                         qualFileName = validParameter.validFile(parameters, "qfile", true);
134                         if (qualFileName == "not open") { abort = true; }
135                         else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true;  }      
136                         else { m->setQualFile(qualFileName); }
137                         
138                         if((fastaFileName == "") && (qualFileName == "")){
139                                 fastaFileName = m->getFastaFile(); 
140                                 if (fastaFileName != "") {  m->mothurOut("Using " + fastaFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
141                                 else { 
142                                         qualFileName = m->getQualFile(); 
143                                         if (qualFileName != "") {  m->mothurOut("Using " + qualFileName + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
144                                         else { 
145                                                 m->mothurOut("You have no current files for fasta or qfile, and fasta or qfile is a required parameter for the reverse.seqs command."); m->mothurOutEndLine();
146                                                 abort = true;
147                                         }
148                                 }
149                         }
150                         
151                         //if the user changes the output directory command factory will send this info to us in the output parameter 
152                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
153                                 outputDir = ""; 
154                         }
155
156                 }
157         }
158         catch(exception& e) {
159                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
160                 exit(1);
161         }
162 }
163 //***************************************************************************************************************
164
165
166 int ReverseSeqsCommand::execute(){
167         try{
168                 
169                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
170                 
171                 string fastaReverseFileName;
172                 
173                 if(fastaFileName != ""){
174                         ifstream inFASTA;
175                         m->openInputFile(fastaFileName, inFASTA);
176                         
177                         ofstream outFASTA;
178                         string tempOutputDir = outputDir;
179                         if (outputDir == "") { tempOutputDir += m->hasPath(fastaFileName); } //if user entered a file with a path then preserve it
180             map<string, string> variables; 
181             variables["[filename]"] = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileName));
182             variables["[extension]"] = m->getExtension(fastaFileName);
183                         fastaReverseFileName = getOutputFileName("fasta", variables);
184                         m->openOutputFile(fastaReverseFileName, outFASTA);
185                         
186                         while(!inFASTA.eof()){
187                                 if (m->control_pressed) {  inFASTA.close();  outFASTA.close(); m->mothurRemove(fastaReverseFileName); return 0; }
188                                  
189                                 Sequence currSeq(inFASTA);  m->gobble(inFASTA);
190                                 if (currSeq.getName() != "") {
191                                         currSeq.reverseComplement();
192                                         currSeq.printSequence(outFASTA);
193                                 }
194                         }
195                         inFASTA.close();
196                         outFASTA.close();
197                         outputNames.push_back(fastaReverseFileName); outputTypes["fasta"].push_back(fastaReverseFileName);
198                 }
199                 
200                 string qualReverseFileName;
201
202                 if(qualFileName != ""){
203                         QualityScores currQual;
204
205                         ifstream inQual;
206                         m->openInputFile(qualFileName, inQual);
207                         
208                         ofstream outQual;
209                         string tempOutputDir = outputDir;
210                         if (outputDir == "") { tempOutputDir += m->hasPath(qualFileName); } //if user entered a file with a path then preserve it
211             map<string, string> variables; 
212             variables["[filename]"] = tempOutputDir + m->getRootName(m->getSimpleName(qualFileName));
213             variables["[extension]"] = m->getExtension(qualFileName);
214                         string qualReverseFileName = getOutputFileName("qfile", variables);
215             m->openOutputFile(qualReverseFileName, outQual);
216
217                         while(!inQual.eof()){
218                                 if (m->control_pressed) {  inQual.close();  outQual.close(); m->mothurRemove(qualReverseFileName); return 0; }
219                                 currQual = QualityScores(inQual);  m->gobble(inQual);
220                                 currQual.flipQScores(); 
221                                 currQual.printQScores(outQual);
222                         }
223                         inQual.close();
224                         outQual.close();
225                         outputNames.push_back(qualReverseFileName); outputTypes["qfile"].push_back(qualReverseFileName);
226                 }
227                 
228                 if (m->control_pressed) {  m->mothurRemove(qualReverseFileName); m->mothurRemove(fastaReverseFileName); return 0; }
229                 
230                 //set fasta file as new current fastafile
231                 string current = "";
232                 itTypes = outputTypes.find("fasta");
233                 if (itTypes != outputTypes.end()) {
234                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
235                 }
236                 
237                 itTypes = outputTypes.find("qfile");
238                 if (itTypes != outputTypes.end()) {
239                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
240                 }
241                 
242                 
243                 m->mothurOutEndLine();
244                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
245                 for(int i=0;i<outputNames.size();i++){
246                         m->mothurOut(outputNames[i]);
247                         m->mothurOutEndLine();
248                 }
249                 
250                 
251                 
252                 return 0;
253         }
254         catch(exception& e) {
255                 m->errorOut(e, "ReverseSeqsCommand", "execute");
256                 exit(1);
257         }
258 }
259
260 //***************************************************************************************************************