]> git.donarmstrong.com Git - mothur.git/blob - countgroupscommand.cpp
changing command name classify.shared to classifyrf.shared
[mothur.git] / countgroupscommand.cpp
1 /*
2  *  countgroupscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 8/9/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "countgroupscommand.h"
11 #include "sharedutilities.h"
12 #include "inputdata.h"
13
14 //**********************************************************************************************************************
15 vector<string> CountGroupsCommand::setParameters(){     
16         try {
17                 CommandParameter pshared("shared", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pshared);
18                 CommandParameter pgroup("group", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pgroup);
19         CommandParameter pcount("count", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pcount);
20                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
21                 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
22                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
23                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
24                 
25                 vector<string> myArray;
26                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
27                 return myArray;
28         }
29         catch(exception& e) {
30                 m->errorOut(e, "CountGroupsCommand", "setParameters");
31                 exit(1);
32         }
33 }
34 //**********************************************************************************************************************
35 string CountGroupsCommand::getOutputPattern(string type) {
36     try {
37         string pattern = "";
38         
39         if (type == "summary") {  pattern = "[filename],count.summary"; }
40         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
41         
42         return pattern;
43     }
44     catch(exception& e) {
45         m->errorOut(e, "PrimerDesignCommand", "getOutputPattern");
46         exit(1);
47     }
48 }
49 //**********************************************************************************************************************
50 string CountGroupsCommand::getHelpString(){     
51         try {
52                 string helpString = "";
53                 helpString += "The count.groups command counts sequences from a specific group or set of groups from the following file types: group, count or shared file.\n";
54                 helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
55                 helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
56                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like.  You can separate group names with dashes.\n";
57                 helpString += "The count.groups command should be in the following format: count.groups(accnos=yourAccnos, group=yourGroupFile).\n";
58                 helpString += "Example count.groups(accnos=amazon.accnos, group=amazon.groups).\n";
59                 helpString += "or count.groups(groups=pasture, group=amazon.groups).\n";
60                 helpString += "Note: No spaces between parameter labels (i.e. group), '=' and parameters (i.e.yourGroupFile).\n";
61                 return helpString;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "CountGroupsCommand", "getHelpString");
65                 exit(1);
66         }
67 }
68 //**********************************************************************************************************************
69 CountGroupsCommand::CountGroupsCommand(){       
70         try {
71                 abort = true; calledHelp = true;
72                 setParameters();
73         vector<string> tempOutNames;
74                 outputTypes["summary"] = tempOutNames;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
78                 exit(1);
79         }
80 }
81 //**********************************************************************************************************************
82 CountGroupsCommand::CountGroupsCommand(string option)  {
83         try {
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                         //if the user changes the output directory command factory will send this info to us in the output parameter 
105                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
106                         
107                         //if the user changes the input directory command factory will send this info to us in the output parameter 
108                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
109                         if (inputDir == "not found"){   inputDir = "";          }
110                         else {
111                                 string path;
112                                 it = parameters.find("accnos");
113                                 //user has given a template file
114                                 if(it != parameters.end()){ 
115                                         path = m->hasPath(it->second);
116                                         //if the user has not given a path then, add inputdir. else leave path alone.
117                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
118                                 }
119                                 
120                                 it = parameters.find("group");
121                                 //user has given a template file
122                                 if(it != parameters.end()){ 
123                                         path = m->hasPath(it->second);
124                                         //if the user has not given a path then, add inputdir. else leave path alone.
125                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
126                                 }
127                                 
128                                 it = parameters.find("shared");
129                                 //user has given a template file
130                                 if(it != parameters.end()){ 
131                                         path = m->hasPath(it->second);
132                                         //if the user has not given a path then, add inputdir. else leave path alone.
133                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
134                                 }
135                 
136                 it = parameters.find("count");
137                                 //user has given a template file
138                                 if(it != parameters.end()){ 
139                                         path = m->hasPath(it->second);
140                                         //if the user has not given a path then, add inputdir. else leave path alone.
141                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
142                                 }
143                         }
144                         
145             vector<string> tempOutNames;
146             outputTypes["summary"] = tempOutNames;
147                         
148                         //check for required parameters
149                         accnosfile = validParameter.validFile(parameters, "accnos", true);
150                         if (accnosfile == "not open") { abort = true; }
151                         else if (accnosfile == "not found") {  accnosfile = ""; }
152                         else { m->setAccnosFile(accnosfile); }
153                         
154                         groups = validParameter.validFile(parameters, "groups", false);                 
155                         if (groups == "not found") { groups = ""; }
156                         else {
157                                 m->splitAtDash(groups, Groups);
158                                 m->setGroups(Groups);
159                         }
160                         
161                         sharedfile = validParameter.validFile(parameters, "shared", true);
162                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
163                         else if (sharedfile == "not found") {  sharedfile = "";  }
164                         else { m->setSharedFile(sharedfile); }
165                         
166                         groupfile = validParameter.validFile(parameters, "group", true);
167                         if (groupfile == "not open") { groupfile = ""; abort = true; }
168                         else if (groupfile == "not found") {    groupfile = ""; }
169                         else { m->setGroupFile(groupfile); }
170             
171             countfile = validParameter.validFile(parameters, "count", true);
172             if (countfile == "not open") { countfile = ""; abort = true; }
173             else if (countfile == "not found") { countfile = "";  }     
174             else { 
175                 m->setCountTableFile(countfile); 
176                 CountTable ct;
177                 if (!ct.testGroups(countfile)) { m->mothurOut("[ERROR]: Your count file does not have any group information, aborting."); m->mothurOutEndLine(); abort=true; }
178             }
179             
180             if ((groupfile != "") && (countfile != "")) {
181                 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
182             }
183
184                         
185                         if ((sharedfile == "") && (groupfile == "") && (countfile == "")) { 
186                                 //give priority to shared, then group
187                                 sharedfile = m->getSharedFile(); 
188                                 if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
189                                 else { 
190                                         groupfile = m->getGroupFile(); 
191                                         if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
192                                         else { 
193                                                 countfile = m->getCountTableFile(); 
194                         if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
195                         else { 
196                             m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
197                         }
198                                         }
199                                 }
200                         }
201                         
202                         if ((accnosfile == "") && (Groups.size() == 0)) { Groups.push_back("all"); m->setGroups(Groups); }
203                 }
204                 
205         }
206         catch(exception& e) {
207                 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
208                 exit(1);
209         }
210 }
211 //**********************************************************************************************************************
212
213 int CountGroupsCommand::execute(){
214         try {
215                 
216                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
217                 
218                 //get groups you want to remove
219                 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
220                 
221                 if (groupfile != "") {
222             map<string, string> variables; 
223             string thisOutputDir = outputDir;
224             if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
225             variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
226             string outputFileName = getOutputFileName("summary", variables);
227             outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
228             ofstream out;
229             m->openOutputFile(outputFileName, out);
230             
231                         GroupMap groupMap(groupfile);
232                         groupMap.readMap();
233                         
234                         //make sure groups are valid
235                         //takes care of user setting groupNames that are invalid or setting groups=all
236                         SharedUtil util;
237                         vector<string> nameGroups = groupMap.getNamesOfGroups();
238                         util.setGroups(Groups, nameGroups);
239                         
240             int total = 0;
241                         for (int i = 0; i < Groups.size(); i++) {
242                 int num = groupMap.getNumSeqs(Groups[i]);
243                 total += num;
244                                 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
245                 out << Groups[i] << '\t' << num << endl;
246                         }
247             out.close();
248             m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
249                 }
250         
251         if (m->control_pressed) { return 0; }
252         
253         if (countfile != "") {
254             map<string, string> variables; 
255             string thisOutputDir = outputDir;
256             if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
257             variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
258             string outputFileName = getOutputFileName("summary", variables);
259             outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
260             ofstream out;
261             m->openOutputFile(outputFileName, out);
262             
263                         CountTable ct;
264                         ct.readTable(countfile, true);
265             
266                         //make sure groups are valid
267                         //takes care of user setting groupNames that are invalid or setting groups=all
268                         SharedUtil util;
269                         vector<string> nameGroups = ct.getNamesOfGroups();
270                         util.setGroups(Groups, nameGroups);
271                         
272             int total = 0;
273                         for (int i = 0; i < Groups.size(); i++) {
274                 int num = ct.getGroupCount(Groups[i]);
275                 total += num;
276                                 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
277                 out << Groups[i] << '\t' << num << endl;
278                         }
279             out.close();
280             
281             m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
282                 }
283                 
284                 if (m->control_pressed) { return 0; }
285                 
286                 if (sharedfile != "")           {               
287                         InputData input(sharedfile, "sharedfile");
288                         vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
289                         
290             map<string, string> variables; 
291             string thisOutputDir = outputDir;
292             if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
293             variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
294             string outputFileName = getOutputFileName("summary", variables);
295             outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
296             ofstream out;
297             m->openOutputFile(outputFileName, out);
298             
299             int total = 0;
300                         for (int i = 0; i < lookup.size(); i++) {
301                 int num = lookup[i]->getNumSeqs();
302                 total += num;
303                                 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(num) + "."); m->mothurOutEndLine();
304                                 delete lookup[i];
305                 out << lookup[i]->getGroup() << '\t' << num << endl;
306                         }
307             out.close();
308                         
309             m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
310                 }
311                         
312         m->mothurOutEndLine();
313                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
314                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
315                 m->mothurOutEndLine();
316         
317                 return 0;               
318         }
319         
320         catch(exception& e) {
321                 m->errorOut(e, "CountGroupsCommand", "execute");
322                 exit(1);
323         }
324 }
325 //**********************************************************************************************************************
326
327