]> git.donarmstrong.com Git - mothur.git/blob - sharedutilities.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / sharedutilities.cpp
1 /*
2  *  sharedutilities.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedutilities.h"
11 #include "sharedrabundvector.h"
12 #include "sharedordervector.h"
13
14 /**************************************************************************************************/
15
16 void SharedUtil::getSharedVectors(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
17         try {
18         
19                 //delete each sharedrabundvector in lookup
20                 for (int j = 0; j < lookup.size(); j++) {
21                         delete lookup[j];
22                 }
23                 
24                 lookup.clear();
25                 
26                 //create and initialize vector of sharedvectors, one for each group
27                 for (int i = 0; i < Groups.size(); i++) { 
28                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
29                         temp->setLabel(order->getLabel());
30                         temp->setGroup(Groups[i]);
31                         lookup.push_back(temp);
32                 }
33         
34                 int numSeqs = order->size();
35                 //sample all the members
36                 for(int i=0;i<numSeqs;i++){
37                         //get first sample
38                         individual chosen = order->get(i);
39                         int abundance; 
40                                         
41                         //set info for sharedvector in chosens group
42                         for (int j = 0; j < lookup.size(); j++) { 
43                                 if (chosen.group == lookup[j]->getGroup()) {
44                                          abundance = lookup[j]->getAbundance(chosen.bin);
45                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
46                                          break;
47                                 }
48                         }
49                 }
50         }
51         catch(exception& e) {
52                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53                 exit(1);
54         }
55         catch(...) {
56                 cout << "An unknown error has occurred in the SharedUtil class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57                 exit(1);
58         }
59 }
60 /**************************************************************************************************/
61
62 void SharedUtil::getSharedVectorswithReplacement(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
63         try {
64         
65                 //delete each sharedrabundvector in lookup
66                 for (int j = 0; j < lookup.size(); j++) {
67                         delete lookup[j];
68                 }
69                 
70                 lookup.clear();
71                 
72                 //create and initialize vector of sharedvectors, one for each group
73                 for (int i = 0; i < Groups.size(); i++) { 
74                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
75                         temp->setLabel(order->getLabel());
76                         temp->setGroup(Groups[i]);
77                         lookup.push_back(temp);
78                 }
79         
80                 int numSeqs = order->size();
81                 
82                 //sample all the members
83                 for(int i=0;i<numSeqs;i++){
84                         //get random number
85                         int random = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
86                         individual chosen = order->get(random);
87
88                         int abundance; 
89                         //set info for sharedvector in chosens group
90                         for (int j = 0; j < lookup.size(); j++) { 
91                                 if (chosen.group == lookup[j]->getGroup()) {
92                                          abundance = lookup[j]->getAbundance(chosen.bin);
93                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
94                                          break;
95                                 }
96                         }
97                 }
98                 
99         }
100         catch(exception& e) {
101                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getSharedVectorswithReplacement. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102                 exit(1);
103         }
104         catch(...) {
105                 cout << "An unknown error has occurred in the SharedUtil class function getSharedVectorswithReplacement. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106                 exit(1);
107         }
108 }
109
110 /**************************************************************************************************/
111 //need to have mode because different commands require different number of valid groups
112 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups) {
113         try {
114 //cout << "groups size" << userGroups.size() << endl;
115                 if (userGroups.size() != 0) {
116                         if (userGroups[0] != "all") {
117                                 //check that groups are valid
118                                 for (int i = 0; i < userGroups.size(); i++) {
119                                         if (isValidGroup(userGroups[i], allGroups) != true) {
120                                                 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
121                                                 // erase the invalid group from userGroups
122                                                 userGroups.erase(userGroups.begin()+i);
123                                                 i--;
124                                         }
125                                 }
126                                 
127                                 //if the user only entered invalid groups
128                                 if (userGroups.size() == 0) { 
129                                         cout << "You provided no valid groups. I will run the command using all the groups in your groupfile." << endl; 
130                                         for (int i = 0; i < allGroups.size(); i++) {
131                                                 userGroups.push_back(allGroups[i]);
132                                         }
133                                 }
134
135                         }else{//user has enter "all" and wants the default groups
136                                 userGroups.clear();
137                                 for (int i = 0; i < allGroups.size(); i++) {
138                                         userGroups.push_back(allGroups[i]);
139                                 }
140                         }
141                 }else { //the user has not entered groups
142                         for (int i = 0; i < allGroups.size(); i++) {
143                                 userGroups.push_back(allGroups[i]);
144                         }
145                 }
146                         
147         }
148         catch(exception& e) {
149                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150                 exit(1);
151         }
152         catch(...) {
153                 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
154                 exit(1);
155         }
156
157 }
158 /**************************************************************************************************/
159 //need to have mode because different commands require different number of valid groups
160 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string mode) {
161         try {
162                 if (userGroups.size() != 0) {
163                         if (userGroups[0] != "all") {
164                                 //check that groups are valid
165                                 for (int i = 0; i < userGroups.size(); i++) {
166                                         if (isValidGroup(userGroups[i], allGroups) != true) {
167                                                 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
168                                                 // erase the invalid group from userGroups
169                                                 userGroups.erase(userGroups.begin()+i);
170                                                 i--;
171                                         }
172                                 }
173
174                         }else{//user has enter "all" and wants the default groups
175                                 userGroups.clear();
176                                 for (int i = 0; i < allGroups.size(); i++) {
177                                         userGroups.push_back(allGroups[i]);
178                                 }
179                         }
180                 }else { //the user has not entered groups
181                         for (int i = 0; i < allGroups.size(); i++) {
182                                 userGroups.push_back(allGroups[i]);
183                         }
184                 }
185                         
186                 if ((mode == "collect") || (mode == "rarefact") || (mode == "summary") || (mode == "treegroup")) {
187                                 //if the user only entered invalid groups
188                                 if ((userGroups.size() == 0) || (userGroups.size() == 1)) { 
189                                         cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl; 
190                                         for (int i = 0; i < allGroups.size(); i++) {
191                                                 userGroups.push_back(allGroups[i]);
192                                         }
193                                 }
194                 }
195         
196         }
197         catch(exception& e) {
198                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }
201         catch(...) {
202                 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203                 exit(1);
204         }
205
206 }
207
208
209 /**************************************************************************************/
210 //for parsimony and unifrac commands you set pairwise groups as well as an allgroups in calc
211 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string& label, int& numGroups, string mode){  //globaldata->Groups, your tree or group map, allgroups, mode
212         try {
213                 numGroups = 0;
214                 label = "";
215
216                 //if the user has not entered specific groups to analyze then do them all
217                 if (userGroups.size() != 0) {
218                         if (userGroups[0] != "all") {
219                                 //check that groups are valid
220                                 for (int i = 0; i < userGroups.size(); i++) {
221                                         if (isValidGroup(userGroups[i], allGroups) != true) {
222                                                 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
223                                                 // erase the invalid group from globaldata->Groups
224                                                 userGroups.erase(userGroups.begin()+i);
225                                                 i--;
226                                         }
227                                 }
228                         }else { //users wants all groups
229                                 userGroups.clear();
230                                 for (int i=0; i < allGroups.size(); i++) { 
231                                         if (allGroups[i] != "xxx") {
232                                                 userGroups.push_back(allGroups[i]);
233                                         }
234                                 }
235                         }
236                 }else { //the user has not entered groups
237                         for (int i=0; i < allGroups.size(); i++) { 
238                                 if (allGroups[i] != "xxx") {
239                                         if (mode == "weighted") {
240                                                 userGroups.push_back(allGroups[i]);
241                                         }else {
242                                                 numGroups = 1;
243                                                 label += allGroups[i] + "-";
244                                         }
245                                 }
246                         }
247                         //rip extra - off allgroups 
248                         label = label.substr(0, label.length()-1);
249                 }
250                 
251                 if (mode == "weighted") {
252                         //if the user only entered invalid groups
253                         if (userGroups.size() == 0) { 
254                                 for (int i=0; i < allGroups.size(); i++) { 
255                                         if (allGroups[i] != "xxx") {
256                                                 userGroups.push_back(allGroups[i]);
257                                         }
258                                 }
259                                 cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl; 
260                         }else if (userGroups.size() == 1) { 
261                                 cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl;
262                                 userGroups.clear();
263                                 for (int i=0; i < allGroups.size(); i++) { 
264                                         if (allGroups[i] != "xxx") {
265                                                 userGroups.push_back(allGroups[i]);
266                                         }
267                                 }
268                         }
269                         numGroups = userGroups.size();
270                         
271                 }else if ((mode == "unweighted") || (mode == "parsimony")) {
272                                 //if the user only entered invalid groups
273                                 if ((userGroups.size() == 0) && (numGroups == 0)) { 
274                                         cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; 
275                                         for (int i = 0; i < allGroups.size(); i++) {
276                                                 if (allGroups[i] != "xxx") {
277                                                         userGroups.push_back(allGroups[i]);
278                                                 }
279                                         }
280                                 }
281                                 
282                                 if (numGroups != 1) { numGroups = userGroups.size(); }
283                 }
284         }
285         catch(exception& e) {
286                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287                 exit(1);
288         }
289         catch(...) {
290                 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291                 exit(1);
292         }
293 }
294 /**************************************************************************************/
295 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
296         try {
297                 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
298                 numComp = 0;
299                 for (int i=0; i< userGroups.size(); i++) { 
300                         numComp += i; 
301                         for (int l = i+1; l < userGroups.size(); l++) {
302                                 //set group comparison labels
303                                 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
304                         }
305                 } 
306         }
307         catch(exception& e) {
308                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
309                 exit(1);
310         }
311         catch(...) {
312                 cout << "An unknown error has occurred in the SharedUtil class function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313                 exit(1);
314         }
315 }
316 /**************************************************************************************/
317 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
318         try {
319                 for (int i = 0; i < groups.size(); i++) {
320                         if (groupname == groups[i]) { return true; }
321                 }
322                 
323                 return false;
324         }
325         catch(exception& e) {
326                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
327                 exit(1);
328         }
329         catch(...) {
330                 cout << "An unknown error has occurred in the SharedUtil class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331                 exit(1);
332         }
333 }
334
335 /**************************************************************************************/
336 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
337         try {
338                 index.clear();
339                 for (int i = 0; i < userGroups.size(); i++) {
340                         index[userGroups[i]] = i;
341                 }
342         }
343         catch(exception& e) {
344                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
345                 exit(1);
346         }
347         catch(...) {
348                 cout << "An unknown error has occurred in the SharedUtil class function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
349                 exit(1);
350         }
351 }