]> git.donarmstrong.com Git - mothur.git/blob - unifracweightedcommand.cpp
parsimony with groups
[mothur.git] / unifracweightedcommand.cpp
1 /*
2  *  unifracweightedcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 2/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "unifracweightedcommand.h"
11
12 /***********************************************************/
13 UnifracWeightedCommand::UnifracWeightedCommand() {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 
17                 T = globaldata->gTree;
18                 tmap = globaldata->gTreemap;
19                 weightedFile = globaldata->getTreeFile() + ".weighted";
20                 openOutputFile(weightedFile, out);
21                 //column headers
22                 out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
23
24                 sumFile = globaldata->getTreeFile() + ".wsummary";
25                 openOutputFile(sumFile, outSum);
26                                 
27                 setGroups();    //sets the groups the user wants to analyze                     
28                 convert(globaldata->getIters(), iters);  //how many random trees to generate
29                 weighted = new Weighted(tmap);
30
31         }
32         catch(exception& e) {
33                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
34                 exit(1);
35         }
36         catch(...) {
37                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
38                 exit(1);
39         }
40 }
41 /***********************************************************/
42 int UnifracWeightedCommand::execute() {
43         try {
44                 
45                 //get weighted for users tree
46                 userData.resize(numComp,0);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
47                 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
48                                 
49                 //create new tree with same num nodes and leaves as users
50                 randT = new Tree();
51                 
52                 //get pscores for users trees
53                 for (int i = 0; i < T.size(); i++) {
54                         rScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
55                         uScores.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
56                         validScores.resize(numComp); 
57                                                         
58                         cout << "Processing tree " << i+1 << endl;
59                         userData = weighted->getValues(T[i]);  //userData[0] = weightedscore
60                         
61                         //save users score
62                         for (int s=0; s<numComp; s++) {
63                                 //add users score to vector of user scores
64                                 uScores[s].push_back(userData[s]);
65                                 
66                                 //add users score to vector of valid scores
67                                 validScores[s].push_back(userData[s]);
68
69                                 //save users tree score for summary file
70                                 utreeScores.push_back(userData[s]);
71                         }
72                         
73                         //get scores for random trees
74                         for (int j = 0; j < iters; j++) {
75                                 int count = 0;
76                                 for (int r=0; r<numGroups; r++) { 
77                                         for (int l = r+1; l < numGroups; l++) {
78                                                 //copy T[i]'s info.
79                                                 randT->getCopy(T[i]);
80                                                  
81                                                 if (globaldata->Groups.size() != 0) {
82                                                         //create a random tree with same topology as T[i], but different labels
83                                                         randT->assembleRandomUnifracTree(globaldata->Groups[r], globaldata->Groups[l]);
84                                                         //get wscore of random tree
85                                                         randomData = weighted->getValues(randT, globaldata->Groups[r], globaldata->Groups[l]);
86                                                 }else {
87                                                         //create a random tree with same topology as T[i], but different labels
88                                                         randT->assembleRandomUnifracTree(tmap->namesOfGroups[r], tmap->namesOfGroups[l]);
89                                                         //get wscore of random tree
90                                                         randomData = weighted->getValues(randT, tmap->namesOfGroups[r], tmap->namesOfGroups[l]);
91                                                 }
92                                                 //save scores
93                                                 rScores[count].push_back(randomData[0]);
94                                                 validScores[count][randomData[0]] = randomData[0];
95                                                 count++;
96                                         }
97                                 }
98                         }
99
100                         removeValidScoresDuplicates(); 
101                         //find the signifigance of the score for summary file
102                         for (int f = 0; f < numComp; f++) {
103                                 //sort random scores
104                                 sort(rScores[f].begin(), rScores[f].end());
105                                 
106                                 //the index of the score higher than yours is returned 
107                                 //so if you have 1000 random trees the index returned is 100 
108                                 //then there are 900 trees with a score greater then you. 
109                                 //giving you a signifigance of 0.900
110                                 int index = findIndex(userData[f], f);    if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
111                         
112                                 //the signifigance is the number of trees with the users score or higher 
113                                 WScoreSig.push_back((iters-index)/(float)iters);
114                         }
115                         
116                         out << "Tree# " << i << endl;
117                         //printWeightedFile();
118                         
119                         //clear data
120                         rScores.clear();
121                         uScores.clear();
122                         validScores.clear();
123                 }
124                 
125                 printWSummaryFile();
126                 
127                 //clear out users groups
128                 globaldata->Groups.clear();
129                 
130                 delete randT;
131                 
132                 return 0;
133                 
134         }
135         catch(exception& e) {
136                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
137                 exit(1);
138         }
139         catch(...) {
140                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141                 exit(1);
142         }
143 }
144 /***********************************************************
145 void UnifracWeightedCommand::printWeightedFile() {
146         try {
147                                                 
148                 //format output
149                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
150                 
151                 //for each group
152                 for (int e = 0; e < numComp; e++) {
153                         //print each line in that group
154                         for (i = 0; i < validScores[e].size(); i++) { 
155                                 out << setprecision(6) <<  groupComb[e] << '\t' << validScores[e][i] << '\t' << '\t' << uscoreFreq[e][it->first] << '\t' << uCumul[e][it->first] << '\t' << rscoreFreq[e][it->first] << '\t' << rCumul[e][it->first] << endl; 
156                         } 
157                 }
158                 
159                 out.close();
160                 
161         }
162         catch(exception& e) {
163                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164                 exit(1);
165         }
166         catch(...) {
167                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168                 exit(1);
169         }
170 }
171
172
173 /***********************************************************/
174 void UnifracWeightedCommand::printWSummaryFile() {
175         try {
176                 //column headers
177                 outSum << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" <<  endl;
178                 cout << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" <<  endl;
179                 
180                 //format output
181                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
182                 
183                 //print each line
184                 int count = 0;
185                 for (int i = 0; i < T.size(); i++) { 
186                         for (int j = 0; j < numComp; j++) {
187                                 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; 
188                                 cout << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; 
189                                 count++;
190                         }
191                 }
192                 outSum.close();
193         }
194         catch(exception& e) {
195                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
196                 exit(1);
197         }
198         catch(...) {
199                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200                 exit(1);
201         }
202 }
203
204 /***********************************************************/
205 void UnifracWeightedCommand::removeValidScoresDuplicates() {
206         try {
207                 for (int e = 0; e < numComp; e++) {
208                         //sort valid scores
209                         sort(validScores[e].begin(), validScores[e].end());
210                         
211                         for (int i = 0; i< validScores[e].size()-1; i++) { 
212                                 if (validScores[e][i] == validScores[e][i+1]) { validScores[e].erase(validScores[e].begin()+i); }
213                         }
214                 }
215         }
216         catch(exception& e) {
217                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
218                 exit(1);
219         }
220         catch(...) {
221                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
222                 exit(1);
223         }
224 }
225
226 /***********************************************************/
227 int UnifracWeightedCommand::findIndex(float score, int index) {
228         try{
229                 for (int i = 0; i < rScores[index].size(); i++) {
230                         if (rScores[index][i] >= score) {       return i;       }
231                 }
232                 return rScores[index].size();
233         }
234         catch(exception& e) {
235                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236                 exit(1);
237         }
238         catch(...) {
239                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240                 exit(1);
241         }
242 }
243
244 /***********************************************************/
245 void UnifracWeightedCommand::setGroups() {
246         try {
247                 //if the user has not entered specific groups to analyze then do them all
248                 if (globaldata->Groups.size() == 0) {
249                         numGroups = tmap->getNumGroups();
250                 }else {
251                         if (globaldata->getGroups() != "all") {
252                                 //check that groups are valid
253                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
254                                         if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
255                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
256                                                 // erase the invalid group from globaldata->Groups
257                                                 globaldata->Groups.erase (globaldata->Groups.begin()+i);
258                                         }
259                                 }
260                         
261                                 //if the user only entered invalid groups
262                                 if (globaldata->Groups.size() == 0) { 
263                                         numGroups = tmap->getNumGroups();
264                                         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; 
265                                 }else if (globaldata->Groups.size() == 1) { 
266                                         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;
267                                         numGroups = tmap->getNumGroups();
268                                         globaldata->Groups.clear();
269                                 }else { numGroups = globaldata->Groups.size(); }
270                         }else { //users wants all groups
271                                 numGroups = tmap->getNumGroups();
272                                 globaldata->Groups.clear();
273                                 globaldata->setGroups("");
274                         }
275                 }
276                 
277                 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
278                 numComp = 0;
279                 int n = 1;
280                 for (int i=1; i<numGroups; i++) { 
281                         numComp += i; 
282                         for (int l = n; l < numGroups; l++) {
283                                 //set group comparison labels
284                                 if (globaldata->Groups.size() != 0) {
285                                         groupComb.push_back(globaldata->Groups[i-1]+globaldata->Groups[l]);
286                                 }else {
287                                         groupComb.push_back(tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]);
288                                 }
289                         }
290                         n++;
291                 }
292         }
293         catch(exception& e) {
294                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295                 exit(1);
296         }
297         catch(...) {
298                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299                 exit(1);
300         }
301 }
302