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";
61 /**************************************************************************************************/
62 //need to have mode because different commands require different number of valid groups
63 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string mode) {
65 if (userGroups.size() != 0) {
66 if (userGroups[0] != "all") {
67 //check that groups are valid
68 for (int i = 0; i < userGroups.size(); i++) {
69 if (isValidGroup(userGroups[i], allGroups) != true) {
70 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
71 // erase the invalid group from userGroups
72 userGroups.erase(userGroups.begin()+i);
77 }else{//user has enter "all" and wants the default groups
79 for (int i = 0; i < allGroups.size(); i++) {
80 userGroups.push_back(allGroups[i]);
83 }else { //the user has not entered groups
84 for (int i = 0; i < allGroups.size(); i++) {
85 userGroups.push_back(allGroups[i]);
90 //if the user only entered invalid groups
91 if (userGroups.size() == 0) {
92 if (allGroups.size() > 4) {
93 cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using the first four groups in your groupfile." << endl;
94 for (int i = 0; i < 4; i++) {
95 userGroups.push_back(allGroups[i]);
98 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;
99 for (int i = 0; i < allGroups.size(); i++) {
100 userGroups.push_back(allGroups[i]);
105 //check to make sure their are only 4 groups
106 if (userGroups.size() > 4) {
107 cout << "You may only use up to 4 groups at a time with this command. I will choose the first four and disregard the rest." << endl;
108 for (int i = 4; i < userGroups.size(); i++) {
109 userGroups.erase(userGroups.begin()+i);
112 }else if (mode == "heat") {
113 //if the user only entered invalid groups
114 if (userGroups.size() == 0) {
115 cout << "When using the groups parameter you must have at least 1 valid groups. I will run the command using all the groups in your groupfile." << endl;
116 for (int i = 0; i < allGroups.size(); i++) {
117 userGroups.push_back(allGroups[i]);
120 }else if ((mode == "collect") || (mode == "rarefact") || (mode == "summary") || (mode == "treegroup")) {
121 //if the user only entered invalid groups
122 if ((userGroups.size() == 0) || (userGroups.size() == 1)) {
123 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;
124 for (int i = 0; i < allGroups.size(); i++) {
125 userGroups.push_back(allGroups[i]);
131 catch(exception& e) {
132 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
136 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142 /**************************************************************************************/
143 //for parsimony and unifrac commands you set pairwise groups as well as an allgroups in calc
144 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string& label, int& numGroups, string mode){ //globaldata->Groups, your tree or group map, allgroups, mode
148 //if the user has not entered specific groups to analyze then do them all
149 if (userGroups.size() != 0) {
150 if (userGroups[0] != "all") {
151 //check that groups are valid
152 for (int i = 0; i < userGroups.size(); i++) {
153 if (isValidGroup(userGroups[i], allGroups) != true) {
154 cout << userGroups[i] << " is not a valid group, and will be disregarded." << endl;
155 // erase the invalid group from globaldata->Groups
156 userGroups.erase(userGroups.begin()+i);
160 }else { //users wants all groups
162 for (int i=0; i < allGroups.size(); i++) {
163 if (allGroups[i] != "xxx") {
164 userGroups.push_back(allGroups[i]);
165 label += allGroups[i] + "-";
169 }else { //the user has not entered groups
170 for (int i=0; i < allGroups.size(); i++) {
171 if (allGroups[i] != "xxx") {
172 label += allGroups[i] + "-";
173 if (mode == "weighted") {
174 userGroups.push_back(allGroups[i]);
183 if (mode == "weighted") {
184 //if the user only entered invalid groups
185 if (userGroups.size() == 0) {
186 for (int i=0; i < allGroups.size(); i++) {
187 if (allGroups[i] != "xxx") {
188 userGroups.push_back(allGroups[i]);
191 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;
192 }else if (userGroups.size() == 1) {
193 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;
195 for (int i=0; i < allGroups.size(); i++) {
196 if (allGroups[i] != "xxx") {
197 userGroups.push_back(allGroups[i]);
201 numGroups = userGroups.size();
203 }else if ((mode == "unweighted") || (mode == "parsimony")) {
204 //if the user only entered invalid groups
205 if ((userGroups.size() == 0) && (numGroups == 0)) {
206 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;
207 for (int i = 0; i < allGroups.size(); i++) {
208 if (allGroups[i] != "xxx") {
209 userGroups.push_back(allGroups[i]);
210 label += allGroups[i] + "-";
214 for (int i = 0; i < userGroups.size(); i++) {
215 label += userGroups[i] + "-";
219 //rip extra - off allgroups
220 label = label.substr(0, label.length()-1);
222 if (numGroups != 1) { numGroups = userGroups.size(); }
226 catch(exception& e) {
227 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
231 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235 /**************************************************************************************/
236 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
238 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
240 for (int i=0; i< userGroups.size(); i++) {
242 for (int l = i+1; l < userGroups.size(); l++) {
243 //set group comparison labels
244 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
248 catch(exception& e) {
249 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253 cout << "An unknown error has occurred in the SharedUtil class function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257 /**************************************************************************************/
258 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
260 for (int i = 0; i < groups.size(); i++) {
261 if (groupname == groups[i]) { return true; }
266 catch(exception& e) {
267 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271 cout << "An unknown error has occurred in the SharedUtil class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
276 /**************************************************************************************/
277 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
280 for (int i = 0; i < userGroups.size(); i++) {
281 index[userGroups[i]] = i;
284 catch(exception& e) {
285 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289 cout << "An unknown error has occurred in the SharedUtil class function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";