5 * Created by Sarah Westcott on 3/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "libshuffcommand.h"
12 //**********************************************************************************************************************
15 LibShuffCommand::LibShuffCommand(){
17 globaldata = GlobalData::getInstance();
18 convert(globaldata->getCutOff(), cutOff);
19 convert(globaldata->getIters(), iters);
20 convert(globaldata->getStep(), step);
21 form = globaldata->getForm();
22 matrix = globaldata->gMatrix;
23 coverageFile = getRootName(globaldata->getPhylipFile()) + "coverage";
24 summaryFile = getRootName(globaldata->getPhylipFile()) + "slsummary";
25 openOutputFile(coverageFile, out);
26 openOutputFile(summaryFile, outSum);
28 //set the groups to be analyzed
31 //file headers for coverage file
33 for (int i = 0; i < groupComb.size(); i++) {
34 out << "C" + groupComb[i] << '\t';
37 for (int i = 0; i < numGroups; i++) {
38 for (int j = 0; j < numGroups; j++) {
39 //don't output AA to AA
41 out << "Delta" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
47 numComp = numGroups*numGroups;
49 coverage = new Coverage();
53 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57 cout << "An unknown error has occurred in the LibShuffCommand class function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
63 //**********************************************************************************************************************
65 LibShuffCommand::~LibShuffCommand(){
69 //**********************************************************************************************************************
71 int LibShuffCommand::execute(){
73 //deltaValues[0] = scores for the difference between AA and AB.
74 //cValues[0][0][0] = AA at distance 0.0, cValues[0][0][1] = AB at distance 0.0, cValues[0][0][2] = AC at distance 0.0, cValues[0][1][0] = BA at distance 0.0, cValues[0][1][1] = BB...
76 reading = new Progress("Comparing to random:", iters);
78 sumDelta.resize(numComp-numGroups, 0.0);
83 if (form != "discrete") { matrix->getDist(dist); }
92 /*****************************/
93 //get values for users matrix
94 /*****************************/
96 //clear out old Values
98 deltaValues.resize(dist.size());
100 coverage->getValues(matrix, cValues, dist, "user");
102 //loop through each distance and load rsumdelta
103 for (int p = 0; p < cValues.size(); p++) {
106 for (int i = 0; i < numGroups; i++) {
107 for (int j = 0; j < numGroups; j++) {
108 //don't save AA to AA
111 deltaValues[p].push_back( (abs(cValues[p][i][i]-cValues[p][i][j]) * abs(cValues[p][i][i]-cValues[p][i][j])) );
112 sumDelta[count] += deltaValues[p][count];
121 /*******************************************************************************/
122 //create and score random matrixes finding the sumDelta values for summary file
123 /******************************************************************************/
125 //initialize rsumDelta
126 rsumDelta.resize(numComp-numGroups);
127 for (int l = 0; l < rsumDelta.size(); l++) {
128 for (int w = 0; w < iters; w++) {
129 rsumDelta[l].push_back(0.0);
134 for (int m = 0; m < iters; m++) {
135 //generate random matrix in getValues
136 //values for random matrix
138 coverage->getValues(matrix, cValues, dist, "random");
140 //loop through each distance and load rsumdelta
141 for (int p = 0; p < cValues.size(); p++) {
144 for (int i = 0; i < numGroups; i++) {
145 for (int j = 0; j < numGroups; j++) {
146 //don't save AA to AA
149 rsumDelta[count][m] += ((abs(cValues[p][i][i]-cValues[p][i][j]) * abs(cValues[p][i][i]-cValues[p][i][j])));
156 //cout << "iter " << m << endl;
157 //clear out old Values
161 //cout << "random sum delta for iter " << m << endl;
162 //for (int i = 0; i < rsumDelta.size(); i++) {
163 // cout << setprecision(6) << rsumDelta[i][m] << '\t';
172 /**********************************************************/
173 //find the signifigance of the user matrix' sumdelta values
174 /**********************************************************/
176 for (int t = 0; t < rsumDelta.size(); t++) {
178 sort(rsumDelta[t].begin(), rsumDelta[t].end());
180 //the index of the score higher than yours is returned
181 //so if you have 1000 random matrices the index returned is 100
182 //then there are 900 matrices with a score greater then you.
183 //giving you a signifigance of 0.900
184 int index = findIndex(sumDelta[t], t);
186 //the signifigance is the number of trees with the users score or higher
187 sumDeltaSig.push_back((iters-index)/(float)iters);
193 //clear out users groups
194 globaldata->Groups.clear();
198 catch(exception& e) {
199 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 //**********************************************************************************************************************
208 void LibShuffCommand::printCoverageFile() {
211 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
213 //loop through each distance
214 for (int p = 0; p < cValues.size(); p++) {
215 out << setprecision(6) << dist[p] << '\t';
216 //print out coverage values
217 for (int i = 0; i < numGroups; i++) {
218 for (int j = 0; j < numGroups; j++) {
219 out << cValues[p][i][j] << '\t';
223 for (int h = 0; h < deltaValues[p].size(); h++) {
224 out << deltaValues[p][h] << '\t';
231 catch(exception& e) {
232 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240 //**********************************************************************************************************************
241 void LibShuffCommand::printSummaryFile() {
244 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
246 for (int i = 0; i < numGroups; i++) {
247 for (int j = 0; j < numGroups; j++) {
248 //don't output AA to AA
250 outSum << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
251 cout << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
258 //print out delta values
259 for (int i = 0; i < sumDelta.size(); i++) {
260 if (sumDeltaSig[i] > (1/(float)iters)) {
261 outSum << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t';
262 cout << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t';
264 outSum << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << '\t';
265 cout << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << '\t';
273 catch(exception& e) {
274 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
278 cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283 //**********************************************************************************************************************
284 void LibShuffCommand::setGroups() {
286 //if the user has not entered specific groups to analyze then do them all
287 if (globaldata->Groups.size() == 0) {
288 numGroups = globaldata->gGroupmap->getNumGroups();
289 for (int i=0; i < numGroups; i++) {
290 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
293 if (globaldata->getGroups() != "all") {
294 //check that groups are valid
295 for (int i = 0; i < globaldata->Groups.size(); i++) {
296 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
297 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
298 // erase the invalid group from globaldata->Groups
299 globaldata->Groups.erase (globaldata->Groups.begin()+i);
303 //if the user only entered invalid groups
304 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
305 numGroups = globaldata->gGroupmap->getNumGroups();
306 for (int i=0; i < numGroups; i++) {
307 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
309 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;
310 }else { numGroups = globaldata->Groups.size(); }
311 }else { //users wants all groups
312 numGroups = globaldata->gGroupmap->getNumGroups();
313 globaldata->Groups.clear();
314 for (int i=0; i < numGroups; i++) {
315 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
320 //sort so labels match
321 sort(globaldata->Groups.begin(), globaldata->Groups.end());
323 // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
324 for (int i=0; i<numGroups; i++) {
325 for (int l = 0; l < numGroups; l++) {
326 //set group comparison labels
327 groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
331 catch(exception& e) {
332 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340 /***********************************************************/
341 int LibShuffCommand::findIndex(float score, int index) {
343 for (int i = 0; i < rsumDelta[index].size(); i++) {
344 if (rsumDelta[index][i] >= score) { return i; }
346 return rsumDelta[index].size();
348 catch(exception& e) {
349 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
353 cout << "An unknown error has occurred in the LibShuffCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
358 /***********************************************************/