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