File: sharedutilities.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (337 lines) | stat: -rw-r--r-- 10,956 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 *  sharedutilities.cpp
 *  Mothur
 *
 *  Created by Sarah Westcott on 4/9/09.
 *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
 *
 */

#include "sharedutilities.h"
#include "sharedrabundvector.h"
#include "sharedordervector.h"

/**************************************************************************************************/

void SharedUtil::getSharedVectors(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
	try {
	
		//delete each sharedrabundvector in lookup
		for (int j = 0; j < lookup.size(); j++) {
			delete lookup[j];
		}
		
		lookup.clear();
        
		sort(Groups.begin(), Groups.end());
        
		//create and initialize vector of sharedvectors, one for each group
		for (int i = 0; i < Groups.size(); i++) { 
			SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
			temp->setLabel(order->getLabel());
			temp->setGroup(Groups[i]);
			lookup.push_back(temp);
		}
	
		int numSeqs = order->size();
		//sample all the members
		for(int i=0;i<numSeqs;i++){
			//get first sample
			individual chosen = order->get(i);
			int abundance; 
					
			//set info for sharedvector in chosens group
			for (int j = 0; j < lookup.size(); j++) { 
				if (chosen.group == lookup[j]->getGroup()) {
					 abundance = lookup[j]->getAbundance(chosen.bin);
					 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
					 break;
				}
			}
		}
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "getSharedVectors");
		exit(1);
	}
}
/**************************************************************************************************/

void SharedUtil::getSharedVectorswithReplacement(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
	try {
	
		//delete each sharedrabundvector in lookup
		for (int j = 0; j < lookup.size(); j++) {
			delete lookup[j];
		}
		lookup.clear();
		
		//create and initialize vector of sharedvectors, one for each group
		for (int i = 0; i < Groups.size(); i++) { 
			SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
			temp->setLabel(order->getLabel());
			temp->setGroup(Groups[i]);
			lookup.push_back(temp);
		}
	
		int numSeqs = order->size();
		
		//sample all the members
		for(int i=0;i<numSeqs;i++){
			//get random number
			int random = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
			individual chosen = order->get(random);

			int abundance; 
			//set info for sharedvector in chosens group
			for (int j = 0; j < lookup.size(); j++) { 
				if (chosen.group == lookup[j]->getGroup()) {
					 abundance = lookup[j]->getAbundance(chosen.bin);
					 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
					 break;
				}
			}
		}
		
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "getSharedVectorswithReplacement");
		exit(1);
	}
}

/**************************************************************************************************/
//need to have mode because different commands require different number of valid groups
void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups) {
	try {
		
		sort(userGroups.begin(), userGroups.end());
		sort(allGroups.begin(), allGroups.end());
		
		if (userGroups.size() != 0) {
			if (userGroups[0] != "all") {
				//check that groups are valid
				for (int i = 0; i < userGroups.size(); i++) {
					if (isValidGroup(userGroups[i], allGroups) != true) {
						m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
						// erase the invalid group from userGroups
						userGroups.erase(userGroups.begin()+i);
						i--;
					}
				}
				
				//if the user only entered invalid groups
				if (userGroups.size() == 0) { 
					m->mothurOut("You provided no valid groups. I will run the command using all the groups in your file."); m->mothurOutEndLine();
					for (int i = 0; i < allGroups.size(); i++) {
						userGroups.push_back(allGroups[i]);
					}
				}

			}else{//user has enter "all" and wants the default groups
				userGroups.clear();
				for (int i = 0; i < allGroups.size(); i++) {
					userGroups.push_back(allGroups[i]);
				}
			}
		}else { //the user has not entered groups
			for (int i = 0; i < allGroups.size(); i++) {
				userGroups.push_back(allGroups[i]);
			}
		}
			
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "setGroups");
		exit(1);
	}
}
/**************************************************************************************************/
//need to have mode because different commands require different number of valid groups
void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string mode) {
	try {
		
		sort(userGroups.begin(), userGroups.end());
		sort(allGroups.begin(), allGroups.end());
		
		if (userGroups.size() != 0) {
			if (userGroups[0] != "all") {
				//check that groups are valid
				for (int i = 0; i < userGroups.size(); i++) {
					if (isValidGroup(userGroups[i], allGroups) != true) {
						m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
						// erase the invalid group from userGroups
						userGroups.erase(userGroups.begin()+i);
						i--;
					}
				}

			}else{//user has enter "all" and wants the default groups
				userGroups.clear();
				for (int i = 0; i < allGroups.size(); i++) {
					userGroups.push_back(allGroups[i]);
				}
			}
		}else { //the user has not entered groups
			for (int i = 0; i < allGroups.size(); i++) {
				userGroups.push_back(allGroups[i]);
			}
		}
			
		if ((mode == "collect") || (mode == "rarefact") || (mode == "summary") || (mode == "treegroup")) {
				//if the user only entered invalid groups
				if ((userGroups.size() == 0) || (userGroups.size() == 1)) { 
					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();
					for (int i = 0; i < allGroups.size(); i++) {
						userGroups.push_back(allGroups[i]);
					}
				}
		}
	
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "setGroups");
		exit(1);
	}
}


