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