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] = AA, cValues[0][1] = AB, cValues[0][2] = AC, cValues[1][0] = BA, cValues[1][1] = BB...
78 sumDelta.resize(numComp-numGroups, 0.0);
82 /*****************************/
83 //get values for users matrix
84 /*****************************/
87 if (form != "discrete") { matrix->getDist(dist); next = 1; }
88 //cout << "Distances" << endl;
89 //for (int i = 0; i < dist.size(); i++) { cout << dist[i] << " "; }
92 //get values for users matrix
94 //clear out old Values
96 coverage->getValues(matrix, D, cValues);
100 for (int i = 0; i < numGroups; i++) {
101 for (int j = 0; j < numGroups; j++) {
102 //don't save AA to AA
105 deltaValues.push_back( (abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j])) );
106 sumDelta[count] += deltaValues[count];
112 printCoverageFile(D);
115 if (form != "discrete") {
116 if (next == dist.size()) { break; }
117 else { D = dist[next]; next++; }
124 for (int i = 0; i < numGroups; i++) {
125 for (int j = 0; j < numGroups; j++) {
126 //don't output AA to AA
128 cout << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
134 for (int i = 0; i < sumDelta.size(); i++) {
135 cout << setprecision(6) << sumDelta[i] << '\t';
139 /*******************************************************************************/
140 //create and score random matrixes finding the sumDelta values for summary file
141 /******************************************************************************/
143 //initialize rsumDelta
144 rsumDelta.resize(numComp-numGroups);
145 for (int l = 0; l < rsumDelta.size(); l++) {
146 for (int w = 0; w < iters; w++) {
147 rsumDelta[l].push_back(0.0);
152 for (int m = 0; m < iters; m++) {
153 //generate random matrix in getValues
154 //values for random matrix
155 cout << "Iteration " << m+1 << endl;
159 while (D <= cutOff) {
160 coverage->getValues(matrix, D, cValues, "random");
164 for (int i = 0; i < numGroups; i++) {
165 for (int j = 0; j < numGroups; j++) {
166 //don't save AA to AA
169 rsumDelta[count][m] += ((abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j])));
170 //cout << "iter " << m << " box " << i << j << " delta = " << ((abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j]))) << endl;
177 if (form != "discrete") {
178 if (next == dist.size()) { break; }
179 else { D = dist[next]; next++; }
183 //clear out old Values
186 cout << "random sum delta for iter " << m << endl;
187 for (int i = 0; i < rsumDelta.size(); i++) {
188 cout << setprecision(6) << rsumDelta[i][m] << '\t';
194 /**********************************************************/
195 //find the signifigance of the user matrix' sumdelta values
196 /**********************************************************/
198 for (int t = 0; t < rsumDelta.size(); t++) {
200 sort(rsumDelta[t].begin(), rsumDelta[t].end());
202 //the index of the score higher than yours is returned
203 //so if you have 1000 random matrices the index returned is 100
204 //then there are 900 matrices with a score greater then you.
205 //giving you a signifigance of 0.900
206 int index = findIndex(sumDelta[t], t);
208 //the signifigance is the number of trees with the users score or higher
209 sumDeltaSig.push_back((iters-index)/(float)iters);
215 //clear out users groups
216 globaldata->Groups.clear();
220 catch(exception& e) {
221 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
229 //**********************************************************************************************************************
230 void LibShuffCommand::printCoverageFile(float d) {
233 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
235 out << setprecision(6) << d << '\t';
237 //print out coverage values
238 for (int i = 0; i < numGroups; i++) {
239 for (int j = 0; j < numGroups; j++) {
240 out << cValues[i][j] << '\t';
244 //print out delta values
245 for (int i = 0; i < deltaValues.size(); i++) {
246 out << deltaValues[i] << '\t';
252 catch(exception& e) {
253 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261 //**********************************************************************************************************************
262 void LibShuffCommand::printSummaryFile() {
265 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
267 for (int i = 0; i < numGroups; i++) {
268 for (int j = 0; j < numGroups; j++) {
269 //don't output AA to AA
271 outSum << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
272 cout << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
279 //print out delta values
280 for (int i = 0; i < sumDelta.size(); i++) {
281 outSum << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t';
282 cout << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t';
289 catch(exception& e) {
290 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294 cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299 //**********************************************************************************************************************
300 void LibShuffCommand::setGroups() {
302 //if the user has not entered specific groups to analyze then do them all
303 if (globaldata->Groups.size() == 0) {
304 numGroups = globaldata->gGroupmap->getNumGroups();
305 for (int i=0; i < numGroups; i++) {
306 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
309 if (globaldata->getGroups() != "all") {
310 //check that groups are valid
311 for (int i = 0; i < globaldata->Groups.size(); i++) {
312 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
313 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
314 // erase the invalid group from globaldata->Groups
315 globaldata->Groups.erase (globaldata->Groups.begin()+i);
319 //if the user only entered invalid groups
320 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
321 numGroups = globaldata->gGroupmap->getNumGroups();
322 for (int i=0; i < numGroups; i++) {
323 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
325 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;
326 }else { numGroups = globaldata->Groups.size(); }
327 }else { //users wants all groups
328 numGroups = globaldata->gGroupmap->getNumGroups();
329 globaldata->Groups.clear();
330 for (int i=0; i < numGroups; i++) {
331 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
336 //sort so labels match
337 sort(globaldata->Groups.begin(), globaldata->Groups.end());
339 // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
340 for (int i=0; i<numGroups; i++) {
341 for (int l = 0; l < numGroups; l++) {
342 //set group comparison labels
343 groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
347 catch(exception& e) {
348 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
356 /***********************************************************/
357 int LibShuffCommand::findIndex(float score, int index) {
359 for (int i = 0; i < rsumDelta[index].size(); i++) {
360 if (rsumDelta[index][i] >= score) { return i; }
362 return rsumDelta[index].size();
364 catch(exception& e) {
365 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
369 cout << "An unknown error has occurred in the LibShuffCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374 /***********************************************************/