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