]> git.donarmstrong.com Git - mothur.git/blob - sharedutilities.cpp
4e35ff3df44fcf24df6accc2c0b920ecac60bcd9
[mothur.git] / sharedutilities.cpp
1 /*
2  *  sharedutilities.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedutilities.h"
11 #include "sharedrabundvector.h"
12 #include "sharedordervector.h"
13
14 /**************************************************************************************************/
15
16 void SharedUtil::getSharedVectors(vector<string> Groups, vector<SharedRAbundVector*>& lookup, SharedOrderVector* order) {
17         try {
18         
19                 //delete each sharedrabundvector in lookup
20                 for (int j = 0; j < lookup.size(); j++) {
21                         delete lookup[j];
22                 }
23                 
24                 lookup.clear();
25                 
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);
32                 }
33                 
34                 int numSeqs = order->size();
35                 //sample all the members
36                 for(int i=0;i<numSeqs;i++){
37                         //get first sample
38                         individual chosen = order->get(i);
39                         int abundance; 
40                                         
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);
46                                          break;
47                                 }
48                         }
49                 }
50         }
51         catch(exception& e) {
52                 cout << "Standard Error: " << e.what() << " has occurred in the SharedUtil class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53                 exit(1);
54         }
55         catch(...) {
56                 cout << "An unknown error has occurred in the SharedUtil class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57                 exit(1);
58         }
59 }
60
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) {
64         try {
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);
73                                                 i--;
74                                         }
75                                 }
76
77                         }else{//user has enter "all" and wants the default groups
78                                 userGroups.clear();
79                                 for (int i = 0; i < allGroups.size(); i++) {
80                                         userGroups.push_back(allGroups[i]);
81                                 }
82                         }
83                 }else { //the user has not entered groups
84                         for (int i = 0; i < allGroups.size(); i++) {
85                                 userGroups.push_back(allGroups[i]);
86                         }
87                 }
88                         
89                 if (mode == "venn")  {
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]);
96                                                 }
97                                         }else {
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]);
101                                                 }
102                                         }
103                                 }
104                                 
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);
110                                         }
111                                 }
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]);
118                                         }
119                                 }
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]);
126                                         }
127                                 }
128                 }
129         
130         }
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";
133                 exit(1);
134         }
135         catch(...) {
136                 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
137                 exit(1);
138         }
139
140 }
141
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
145         try {
146                 numGroups = 0;
147                 label = "";
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);
157                                                 i--;
158                                         }
159                                 }
160                         }else { //users wants all groups
161                                 userGroups.clear();
162                                 for (int i=0; i < allGroups.size(); i++) { 
163                                         if (allGroups[i] != "xxx") {
164                                                 userGroups.push_back(allGroups[i]);
165                                                 label += allGroups[i] + "-";
166                                         }
167                                 }
168                         }
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]);
175                                         }else {
176                                                 numGroups = 1;
177                                         }
178                                 }
179                         }
180                 }
181                 
182                 
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]);
189                                         }
190                                 }
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;
194                                 userGroups.clear();
195                                 for (int i=0; i < allGroups.size(); i++) { 
196                                         if (allGroups[i] != "xxx") {
197                                                 userGroups.push_back(allGroups[i]);
198                                         }
199                                 }
200                         }
201                         numGroups = userGroups.size();
202                         
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] + "-";
211                                                 }
212                                         }
213                                 }else {
214                                         for (int i = 0; i < userGroups.size(); i++) {
215                                                 label += userGroups[i] + "-";
216                                         }
217                                 }
218                                 
219                                 //rip extra - off allgroups 
220                                 label = label.substr(0, label.length()-1);
221                                 
222                                 if (numGroups != 1) { numGroups = userGroups.size(); }
223                 
224                 }
225         }
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";
228                 exit(1);
229         }
230         catch(...) {
231                 cout << "An unknown error has occurred in the SharedUtil class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232                 exit(1);
233         }
234 }
235 /**************************************************************************************/
236 void SharedUtil::getCombos(vector<string>& groupComb, vector<string> userGroups, int& numComp) { //groupcomb, globaldata->Groups, numcomb
237         try {
238                 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
239                 numComp = 0;
240                 for (int i=0; i< userGroups.size(); i++) { 
241                         numComp += i; 
242                         for (int l = i+1; l < userGroups.size(); l++) {
243                                 //set group comparison labels
244                                 groupComb.push_back(userGroups[i] + "-" + userGroups[l]);
245                         }
246                 } 
247         }
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";
250                 exit(1);
251         }
252         catch(...) {
253                 cout << "An unknown error has occurred in the SharedUtil class function getCombos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254                 exit(1);
255         }
256 }
257 /**************************************************************************************/
258 bool SharedUtil::isValidGroup(string groupname, vector<string> groups) {
259         try {
260                 for (int i = 0; i < groups.size(); i++) {
261                         if (groupname == groups[i]) { return true; }
262                 }
263                 
264                 return false;
265         }
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";
268                 exit(1);
269         }
270         catch(...) {
271                 cout << "An unknown error has occurred in the SharedUtil class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
272                 exit(1);
273         }
274 }
275
276 /**************************************************************************************/
277 void SharedUtil::updateGroupIndex(vector<string>& userGroups, map<string, int>& index) {
278         try {
279                 index.clear();
280                 for (int i = 0; i < userGroups.size(); i++) {
281                         index[userGroups[i]] = i;
282                 }
283         }
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";
286                 exit(1);
287         }
288         catch(...) {
289                 cout << "An unknown error has occurred in the SharedUtil class function updateGroupIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }
292 }