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 matrix = globaldata->gMatrix;
21 coverageFile = getRootName(globaldata->getPhylipFile()) + "coverage";
22 summaryFile = getRootName(globaldata->getPhylipFile()) + "slsummary";
23 openOutputFile(coverageFile, out);
24 openOutputFile(summaryFile, outSum);
26 //set the groups to be analyzed
29 //file headers for coverage file
31 for (int i = 0; i < groupComb.size(); i++) {
32 out << "C" + groupComb[i] << '\t';
35 for (int i = 0; i < numGroups; i++) {
36 for (int j = 0; j < numGroups; j++) {
37 //don't output AA to AA
39 out << "Delta" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
45 numComp = numGroups*numGroups;
47 coverage = new Coverage();
51 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55 cout << "An unknown error has occurred in the LibShuffCommand class function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
61 //**********************************************************************************************************************
63 LibShuffCommand::~LibShuffCommand(){
67 //**********************************************************************************************************************
69 int LibShuffCommand::execute(){
71 //deltaValues[0] = scores for the difference between AA and AB.
72 //cValues[0][0] = AA, cValues[0][1] = AB, cValues[0][2] = AC, cValues[1][0] = BA, cValues[1][1] = BB...
74 sumDelta.resize(numComp-numGroups, 0.0);
78 /*****************************/
79 //get values for users matrix
80 /*****************************/
81 matrix->getMinsForRowsVectors();
83 //get values for users matrix
85 cValues = coverage->getValues(matrix, D);
89 for (int i = 0; i < numGroups; i++) {
90 for (int j = 0; j < numGroups; j++) {
94 deltaValues.push_back( (abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j])) );
95 sumDelta[count] += deltaValues[count];
103 printCoverageFile(D);
105 //clear out old Values
110 /*******************************************************************************/
111 //create and score random matrixes finding the sumDelta values for summary file
112 /******************************************************************************/
114 //initialize rsumDelta
115 rsumDelta.resize(numComp-numGroups);
116 for (int l = 0; l < rsumDelta.size(); l++) {
117 for (int w = 0; w < iters; w++) {
118 rsumDelta[l].push_back(0.0);
122 for (int m = 0; m < iters; m++) {
123 //generate random matrix in getValues
124 //values for random matrix
125 while (D <= cutOff) {
126 cValues = coverage->getValues(matrix, D);
130 for (int i = 0; i < numGroups; i++) {
131 for (int j = 0; j < numGroups; j++) {
132 //don't save AA to AA
135 rsumDelta[count][m] += (abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j]));
143 //clear out old Values
148 /**********************************************************/
149 //find the signifigance of the user matrix' sumdelta values
150 /**********************************************************/
152 for (int t = 0; t < rsumDelta.size(); t++) {
154 sort(rsumDelta[t].begin(), rsumDelta[t].end());
156 //the index of the score higher than yours is returned
157 //so if you have 1000 random matrices the index returned is 100
158 //then there are 900 matrices with a score greater then you.
159 //giving you a signifigance of 0.900
160 int index = findIndex(sumDelta[t], t);
162 //the signifigance is the number of trees with the users score or higher
163 sumDeltaSig.push_back((iters-index)/(float)iters);
169 //clear out users groups
170 globaldata->Groups.clear();
174 catch(exception& e) {
175 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183 //**********************************************************************************************************************
184 void LibShuffCommand::printCoverageFile(float d) {
187 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
189 out << setprecision(globaldata->getIters().length()) << d << '\t';
191 //print out coverage values
192 for (int i = 0; i < numGroups; i++) {
193 for (int j = 0; j < numGroups; j++) {
194 out << cValues[i][j] << '\t';
198 //print out delta values
199 for (int i = 0; i < deltaValues.size(); i++) {
200 out << deltaValues[i] << '\t';
206 catch(exception& e) {
207 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
215 //**********************************************************************************************************************
216 void LibShuffCommand::printSummaryFile() {
219 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
221 for (int i = 0; i < numGroups; i++) {
222 for (int j = 0; j < numGroups; j++) {
223 //don't output AA to AA
225 outSum << "Delta" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
231 //print out delta values
232 for (int i = 0; i < sumDelta.size(); i++) {
233 outSum << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t';
239 catch(exception& e) {
240 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249 //**********************************************************************************************************************
250 void LibShuffCommand::setGroups() {
252 //if the user has not entered specific groups to analyze then do them all
253 if (globaldata->Groups.size() == 0) {
254 numGroups = globaldata->gGroupmap->getNumGroups();
255 for (int i=0; i < numGroups; i++) {
256 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
259 if (globaldata->getGroups() != "all") {
260 //check that groups are valid
261 for (int i = 0; i < globaldata->Groups.size(); i++) {
262 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
263 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
264 // erase the invalid group from globaldata->Groups
265 globaldata->Groups.erase (globaldata->Groups.begin()+i);
269 //if the user only entered invalid groups
270 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
271 numGroups = globaldata->gGroupmap->getNumGroups();
272 for (int i=0; i < numGroups; i++) {
273 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
275 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;
276 }else { numGroups = globaldata->Groups.size(); }
277 }else { //users wants all groups
278 numGroups = globaldata->gGroupmap->getNumGroups();
279 globaldata->Groups.clear();
280 for (int i=0; i < numGroups; i++) {
281 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
286 // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
287 for (int i=0; i<numGroups; i++) {
288 for (int l = 0; l < numGroups; l++) {
289 //set group comparison labels
290 groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
294 catch(exception& e) {
295 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303 /***********************************************************/
304 int LibShuffCommand::findIndex(float score, int index) {
306 for (int i = 0; i < rsumDelta[index].size(); i++) {
307 if (rsumDelta[index][i] >= score) { return i; }
309 return rsumDelta[index].size();
311 catch(exception& e) {
312 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316 cout << "An unknown error has occurred in the LibShuffCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
321 /***********************************************************/