/**************************************************************************************/
//for parsimony and unifrac commands you set pairwise groups as well as an allgroups in calc
void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string& label, int& numGroups, string mode){  //globaldata->Groups, your tree or group map, allgroups, mode
	try {
		sort(userGroups.begin(), userGroups.end());
		sort(allGroups.begin(), allGroups.end());
		
		numGroups = 0;
		label = "";

		//if the user has not entered specific groups to analyze then do them all
		if (userGroups.size() != 0) {
			if (userGroups[0] != "all") {
				//check that groups are valid
				for (int i = 0; i < userGroups.size(); i++) {
					if (isValidGroup(userGroups[i], allGroups) != true) {
						m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
						// erase the invalid group from globaldata->Groups
						userGroups.erase(userGroups.begin()+i);
						i--;
					}
				}
			}else { //users wants all groups
				userGroups.clear();
				for (int i=0; i < allGroups.size(); i++) { 
					if (allGroups[i] != "xxx") {
						userGroups.push_back(allGroups[i]);
					}
				}
			}
		}else { //the user has not entered groups
			for (int i=0; i < allGroups.size(); i++) { 
				if (allGroups[i] != "xxx") {
					if (mode == "weighted") {
						userGroups.push_back(allGroups[i]);
					}else {
						numGroups = 1;
						label += allGroups[i] + "-";
					}
				}
			}
			//rip extra - off allgroups 
			label = label.substr(0, label.length()-1);
			if ((mode != "weighted") && (allGroups.size() > 10)) {  label = "merged";  }
		}
		
		if (mode == "weighted") {
			//if the user only entered invalid groups
			if (userGroups.size() == 0) { 
				for (int i=0; i < allGroups.size(); i++) { 
					if (allGroups[i] != "xxx") {
						userGroups.push_back(allGroups[i]);
					}
				}
				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();
			}else if (userGroups.size() == 1) { 
				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();
				userGroups.clear();
				for (int i=0; i < allGroups.size(); i++) { 
					if (allGroups[i] != "xxx") {
						userGroups.push_back(allGroups[i]);
					}
				}
			}
			numGroups = userGroups.size();
			
		}else if ((mode == "unweighted") || (mode == "parsimony")) {
				//if the user only entered invalid groups
				if ((userGroups.size() == 0) && (numGroups == 0)) { 
					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();
					for (int i = 0; i < allGroups.size(); i++) {
						if (allGroups[i] != "xxx") {
							userGroups.push_back(allGroups[i]);
						}
					}
				}
				
				if (numGroups != 1) { numGroups = userGroups.size(); }
		}
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "setGroups");
		exit(1);
	}
}
/**************************************************************************************/
void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
	try {
		sort(userGroups.begin(), userGroups.end());
		//calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
		numComp = 0;
		for (int i=0; i< userGroups.size(); i++) { 
			numComp += i; 
			for (int l = 0; l < i; l++) {
				if (userGroups[i] > userGroups[l]) {
					//set group comparison labels
					groupComb.push_back(userGroups[l] + "-" + userGroups[i]);
				}else{
					groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
				}
			}
		} 
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "getCombos");
		exit(1);
	}
}
/**************************************************************************************/
bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
	try {
		for (int i = 0; i < groups.size(); i++) {
			if (groupname == groups[i]) { return true; }
		}
		
		return false;
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "isValidGroup");
		exit(1);
	}
}

/**************************************************************************************/
void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
	try {
		index.clear();
		for (int i = 0; i < userGroups.size(); i++) {
			index[userGroups[i]] = i;
		}
	}
	catch(exception& e) {
		m->errorOut(e, "SharedUtil", "updateGroupIndex");
		exit(1);
	}
}
/**************************************************************************************/