]> git.donarmstrong.com Git - mothur.git/blob - sharedutilities.cpp
454b7d4073396f74c804ad1bba68cbea57ac5f57
[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, string mode) {
113         try {
114                 if (userGroups.size() != 0) {
115                         if (userGroups[0] != "all") {
116                                 //check that groups are valid
117                                 for (int i = 0; i < userGroups.size(); i++) {
118                                         if (isValidGroup(userGroups[i], allGroups) != true) {
119                                                 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
120                                                 // erase the invalid group from userGroups
121                                                 userGroups.erase(userGroups.begin()+i);
122                                                 i--;
123                                         }
124                                 }
125
126                         }else{//user has enter "all" and wants the default groups
127                                 userGroups.clear();
128                                 for (int i = 0; i < allGroups.size(); i++) {
129                                         userGroups.push_back(allGroups[i]);
130                                 }
131                         }
132                 }else { //the user has not entered groups
133                         for (int i = 0; i < allGroups.size(); i++) {
134                                 userGroups.push_back(allGroups[i]);
135                         }
136                 }
137                         
138                 if (mode == "venn")  {
139                                 //if the user only entered invalid groups
140                                 if (userGroups.size() == 0) { 
141                                         if (allGroups.size() > 4) {
142                                                 cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using the first four groups in your groupfile." << endl; 
143                                                 for (int i = 0; i < 4; i++) {
144                                                         userGroups.push_back(allGroups[i]);
145                                                 }
146                                         }else {
147                                                 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; 
148                                                 for (int i = 0; i < allGroups.size(); i++) {
149                                                         userGroups.push_back(allGroups[i]);
150                                                 }
151                                         }
152                                 }
153                                 
154                                 //check to make sure their are only 4 groups
155                                 if (userGroups.size() > 4) {
156                                         cout << "You may only use up to 4 groups at a time with this command.  I will choose the first four and disregard the rest." << endl;
157                                         for (int i = 4; i < userGroups.size(); i++) {
158                                                 userGroups.erase(userGroups.begin()+i);
159                                         }
160                                 }
161                 }else if (mode == "heat") {
162                                 //if the user only entered invalid groups
163                                 if (userGroups.size() == 0) { 
164                                         cout << "When using the groups parameter you must have at least 1 valid groups. I will run the command using all the groups in your groupfile." << endl; 
165                                         for (int i = 0; i < allGroups.size(); i++) {
166                                                 userGroups.push_back(allGroups[i]);
167                                         }
168                                 }
169                 }else if ((mode == "collect") || (mode == "rarefact") || (mode == "summary") || (mode == "treegroup")) {
170                                 //if the user only entered invalid groups
171                                 if ((userGroups.size() == 0) || (userGroups.size() == 1)) { 
172                                         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; 
173                                         for (int i = 0; i < allGroups.size(); i++) {
174                                                 userGroups.push_back(allGroups[i]);
175                                         }
176                                 }
177                 }
178         
179         }
180         catch(exception& e) {
181                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182                 exit(1);
183         }
184         catch(...) {
185                 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }
188
189 }
190
191 /**************************************************************************************/
192 //for parsimony and unifrac commands you set pairwise groups as well as an allgroups in calc
193 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string& label, int& numGroups, string mode){  //globaldata->Groups, your tree or group map, allgroups, mode
194         try {
195                 numGroups = 0;
196                 label = "";
197                 //if the user has not entered specific groups to analyze then do them all
198                 if (userGroups.size() != 0) {
199                         if (userGroups[0] != "all") {
200                                 //check that groups are valid
201                                 for (int i = 0; i < userGroups.size(); i++) {
202                                         if (isValidGroup(userGroups[i], allGroups) != true) {
203                                                 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
204                                                 // erase the invalid group from globaldata->Groups
205                                                 userGroups.erase(userGroups.begin()+i);
206                                                 i--;
207                                         }
208                                 }
209                         }else { //users wants all groups
210                                 userGroups.clear();
211                                 for (int i=0; i < allGroups.size(); i++) { 
212                                         if (allGroups[i] != "xxx") {
213                                                 userGroups.push_back(allGroups[i]);
214                                         }
215                                 }
216                         }
217                 }else { //the user has not entered groups
218                         for (int i=0; i < allGroups.size(); i++) { 
219                                 if (allGroups[i] != "xxx") {
220                                         if (mode == "weighted") {
221                                                 userGroups.push_back(allGroups[i]);
222                                         }else {
223                                                 numGroups = 1;
224                                                 label += allGroups[i] + "-";
225                                         }
226                                 }
227                         }
228                         //rip extra - off allgroups 
229                         label = label.substr(0, label.length()-1);
230                 }
231                 
232                 if (mode == "weighted") {
233                         //if the user only entered invalid groups
234                         if (userGroups.size() == 0) { 
235                                 for (int i=0; i < allGroups.size(); i++) { 
236                                         if (allGroups[i] != "xxx") {
237                                                 userGroups.push_back(allGroups[i]);
238                                         }
239                                 }
240                                 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; 
241                         }else if (userGroups.size() == 1) { 
242                                 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;
243                                 userGroups.clear();
244                                 for (int i=0; i < allGroups.size(); i++) { 
245                                         if (allGroups[i] != "xxx") {
246                                                 userGroups.push_back(allGroups[i]);
247                                         }
248                                 }
249                         }
250                         numGroups = userGroups.size();
251                         
252                 }else if ((mode == "unweighted") || (mode == "parsimony")) {
253                                 //if the user only entered invalid groups
254                                 if ((userGroups.size() == 0) && (numGroups == 0)) { 
255                                         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; 
256                                         for (int i = 0; i < allGroups.size(); i++) {
257                                                 if (allGroups[i] != "xxx") {
258                                                         userGroups.push_back(allGroups[i]);
259                                                 }
260                                         }
261                                 }
262                                 
263                                 if (numGroups != 1) { numGroups = userGroups.size(); }
264                 }
265         }
266         catch(exception& e) {
267                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268                 exit(1);
269         }
270         catch(...) {
271                 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
272                 exit(1);
273         }
274 }
275 /**************************************************************************************/
276 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
277         try {
278                 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
279                 numComp = 0;
280                 for (int i=0; i< userGroups.size(); i++) { 
281                         numComp += i; 
282                         for (int l = i+1; l < userGroups.size(); l++) {
283                                 //set group comparison labels
284                                 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
285                         }
286                 } 
287         }
288         catch(exception& e) {
289                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }
292         catch(...) {
293                 cout << "An unknown error has occurred in the SharedUtil class function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
296 }
297 /**************************************************************************************/
298 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
299         try {
300                 for (int i = 0; i < groups.size(); i++) {
301                         if (groupname == groups[i]) { return true; }
302                 }
303                 
304                 return false;
305         }
306         catch(exception& e) {
307                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308                 exit(1);
309         }
310         catch(...) {
311                 cout << "An unknown error has occurred in the SharedUtil class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312                 exit(1);
313         }
314 }
315
316 /**************************************************************************************/
317 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
318         try {
319                 index.clear();
320                 for (int i = 0; i < userGroups.size(); i++) {
321                         index[userGroups[i]] = i;
322                 }
323         }
324         catch(exception& e) {
325                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
326                 exit(1);
327         }
328         catch(...) {
329                 cout << "An unknown error has occurred in the SharedUtil class function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
330                 exit(1);
331         }
332 }