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 sumDelta.resize(numComp-numGroups, 0.0);
81 if (form != "discrete") { matrix->getDist(dist); }
90 /*****************************/
91 //get values for users matrix
92 /*****************************/
94 //clear out old Values
96 deltaValues.resize(dist.size());
98 coverage->getValues(matrix, cValues, dist, "user");
100 //loop through each distance and load rsumdelta
101 for (int p = 0; p < cValues.size(); p++) {
104 for (int i = 0; i < numGroups; i++) {
105 for (int j = 0; j < numGroups; j++) {
106 //don't save AA to AA
109 deltaValues[p].push_back( (abs(cValues[p][i][i]-cValues[p][i][j]) * abs(cValues[p][i][i]-cValues[p][i][j])) );
110 sumDelta[count] += deltaValues[p][count];
119 /*******************************************************************************/
120 //create and score random matrixes finding the sumDelta values for summary file
121 /******************************************************************************/
123 //initialize rsumDelta
124 rsumDelta.resize(numComp-numGroups);
125 for (int l = 0; l < rsumDelta.size(); l++) {
126 for (int w = 0; w < iters; w++) {
127 rsumDelta[l].push_back(0.0);
132 for (int m = 0; m < iters; m++) {
133 //generate random matrix in getValues
134 //values for random matrix
136 coverage->getValues(matrix, cValues, dist, "random");
138 //loop through each distance and load rsumdelta
139 for (int p = 0; p < cValues.size(); p++) {
142 for (int i = 0; i < numGroups; i++) {
143 for (int j = 0; j < numGroups; j++) {
144 //don't save AA to AA
147 rsumDelta[count][m] += ((abs(cValues[p][i][i]-cValues[p][i][j]) * abs(cValues[p][i][i]-cValues[p][i][j])));
154 //cout << "iter " << m << endl;
155 //clear out old Values
158 //cout << "random sum delta for iter " << m << endl;
159 //for (int i = 0; i < rsumDelta.size(); i++) {
160 // cout << setprecision(6) << rsumDelta[i][m] << '\t';
167 /**********************************************************/
168 //find the signifigance of the user matrix' sumdelta values
169 /**********************************************************/
171 for (int t = 0; t < rsumDelta.size(); t++) {
173 sort(rsumDelta[t].begin(), rsumDelta[t].end());
175 //the index of the score higher than yours is returned
176 //so if you have 1000 random matrices the index returned is 100
177 //then there are 900 matrices with a score greater then you.
178 //giving you a signifigance of 0.900
179 int index = findIndex(sumDelta[t], t);
181 //the signifigance is the number of trees with the users score or higher
182 sumDeltaSig.push_back((iters-index)/(float)iters);
188 //clear out users groups
189 globaldata->Groups.clear();
193 catch(exception& e) {
194 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
198 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
202 //**********************************************************************************************************************
203 void LibShuffCommand::printCoverageFile() {
206 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
208 //loop through each distance
209 for (int p = 0; p < cValues.size(); p++) {
210 out << setprecision(6) << dist[p] << '\t';
211 //print out coverage values
212 for (int i = 0; i < numGroups; i++) {
213 for (int j = 0; j < numGroups; j++) {
214 out << cValues[p][i][j] << '\t';
218 for (int h = 0; h < deltaValues[p].size(); h++) {
219 out << deltaValues[p][h] << '\t';
226 catch(exception& e) {
227 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
231 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235 //**********************************************************************************************************************
236 void LibShuffCommand::printSummaryFile() {
239 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
241 for (int i = 0; i < numGroups; i++) {
242 for (int j = 0; j < numGroups; j++) {
243 //don't output AA to AA
245 outSum << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
246 cout << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
253 //print out delta values
254 for (int i = 0; i < sumDelta.size(); i++) {
255 if (sumDeltaSig[i] > (1/(float)iters)) {
256 outSum << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t';
257 cout << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t';
259 outSum << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << '\t';
260 cout << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << '\t';
268 catch(exception& e) {
269 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273 cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
278 //**********************************************************************************************************************
279 void LibShuffCommand::setGroups() {
281 //if the user has not entered specific groups to analyze then do them all
282 if (globaldata->Groups.size() == 0) {
283 numGroups = globaldata->gGroupmap->getNumGroups();
284 for (int i=0; i < numGroups; i++) {
285 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
288 if (globaldata->getGroups() != "all") {
289 //check that groups are valid
290 for (int i = 0; i < globaldata->Groups.size(); i++) {
291 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
292 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
293 // erase the invalid group from globaldata->Groups
294 globaldata->Groups.erase (globaldata->Groups.begin()+i);
298 //if the user only entered invalid groups
299 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
300 numGroups = globaldata->gGroupmap->getNumGroups();
301 for (int i=0; i < numGroups; i++) {
302 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
304 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;
305 }else { numGroups = globaldata->Groups.size(); }
306 }else { //users wants all groups
307 numGroups = globaldata->gGroupmap->getNumGroups();
308 globaldata->Groups.clear();
309 for (int i=0; i < numGroups; i++) {
310 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
315 //sort so labels match
316 sort(globaldata->Groups.begin(), globaldata->Groups.end());
318 // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
319 for (int i=0; i<numGroups; i++) {
320 for (int l = 0; l < numGroups; l++) {
321 //set group comparison labels
322 groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
326 catch(exception& e) {
327 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
335 /***********************************************************/
336 int LibShuffCommand::findIndex(float score, int index) {
338 for (int i = 0; i < rsumDelta[index].size(); i++) {
339 if (rsumDelta[index][i] >= score) { return i; }
341 return rsumDelta[index].size();
343 catch(exception& e) {
344 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348 cout << "An unknown error has occurred in the LibShuffCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
353 /***********************************************************/