]> git.donarmstrong.com Git - mothur.git/blob - degapseqscommand.cpp
Revert to previous commit
[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 DegapSeqsCommand::DegapSeqsCommand(){   
50         try {
51                 abort = true; calledHelp = true; 
52                 setParameters();
53                 vector<string> tempOutNames;
54                 outputTypes["fasta"] = tempOutNames;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
58                 exit(1);
59         }
60 }
61 //***************************************************************************************************************
62 DegapSeqsCommand::DegapSeqsCommand(string option)  {
63         try {
64                 abort = false; calledHelp = false;   
65                 
66                 //allow user to run help
67                 if(option == "help") { help(); abort = true; calledHelp = true; }
68                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
69                 
70                 else {
71                         vector<string> myArray = setParameters();
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") {                                 
96                                 fastafile = m->getFastaFile(); 
97                                 if (fastafile != "") { fastaFileNames.push_back(fastafile); m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
98                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
99                         }
100                         else { 
101                                 m->splitAtDash(fastafile, fastaFileNames);
102                                 
103                                 //go through files and make sure they are good, if not, then disregard them
104                                 for (int i = 0; i < fastaFileNames.size(); i++) {
105                                         
106                                         bool ignore = false;
107                                         if (fastaFileNames[i] == "current") { 
108                                                 fastaFileNames[i] = m->getFastaFile(); 
109                                                 if (fastaFileNames[i] != "") {  m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
110                                                 else {  
111                                                         m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
112                                                         //erase from file list
113                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
114                                                         i--;
115                                                 }
116                                         }
117                                         
118                                         if (!ignore) {
119                                                 if (inputDir != "") {
120                                                         string path = m->hasPath(fastaFileNames[i]);
121                                                         //if the user has not given a path then, add inputdir. else leave path alone.
122                                                         if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
123                                                 }
124                 
125                                                 ifstream in;
126                                                 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
127                                         
128                                                 //if you can't open it, try default location
129                                                 if (ableToOpen == 1) {
130                                                         if (m->getDefaultPath() != "") { //default path is set
131                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
132                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
133                                                                 ifstream in2;
134                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
135                                                                 in2.close();
136                                                                 fastaFileNames[i] = tryPath;
137                                                         }
138                                                 }
139                                                 
140                                                 //if you can't open it, try default location
141                                                 if (ableToOpen == 1) {
142                                                         if (m->getOutputDir() != "") { //default path is set
143                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
144                                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
145                                                                 ifstream in2;
146                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
147                                                                 in2.close();
148                                                                 fastaFileNames[i] = tryPath;
149                                                         }
150                                                 }
151                                                 
152                                                 in.close();
153                                                 
154                                                 if (ableToOpen == 1) { 
155                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
156                                                         //erase from file list
157                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
158                                                         i--;
159                                                 }else { m->setFastaFile(fastaFileNames[i]); }
160                                         }
161                                 }
162                                 
163                                 //make sure there is at least one valid file left
164                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
165                         }
166
167                         
168                         //if the user changes the output directory command factory will send this info to us in the output parameter 
169                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
170                                 outputDir = ""; 
171                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
172                         }
173
174                 }
175         }
176         catch(exception& e) {
177                 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
178                 exit(1);
179         }
180 }
181 //***************************************************************************************************************
182 int DegapSeqsCommand::execute(){
183         try{
184                 
185                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
186                 
187                 for (int s = 0; s < fastaFileNames.size(); s++) {
188                                 
189                         m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
190                         ifstream inFASTA;
191                         m->openInputFile(fastaFileNames[s], inFASTA);
192                         
193                         ofstream outFASTA;
194                         string tempOutputDir = outputDir;
195                         if (outputDir == "") { tempOutputDir = m->hasPath(fastaFileNames[s]); }
196                         string degapFile = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "ng.fasta";
197                         m->openOutputFile(degapFile, outFASTA);
198                         
199                         while(!inFASTA.eof()){
200                                 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; }
201                                  
202                                 Sequence currSeq(inFASTA);  m->gobble(inFASTA);
203                                 if (currSeq.getName() != "") {
204                                         outFASTA << ">" << currSeq.getName() << endl;
205                                         outFASTA << currSeq.getUnaligned() << endl;
206                                 }
207                         }
208                         inFASTA.close();
209                         outFASTA.close();
210                         
211                         outputNames.push_back(degapFile); outputTypes["fasta"].push_back(degapFile);
212                         
213                         if (m->control_pressed) {  outputTypes.clear(); m->mothurRemove(degapFile); for (int j = 0; j < outputNames.size(); j++) {      m->mothurRemove(outputNames[j]);        } return 0; }
214                 }
215                 
216                 //set fasta file as new current fastafile
217                 string current = "";
218                 itTypes = outputTypes.find("fasta");
219                 if (itTypes != outputTypes.end()) {
220                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
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