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