]> git.donarmstrong.com Git - mothur.git/blob - reversecommand.cpp
created mothurOut class to handle logfiles
[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", "outputdir","inputdir"};
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                         map<string,string>::iterator it;
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                         //if the user changes the input directory command factory will send this info to us in the output parameter 
40                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
41                         if (inputDir == "not found"){   inputDir = "";          }
42                         else {
43                                 string path;
44                                 it = parameters.find("fasta");
45                                 //user has given a template file
46                                 if(it != parameters.end()){ 
47                                         path = hasPath(it->second);
48                                         //if the user has not given a path then, add inputdir. else leave path alone.
49                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
50                                 }
51                         }
52
53                         //check for required parameters
54                         fasta = validParameter.validFile(parameters, "fasta", true);
55                         if (fasta == "not open") { abort = true; }
56                         else if (fasta == "not found") { fasta = ""; m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true;  }       
57                         
58                         //if the user changes the output directory command factory will send this info to us in the output parameter 
59                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
60                                 outputDir = ""; 
61                                 outputDir += hasPath(fasta); //if user entered a file with a path then preserve it      
62                         }
63
64                 }
65         }
66         catch(exception& e) {
67                 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
68                 exit(1);
69         }
70 }
71 //**********************************************************************************************************************
72
73 void ReverseSeqsCommand::help(){
74         try {
75                 m->mothurOut("The reverse.seqs command reads a fastafile and ....\n");
76                 m->mothurOut("The reverse.seqs command parameter is fasta and it is required.\n");
77                 m->mothurOut("The reverse.seqs command should be in the following format: \n");
78                 m->mothurOut("reverse.seqs(fasta=yourFastaFile) \n");   
79         }
80         catch(exception& e) {
81                 m->errorOut(e, "ReverseSeqsCommand", "help");
82                 exit(1);
83         }
84 }
85
86 //***************************************************************************************************************
87
88 ReverseSeqsCommand::~ReverseSeqsCommand(){      /*      do nothing      */      }
89
90 //***************************************************************************************************************
91
92
93 int ReverseSeqsCommand::execute(){
94         try{
95                 
96                 if (abort == true) { return 0; }
97                 
98                 ifstream inFASTA;
99                 openInputFile(fasta, inFASTA);
100                 
101                 ofstream outFASTA;
102                 string reverseFile = outputDir + getRootName(getSimpleName(fasta)) + "rc" + getExtension(fasta);
103                 openOutputFile(reverseFile, outFASTA);
104                 
105                 while(!inFASTA.eof()){
106                         Sequence currSeq(inFASTA);  gobble(inFASTA);
107                         if (currSeq.getName() != "") {
108                                 currSeq.reverseComplement();
109                                 currSeq.printSequence(outFASTA);
110                         }
111                 }
112                 inFASTA.close();
113                 outFASTA.close();
114                 
115                 m->mothurOutEndLine();
116                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
117                 m->mothurOut(reverseFile); m->mothurOutEndLine();       
118                 m->mothurOutEndLine();
119
120                 
121                 return 0;
122                 
123         }
124         catch(exception& e) {
125                 m->errorOut(e, "ReverseSeqsCommand", "execute");
126                 exit(1);
127         }
128 }
129
130 //***************************************************************************************************************