]> git.donarmstrong.com Git - mothur.git/blob - makegroupcommand.cpp
1c04fecb477d09e0e6de73b1307a3878fcc8e4b3
[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 vector<string> MakeGroupCommand::getValidParameters(){  
15         try {
16                 string Array[] =  {"fasta", "output","groups","outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "MakeGroupCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 MakeGroupCommand::MakeGroupCommand(){   
27         try {
28                 abort = true; calledHelp = true; 
29                 vector<string> tempOutNames;
30                 outputTypes["group"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> MakeGroupCommand::getRequiredParameters(){       
39         try {
40                 string Array[] =  {"fasta","groups"};
41                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "MakeGroupCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> MakeGroupCommand::getRequiredFiles(){    
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "MakeGroupCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61
62 MakeGroupCommand::MakeGroupCommand(string option)  {
63         try {
64                 
65                 abort = false; calledHelp = false;   
66         
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; calledHelp = true; }
69                 
70                 else {
71                         
72                         //valid paramters for this command
73                         string AlignArray[] =  {"fasta","groups","output","outputdir","inputdir"};
74                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string, string> parameters = parser.getParameters(); 
78                         
79                         ValidParameters validParameter;
80                         map<string, string>::iterator it;
81                         
82                         //check to make sure all parameters are valid for command
83                         for (it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["group"] = tempOutNames;
90                 
91                         //if the user changes the input directory command factory will send this info to us in the output parameter 
92                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
93                         if (inputDir == "not found"){   inputDir = "";          }
94
95                         fastaFileName = validParameter.validFile(parameters, "fasta", false);
96                         if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
97                         else { 
98                                 m->splitAtDash(fastaFileName, fastaFileNames);
99                                 
100                                 //go through files and make sure they are good, if not, then disregard them
101                                 for (int i = 0; i < fastaFileNames.size(); i++) {
102                                         if (inputDir != "") {
103                                                 string path = m->hasPath(fastaFileNames[i]);
104                                                 //if the user has not given a path then, add inputdir. else leave path alone.
105                                                 if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
106                                         }
107         
108                                         ifstream in;
109                                         int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
110                                 
111                                         //if you can't open it, try default location
112                                         if (ableToOpen == 1) {
113                                                 if (m->getDefaultPath() != "") { //default path is set
114                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
115                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
116                                                         ifstream in2;
117                                                         ableToOpen = m->openInputFile(tryPath, in2, "noerror");
118                                                         in2.close();
119                                                         fastaFileNames[i] = tryPath;
120                                                 }
121                                         }
122                                         
123                                         //if you can't open it, try default location
124                                         if (ableToOpen == 1) {
125                                                 if (m->getOutputDir() != "") { //default path is set
126                                                         string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
127                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
128                                                         ifstream in2;
129                                                         ableToOpen = m->openInputFile(tryPath, in2, "noerror");
130                                                         in2.close();
131                                                         fastaFileNames[i] = tryPath;
132                                                 }
133                                         }
134                                         in.close();
135                                         
136                                         if (ableToOpen == 1) { 
137                                                 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
138                                                 //erase from file list
139                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
140                                                 i--;
141                                         }else{  filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));  }
142                                 }
143                                 
144                                 //prevent giantic file name
145                                 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
146                                 else {  filename += "groups";  }
147                                 
148                                 //make sure there is at least one valid file left
149                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
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"){  outputDir = "";         }
154                         
155                         output = validParameter.validFile(parameters, "output", false);                 
156                         if (output == "not found") { output = "";  }
157                         else{ filename = output; }
158                         
159                         groups = validParameter.validFile(parameters, "groups", false);                 
160                         if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true;  }
161                         else { m->splitAtDash(groups, groupsNames);     }
162
163                         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;  }
164                 }
165                 cout << "here" << endl;
166         }
167         catch(exception& e) {
168                 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
169                 exit(1);
170         }
171 }
172
173 //**********************************************************************************************************************
174
175 MakeGroupCommand::~MakeGroupCommand(){  }
176
177 //**********************************************************************************************************************
178
179 void MakeGroupCommand::help(){
180         try {
181                 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
182                 m->mothurOut("The make.group command parameters are fasta, groups and output. Fasta and group are required.\n");
183                 m->mothurOut("The output parameter allows you to specify the name of groupfile created. \n");
184                 m->mothurOut("The make.group command should be in the following format: \n");
185                 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups). \n");
186                 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
187                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
188         }
189         catch(exception& e) {
190                 m->errorOut(e, "MakeGroupCommand", "help");
191                 exit(1);
192         }
193 }
194
195
196 //**********************************************************************************************************************
197
198 int MakeGroupCommand::execute(){
199         try {
200                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
201                 
202                 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
203                         
204                 filename = outputDir + filename;
205                 
206                 ofstream out;
207                 m->openOutputFile(filename, out);
208                 
209                 for (int i = 0; i < fastaFileNames.size(); i++) {
210                 
211                         if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); return 0; }
212                         
213                         ifstream in;
214                         m->openInputFile(fastaFileNames[i], in);
215                         
216                         while (!in.eof()) {
217                                 
218                                 Sequence seq(in, "no align"); m->gobble(in);
219                                 
220                                 if (m->control_pressed) { outputTypes.clear();  in.close(); out.close(); remove(filename.c_str()); return 0; }
221                                 
222                                 if (seq.getName() != "") {      out << seq.getName() << '\t' << groupsNames[i] << endl;         }
223                         }
224                         in.close();
225                 }
226                 
227                 out.close();
228                 
229                 m->mothurOutEndLine();
230                 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename); 
231                 m->mothurOutEndLine();
232
233                 return 0;
234         }
235         catch(exception& e) {
236                 m->errorOut(e, "MakeGroupCommand", "execute");
237                 exit(1);
238         }
239 }
240 //**********************************************************************************************************************
241
242