]> git.donarmstrong.com Git - mothur.git/blob - degapseqscommand.cpp
fixes while testing
[mothur.git] / degapseqscommand.cpp
1 /*
2  *  degapseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 6/21/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "degapseqscommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> DegapSeqsCommand::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, "DegapSeqsCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 DegapSeqsCommand::DegapSeqsCommand(){   
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, "DegapSeqsCommand", "DegapSeqsCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> DegapSeqsCommand::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, "DegapSeqsCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> DegapSeqsCommand::getRequiredFiles(){    
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "DegapSeqsCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //***************************************************************************************************************
62 DegapSeqsCommand::DegapSeqsCommand(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                         
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                         
94                         //check for required parameters
95                         fastafile = validParameter.validFile(parameters, "fasta", false);
96                         if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the degap.seqs command."); m->mothurOutEndLine(); abort = true;  }
97                         else { 
98                                 m->splitAtDash(fastafile, fastaFileNames);
99                                 
100                                 //go through files and make sure they are good, if not, then disregard them
101                                 for (int i = 0; i < fastaFileNames.size(); i++) {
102                                         if (inputDir != "") {
103                                                 string path = m->hasPath(fastaFileNames[i]);
104                                                 //if the user has not given a path then, add inputdir. else leave path alone.
105                                                 if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
106                                         }
107         
108                                         ifstream in;
109                                         int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
110                                 
111                                         //if you can't open it, try default location
112                                         if (ableToOpen == 1) {
113                                                 if (m->getDefaultPath() != "") { //default path is set
114                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
115                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
116                                                         ifstream in2;
117                                                         ableToOpen = m->openInputFile(tryPath, in2, "noerror");
118                                                         in2.close();
119                                                         fastaFileNames[i] = tryPath;
120                                                 }
121                                         }
122                                         
123                                         //if you can't open it, try default location
124                                         if (ableToOpen == 1) {
125                                                 if (m->getOutputDir() != "") { //default path is set
126                                                         string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
127                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
128                                                         ifstream in2;
129                                                         ableToOpen = m->openInputFile(tryPath, in2, "noerror");
130                                                         in2.close();
131                                                         fastaFileNames[i] = tryPath;
132                                                 }
133                                         }
134                                         
135                                         in.close();
136                                         
137                                         if (ableToOpen == 1) { 
138                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
139                                                 //erase from file list
140                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
141                                                 i--;
142                                         }
143                                 }
144                                 
145                                 //make sure there is at least one valid file left
146                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
147                         }
148
149                         
150                         //if the user changes the output directory command factory will send this info to us in the output parameter 
151                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
152                                 outputDir = ""; 
153                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
154                         }
155
156                 }
157         }
158         catch(exception& e) {
159                 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
160                 exit(1);
161         }
162 }
163 //**********************************************************************************************************************
164
165 void DegapSeqsCommand::help(){
166         try {
167                 m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n");
168                 m->mothurOut("The degap.seqs command parameter is fasta.\n");
169                 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
170                 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
171                 m->mothurOut("The degap.seqs command should be in the following format: \n");
172                 m->mothurOut("degap.seqs(fasta=yourFastaFile) \n");     
173                 m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n");
174                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");     
175         }
176         catch(exception& e) {
177                 m->errorOut(e, "DegapSeqsCommand", "help");
178                 exit(1);
179         }
180 }
181
182 //***************************************************************************************************************
183
184 DegapSeqsCommand::~DegapSeqsCommand(){  /*      do nothing      */      }
185
186 //***************************************************************************************************************
187
188
189 int DegapSeqsCommand::execute(){
190         try{
191                 
192                 if (abort == true) { return 0; }
193                 
194                 for (int s = 0; s < fastaFileNames.size(); s++) {
195                                 
196                         m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
197                         ifstream inFASTA;
198                         m->openInputFile(fastaFileNames[s], inFASTA);
199                         
200                         ofstream outFASTA;
201                         string tempOutputDir = outputDir;
202                         if (outputDir == "") { tempOutputDir = m->hasPath(fastaFileNames[s]); }
203                         string degapFile = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "ng.fasta";
204                         m->openOutputFile(degapFile, outFASTA);
205                         
206                         while(!inFASTA.eof()){
207                                 if (m->control_pressed) {   outputTypes.clear(); inFASTA.close();  outFASTA.close(); remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) {  remove(outputNames[j].c_str()); } return 0; }
208                                  
209                                 Sequence currSeq(inFASTA);  m->gobble(inFASTA);
210                                 if (currSeq.getName() != "") {
211                                         outFASTA << ">" << currSeq.getName() << endl;
212                                         outFASTA << currSeq.getUnaligned() << endl;
213                                 }
214                         }
215                         inFASTA.close();
216                         outFASTA.close();
217                         
218                         outputNames.push_back(degapFile); outputTypes["fasta"].push_back(degapFile);
219                         
220                         if (m->control_pressed) {  outputTypes.clear(); remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) {       remove(outputNames[j].c_str()); } return 0; }
221                 }
222                 
223                 m->mothurOutEndLine();
224                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
225                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
226                 m->mothurOutEndLine();
227
228                 
229                 return 0;
230                 
231         }
232         catch(exception& e) {
233                 m->errorOut(e, "DegapSeqsCommand", "execute");
234                 exit(1);
235         }
236 }
237
238 //***************************************************************************************************************
239