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