]> git.donarmstrong.com Git - mothur.git/blob - countgroupscommand.cpp
Merge remote-tracking branch 'mothur/master'
[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",false,false); parameters.push_back(pshared);
18                 CommandParameter pgroup("group", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none",false,false); parameters.push_back(pgroup);
19         CommandParameter pcount("count", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none",false,false); 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::getHelpString(){     
36         try {
37                 string helpString = "";
38                 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";
39                 helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
40                 helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
41                 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";
42                 helpString += "The count.groups command should be in the following format: count.groups(accnos=yourAccnos, group=yourGroupFile).\n";
43                 helpString += "Example count.groups(accnos=amazon.accnos, group=amazon.groups).\n";
44                 helpString += "or count.groups(groups=pasture, group=amazon.groups).\n";
45                 helpString += "Note: No spaces between parameter labels (i.e. group), '=' and parameters (i.e.yourGroupFile).\n";
46                 return helpString;
47         }
48         catch(exception& e) {
49                 m->errorOut(e, "CountGroupsCommand", "getHelpString");
50                 exit(1);
51         }
52 }
53 //**********************************************************************************************************************
54 CountGroupsCommand::CountGroupsCommand(){       
55         try {
56                 abort = true; calledHelp = true;
57                 setParameters();
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65 CountGroupsCommand::CountGroupsCommand(string option)  {
66         try {
67                 abort = false; calledHelp = false;   
68                 
69                 //allow user to run help
70                 if(option == "help") { help(); abort = true; calledHelp = true; }
71                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
72                 
73                 else {
74                         vector<string> myArray = setParameters();
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                         //if the user changes the output directory command factory will send this info to us in the output parameter 
88                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
89                         
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("accnos");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
101                                 }
102                                 
103                                 it = parameters.find("group");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
109                                 }
110                                 
111                                 it = parameters.find("shared");
112                                 //user has given a template file
113                                 if(it != parameters.end()){ 
114                                         path = m->hasPath(it->second);
115                                         //if the user has not given a path then, add inputdir. else leave path alone.
116                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
117                                 }
118                 
119                 it = parameters.find("count");
120                                 //user has given a template file
121                                 if(it != parameters.end()){ 
122                                         path = m->hasPath(it->second);
123                                         //if the user has not given a path then, add inputdir. else leave path alone.
124                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
125                                 }
126                         }
127                         
128                         
129                         //check for required parameters
130                         accnosfile = validParameter.validFile(parameters, "accnos", true);
131                         if (accnosfile == "not open") { abort = true; }
132                         else if (accnosfile == "not found") {  accnosfile = ""; }
133                         else { m->setAccnosFile(accnosfile); }
134                         
135                         groups = validParameter.validFile(parameters, "groups", false);                 
136                         if (groups == "not found") { groups = ""; }
137                         else {
138                                 m->splitAtDash(groups, Groups);
139                                 m->setGroups(Groups);
140                         }
141                         
142                         sharedfile = validParameter.validFile(parameters, "shared", true);
143                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
144                         else if (sharedfile == "not found") {  sharedfile = "";  }
145                         else { m->setSharedFile(sharedfile); }
146                         
147                         groupfile = validParameter.validFile(parameters, "group", true);
148                         if (groupfile == "not open") { groupfile = ""; abort = true; }
149                         else if (groupfile == "not found") {    groupfile = ""; }
150                         else { m->setGroupFile(groupfile); }
151             
152             countfile = validParameter.validFile(parameters, "count", true);
153             if (countfile == "not open") { countfile = ""; abort = true; }
154             else if (countfile == "not found") { countfile = "";  }     
155             else { 
156                 m->setCountTableFile(countfile); 
157                 CountTable ct;
158                 if (!ct.testGroups(countfile)) { m->mothurOut("[ERROR]: Your count file does not have any group information, aborting."); m->mothurOutEndLine(); abort=true; }
159             }
160             
161             if ((groupfile != "") && (countfile != "")) {
162                 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
163             }
164
165                         
166                         if ((sharedfile == "") && (groupfile == "") && (countfile == "")) { 
167                                 //give priority to shared, then group
168                                 sharedfile = m->getSharedFile(); 
169                                 if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
170                                 else { 
171                                         groupfile = m->getGroupFile(); 
172                                         if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
173                                         else { 
174                                                 countfile = m->getCountTableFile(); 
175                         if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
176                         else { 
177                             m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
178                         }
179                                         }
180                                 }
181                         }
182                         
183                         if ((accnosfile == "") && (Groups.size() == 0)) { Groups.push_back("all"); m->setGroups(Groups); }
184                 }
185                 
186         }
187         catch(exception& e) {
188                 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
189                 exit(1);
190         }
191 }
192 //**********************************************************************************************************************
193
194 int CountGroupsCommand::execute(){
195         try {
196                 
197                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
198                 
199                 //get groups you want to remove
200                 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
201                 
202                 if (groupfile != "") {
203                         GroupMap groupMap(groupfile);
204                         groupMap.readMap();
205                         
206                         //make sure groups are valid
207                         //takes care of user setting groupNames that are invalid or setting groups=all
208                         SharedUtil util;
209                         vector<string> nameGroups = groupMap.getNamesOfGroups();
210                         util.setGroups(Groups, nameGroups);
211                         
212             int total = 0;
213                         for (int i = 0; i < Groups.size(); i++) {
214                 int num = groupMap.getNumSeqs(Groups[i]);
215                 total += num;
216                                 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
217                         }
218             
219             m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
220                 }
221         
222         if (m->control_pressed) { return 0; }
223         
224         if (countfile != "") {
225                         CountTable ct;
226                         ct.readTable(countfile);
227             
228                         //make sure groups are valid
229                         //takes care of user setting groupNames that are invalid or setting groups=all
230                         SharedUtil util;
231                         vector<string> nameGroups = ct.getNamesOfGroups();
232                         util.setGroups(Groups, nameGroups);
233                         
234             int total = 0;
235                         for (int i = 0; i < Groups.size(); i++) {
236                 int num = ct.getGroupCount(Groups[i]);
237                 total += num;
238                                 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
239                         }
240             
241             m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
242                 }
243                 
244                 if (m->control_pressed) { return 0; }
245                 
246                 if (sharedfile != "")           {               
247                         InputData input(sharedfile, "sharedfile");
248                         vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
249                         
250             int total = 0;
251                         for (int i = 0; i < lookup.size(); i++) {
252                 int num = lookup[i]->getNumSeqs();
253                 total += num;
254                                 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(num) + "."); m->mothurOutEndLine();
255                                 delete lookup[i];
256                         }
257                         
258             m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
259                 }
260                                 
261                 return 0;               
262         }
263         
264         catch(exception& e) {
265                 m->errorOut(e, "CountGroupsCommand", "execute");
266                 exit(1);
267         }
268 }
269 //**********************************************************************************************************************
270
271