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