]> git.donarmstrong.com Git - mothur.git/blob - reversecommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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 //***************************************************************************************************************
15
16 ReverseSeqsCommand::ReverseSeqsCommand(string option){
17         try {
18                 abort = false;
19                 
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
24                         //valid paramters for this command
25                         string Array[] =  {"fasta"};
26                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                 
33                         //check to make sure all parameters are valid for command
34                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
36                         }
37                         
38                         //check for required parameters
39                         fasta = validParameter.validFile(parameters, "fasta", true);
40                         if (fasta == "not open") { abort = true; }
41                         else if (fasta == "not found") { fasta = ""; cout << "fasta is a required parameter for the reverse.seqs command." << endl; abort = true;  }    
42                         
43                 }
44         }
45         catch(exception& e) {
46                 cout << "Standard Error: " << e.what() << " has occurred in the ReverseSeqsCommand class Function ReverseSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47                 exit(1);
48         }
49         catch(...) {
50                 cout << "An unknown error has occurred in the ReverseSeqsCommand class function ReverseSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51                 exit(1);
52         }       
53 }
54 //**********************************************************************************************************************
55
56 void ReverseSeqsCommand::help(){
57         try {
58                 cout << "The reverse.seqs command reads a fastafile and ...." << "\n";
59                 cout << "The reverse.seqs command parameter is fasta and it is required." << "\n";
60                 cout << "The reverse.seqs command should be in the following format: " << "\n";
61                 cout << "reverse.seqs(fasta=yourFastaFile) " << "\n";   
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the ReverseSeqsCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the ReverseSeqsCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }       
71 }
72
73 //***************************************************************************************************************
74
75 ReverseSeqsCommand::~ReverseSeqsCommand(){      /*      do nothing      */      }
76
77 //***************************************************************************************************************
78
79
80 int ReverseSeqsCommand::execute(){
81         try{
82                 
83                 if (abort == true) { return 0; }
84                 
85                 ifstream inFASTA;
86                 openInputFile(fasta, inFASTA);
87                 
88                 ofstream outFASTA;
89                 string reverseFile = getRootName(fasta) + "rc" + getExtension(fasta);
90                 openOutputFile(reverseFile, outFASTA);
91                 
92                 while(!inFASTA.eof()){
93                         Sequence currSeq(inFASTA);
94                         currSeq.reverseComplement();
95                         currSeq.printSequence(outFASTA);
96                 }
97                 inFASTA.close();
98                 outFASTA.close();
99                 
100                 return 0;
101                 
102         }
103         catch(exception& e) {
104                 cout << "Standard Error: " << e.what() << " has occurred in the ReverseSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105                 exit(1);
106         }
107         catch(...) {
108                 cout << "An unknown error has occurred in the ReverseSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }
111 }
112
113 //***************************************************************************************************************