2 * unifracweightedcommand.cpp
5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "unifracweightedcommand.h"
12 /***********************************************************/
13 UnifracWeightedCommand::UnifracWeightedCommand() {
15 globaldata = GlobalData::getInstance();
17 T = globaldata->gTree;
18 tmap = globaldata->gTreemap;
19 //weightedFile = globaldata->getTreeFile() + ".weighted";
20 //openOutputFile(weightedFile, out);
22 //out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
24 sumFile = globaldata->getTreeFile() + ".wsummary";
25 openOutputFile(sumFile, outSum);
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);
33 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
37 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
41 /***********************************************************/
42 int UnifracWeightedCommand::execute() {
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...
49 //create new tree with same num nodes and leaves as users
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);
58 cout << "Processing tree " << i+1 << endl;
59 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
62 for (int s=0; s<numComp; s++) {
63 //add users score to vector of user scores
64 uScores[s].push_back(userData[s]);
66 //add users score to vector of valid scores
67 validScores[s].push_back(userData[s]);
69 //save users tree score for summary file
70 utreeScores.push_back(userData[s]);
73 //get scores for random trees
74 for (int j = 0; j < iters; j++) {
76 for (int r=0; r<numGroups; r++) {
77 for (int l = r+1; l < numGroups; l++) {
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]);
87 rScores[count].push_back(randomData[0]);
88 validScores[count][randomData[0]] = randomData[0];
94 removeValidScoresDuplicates();
95 //find the signifigance of the score for summary file
96 for (int f = 0; f < numComp; f++) {
98 sort(rScores[f].begin(), rScores[f].end());
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
106 //the signifigance is the number of trees with the users score or higher
107 WScoreSig.push_back((iters-index)/(float)iters);
110 //out << "Tree# " << i << endl;
111 //printWeightedFile();
121 //clear out users groups
122 globaldata->Groups.clear();
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";
134 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
138 /***********************************************************
139 void UnifracWeightedCommand::printWeightedFile() {
143 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
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;
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";
161 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
167 /***********************************************************/
168 void UnifracWeightedCommand::printWSummaryFile() {
171 outSum << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" << endl;
172 cout << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" << endl;
175 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
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;
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";
193 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
198 /***********************************************************/
199 void UnifracWeightedCommand::removeValidScoresDuplicates() {
201 for (int e = 0; e < numComp; e++) {
203 sort(validScores[e].begin(), validScores[e].end());
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); }
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";
215 cout << "An unknown error has occurred in the UnifracWeightedCommand class function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220 /***********************************************************/
221 int UnifracWeightedCommand::findIndex(float score, int index) {
223 for (int i = 0; i < rScores[index].size(); i++) {
224 if (rScores[index][i] >= score) { return i; }
226 return rScores[index].size();
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";
233 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
238 /***********************************************************/
239 void UnifracWeightedCommand::setGroups() {
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]);
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);
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]);
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]);
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]);
283 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
285 for (int i=0; i<numGroups; 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]);
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";
298 cout << "An unknown error has occurred in the UnifracWeightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";