]> git.donarmstrong.com Git - mothur.git/blob - countgroupscommand.cpp
added multiple processors option for Windows users to align.seqs, dist.seqs, summary...
[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 paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
20                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
21                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "CountGroupsCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string CountGroupsCommand::getHelpString(){     
35         try {
36                 string helpString = "";
37                 helpString += "The count.groups command counts sequences from a specfic group or set of groups from the following file types: group or shared file.\n";
38                 helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
39                 helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
40                 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";
41                 helpString += "The count.groups command should be in the following format: count.groups(accnos=yourAccnos, group=yourGroupFile).\n";
42                 helpString += "Example count.groups(accnos=amazon.accnos, group=amazon.groups).\n";
43                 helpString += "or count.groups(groups=pasture, group=amazon.groups).\n";
44                 helpString += "Note: No spaces between parameter labels (i.e. group), '=' and parameters (i.e.yourGroupFile).\n";
45                 return helpString;
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "CountGroupsCommand", "getHelpString");
49                 exit(1);
50         }
51 }
52 //**********************************************************************************************************************
53 CountGroupsCommand::CountGroupsCommand(){       
54         try {
55                 abort = true; calledHelp = true;
56                 setParameters();
57         }
58         catch(exception& e) {
59                 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
60                 exit(1);
61         }
62 }
63 //**********************************************************************************************************************
64 CountGroupsCommand::CountGroupsCommand(string option)  {
65         try {
66                 abort = false; calledHelp = false;   
67                 
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; calledHelp = true; }
70                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
71                 
72                 else {
73                         vector<string> myArray = setParameters();
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string,string>::iterator it;
80                         
81                         //check to make sure all parameters are valid for command
82                         for (it = parameters.begin(); it != parameters.end(); it++) { 
83                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
84                         }
85                         
86                         //if the user changes the output directory command factory will send this info to us in the output parameter 
87                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
88                         
89                         //if the user changes the input directory command factory will send this info to us in the output parameter 
90                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
91                         if (inputDir == "not found"){   inputDir = "";          }
92                         else {
93                                 string path;
94                                 it = parameters.find("accnos");
95                                 //user has given a template file
96                                 if(it != parameters.end()){ 
97                                         path = m->hasPath(it->second);
98                                         //if the user has not given a path then, add inputdir. else leave path alone.
99                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
100                                 }
101                                 
102                                 it = parameters.find("group");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
108                                 }
109                                 
110                                 it = parameters.find("shared");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
116                                 }
117                         }
118                         
119                         
120                         //check for required parameters
121                         accnosfile = validParameter.validFile(parameters, "accnos", true);
122                         if (accnosfile == "not open") { abort = true; }
123                         else if (accnosfile == "not found") {  accnosfile = ""; }
124                         else { m->setAccnosFile(accnosfile); }
125                         
126                         groups = validParameter.validFile(parameters, "groups", false);                 
127                         if (groups == "not found") { groups = ""; }
128                         else {
129                                 m->splitAtDash(groups, Groups);
130                                 m->setGroups(Groups);
131                         }
132                         
133                         sharedfile = validParameter.validFile(parameters, "shared", true);
134                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
135                         else if (sharedfile == "not found") {  sharedfile = "";  }
136                         else { m->setSharedFile(sharedfile); }
137                         
138                         groupfile = validParameter.validFile(parameters, "group", true);
139                         if (groupfile == "not open") { groupfile = ""; abort = true; }
140                         else if (groupfile == "not found") {    groupfile = ""; }
141                         else { m->setGroupFile(groupfile); }    
142                         
143                         if ((sharedfile == "") && (groupfile == "")) { 
144                                 //give priority to shared, then group
145                                 sharedfile = m->getSharedFile(); 
146                                 if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
147                                 else { 
148                                         groupfile = m->getGroupFile(); 
149                                         if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
150                                         else { 
151                                                 m->mothurOut("You have no current groupfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
152                                         }
153                                 }
154                         }
155                         
156                         if ((accnosfile == "") && (Groups.size() == 0)) { Groups.push_back("all"); m->setGroups(Groups); }
157                 }
158                 
159         }
160         catch(exception& e) {
161                 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
162                 exit(1);
163         }
164 }
165 //**********************************************************************************************************************
166
167 int CountGroupsCommand::execute(){
168         try {
169                 
170                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
171                 
172                 //get groups you want to remove
173                 if (accnosfile != "") { readAccnos(); }
174                 
175                 if (groupfile != "") {
176                         GroupMap groupMap(groupfile);
177                         groupMap.readMap();
178                         
179                         //make sure groups are valid
180                         //takes care of user setting groupNames that are invalid or setting groups=all
181                         SharedUtil util;
182                         vector<string> nameGroups = groupMap.getNamesOfGroups();
183                         util.setGroups(Groups, nameGroups);
184                         
185                         for (int i = 0; i < Groups.size(); i++) {
186                                 m->mothurOut(Groups[i] + " contains " + toString(groupMap.getNumSeqs(Groups[i])) + "."); m->mothurOutEndLine();
187                         }
188                 }
189                 
190                 if (m->control_pressed) { return 0; }
191                 
192                 if (sharedfile != "")           {               
193                         InputData input(sharedfile, "sharedfile");
194                         vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
195                         
196                         for (int i = 0; i < lookup.size(); i++) {
197                                 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + "."); m->mothurOutEndLine();
198                                 delete lookup[i];
199                         }                       
200                 }
201                                 
202                 return 0;               
203         }
204         
205         catch(exception& e) {
206                 m->errorOut(e, "CountGroupsCommand", "execute");
207                 exit(1);
208         }
209 }
210 //**********************************************************************************************************************
211 void CountGroupsCommand::readAccnos(){
212         try {
213                 Groups.clear();
214                 
215                 ifstream in;
216                 m->openInputFile(accnosfile, in);
217                 string name;
218                 
219                 while(!in.eof()){
220                         in >> name;
221                         
222                         Groups.push_back(name);
223                         
224                         m->gobble(in);
225                 }
226                 in.close();             
227                 
228                 m->setGroups(Groups);
229                 
230         }
231         catch(exception& e) {
232                 m->errorOut(e, "CountGroupsCommand", "readAccnos");
233                 exit(1);
234         }
235 }
236 //**********************************************************************************************************************
237
238