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) {
105 if (userGroups.size() != 0) {
106 if (userGroups[0] != "all") {
107 //check that groups are valid
108 for (int i = 0; i < userGroups.size(); i++) {
109 if (isValidGroup(userGroups[i], allGroups) != true) {
110 m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
111 // erase the invalid group from userGroups
112 userGroups.erase(userGroups.begin()+i);
117 //if the user only entered invalid groups
118 if (userGroups.size() == 0) {
119 m->mothurOut("You provided no valid groups. I will run the command using all the groups in your groupfile."); m->mothurOutEndLine();
120 for (int i = 0; i < allGroups.size(); i++) {
121 userGroups.push_back(allGroups[i]);
125 }else{//user has enter "all" and wants the default groups
127 for (int i = 0; i < allGroups.size(); i++) {
128 userGroups.push_back(allGroups[i]);
131 }else { //the user has not entered groups
132 for (int i = 0; i < allGroups.size(); i++) {
133 userGroups.push_back(allGroups[i]);
138 catch(exception& e) {
139 m->errorOut(e, "SharedUtil", "setGroups");
143 /**************************************************************************************************/
144 //need to have mode because different commands require different number of valid groups
145 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string mode) {
147 if (userGroups.size() != 0) {
148 if (userGroups[0] != "all") {
149 //check that groups are valid
150 for (int i = 0; i < userGroups.size(); i++) {
151 if (isValidGroup(userGroups[i], allGroups) != true) {
152 m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
153 // erase the invalid group from userGroups
154 userGroups.erase(userGroups.begin()+i);
159 }else{//user has enter "all" and wants the default groups
161 for (int i = 0; i < allGroups.size(); i++) {
162 userGroups.push_back(allGroups[i]);
165 }else { //the user has not entered groups
166 for (int i = 0; i < allGroups.size(); i++) {
167 userGroups.push_back(allGroups[i]);
171 if ((mode == "collect") || (mode == "rarefact") || (mode == "summary") || (mode == "treegroup")) {
172 //if the user only entered invalid groups
173 if ((userGroups.size() == 0) || (userGroups.size() == 1)) {
174 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();
175 for (int i = 0; i < allGroups.size(); i++) {
176 userGroups.push_back(allGroups[i]);
182 catch(exception& e) {
183 m->errorOut(e, "SharedUtil", "setGroups");
189 /**************************************************************************************/
190 //for parsimony and unifrac commands you set pairwise groups as well as an allgroups in calc
191 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string& label, int& numGroups, string mode){ //globaldata->Groups, your tree or group map, allgroups, mode
196 //if the user has not entered specific groups to analyze then do them all
197 if (userGroups.size() != 0) {
198 if (userGroups[0] != "all") {
199 //check that groups are valid
200 for (int i = 0; i < userGroups.size(); i++) {
201 if (isValidGroup(userGroups[i], allGroups) != true) {
202 m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
203 // erase the invalid group from globaldata->Groups
204 userGroups.erase(userGroups.begin()+i);
208 }else { //users wants all groups
210 for (int i=0; i < allGroups.size(); i++) {
211 if (allGroups[i] != "xxx") {
212 userGroups.push_back(allGroups[i]);
216 }else { //the user has not entered groups
217 for (int i=0; i < allGroups.size(); i++) {
218 if (allGroups[i] != "xxx") {
219 if (mode == "weighted") {
220 userGroups.push_back(allGroups[i]);
223 label += allGroups[i] + "-";
227 //rip extra - off allgroups
228 label = label.substr(0, label.length()-1);
229 if ((mode != "weighted") && (allGroups.size() > 10)) { label = "merged"; }
232 if (mode == "weighted") {
233 //if the user only entered invalid groups
234 if (userGroups.size() == 0) {
235 for (int i=0; i < allGroups.size(); i++) {
236 if (allGroups[i] != "xxx") {
237 userGroups.push_back(allGroups[i]);
240 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();
241 }else if (userGroups.size() == 1) {
242 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();
244 for (int i=0; i < allGroups.size(); i++) {
245 if (allGroups[i] != "xxx") {
246 userGroups.push_back(allGroups[i]);
250 numGroups = userGroups.size();
252 }else if ((mode == "unweighted") || (mode == "parsimony")) {
253 //if the user only entered invalid groups
254 if ((userGroups.size() == 0) && (numGroups == 0)) {
255 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();
256 for (int i = 0; i < allGroups.size(); i++) {
257 if (allGroups[i] != "xxx") {
258 userGroups.push_back(allGroups[i]);
263 if (numGroups != 1) { numGroups = userGroups.size(); }
266 catch(exception& e) {
267 m->errorOut(e, "SharedUtil", "setGroups");
271 /**************************************************************************************/
272 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
274 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
276 for (int i=0; i< userGroups.size(); i++) {
278 for (int l = 0; l < i; l++) {
279 if (userGroups[i] > userGroups[l]) {
280 //set group comparison labels
281 groupComb.push_back(userGroups[l] + "-" + userGroups[i]);
283 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
288 catch(exception& e) {
289 m->errorOut(e, "SharedUtil", "getCombos");
293 /**************************************************************************************/
294 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
296 for (int i = 0; i < groups.size(); i++) {
297 if (groupname == groups[i]) { return true; }
302 catch(exception& e) {
303 m->errorOut(e, "SharedUtil", "isValidGroup");
308 /**************************************************************************************/
309 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
312 for (int i = 0; i < userGroups.size(); i++) {
313 index[userGroups[i]] = i;
316 catch(exception& e) {
317 m->errorOut(e, "SharedUtil", "updateGroupIndex");