]> git.donarmstrong.com Git - mothur.git/blob - libshuffcommand.cpp
working on libshuff
[mothur.git] / libshuffcommand.cpp
1 /*
2  *  libshuffcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "libshuffcommand.h"
11
12 //**********************************************************************************************************************
13
14
15 LibShuffCommand::LibShuffCommand(){
16         try {
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);
25                 
26                 //set the groups to be analyzed
27                 setGroups();
28
29                 //file headers for coverage file
30                 out << "D" << '\t';
31                 for (int i = 0; i < groupComb.size(); i++) {
32                         out << "C" + groupComb[i] << '\t';
33                 }
34                 
35                 for (int i = 0; i < numGroups; i++) {
36                         for (int j = 0; j < numGroups; j++) {
37                                 //don't output AA to AA
38                                 if (i != j) {
39                                         out << "Delta" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
40                                 }
41                         }
42                 }
43                 out << endl;
44
45                 numComp = numGroups*numGroups;
46                 
47                 coverage = new Coverage();
48                 
49         }
50         catch(exception& e) {
51                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52                 exit(1);
53         }
54         catch(...) {
55                 cout << "An unknown error has occurred in the LibShuffCommand class function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56                 exit(1);
57         }       
58                         
59 }
60
61 //**********************************************************************************************************************
62
63 LibShuffCommand::~LibShuffCommand(){
64         delete coverage;
65 }
66
67 //**********************************************************************************************************************
68
69 int LibShuffCommand::execute(){
70         try {
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...
73                 
74                 sumDelta.resize(numComp-numGroups, 0.0);
75                                 
76                 float D = 0.0;
77                 
78                 /*****************************/
79                 //get values for users matrix
80                 /*****************************/
81                 matrix->getMinsForRowsVectors();
82                 
83                 //get values for users matrix
84                 while (D <= cutOff) {
85                         cValues = coverage->getValues(matrix, D);
86                         
87                         //find delta values
88                         int count = 0;
89                         for (int i = 0; i < numGroups; i++) {
90                                 for (int j = 0; j < numGroups; j++) {
91                                         //don't save AA to AA
92                                         if (i != j) {
93                                                 //(Caa - Cab)^2
94                                                 deltaValues.push_back( (abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j])) ); 
95                                                 sumDelta[count] += deltaValues[count];
96                                                 count++;
97                                         }
98                                 }
99                         }
100                         
101                         D += 0.01;
102                         
103                         printCoverageFile(D);
104                         
105                         //clear out old Values
106                         cValues.clear();
107                         deltaValues.clear();
108                 }
109                 
110                 /*******************************************************************************/
111                 //create and score random matrixes finding the sumDelta values for summary file
112                 /******************************************************************************/
113
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);
119                         }
120                 }
121                 
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);
127                         
128                                 //find delta values
129                                 int count = 0;
130                                 for (int i = 0; i < numGroups; i++) {
131                                         for (int j = 0; j < numGroups; j++) {
132                                                 //don't save AA to AA
133                                                 if (i != j) {
134                                                         //(Caa - Cab)^2
135                                                         rsumDelta[count][m] += (abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j]));
136                                                         count++;
137                                                 }
138                                         }
139                                 }
140                         
141                                 D += 0.01;
142                         
143                                 //clear out old Values
144                                 cValues.clear();
145                         }
146                 }
147                 
148                 /**********************************************************/
149                 //find the signifigance of the user matrix' sumdelta values
150                 /**********************************************************/
151                 
152                 for (int t = 0; t < rsumDelta.size(); t++) {
153                         //sort rsumDelta t
154                         sort(rsumDelta[t].begin(), rsumDelta[t].end());
155                         
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);    
161                         
162                         //the signifigance is the number of trees with the users score or higher 
163                         sumDeltaSig.push_back((iters-index)/(float)iters);
164
165                 }
166                 
167                 printSummaryFile();
168                 
169                 //clear out users groups
170                 globaldata->Groups.clear();
171                 
172                 return 0;
173         }
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";
176                 exit(1);
177         }
178         catch(...) {
179                 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180                 exit(1);
181         }       
182 }
183 //**********************************************************************************************************************
184 void LibShuffCommand::printCoverageFile(float d) {
185         try {
186                 //format output
187                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
188                 
189                 out << setprecision(globaldata->getIters().length()) << d << '\t';
190                 
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';
195                         }
196                 }
197                 
198                 //print out delta values
199                 for (int i = 0; i < deltaValues.size(); i++) {
200                         out << deltaValues[i] << '\t';
201                 }
202                 
203                 out << endl;
204         
205         }
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";
208                 exit(1);
209         }
210         catch(...) {
211                 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212                 exit(1);
213         }       
214
215 //**********************************************************************************************************************
216 void LibShuffCommand::printSummaryFile() {
217         try {
218                 //format output
219                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
220                 
221                 for (int i = 0; i < numGroups; i++) {
222                         for (int j = 0; j < numGroups; j++) {
223                                 //don't output AA to AA
224                                 if (i != j) {
225                                         outSum << "Delta" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t';
226                                 }
227                         }
228                 }
229                 outSum << endl;
230                 
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';
234                 }
235                 
236                 outSum << endl;
237         
238         }
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";
241                 exit(1);
242         }
243         catch(...) {
244                 cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }       
247
248
249 //**********************************************************************************************************************
250 void LibShuffCommand::setGroups() {
251         try {
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]);
257                         }
258                 }else {
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);
266                                         }
267                                 }
268                         
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]);
274                                         }
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]);
282                                 }
283                         }
284                 }
285                 
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]);
291                         }
292                 }
293         }
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";
296                 exit(1);
297         }
298         catch(...) {
299                 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300                 exit(1);
301         }
302 }
303 /***********************************************************/
304 int LibShuffCommand::findIndex(float score, int index) {
305         try{
306                 for (int i = 0; i < rsumDelta[index].size(); i++) {
307                         if (rsumDelta[index][i] >= score)       {       return i;       }
308                 }
309                 return rsumDelta[index].size();
310         }
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";
313                 exit(1);
314         }
315         catch(...) {
316                 cout << "An unknown error has occurred in the LibShuffCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317                 exit(1);
318         }
319 }
320
321 /***********************************************************/
322