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