]> git.donarmstrong.com Git - mothur.git/blob - reversecommand.cpp
added citation function to commands
[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                         
117                         qualFileName = validParameter.validFile(parameters, "qfile", true);
118                         if (qualFileName == "not open") { abort = true; }
119                         else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true;  }      
120
121                         if((fastaFileName == "") && (qualFileName == "")){
122                                 fastaFileName = m->getFastaFile(); 
123                                 if (fastaFileName != "") {  m->mothurOut("Using " + fastaFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
124                                 else { 
125                                         qualFileName = m->getQualFile(); 
126                                         if (qualFileName != "") {  m->mothurOut("Using " + qualFileName + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
127                                         else { 
128                                                 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();
129                                                 abort = true;
130                                         }
131                                 }
132                         }
133                         
134                         //if the user changes the output directory command factory will send this info to us in the output parameter 
135                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
136                                 outputDir = ""; 
137                         }
138
139                 }
140         }
141         catch(exception& e) {
142                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
143                 exit(1);
144         }
145 }
146 //***************************************************************************************************************
147
148
149 int ReverseSeqsCommand::execute(){
150         try{
151                 
152                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
153                 
154                 string fastaReverseFileName;
155                 
156                 if(fastaFileName != ""){
157                         ifstream inFASTA;
158                         m->openInputFile(fastaFileName, inFASTA);
159                         
160                         ofstream outFASTA;
161                         string tempOutputDir = outputDir;
162                         if (outputDir == "") { tempOutputDir += m->hasPath(fastaFileName); } //if user entered a file with a path then preserve it
163                         fastaReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileName)) + "rc" + m->getExtension(fastaFileName);
164                         m->openOutputFile(fastaReverseFileName, outFASTA);
165                         
166                         while(!inFASTA.eof()){
167                                 if (m->control_pressed) {  inFASTA.close();  outFASTA.close(); remove(fastaReverseFileName.c_str()); return 0; }
168                                  
169                                 Sequence currSeq(inFASTA);  m->gobble(inFASTA);
170                                 if (currSeq.getName() != "") {
171                                         currSeq.reverseComplement();
172                                         currSeq.printSequence(outFASTA);
173                                 }
174                         }
175                         inFASTA.close();
176                         outFASTA.close();
177                         outputNames.push_back(fastaReverseFileName); outputTypes["fasta"].push_back(fastaReverseFileName);
178                 }
179                 
180                 string qualReverseFileName;
181
182                 if(qualFileName != ""){
183                         QualityScores currQual;
184
185                         ifstream inQual;
186                         m->openInputFile(qualFileName, inQual);
187                         
188                         ofstream outQual;
189                         string tempOutputDir = outputDir;
190                         if (outputDir == "") { tempOutputDir += m->hasPath(qualFileName); } //if user entered a file with a path then preserve it
191                         string qualReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(qualFileName)) + "rc" + m->getExtension(qualFileName);
192                         m->openOutputFile(qualReverseFileName, outQual);
193
194                         while(!inQual.eof()){
195                                 if (m->control_pressed) {  inQual.close();  outQual.close(); remove(qualReverseFileName.c_str()); return 0; }
196                                 currQual = QualityScores(inQual);  m->gobble(inQual);
197                                 currQual.flipQScores(); 
198                                 currQual.printQScores(outQual);
199                         }
200                         inQual.close();
201                         outQual.close();
202                         outputNames.push_back(qualReverseFileName); outputTypes["qfile"].push_back(qualReverseFileName);
203                 }
204                 
205                 if (m->control_pressed) {  remove(qualReverseFileName.c_str()); remove(fastaReverseFileName.c_str()); return 0; }
206                 
207                 //set fasta file as new current fastafile
208                 string current = "";
209                 itTypes = outputTypes.find("fasta");
210                 if (itTypes != outputTypes.end()) {
211                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
212                 }
213                 
214                 itTypes = outputTypes.find("qfile");
215                 if (itTypes != outputTypes.end()) {
216                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
217                 }
218                 
219                 
220                 m->mothurOutEndLine();
221                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
222                 for(int i=0;i<outputNames.size();i++){
223                         m->mothurOut(outputNames[i]);
224                         m->mothurOutEndLine();
225                 }
226                 
227                 
228                 
229                 return 0;
230         }
231         catch(exception& e) {
232                 m->errorOut(e, "ReverseSeqsCommand", "execute");
233                 exit(1);
234         }
235 }
236
237 //***************************************************************************************************************