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 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56 cout << "An unknown error has occurred in the SharedUtil class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
60 /**************************************************************************************************/
62 void SharedUtil::getSharedVectorswithReplacement(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
65 //delete each sharedrabundvector in lookup
66 for (int j = 0; j < lookup.size(); j++) {
72 //create and initialize vector of sharedvectors, one for each group
73 for (int i = 0; i < Groups.size(); i++) {
74 SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
75 temp->setLabel(order->getLabel());
76 temp->setGroup(Groups[i]);
77 lookup.push_back(temp);
80 int numSeqs = order->size();
82 //sample all the members
83 for(int i=0;i<numSeqs;i++){
85 int random = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
86 individual chosen = order->get(random);
89 //set info for sharedvector in chosens group
90 for (int j = 0; j < lookup.size(); j++) {
91 if (chosen.group == lookup[j]->getGroup()) {
92 abundance = lookup[j]->getAbundance(chosen.bin);
93 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
100 catch(exception& e) {
101 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getSharedVectorswithReplacement. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105 cout << "An unknown error has occurred in the SharedUtil class function getSharedVectorswithReplacement. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110 /**************************************************************************************************/
111 //need to have mode because different commands require different number of valid groups
112 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups) {
114 //cout << "groups size" << userGroups.size() << endl;
115 if (userGroups.size() != 0) {
116 if (userGroups[0] != "all") {
117 //check that groups are valid
118 for (int i = 0; i < userGroups.size(); i++) {
119 if (isValidGroup(userGroups[i], allGroups) != true) {
120 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
121 // erase the invalid group from userGroups
122 userGroups.erase(userGroups.begin()+i);
127 //if the user only entered invalid groups
128 if (userGroups.size() == 0) {
129 cout << "You provided no valid groups. I will run the command using all the groups in your groupfile." << endl;
130 for (int i = 0; i < allGroups.size(); i++) {
131 userGroups.push_back(allGroups[i]);
135 }else{//user has enter "all" and wants the default groups
137 for (int i = 0; i < allGroups.size(); i++) {
138 userGroups.push_back(allGroups[i]);
141 }else { //the user has not entered groups
142 for (int i = 0; i < allGroups.size(); i++) {
143 userGroups.push_back(allGroups[i]);
148 catch(exception& e) {
149 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
158 /**************************************************************************************************/
159 //need to have mode because different commands require different number of valid groups
160 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string mode) {
162 if (userGroups.size() != 0) {
163 if (userGroups[0] != "all") {
164 //check that groups are valid
165 for (int i = 0; i < userGroups.size(); i++) {
166 if (isValidGroup(userGroups[i], allGroups) != true) {
167 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
168 // erase the invalid group from userGroups
169 userGroups.erase(userGroups.begin()+i);
174 }else{//user has enter "all" and wants the default groups
176 for (int i = 0; i < allGroups.size(); i++) {
177 userGroups.push_back(allGroups[i]);
180 }else { //the user has not entered groups
181 for (int i = 0; i < allGroups.size(); i++) {
182 userGroups.push_back(allGroups[i]);
186 if ((mode == "collect") || (mode == "rarefact") || (mode == "summary") || (mode == "treegroup")) {
187 //if the user only entered invalid groups
188 if ((userGroups.size() == 0) || (userGroups.size() == 1)) {
189 cout << "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." << endl;
190 for (int i = 0; i < allGroups.size(); i++) {
191 userGroups.push_back(allGroups[i]);
197 catch(exception& e) {
198 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
202 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209 /**************************************************************************************/
210 //for parsimony and unifrac commands you set pairwise groups as well as an allgroups in calc
211 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string& label, int& numGroups, string mode){ //globaldata->Groups, your tree or group map, allgroups, mode
215 //if the user has not entered specific groups to analyze then do them all
216 if (userGroups.size() != 0) {
217 if (userGroups[0] != "all") {
218 //check that groups are valid
219 for (int i = 0; i < userGroups.size(); i++) {
220 if (isValidGroup(userGroups[i], allGroups) != true) {
221 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
222 // erase the invalid group from globaldata->Groups
223 userGroups.erase(userGroups.begin()+i);
227 }else { //users wants all groups
229 for (int i=0; i < allGroups.size(); i++) {
230 if (allGroups[i] != "xxx") {
231 userGroups.push_back(allGroups[i]);
235 }else { //the user has not entered groups
236 for (int i=0; i < allGroups.size(); i++) {
237 if (allGroups[i] != "xxx") {
238 if (mode == "weighted") {
239 userGroups.push_back(allGroups[i]);
242 label += allGroups[i] + "-";
246 //rip extra - off allgroups
247 label = label.substr(0, label.length()-1);
250 if (mode == "weighted") {
251 //if the user only entered invalid groups
252 if (userGroups.size() == 0) {
253 for (int i=0; i < allGroups.size(); i++) {
254 if (allGroups[i] != "xxx") {
255 userGroups.push_back(allGroups[i]);
258 cout << "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." << endl;
259 }else if (userGroups.size() == 1) {
260 cout << "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." << endl;
262 for (int i=0; i < allGroups.size(); i++) {
263 if (allGroups[i] != "xxx") {
264 userGroups.push_back(allGroups[i]);
268 numGroups = userGroups.size();
270 }else if ((mode == "unweighted") || (mode == "parsimony")) {
271 //if the user only entered invalid groups
272 if ((userGroups.size() == 0) && (numGroups == 0)) {
273 cout << "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." << endl;
274 for (int i = 0; i < allGroups.size(); i++) {
275 if (allGroups[i] != "xxx") {
276 userGroups.push_back(allGroups[i]);
281 if (numGroups != 1) { numGroups = userGroups.size(); }
284 catch(exception& e) {
285 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293 /**************************************************************************************/
294 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
296 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
298 for (int i=0; i< userGroups.size(); i++) {
300 for (int l = i+1; l < userGroups.size(); l++) {
301 //set group comparison labels
302 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
306 catch(exception& e) {
307 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311 cout << "An unknown error has occurred in the SharedUtil class function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315 /**************************************************************************************/
316 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
318 for (int i = 0; i < groups.size(); i++) {
319 if (groupname == groups[i]) { return true; }
324 catch(exception& e) {
325 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
329 cout << "An unknown error has occurred in the SharedUtil class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
334 /**************************************************************************************/
335 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
338 for (int i = 0; i < userGroups.size(); i++) {
339 index[userGroups[i]] = i;
342 catch(exception& e) {
343 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
347 cout << "An unknown error has occurred in the SharedUtil class function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";