From 813502505663a3d00555bf7633163432b643dd68 Mon Sep 17 00:00:00 2001 From: westcott Date: Wed, 4 Mar 2009 15:54:07 +0000 Subject: [PATCH] unifrac.weighted --- parsimonycommand.cpp | 12 +-- unifracunweightedcommand.h | 2 +- unifracweightedcommand.cpp | 198 ++++++++++++++++++++++++++++--------- unifracweightedcommand.h | 22 +++-- 4 files changed, 175 insertions(+), 59 deletions(-) diff --git a/parsimonycommand.cpp b/parsimonycommand.cpp index 4529f77..ec1549a 100644 --- a/parsimonycommand.cpp +++ b/parsimonycommand.cpp @@ -236,8 +236,8 @@ void ParsimonyCommand::printUSummaryFile() { //print each line for (int i = 0; i< T.size(); i++) { for(int a = 0; a < numComp; a++) { - outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl; - cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl; + outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl; + cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl; } } @@ -421,16 +421,16 @@ void ParsimonyCommand::output(vector data){ getline(inFile, inputBuffer); if (randomtree == "") { - out << inputBuffer << '\t' << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl; + out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl; }else{ - out << inputBuffer << '\t' << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << endl; + out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl; } } else{ if (randomtree == "") { - out << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl; + out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl; }else{ - out << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << endl; + out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl; } } diff --git a/unifracunweightedcommand.h b/unifracunweightedcommand.h index b572e46..351e052 100644 --- a/unifracunweightedcommand.h +++ b/unifracunweightedcommand.h @@ -37,7 +37,7 @@ class UnifracUnweightedCommand : public Command { EstOutput randomData; //unweighted score info for random trees vector< vector > utreeScores; //scores for users trees for each comb. vector< vector > UWScoreSig; //tree score signifigance when compared to random trees - percentage of random trees with that score or higher. - vector< map > validScores; //map contains scores from both user and random + map validScores; //map contains scores from random vector< map > rscoreFreq; //map -vector entry for each combination. vector< map > rCumul; //map -vector entry for each combination. map::iterator it2; diff --git a/unifracweightedcommand.cpp b/unifracweightedcommand.cpp index 73eab59..91e898f 100644 --- a/unifracweightedcommand.cpp +++ b/unifracweightedcommand.cpp @@ -16,11 +16,6 @@ UnifracWeightedCommand::UnifracWeightedCommand() { T = globaldata->gTree; tmap = globaldata->gTreemap; - //weightedFile = globaldata->getTreeFile() + ".weighted"; - //openOutputFile(weightedFile, out); - //column headers - //out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl; - sumFile = globaldata->getTreeFile() + ".wsummary"; openOutputFile(sumFile, outSum); @@ -49,11 +44,13 @@ int UnifracWeightedCommand::execute() { //create new tree with same num nodes and leaves as users randT = new Tree(); - //get pscores for users trees + //get weighted scores for users trees for (int i = 0; i < T.size(); i++) { + counter = 0; rScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC... uScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC... - validScores.resize(numComp); + weightedFile = globaldata->getTreeFile() + toString(i+1) + ".weighted"; + weightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".weighted"; cout << "Processing tree " << i+1 << endl; userData = weighted->getValues(T[i]); //userData[0] = weightedscore @@ -63,9 +60,6 @@ int UnifracWeightedCommand::execute() { //add users score to vector of user scores uScores[s].push_back(userData[s]); - //add users score to vector of valid scores - validScores[s].push_back(userData[s]); - //save users tree score for summary file utreeScores.push_back(userData[s]); } @@ -85,13 +79,12 @@ int UnifracWeightedCommand::execute() { //save scores rScores[count].push_back(randomData[0]); - validScores[count][randomData[0]] = randomData[0]; count++; } } } - removeValidScoresDuplicates(); + //removeValidScoresDuplicates(); //find the signifigance of the score for summary file for (int f = 0; f < numComp; f++) { //sort random scores @@ -108,7 +101,8 @@ int UnifracWeightedCommand::execute() { } //out << "Tree# " << i << endl; - //printWeightedFile(); + calculateFreqsCumuls(); + printWeightedFile(); //clear data rScores.clear(); @@ -135,22 +129,25 @@ int UnifracWeightedCommand::execute() { exit(1); } } -/*********************************************************** +/***********************************************************/ void UnifracWeightedCommand::printWeightedFile() { try { - - //format output - out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint); + vector data; - //for each group - for (int e = 0; e < numComp; e++) { - //print each line in that group - for (i = 0; i < validScores[e].size(); i++) { - 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; + for(int a = 0; a < numComp; a++) { + initFile(groupComb[a]); + //print each line + for (it = validScores.begin(); it != validScores.end(); it++) { + data.push_back(it->first); data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); + output(data); + data.clear(); } + resetFile(); } out.close(); + inFile.close(); + remove(weightedFileout.c_str()); } catch(exception& e) { @@ -178,8 +175,8 @@ void UnifracWeightedCommand::printWSummaryFile() { int count = 0; for (int i = 0; i < T.size(); i++) { for (int j = 0; j < numComp; j++) { - outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; - cout << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; + outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; + cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; count++; } } @@ -195,28 +192,6 @@ void UnifracWeightedCommand::printWSummaryFile() { } } -/***********************************************************/ -void UnifracWeightedCommand::removeValidScoresDuplicates() { - try { - for (int e = 0; e < numComp; e++) { - //sort valid scores - sort(validScores[e].begin(), validScores[e].end()); - - for (int i = 0; i< validScores[e].size()-1; i++) { - if (validScores[e][i] == validScores[e][i+1]) { validScores[e].erase(validScores[e].begin()+i); } - } - } - } - catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the UnifracWeightedCommand class function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } -} - /***********************************************************/ int UnifracWeightedCommand::findIndex(float score, int index) { try{ @@ -300,3 +275,134 @@ void UnifracWeightedCommand::setGroups() { } } +/***********************************************************/ + +void UnifracWeightedCommand::calculateFreqsCumuls() { + try { + //clear out old tree values + rScoreFreq.clear(); + rScoreFreq.resize(numComp); + rCumul.clear(); + rCumul.resize(numComp); + validScores.clear(); + + //calculate frequency + for (int f = 0; f < numComp; f++) { + 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... + validScores[rScores[f][i]] = rScores[f][i]; + it = rScoreFreq[f].find(rScores[f][i]); + if (it != rScoreFreq[f].end()) { + rScoreFreq[f][rScores[f][i]]++; + }else{ + rScoreFreq[f][rScores[f][i]] = 1; + } + } + } + + //calculate rcumul + for(int a = 0; a < numComp; a++) { + float rcumul = 1.0000; + //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print. + for (it = validScores.begin(); it != validScores.end(); it++) { + //make rscoreFreq map and rCumul + it2 = rScoreFreq[a].find(it->first); + rCumul[a][it->first] = rcumul; + //get percentage of random trees with that info + if (it2 != rScoreFreq[a].end()) { rScoreFreq[a][it->first] /= iters; rcumul-= it2->second; } + else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score + } + } + + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the UnifracWeightedCommand class function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + +} + +/*****************************************************************/ + +void UnifracWeightedCommand::initFile(string label){ + try { + if(counter != 0){ + openOutputFile(weightedFileout, out); + openInputFile(weightedFile, inFile); + + string inputBuffer; + getline(inFile, inputBuffer); + + out << inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl; + }else{ + openOutputFile(weightedFileout, out); + out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl; + } + + out.setf(ios::fixed, ios::floatfield); + out.setf(ios::showpoint); + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the UnifracWeightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } +} + +/***********************************************************************/ + +void UnifracWeightedCommand::output(vector data){ + try { + if(counter != 0){ + string inputBuffer; + getline(inFile, inputBuffer); + + out << inputBuffer << setprecision(globaldata->getIters().length()) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl; + } + else{ + out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl; + } + + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the UnifracWeightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } +}; + +/***********************************************************************/ + +void UnifracWeightedCommand::resetFile(){ + try { + if(counter != 0){ + out.close(); + inFile.close(); + } + else{ + out.close(); + } + counter = 1; + + remove(weightedFile.c_str()); + rename(weightedFileout.c_str(), weightedFile.c_str()); + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the UnifracWeightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } +} + diff --git a/unifracweightedcommand.h b/unifracweightedcommand.h index 4d8f662..e10aa44 100644 --- a/unifracweightedcommand.h +++ b/unifracweightedcommand.h @@ -34,21 +34,31 @@ class UnifracWeightedCommand : public Command { Tree* randT; //random tree TreeMap* tmap; Weighted* weighted; - string weightedFile, sumFile; - int iters, numGroups, numComp; + string weightedFile, weightedFileout, sumFile; + int iters, numGroups, numComp, counter; EstOutput userData; //weighted score info for user tree EstOutput randomData; //weighted score info for random trees - vector< vector > validScores; //vector each group comb has an entry vector< vector > rScores; //vector each group comb has an entry vector< vector > uScores; //vector each group comb has an entry - + vector< map > rScoreFreq; //map -vector entry for each combination. + vector< map > rCumul; //map -vector entry for each c + map validScores; //map contains scores from random + map::iterator it2; + map::iterator it; + ofstream outSum, out; + ifstream inFile; void printWSummaryFile(); - // void printWeightedFile(); - void removeValidScoresDuplicates(); + void printWeightedFile(); + //void removeValidScoresDuplicates(); int findIndex(float, int); void setGroups(); + void calculateFreqsCumuls(); + void initFile(string); + void output(vector); + void resetFile(); + }; -- 2.39.2