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(string option) {
15 globaldata = GlobalData::getInstance();
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"groups","iters"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 parser = new OptionParser();
28 parser->parse(option, parameters); delete parser;
30 ValidParameters* validParameter = new ValidParameters();
32 //check to make sure all parameters are valid for command
33 for (it4 = parameters.begin(); it4 != parameters.end(); it4++) {
34 if (validParameter->isValidParameter(it4->first, myArray, it4->second) != true) { abort = true; }
37 if (globaldata->gTree.size() == 0) {//no trees were read
38 cout << "You must execute the read.tree command, before you may execute the unifrac.weighted command." << endl; abort = true; }
40 //check for optional parameter and set defaults
41 // ...at some point should added some additional type checking...
42 groups = validParameter->validFile(parameters, "groups", false);
43 if (groups == "not found") { groups = ""; }
45 splitAtDash(groups, Groups);
46 globaldata->Groups = Groups;
49 itersString = validParameter->validFile(parameters, "iters", false); if (itersString == "not found") { itersString = "1000"; }
50 convert(itersString, iters);
52 delete validParameter;
55 T = globaldata->gTree;
56 tmap = globaldata->gTreemap;
57 sumFile = globaldata->getTreeFile() + ".wsummary";
58 openOutputFile(sumFile, outSum);
60 util = new SharedUtil();
61 string s; //to make work with setgroups
62 util->setGroups(globaldata->Groups, tmap->namesOfGroups, s, numGroups, "weighted"); //sets the groups the user wants to analyze
63 util->getCombos(groupComb, globaldata->Groups, numComp);
65 weighted = new Weighted(tmap);
73 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81 //**********************************************************************************************************************
83 void UnifracWeightedCommand::help(){
85 cout << "The unifrac.weighted command can only be executed after a successful read.tree command." << "\n";
86 cout << "The unifrac.weighted command parameters are groups and iters. No parameters are required." << "\n";
87 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed. You must enter at least 2 valid groups." << "\n";
88 cout << "The group names are separated by dashes. The iters parameter allows you to specify how many random trees you would like compared to your tree." << "\n";
89 cout << "The unifrac.weighted command should be in the following format: unifrac.weighted(groups=yourGroups, iters=yourIters)." << "\n";
90 cout << "Example unifrac.weighted(groups=A-B-C, iters=500)." << "\n";
91 cout << "The default value for groups is all the groups in your groupfile, and iters is 1000." << "\n";
92 cout << "The unifrac.weighted command output two files: .weighted and .wsummary their descriptions are in the manual." << "\n";
93 cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
96 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 cout << "An unknown error has occurred in the UnifracWeightedCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105 /***********************************************************/
106 int UnifracWeightedCommand::execute() {
109 if (abort == true) { return 0; }
112 reading = new Progress("Comparing to random:", iters);
114 //get weighted for users tree
115 userData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
116 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
118 //create new tree with same num nodes and leaves as users
121 //get weighted scores for users trees
122 for (int i = 0; i < T.size(); i++) {
124 rScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
125 uScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
127 output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".weighted", itersString);
129 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
132 for (int s=0; s<numComp; s++) {
133 //add users score to vector of user scores
134 uScores[s].push_back(userData[s]);
136 //save users tree score for summary file
137 utreeScores.push_back(userData[s]);
140 //get scores for random trees
141 for (int j = 0; j < iters; j++) {
143 for (int r=0; r<numGroups; r++) {
144 for (int l = r+1; l < numGroups; l++) {
146 randT->getCopy(T[i]);
148 //create a random tree with same topology as T[i], but different labels
149 randT->assembleRandomUnifracTree(globaldata->Groups[r], globaldata->Groups[l]);
150 //get wscore of random tree
151 randomData = weighted->getValues(randT, globaldata->Groups[r], globaldata->Groups[l]);
154 rScores[count].push_back(randomData[0]);
159 //update progress bar
164 //removeValidScoresDuplicates();
165 //find the signifigance of the score for summary file
166 for (int f = 0; f < numComp; f++) {
168 sort(rScores[f].begin(), rScores[f].end());
170 //the index of the score higher than yours is returned
171 //so if you have 1000 random trees the index returned is 100
172 //then there are 900 trees with a score greater then you.
173 //giving you a signifigance of 0.900
174 int index = findIndex(userData[f], f); if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
176 //the signifigance is the number of trees with the users score or higher
177 WScoreSig.push_back((iters-index)/(float)iters);
180 //out << "Tree# " << i << endl;
181 calculateFreqsCumuls();
192 //finish progress bar
198 //clear out users groups
199 globaldata->Groups.clear();
206 catch(exception& e) {
207 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
215 /***********************************************************/
216 void UnifracWeightedCommand::printWeightedFile() {
220 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
222 for(int a = 0; a < numComp; a++) {
223 output->initFile(groupComb[a], tags);
225 for (it = validScores.begin(); it != validScores.end(); it++) {
226 data.push_back(it->first); data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
227 output->output(data);
233 catch(exception& e) {
234 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
238 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 /***********************************************************/
245 void UnifracWeightedCommand::printWSummaryFile() {
248 outSum << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
249 cout << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
252 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
256 for (int i = 0; i < T.size(); i++) {
257 for (int j = 0; j < numComp; j++) {
258 if (WScoreSig[count] > (1/(float)iters)) {
259 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << WScoreSig[count] << endl;
260 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << WScoreSig[count] << endl;
262 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
263 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
270 catch(exception& e) {
271 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280 /***********************************************************/
281 int UnifracWeightedCommand::findIndex(float score, int index) {
283 for (int i = 0; i < rScores[index].size(); i++) {
284 if (rScores[index][i] >= score) { return i; }
286 return rScores[index].size();
288 catch(exception& e) {
289 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298 /***********************************************************/
300 void UnifracWeightedCommand::calculateFreqsCumuls() {
302 //clear out old tree values
304 rScoreFreq.resize(numComp);
306 rCumul.resize(numComp);
309 //calculate frequency
310 for (int f = 0; f < numComp; f++) {
311 for (int i = 0; i < rScores[f].size(); i++) { //looks like 0,0,1,1,1,2,4,7... you want to make a map that say rScoreFreq[0] = 2, rScoreFreq[1] = 3...
312 validScores[rScores[f][i]] = rScores[f][i];
313 it = rScoreFreq[f].find(rScores[f][i]);
314 if (it != rScoreFreq[f].end()) {
315 rScoreFreq[f][rScores[f][i]]++;
317 rScoreFreq[f][rScores[f][i]] = 1;
323 for(int a = 0; a < numComp; a++) {
324 float rcumul = 1.0000;
325 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
326 for (it = validScores.begin(); it != validScores.end(); it++) {
327 //make rscoreFreq map and rCumul
328 it2 = rScoreFreq[a].find(it->first);
329 rCumul[a][it->first] = rcumul;
330 //get percentage of random trees with that info
331 if (it2 != rScoreFreq[a].end()) { rScoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
332 else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score
337 catch(exception& e) {
338 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
342 cout << "An unknown error has occurred in the UnifracWeightedCommand class function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348 /***********************************************************/