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