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