5 * Created by Sarah Westcott on 4/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedutilities.h"
11 #include "sharedrabundvector.h"
12 #include "sharedordervector.h"
14 /**************************************************************************************************/
16 void SharedUtil::getSharedVectors(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
19 //delete each sharedrabundvector in lookup
20 for (int j = 0; j < lookup.size(); j++) {
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);
34 int numSeqs = order->size();
35 //sample all the members
36 for(int i=0;i<numSeqs;i++){
38 individual chosen = order->get(i);
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);
52 m->errorOut(e, "SharedUtil", "getSharedVectors");
56 /**************************************************************************************************/
58 void SharedUtil::getSharedVectorswithReplacement(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
61 //delete each sharedrabundvector in lookup
62 for (int j = 0; j < lookup.size(); j++) {
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);
75 int numSeqs = order->size();
77 //sample all the members
78 for(int i=0;i<numSeqs;i++){
80 int random = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
81 individual chosen = order->get(random);
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);
96 m->errorOut(e, "SharedUtil", "getSharedVectorswithReplacement");
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) {
106 sort(userGroups.begin(), userGroups.end());
107 sort(allGroups.begin(), allGroups.end());
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);
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]);
129 }else{//user has enter "all" and wants the default groups
131 for (int i = 0; i < allGroups.size(); i++) {
132 userGroups.push_back(allGroups[i]);
135 }else { //the user has not entered groups
136 for (int i = 0; i < allGroups.size(); i++) {
137 userGroups.push_back(allGroups[i]);
142 catch(exception& e) {
143 m->errorOut(e, "SharedUtil", "setGroups");
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) {
152 sort(userGroups.begin(), userGroups.end());
153 sort(allGroups.begin(), allGroups.end());
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);
167 }else{//user has enter "all" and wants the default groups
169 for (int i = 0; i < allGroups.size(); i++) {
170 userGroups.push_back(allGroups[i]);
173 }else { //the user has not entered groups
174 for (int i = 0; i < allGroups.size(); i++) {
175 userGroups.push_back(allGroups[i]);
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]);
190 catch(exception& e) {
191 m->errorOut(e, "SharedUtil", "setGroups");
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
201 sort(userGroups.begin(), userGroups.end());
202 sort(allGroups.begin(), allGroups.end());
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);
219 }else { //users wants all groups
221 for (int i=0; i < allGroups.size(); i++) {
222 if (allGroups[i] != "xxx") {
223 userGroups.push_back(allGroups[i]);
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]);
234 label += allGroups[i] + "-";
238 //rip extra - off allgroups
239 label = label.substr(0, label.length()-1);
240 if ((mode != "weighted") && (allGroups.size() > 10)) { label = "merged"; }
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]);
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();
255 for (int i=0; i < allGroups.size(); i++) {
256 if (allGroups[i] != "xxx") {
257 userGroups.push_back(allGroups[i]);
261 numGroups = userGroups.size();
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]);
274 if (numGroups != 1) { numGroups = userGroups.size(); }
277 catch(exception& e) {
278 m->errorOut(e, "SharedUtil", "setGroups");
282 /**************************************************************************************/
283 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
285 sort(userGroups.begin(), userGroups.end());
286 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
288 for (int i=0; i< userGroups.size(); 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]);
295 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
300 catch(exception& e) {
301 m->errorOut(e, "SharedUtil", "getCombos");
305 /**************************************************************************************/
306 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
308 for (int i = 0; i < groups.size(); i++) {
309 if (groupname == groups[i]) { return true; }
314 catch(exception& e) {
315 m->errorOut(e, "SharedUtil", "isValidGroup");
320 /**************************************************************************************/
321 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
324 for (int i = 0; i < userGroups.size(); i++) {
325 index[userGroups[i]] = i;
328 catch(exception& e) {
329 m->errorOut(e, "SharedUtil", "updateGroupIndex");
333 /**************************************************************************************/