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