]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
added modify names parameter to set.dir
[mothur.git] / makegroupcommand.cpp
1 /*
2  *  makegroupcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/7/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "makegroupcommand.h"
11 #include "sequence.hpp"
12
13
14 //**********************************************************************************************************************
15 vector<string> MakeGroupCommand::setParameters(){       
16         try {
17                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","group",false,true,true); parameters.push_back(pfasta);
18                 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false,true); parameters.push_back(pgroups);
19                 CommandParameter poutput("output", "String", "", "", "", "", "","",false,false); parameters.push_back(poutput);
20                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
21                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "MakeGroupCommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string MakeGroupCommand::getHelpString(){       
34         try {
35                 string helpString = "";
36                 helpString += "The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n";
37                 helpString += "The make.group command parameters are fasta, groups and output. Fasta and group are required.\n";
38                 helpString += "The output parameter allows you to specify the name of groupfile created. \n";
39                 helpString += "The make.group command should be in the following format: \n";
40                 helpString += "make.group(fasta=yourFastaFiles, groups=yourGroups). \n";
41                 helpString += "Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n";
42                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n";
43                 return helpString;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "MakeGroupCommand", "getHelpString");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 string MakeGroupCommand::getOutputPattern(string type) {
52     try {
53         string pattern = "";
54         
55         if (type == "group") {  pattern = "[filename],groups"; } 
56         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
57         
58         return pattern;
59     }
60     catch(exception& e) {
61         m->errorOut(e, "MakeGroupCommand", "getOutputPattern");
62         exit(1);
63     }
64 }
65 //**********************************************************************************************************************
66 MakeGroupCommand::MakeGroupCommand(){   
67         try {
68                 abort = true; calledHelp = true; 
69                 setParameters();
70                 vector<string> tempOutNames;
71                 outputTypes["group"] = tempOutNames;
72         }
73         catch(exception& e) {
74                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
75                 exit(1);
76         }
77 }
78
79 //**********************************************************************************************************************
80
81 MakeGroupCommand::MakeGroupCommand(string option)  {
82         try {
83                 
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                         //initialize outputTypes
105                         vector<string> tempOutNames;
106                         outputTypes["group"] = tempOutNames;
107                 
108                         //if the user changes the input directory command factory will send this info to us in the output parameter 
109                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
110                         if (inputDir == "not found"){   inputDir = "";          }
111
112                         fastaFileName = validParameter.validFile(parameters, "fasta", false);
113                         if (fastaFileName == "not found") {                             //if there is a current fasta file, use it
114                                 string filename = m->getFastaFile(); 
115                                 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
116                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
117                         }else { 
118                                 m->splitAtDash(fastaFileName, fastaFileNames);
119                                 
120                                 //go through files and make sure they are good, if not, then disregard them
121                                 for (int i = 0; i < fastaFileNames.size(); i++) {
122                                         
123                                         bool ignore = false;
124                                         if (fastaFileNames[i] == "current") { 
125                                                 fastaFileNames[i] = m->getFastaFile(); 
126                                                 if (fastaFileNames[i] != "") {  
127                                                         m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); 
128                                                         filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));
129                                                 }
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                                                 in.close();
172                                                 
173                                                 if (ableToOpen == 1) { 
174                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
175                                                         //erase from file list
176                                                         fastaFileNames.erase(fastaFileNames.begin()+i);
177                                                         i--;
178                                                 }else{  filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));  m->setFastaFile(fastaFileNames[i]); }
179                                         }
180                                 }
181                                 
182                                 //prevent giantic file name
183                 map<string, string> variables; 
184                 variables["[filename]"] = filename;
185                                 if (fastaFileNames.size() > 3) { variables["[filename]"] = outputDir + "merge"; }
186                                 filename = getOutputFileName("group",variables);  
187                                 
188                                 //make sure there is at least one valid file left
189                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
190                         }
191                         
192                         //if the user changes the output directory command factory will send this info to us in the output parameter 
193                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
194                         
195                         output = validParameter.validFile(parameters, "output", false);                 
196                         if (output == "not found") { output = "";  }
197                         else{ filename = output; }
198                         
199                         groups = validParameter.validFile(parameters, "groups", false);                 
200                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
201                         else { m->splitAtDash(groups, groupsNames);     }
202
203                         if (groupsNames.size() != fastaFileNames.size()) { m->mothurOut("You do not have the same number of valid fastfile files as groups.  This could be because we could not open a fastafile."); m->mothurOutEndLine(); abort = true;  }
204                 }
205         }
206         catch(exception& e) {
207                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
208                 exit(1);
209         }
210 }
211 //**********************************************************************************************************************
212
213 int MakeGroupCommand::execute(){
214         try {
215                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
216                 
217                 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
218                         
219                 filename = outputDir + filename;
220                 
221                 ofstream out;
222                 m->openOutputFile(filename, out);
223                 
224                 for (int i = 0; i < fastaFileNames.size(); i++) {
225                 
226                         if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); return 0; }
227                         
228                         ifstream in;
229                         m->openInputFile(fastaFileNames[i], in);
230                         
231                         while (!in.eof()) {
232                                 
233                                 Sequence seq(in, "no align"); m->gobble(in);
234                                 
235                                 if (m->control_pressed) { outputTypes.clear();  in.close(); out.close(); m->mothurRemove(filename); return 0; }
236                                 
237                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
238                         }
239                         in.close();
240                 }
241                 
242                 out.close();
243                 
244                 m->mothurOutEndLine();
245                 m->mothurOut("Output File Names: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename); 
246                 m->mothurOutEndLine();
247                 
248                 //set group file as new current groupfile
249                 string current = "";
250                 itTypes = outputTypes.find("group");
251                 if (itTypes != outputTypes.end()) {
252                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
253                 }
254                 
255                 return 0;
256         }
257         catch(exception& e) {
258                 m->errorOut(e, "MakeGroupCommand", "execute");
259                 exit(1);
260         }
261 }
262 //**********************************************************************************************************************
263
264