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