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 sort(Groups.begin(), Groups.end());
28 //create and initialize vector of sharedvectors, one for each group
29 for (int i = 0; i < Groups.size(); i++) {
30 SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
31 temp->setLabel(order->getLabel());
32 temp->setGroup(Groups[i]);
33 lookup.push_back(temp);
36 int numSeqs = order->size();
37 //sample all the members
38 for(int i=0;i<numSeqs;i++){
40 individual chosen = order->get(i);
43 //set info for sharedvector in chosens group
44 for (int j = 0; j < lookup.size(); j++) {
45 if (chosen.group == lookup[j]->getGroup()) {
46 abundance = lookup[j]->getAbundance(chosen.bin);
47 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
54 m->errorOut(e, "SharedUtil", "getSharedVectors");
58 /**************************************************************************************************/
60 void SharedUtil::getSharedVectorswithReplacement(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
63 //delete each sharedrabundvector in lookup
64 for (int j = 0; j < lookup.size(); j++) {
69 //create and initialize vector of sharedvectors, one for each group
70 for (int i = 0; i < Groups.size(); i++) {
71 SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
72 temp->setLabel(order->getLabel());
73 temp->setGroup(Groups[i]);
74 lookup.push_back(temp);
77 int numSeqs = order->size();
79 //sample all the members
80 for(int i=0;i<numSeqs;i++){
82 int random = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
83 individual chosen = order->get(random);
86 //set info for sharedvector in chosens group
87 for (int j = 0; j < lookup.size(); j++) {
88 if (chosen.group == lookup[j]->getGroup()) {
89 abundance = lookup[j]->getAbundance(chosen.bin);
90 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
98 m->errorOut(e, "SharedUtil", "getSharedVectorswithReplacement");
103 /**************************************************************************************************/
104 //need to have mode because different commands require different number of valid groups
105 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups) {
108 sort(userGroups.begin(), userGroups.end());
109 sort(allGroups.begin(), allGroups.end());
111 if (userGroups.size() != 0) {
112 if (userGroups[0] != "all") {
113 //check that groups are valid
114 for (int i = 0; i < userGroups.size(); i++) {
115 if (isValidGroup(userGroups[i], allGroups) != true) {
116 m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
117 // erase the invalid group from userGroups
118 userGroups.erase(userGroups.begin()+i);
123 //if the user only entered invalid groups
124 if (userGroups.size() == 0) {
125 m->mothurOut("You provided no valid groups. I will run the command using all the groups in your file."); m->mothurOutEndLine();
126 for (int i = 0; i < allGroups.size(); i++) {
127 userGroups.push_back(allGroups[i]);
131 }else{//user has enter "all" and wants the default groups
133 for (int i = 0; i < allGroups.size(); i++) {
134 userGroups.push_back(allGroups[i]);
137 }else { //the user has not entered groups
138 for (int i = 0; i < allGroups.size(); i++) {
139 userGroups.push_back(allGroups[i]);
144 catch(exception& e) {
145 m->errorOut(e, "SharedUtil", "setGroups");
149 /**************************************************************************************************/
150 //need to have mode because different commands require different number of valid groups
151 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string mode) {
154 sort(userGroups.begin(), userGroups.end());
155 sort(allGroups.begin(), allGroups.end());
157 if (userGroups.size() != 0) {
158 if (userGroups[0] != "all") {
159 //check that groups are valid
160 for (int i = 0; i < userGroups.size(); i++) {
161 if (isValidGroup(userGroups[i], allGroups) != true) {
162 m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
163 // erase the invalid group from userGroups
164 userGroups.erase(userGroups.begin()+i);
169 }else{//user has enter "all" and wants the default groups
171 for (int i = 0; i < allGroups.size(); i++) {
172 userGroups.push_back(allGroups[i]);
175 }else { //the user has not entered groups
176 for (int i = 0; i < allGroups.size(); i++) {
177 userGroups.push_back(allGroups[i]);
181 if ((mode == "collect") || (mode == "rarefact") || (mode == "summary") || (mode == "treegroup")) {
182 //if the user only entered invalid groups
183 if ((userGroups.size() == 0) || (userGroups.size() == 1)) {
184 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();
185 for (int i = 0; i < allGroups.size(); i++) {
186 userGroups.push_back(allGroups[i]);
192 catch(exception& e) {
193 m->errorOut(e, "SharedUtil", "setGroups");
199 /**************************************************************************************/
200 //for parsimony and unifrac commands you set pairwise groups as well as an allgroups in calc
201 void SharedUtil::setGroups(vector<string>& userGroups, vector<string>& allGroups, string& label, int& numGroups, string mode){ //globaldata->Groups, your tree or group map, allgroups, mode
203 sort(userGroups.begin(), userGroups.end());
204 sort(allGroups.begin(), allGroups.end());
209 //if the user has not entered specific groups to analyze then do them all
210 if (userGroups.size() != 0) {
211 if (userGroups[0] != "all") {
212 //check that groups are valid
213 for (int i = 0; i < userGroups.size(); i++) {
214 if (isValidGroup(userGroups[i], allGroups) != true) {
215 m->mothurOut(userGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
216 // erase the invalid group from globaldata->Groups
217 userGroups.erase(userGroups.begin()+i);
221 }else { //users wants all groups
223 for (int i=0; i < allGroups.size(); i++) {
224 if (allGroups[i] != "xxx") {
225 userGroups.push_back(allGroups[i]);
229 }else { //the user has not entered groups
230 for (int i=0; i < allGroups.size(); i++) {
231 if (allGroups[i] != "xxx") {
232 if (mode == "weighted") {
233 userGroups.push_back(allGroups[i]);
236 label += allGroups[i] + "-";
240 //rip extra - off allgroups
241 label = label.substr(0, label.length()-1);
242 if ((mode != "weighted") && (allGroups.size() > 10)) { label = "merged"; }
245 if (mode == "weighted") {
246 //if the user only entered invalid groups
247 if (userGroups.size() == 0) {
248 for (int i=0; i < allGroups.size(); i++) {
249 if (allGroups[i] != "xxx") {
250 userGroups.push_back(allGroups[i]);
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();
254 }else if (userGroups.size() == 1) {
255 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();
257 for (int i=0; i < allGroups.size(); i++) {
258 if (allGroups[i] != "xxx") {
259 userGroups.push_back(allGroups[i]);
263 numGroups = userGroups.size();
265 }else if ((mode == "unweighted") || (mode == "parsimony")) {
266 //if the user only entered invalid groups
267 if ((userGroups.size() == 0) && (numGroups == 0)) {
268 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();
269 for (int i = 0; i < allGroups.size(); i++) {
270 if (allGroups[i] != "xxx") {
271 userGroups.push_back(allGroups[i]);
276 if (numGroups != 1) { numGroups = userGroups.size(); }
279 catch(exception& e) {
280 m->errorOut(e, "SharedUtil", "setGroups");
284 /**************************************************************************************/
285 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
287 sort(userGroups.begin(), userGroups.end());
288 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
290 for (int i=0; i< userGroups.size(); i++) {
292 for (int l = 0; l < i; l++) {
293 if (userGroups[i] > userGroups[l]) {
294 //set group comparison labels
295 groupComb.push_back(userGroups[l] + "-" + userGroups[i]);
297 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
302 catch(exception& e) {
303 m->errorOut(e, "SharedUtil", "getCombos");
307 /**************************************************************************************/
308 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
310 for (int i = 0; i < groups.size(); i++) {
311 if (groupname == groups[i]) { return true; }
316 catch(exception& e) {
317 m->errorOut(e, "SharedUtil", "isValidGroup");
322 /**************************************************************************************/
323 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
326 for (int i = 0; i < userGroups.size(); i++) {
327 index[userGroups[i]] = i;
330 catch(exception& e) {
331 m->errorOut(e, "SharedUtil", "updateGroupIndex");
335 /**************************************************************************************/