]> git.donarmstrong.com Git - mothur.git/blob - reversecommand.cpp
fixes while testing
[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
13 //**********************************************************************************************************************
14 vector<string> ReverseSeqsCommand::getValidParameters(){        
15         try {
16                 string Array[] =  {"fasta", "outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "ReverseSeqsCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 ReverseSeqsCommand::ReverseSeqsCommand(){       
27         try {
28                 abort = true;
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["fasta"] = tempOutNames;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> ReverseSeqsCommand::getRequiredParameters(){     
40         try {
41                 string Array[] =  {"fasta"};
42                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "ReverseSeqsCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> ReverseSeqsCommand::getRequiredFiles(){  
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "ReverseSeqsCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //***************************************************************************************************************
62
63 ReverseSeqsCommand::ReverseSeqsCommand(string option)  {
64         try {
65                 abort = false;
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string Array[] =  {"fasta", "outputdir","inputdir"};
73                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string,string>::iterator it;
80                         
81                         //check to make sure all parameters are valid for command
82                         for (it = parameters.begin(); it != parameters.end(); it++) { 
83                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["fasta"] = 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
104                         //check for required parameters
105                         fasta = validParameter.validFile(parameters, "fasta", true);
106                         if (fasta == "not open") { abort = true; }
107                         else if (fasta == "not found") { fasta = ""; m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true;  }       
108                         
109                         //if the user changes the output directory command factory will send this info to us in the output parameter 
110                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
111                                 outputDir = ""; 
112                                 outputDir += m->hasPath(fasta); //if user entered a file with a path then preserve it   
113                         }
114
115                 }
116         }
117         catch(exception& e) {
118                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
119                 exit(1);
120         }
121 }
122 //**********************************************************************************************************************
123
124 void ReverseSeqsCommand::help(){
125         try {
126                 m->mothurOut("The reverse.seqs command reads a fastafile and outputs a fasta file containing the reverse compliment.\n");
127                 m->mothurOut("The reverse.seqs command parameter is fasta and it is required.\n");
128                 m->mothurOut("The reverse.seqs command should be in the following format: \n");
129                 m->mothurOut("reverse.seqs(fasta=yourFastaFile) \n");   
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "ReverseSeqsCommand", "help");
133                 exit(1);
134         }
135 }
136
137 //***************************************************************************************************************
138
139 ReverseSeqsCommand::~ReverseSeqsCommand(){      /*      do nothing      */      }
140
141 //***************************************************************************************************************
142
143
144 int ReverseSeqsCommand::execute(){
145         try{
146                 
147                 if (abort == true) { return 0; }
148                 
149                 ifstream inFASTA;
150                 m->openInputFile(fasta, inFASTA);
151                 
152                 ofstream outFASTA;
153                 string reverseFile = outputDir + m->getRootName(m->getSimpleName(fasta)) + "rc" + m->getExtension(fasta);
154                 m->openOutputFile(reverseFile, outFASTA);
155                 
156                 while(!inFASTA.eof()){
157                         if (m->control_pressed) {  inFASTA.close();  outFASTA.close(); remove(reverseFile.c_str()); return 0; }
158                          
159                         Sequence currSeq(inFASTA);  m->gobble(inFASTA);
160                         if (currSeq.getName() != "") {
161                                 currSeq.reverseComplement();
162                                 currSeq.printSequence(outFASTA);
163                         }
164                 }
165                 inFASTA.close();
166                 outFASTA.close();
167                 
168                 if (m->control_pressed) {  remove(reverseFile.c_str()); return 0; }
169                 
170                 m->mothurOutEndLine();
171                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
172                 m->mothurOut(reverseFile); m->mothurOutEndLine();       outputNames.push_back(reverseFile); outputTypes["fasta"].push_back(reverseFile);
173                 m->mothurOutEndLine();
174
175                 
176                 return 0;
177                 
178         }
179         catch(exception& e) {
180                 m->errorOut(e, "ReverseSeqsCommand", "execute");
181                 exit(1);
182         }
183 }
184
185 //***************************************************************************************************************