]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
worked on unifrac.unweighted()
[mothur.git] / unifracunweightedcommand.cpp
1 /*
2  *  unifracunweightedcommand.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 "unifracunweightedcommand.h"
11
12 /***********************************************************/
13 UnifracUnweightedCommand::UnifracUnweightedCommand() {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 
17                 T = globaldata->gTree;
18                 tmap = globaldata->gTreemap;
19                 unweightedFile = globaldata->getTreeFile() + ".unweighted";
20                 openOutputFile(unweightedFile, out);
21                 //column headers
22                 out << "Comb" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
23                                 
24                 sumFile = globaldata->getTreeFile() + ".uwsummary";
25                 openOutputFile(sumFile, outSum);
26                 //column headers
27                 outSum << "Tree#" << '\t' << "Comb" << '\t'  <<  "UWScore" << '\t' << '\t' << "UWSig" <<  endl;
28
29                 setGroups(); //sets users groups to analyze
30                 convert(globaldata->getIters(), iters);  //how many random trees to generate
31                 unweighted = new Unweighted(tmap);
32
33         }
34         catch(exception& e) {
35                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
36                 exit(1);
37         }
38         catch(...) {
39                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
40                 exit(1);
41         }
42 }
43 /***********************************************************/
44 int UnifracUnweightedCommand::execute() {
45         try {
46         
47                 userData.resize(numComp,0);  //data[0] = unweightedscore 
48                 randomData.resize(numComp,0); //data[0] = unweightedscore
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                         //get unweighted for users tree
55                         rscoreFreq.resize(numComp);  
56                         uscoreFreq.resize(numComp);  
57                         rCumul.resize(numComp);  
58                         uCumul.resize(numComp);  
59                         validScores.resize(numComp); 
60                         utreeScores.resize(numComp);  
61                         UWScoreSig.resize(numComp); 
62
63                         cout << "Processing tree " << i+1 << endl;
64                         outSum << "Tree#" << i+1 << endl;
65                         out << "Tree#" << i+1 << endl;
66                         userData = unweighted->getValues(T[i]);  //userData[0] = unweightedscore
67                         
68                         //output scores for each combination
69                         for(int k = 0; k < numComp; k++) {
70                                 //update uscoreFreq
71                                 it = uscoreFreq[k].find(userData[k]);
72                                 if (it == uscoreFreq[k].end()) {//new score
73                                         uscoreFreq[k][userData[k]] = 1;
74                                 }else{ uscoreFreq[k][userData[k]]++; }
75                         
76                                 //add users score to valid scores
77                                 validScores[k][userData[k]] = userData[k];
78                         
79                                 //saves users score
80                                 utreeScores[k].push_back(userData[k]);
81                         }
82                         
83                         //copy T[i]'s info.
84                         randT->getCopy(T[i]); 
85                         
86                         //get unweighted scores for random trees
87                         for (int j = 0; j < iters; j++) {
88                                 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
89                                 randomData = unweighted->getValues(randT, "", "");
90                                 
91                                 for(int k = 0; k < numComp; k++) {      
92 cout << "iter " << j << " comp " << k << " = " << randomData[k] << endl;
93                                         //add trees unweighted score to map of scores
94                                         it2 = rscoreFreq[k].find(randomData[k]);
95                                         if (it2 != rscoreFreq[k].end()) {//already have that score
96                                                 rscoreFreq[k][randomData[k]]++;
97                                         }else{//first time we have seen this score
98                                                 rscoreFreq[k][randomData[k]] = 1;
99                                         }
100                                 
101                                         //add randoms score to validscores
102                                         validScores[k][randomData[k]] = randomData[k];
103                                 }
104                         }
105                 
106                 for(int a = 0; a < numComp; a++) {
107                         float ucumul = 1.0000;
108                         float rcumul = 1.0000;
109                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
110                         for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
111                                 it2 = uscoreFreq[a].find(it->first);
112                                 //make uCumul map
113                                 uCumul[a][it->first] = ucumul;
114                                 //user data has that score 
115                                 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul-= it2->second;  }
116                                 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
117                                                 
118                                 //make rscoreFreq map and rCumul
119                                 it2 = rscoreFreq[a].find(it->first);
120                                 rCumul[a][it->first] = rcumul;
121                                 //get percentage of random trees with that info
122                                 if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
123                                 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
124                         }
125                         UWScoreSig[a].push_back(rCumul[a][userData[a]]);
126                 }
127                 
128                 printUnweightedFile();
129                 printUWSummaryFile();
130                 
131                 rscoreFreq.clear();  
132                 uscoreFreq.clear();  
133                 rCumul.clear();  
134                 uCumul.clear();  
135                 validScores.clear(); 
136                 utreeScores.clear();  
137                 UWScoreSig.clear(); 
138         }
139                 //reset groups parameter
140                 globaldata->Groups.clear(); globaldata->setGroups("");
141                 
142                 delete randT;
143                 
144                 return 0;
145                 
146         }
147         catch(exception& e) {
148                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149                 exit(1);
150         }
151         catch(...) {
152                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153                 exit(1);
154         }
155 }
156 /***********************************************************/
157 void UnifracUnweightedCommand::printUnweightedFile() {
158         try {
159                 //format output
160                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
161                 
162                 for(int a = 0; a < numComp; a++) {
163                         //print each line
164                         for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
165                                 out << setprecision(6) << groupComb[a] << '\t' << it->first << '\t' << '\t' << uscoreFreq[a][it->first] << '\t' << uCumul[a][it->first] << '\t' << rscoreFreq[a][it->first] << '\t' << rCumul[a][it->first] << endl; 
166                         } 
167                 }
168                 out.close();
169                 
170         }
171         catch(exception& e) {
172                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173                 exit(1);
174         }
175         catch(...) {
176                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177                 exit(1);
178         }
179 }
180
181 /***********************************************************/
182 void UnifracUnweightedCommand::printUWSummaryFile() {
183         try {
184                                 
185                 //format output
186                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
187                 
188                 //print each line
189                 for (int i = 0; i< T.size(); i++) {
190                         for(int a = 0; a < numComp; a++) {
191                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl;
192                                 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl; 
193                         }       
194                 }
195                 
196                 outSum.close();
197         }
198         catch(exception& e) {
199                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200                 exit(1);
201         }
202         catch(...) {
203                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204                 exit(1);
205         }
206 }
207 /***********************************************************/
208
209 void UnifracUnweightedCommand::setGroups() {
210         try {
211                 string allGroups = "";
212                 numGroups = 0;
213                 //if the user has not entered specific groups to analyze then do them all
214                 if (globaldata->Groups.size() != 0) {
215                         if (globaldata->Groups[0] != "all") {
216                                 //check that groups are valid
217                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
218                                         if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
219                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
220                                                 // erase the invalid group from globaldata->Groups
221                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
222                                         }
223                                 }
224                         
225                                 //if the user only entered invalid groups
226                                 if (globaldata->Groups.size() == 0) { 
227                                         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; 
228                                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
229                                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
230                                                 numGroups++;
231                                                 allGroups += tmap->namesOfGroups[i];
232                                         }
233                                 }else {
234                                         for (int i = 0; i < globaldata->Groups.size(); i++) {
235                                                 allGroups += globaldata->Groups[i];
236                                                 numGroups++;
237                                         }
238                                 }
239                         }else{//user has enter "all" and wants the default groups
240                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
241                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
242                                         numGroups++;
243                                         allGroups += tmap->namesOfGroups[i];
244                                 }
245                                 globaldata->setGroups("");
246                         }
247                 }else {
248                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
249                                 allGroups += tmap->namesOfGroups[i];
250                         }
251                         numGroups = 1;
252                 }
253                 
254                 //calculate number of comparsions
255                 numComp = 0;
256                 for (int r=0; r<numGroups; r++) { 
257                         for (int l = r+1; l < numGroups; l++) {
258                                 groupComb.push_back(globaldata->Groups[r]+globaldata->Groups[l]);
259                                 numComp++;
260                         }
261                 }
262                 
263                 //ABC
264                 if (numComp != 1) {
265                         groupComb.push_back(allGroups);
266                         numComp++;
267                 }
268         }
269         catch(exception& e) {
270                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271                 exit(1);
272         }
273         catch(...) {
274                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275                 exit(1);
276         }               
277
278 }
279 /*****************************************************************/
280