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