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