]> git.donarmstrong.com Git - mothur.git/blob - reversecommand.cpp
rewrote metastats command in c++, added mothurRemove function to handle ~ error....
[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",false,false); parameters.push_back(pfasta);
18                 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "fastaQual", "none",false,false); 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 //**********************************************************************************************************************
48 ReverseSeqsCommand::ReverseSeqsCommand(){       
49         try {
50                 abort = true; calledHelp = true; 
51                 setParameters();
52                 vector<string> tempOutNames;
53                 outputTypes["fasta"] = tempOutNames;
54                 outputTypes["qfile"] = tempOutNames;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
58                 exit(1);
59         }
60 }
61 //***************************************************************************************************************
62
63 ReverseSeqsCommand::ReverseSeqsCommand(string option)  {
64         try {
65                 abort = false; calledHelp = false;   
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; calledHelp = true; }
69                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
70                 
71                 else {
72                         vector<string> myArray = setParameters();
73                         
74                         OptionParser parser(option);
75                         map<string,string> parameters = parser.getParameters();
76                         
77                         ValidParameters validParameter;
78                         map<string,string>::iterator it;
79                         
80                         //check to make sure all parameters are valid for command
81                         for (it = parameters.begin(); it != parameters.end(); it++) { 
82                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
83                         }
84                         
85                         //initialize outputTypes
86                         vector<string> tempOutNames;
87                         outputTypes["fasta"] = tempOutNames;
88                         outputTypes["qfile"] = tempOutNames;
89
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("fasta");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
101                                 }
102                                 
103                                 it = parameters.find("qfile");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
109                                 }
110                         }
111
112                         //check for required parameters
113                         fastaFileName = validParameter.validFile(parameters, "fasta", true);
114                         if (fastaFileName == "not open") { abort = true; }
115                         else if (fastaFileName == "not found") { fastaFileName = "";}// m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true;  }    
116                         else { m->setFastaFile(fastaFileName); }
117                         
118                         qualFileName = validParameter.validFile(parameters, "qfile", true);
119                         if (qualFileName == "not open") { abort = true; }
120                         else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true;  }      
121                         else { m->setQualFile(qualFileName); }
122                         
123                         if((fastaFileName == "") && (qualFileName == "")){
124                                 fastaFileName = m->getFastaFile(); 
125                                 if (fastaFileName != "") {  m->mothurOut("Using " + fastaFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
126                                 else { 
127                                         qualFileName = m->getQualFile(); 
128                                         if (qualFileName != "") {  m->mothurOut("Using " + qualFileName + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
129                                         else { 
130                                                 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();
131                                                 abort = true;
132                                         }
133                                 }
134                         }
135                         
136                         //if the user changes the output directory command factory will send this info to us in the output parameter 
137                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
138                                 outputDir = ""; 
139                         }
140
141                 }
142         }
143         catch(exception& e) {
144                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
145                 exit(1);
146         }
147 }
148 //***************************************************************************************************************
149
150
151 int ReverseSeqsCommand::execute(){
152         try{
153                 
154                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
155                 
156                 string fastaReverseFileName;
157                 
158                 if(fastaFileName != ""){
159                         ifstream inFASTA;
160                         m->openInputFile(fastaFileName, inFASTA);
161                         
162                         ofstream outFASTA;
163                         string tempOutputDir = outputDir;
164                         if (outputDir == "") { tempOutputDir += m->hasPath(fastaFileName); } //if user entered a file with a path then preserve it
165                         fastaReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileName)) + "rc" + m->getExtension(fastaFileName);
166                         m->openOutputFile(fastaReverseFileName, outFASTA);
167                         
168                         while(!inFASTA.eof()){
169                                 if (m->control_pressed) {  inFASTA.close();  outFASTA.close(); m->mothurRemove(fastaReverseFileName); return 0; }
170                                  
171                                 Sequence currSeq(inFASTA);  m->gobble(inFASTA);
172                                 if (currSeq.getName() != "") {
173                                         currSeq.reverseComplement();
174                                         currSeq.printSequence(outFASTA);
175                                 }
176                         }
177                         inFASTA.close();
178                         outFASTA.close();
179                         outputNames.push_back(fastaReverseFileName); outputTypes["fasta"].push_back(fastaReverseFileName);
180                 }
181                 
182                 string qualReverseFileName;
183
184                 if(qualFileName != ""){
185                         QualityScores currQual;
186
187                         ifstream inQual;
188                         m->openInputFile(qualFileName, inQual);
189                         
190                         ofstream outQual;
191                         string tempOutputDir = outputDir;
192                         if (outputDir == "") { tempOutputDir += m->hasPath(qualFileName); } //if user entered a file with a path then preserve it
193                         string qualReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(qualFileName)) + "rc" + m->getExtension(qualFileName);
194                         m->openOutputFile(qualReverseFileName, outQual);
195
196                         while(!inQual.eof()){
197                                 if (m->control_pressed) {  inQual.close();  outQual.close(); m->mothurRemove(qualReverseFileName); return 0; }
198                                 currQual = QualityScores(inQual);  m->gobble(inQual);
199                                 currQual.flipQScores(); 
200                                 currQual.printQScores(outQual);
201                         }
202                         inQual.close();
203                         outQual.close();
204                         outputNames.push_back(qualReverseFileName); outputTypes["qfile"].push_back(qualReverseFileName);
205                 }
206                 
207                 if (m->control_pressed) {  m->mothurRemove(qualReverseFileName); m->mothurRemove(fastaReverseFileName); return 0; }
208                 
209                 //set fasta file as new current fastafile
210                 string current = "";
211                 itTypes = outputTypes.find("fasta");
212                 if (itTypes != outputTypes.end()) {
213                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
214                 }
215                 
216                 itTypes = outputTypes.find("qfile");
217                 if (itTypes != outputTypes.end()) {
218                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
219                 }
220                 
221                 
222                 m->mothurOutEndLine();
223                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
224                 for(int i=0;i<outputNames.size();i++){
225                         m->mothurOut(outputNames[i]);
226                         m->mothurOutEndLine();
227                 }
228                 
229                 
230                 
231                 return 0;
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "ReverseSeqsCommand", "execute");
235                 exit(1);
236         }
237 }
238
239 //***************************************************************************************************************