]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
fixed parsimony with groups and worked on unifrac.unweighted with groups
[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                                 int count = 0;
89                                 for (int r=0; r<numGroups; r++) { 
90                                         for (int l = r+1; l < numGroups; l++) {
91                                                 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
92                                                 randomData = unweighted->getValues(randT, "", "");
93                         
94                                                 //add trees unweighted score to map of scores
95                                                 it2 = rscoreFreq[count].find(randomData[count]);
96                                                 if (it2 != rscoreFreq[count].end()) {//already have that score
97                                                         rscoreFreq[count][randomData[count]]++;
98                                                 }else{//first time we have seen this score
99                                                         rscoreFreq[count][randomData[count]] = 1;
100                                                 }
101                                 
102                                                 //add randoms score to validscores
103                                                 validScores[count][randomData[count]] = randomData[count];
104                                                 count++;
105                                         }
106                                 }
107                         }
108                 
109                 for(int a = 0; a < numComp; a++) {
110                         float ucumul = 1.0000;
111                         float rcumul = 1.0000;
112                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
113                         for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
114                                 it2 = uscoreFreq[a].find(it->first);
115                                 //make uCumul map
116                                 uCumul[a][it->first] = ucumul;
117                                 //user data has that score 
118                                 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul-= it2->second;  }
119                                 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
120                                                 
121                                 //make rscoreFreq map and rCumul
122                                 it2 = rscoreFreq[a].find(it->first);
123                                 rCumul[a][it->first] = rcumul;
124                                 //get percentage of random trees with that info
125                                 if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
126                                 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
127                         }
128                         UWScoreSig[a].push_back(rCumul[a][userData[a]]);
129                 }
130                 
131                 printUnweightedFile();
132                 printUWSummaryFile();
133                 
134                 rscoreFreq.clear();  
135                 uscoreFreq.clear();  
136                 rCumul.clear();  
137                 uCumul.clear();  
138                 validScores.clear(); 
139                 utreeScores.clear();  
140                 UWScoreSig.clear(); 
141         }
142                 //reset groups parameter
143                 globaldata->Groups.clear(); globaldata->setGroups("");
144                 
145                 delete randT;
146                 
147                 return 0;
148                 
149         }
150         catch(exception& e) {
151                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152                 exit(1);
153         }
154         catch(...) {
155                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156                 exit(1);
157         }
158 }
159 /***********************************************************/
160 void UnifracUnweightedCommand::printUnweightedFile() {
161         try {
162                 //format output
163                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
164                 
165                 for(int a = 0; a < numComp; a++) {
166                         //print each line
167                         for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
168                                 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; 
169                         } 
170                 }
171                 out.close();
172                 
173         }
174         catch(exception& e) {
175                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176                 exit(1);
177         }
178         catch(...) {
179                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180                 exit(1);
181         }
182 }
183
184 /***********************************************************/
185 void UnifracUnweightedCommand::printUWSummaryFile() {
186         try {
187                                 
188                 //format output
189                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
190                 
191                 //print each line
192                 for (int i = 0; i< T.size(); i++) {
193                         for(int a = 0; a < numComp; a++) {
194                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl;
195                                 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl; 
196                         }       
197                 }
198                 
199                 outSum.close();
200         }
201         catch(exception& e) {
202                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203                 exit(1);
204         }
205         catch(...) {
206                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209 }
210 /***********************************************************/
211
212 void UnifracUnweightedCommand::setGroups() {
213         try {
214                 string allGroups = "";
215                 numGroups = 0;
216                 //if the user has not entered specific groups to analyze then do them all
217                 if (globaldata->Groups.size() != 0) {
218                         if (globaldata->Groups[0] != "all") {
219                                 //check that groups are valid
220                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
221                                         if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
222                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
223                                                 // erase the invalid group from globaldata->Groups
224                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
225                                         }
226                                 }
227                         
228                                 //if the user only entered invalid groups
229                                 if (globaldata->Groups.size() == 0) { 
230                                         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; 
231                                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
232                                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
233                                                 numGroups++;
234                                                 allGroups += tmap->namesOfGroups[i];
235                                         }
236                                 }else {
237                                         for (int i = 0; i < globaldata->Groups.size(); i++) {
238                                                 allGroups += tmap->namesOfGroups[i];
239                                                 numGroups++;
240                                         }
241                                 }
242                         }else{//user has enter "all" and wants the default groups
243                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
244                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
245                                         numGroups++;
246                                         allGroups += tmap->namesOfGroups[i];
247                                 }
248                                 globaldata->setGroups("");
249                         }
250                 }else {
251                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
252                                 allGroups += tmap->namesOfGroups[i];
253                         }
254                         numGroups = 1;
255                 }
256                 
257                 //calculate number of comparsions
258                 numComp = 0;
259                 for (int r=0; r<numGroups; r++) { 
260                         for (int l = r+1; l < numGroups; l++) {
261                                 groupComb.push_back(globaldata->Groups[r]+globaldata->Groups[l]);
262                                 numComp++;
263                         }
264                 }
265                 
266                 //ABC
267                 if (numComp != 1) {
268                         groupComb.push_back(allGroups);
269                         numComp++;
270                 }
271         }
272         catch(exception& e) {
273                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274                 exit(1);
275         }
276         catch(...) {
277                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
278                 exit(1);
279         }               
280
281 }
282 /*****************************************************************/
283