]> git.donarmstrong.com Git - mothur.git/blob - unifracweightedcommand.cpp
fixed parsimony with groups and worked on unifrac.unweighted 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                                                 //create a random tree with same topology as T[i], but different labels
82                                                 randT->assembleRandomUnifracTree(globaldata->Groups[r], globaldata->Groups[l]);
83                                                 //get wscore of random tree
84                                                 randomData = weighted->getValues(randT, globaldata->Groups[r], globaldata->Groups[l]);
85                                                 
86                                                 //save scores
87                                                 rScores[count].push_back(randomData[0]);
88                                                 validScores[count][randomData[0]] = randomData[0];
89                                                 count++;
90                                         }
91                                 }
92                         }
93
94                         removeValidScoresDuplicates(); 
95                         //find the signifigance of the score for summary file
96                         for (int f = 0; f < numComp; f++) {
97                                 //sort random scores
98                                 sort(rScores[f].begin(), rScores[f].end());
99                                 
100                                 //the index of the score higher than yours is returned 
101                                 //so if you have 1000 random trees the index returned is 100 
102                                 //then there are 900 trees with a score greater then you. 
103                                 //giving you a signifigance of 0.900
104                                 int index = findIndex(userData[f], f);    if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
105                         
106                                 //the signifigance is the number of trees with the users score or higher 
107                                 WScoreSig.push_back((iters-index)/(float)iters);
108                         }
109                         
110                         //out << "Tree# " << i << endl;
111                         //printWeightedFile();
112                         
113                         //clear data
114                         rScores.clear();
115                         uScores.clear();
116                         validScores.clear();
117                 }
118                 
119                 printWSummaryFile();
120                 
121                 //clear out users groups
122                 globaldata->Groups.clear();
123                 
124                 delete randT;
125                 
126                 return 0;
127                 
128         }
129         catch(exception& e) {
130                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
131                 exit(1);
132         }
133         catch(...) {
134                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
135                 exit(1);
136         }
137 }
138 /***********************************************************
139 void UnifracWeightedCommand::printWeightedFile() {
140         try {
141                                                 
142                 //format output
143                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
144                 
145                 //for each group
146                 for (int e = 0; e < numComp; e++) {
147                         //print each line in that group
148                         for (i = 0; i < validScores[e].size(); i++) { 
149                                 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; 
150                         } 
151                 }
152                 
153                 out.close();
154                 
155         }
156         catch(exception& e) {
157                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
158                 exit(1);
159         }
160         catch(...) {
161                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
162                 exit(1);
163         }
164 }
165
166
167 /***********************************************************/
168 void UnifracWeightedCommand::printWSummaryFile() {
169         try {
170                 //column headers
171                 outSum << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" <<  endl;
172                 cout << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" <<  endl;
173                 
174                 //format output
175                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
176                 
177                 //print each line
178                 int count = 0;
179                 for (int i = 0; i < T.size(); i++) { 
180                         for (int j = 0; j < numComp; j++) {
181                                 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; 
182                                 cout << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; 
183                                 count++;
184                         }
185                 }
186                 outSum.close();
187         }
188         catch(exception& e) {
189                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190                 exit(1);
191         }
192         catch(...) {
193                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194                 exit(1);
195         }
196 }
197
198 /***********************************************************/
199 void UnifracWeightedCommand::removeValidScoresDuplicates() {
200         try {
201                 for (int e = 0; e < numComp; e++) {
202                         //sort valid scores
203                         sort(validScores[e].begin(), validScores[e].end());
204                         
205                         for (int i = 0; i< validScores[e].size()-1; i++) { 
206                                 if (validScores[e][i] == validScores[e][i+1]) { validScores[e].erase(validScores[e].begin()+i); }
207                         }
208                 }
209         }
210         catch(exception& e) {
211                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212                 exit(1);
213         }
214         catch(...) {
215                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
216                 exit(1);
217         }
218 }
219
220 /***********************************************************/
221 int UnifracWeightedCommand::findIndex(float score, int index) {
222         try{
223                 for (int i = 0; i < rScores[index].size(); i++) {
224                         if (rScores[index][i] >= score) {       return i;       }
225                 }
226                 return rScores[index].size();
227         }
228         catch(exception& e) {
229                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
230                 exit(1);
231         }
232         catch(...) {
233                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234                 exit(1);
235         }
236 }
237
238 /***********************************************************/
239 void UnifracWeightedCommand::setGroups() {
240         try {
241                 //if the user has not entered specific groups to analyze then do them all
242                 if (globaldata->Groups.size() == 0) {
243                         numGroups = tmap->getNumGroups();
244                         for (int i=0; i < numGroups; i++) { 
245                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
246                         }
247                 }else {
248                         if (globaldata->getGroups() != "all") {
249                                 //check that groups are valid
250                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
251                                         if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
252                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
253                                                 // erase the invalid group from globaldata->Groups
254                                                 globaldata->Groups.erase (globaldata->Groups.begin()+i);
255                                         }
256                                 }
257                         
258                                 //if the user only entered invalid groups
259                                 if (globaldata->Groups.size() == 0) { 
260                                         numGroups = tmap->getNumGroups();
261                                         for (int i=0; i < numGroups; i++) { 
262                                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
263                                         }
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                                         for (int i=0; i < numGroups; i++) { 
270                                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
271                                         }
272                                 }else { numGroups = globaldata->Groups.size(); }
273                         }else { //users wants all groups
274                                 numGroups = tmap->getNumGroups();
275                                 globaldata->Groups.clear();
276                                 globaldata->setGroups("");
277                                 for (int i=0; i < numGroups; i++) { 
278                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
279                                 }
280                         }
281                 }
282                 
283                 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
284                 numComp = 0;
285                 for (int i=0; i<numGroups; i++) { 
286                         numComp += i; 
287                         for (int l = i+1; l < numGroups; l++) {
288                                 //set group comparison labels
289                                 groupComb.push_back(globaldata->Groups[i]+globaldata->Groups[l]);
290                         }
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