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