]> git.donarmstrong.com Git - mothur.git/blob - libshuffcommand.cpp
broke up globaldata and moved error checking and help into commands
[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 /* This class is designed to implement an integral form of the Cramer-von Mises statistic.
11         you may refer to the "Integration of Microbial Ecology and Statistics: A Test To Compare Gene Libraries" 
12         paper in Applied and Environmental Microbiology, Sept. 2004, p. 5485-5492 0099-2240/04/$8.00+0  
13         DOI: 10.1128/AEM.70.9.5485-5492.2004 Copyright 2004 American Society for Microbiology for more information. */
14
15
16 #include "libshuffcommand.h"
17 #include "libshuff.h"
18 #include "slibshuff.h"
19 #include "dlibshuff.h"
20
21 //**********************************************************************************************************************
22
23 LibShuffCommand::LibShuffCommand(string option){
24         try {
25                 srand( (unsigned)time( NULL ) );
26                 
27                 globaldata = GlobalData::getInstance();
28                 abort = false;
29                 Groups.clear();
30                 
31                 
32                 //allow user to run help
33                 if(option == "help") { help(); abort = true; }
34                 
35                 else {
36                         //valid paramters for this command
37                         string Array[] =  {"iters","groups","step","form","cutoff"};
38                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
39                         
40                         parser = new OptionParser();
41                         parser->parse(option, parameters);  delete parser;
42                         
43                         ValidParameters* validParameter = new ValidParameters();
44                 
45                         //check to make sure all parameters are valid for command
46                         for (it = parameters.begin(); it != parameters.end(); it++) { 
47                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
48                         }
49                         
50                         //make sure the user has already run the read.dist command
51                         if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) {
52                                 cout << "You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. " << endl; abort = true;; 
53                         }
54                                                 
55                         //check for optional parameter and set defaults
56                         // ...at some point should added some additional type checking...
57                         groups = validParameter->validFile(parameters, "groups", false);                        
58                         if (groups == "not found") { groups = ""; savegroups = groups; }
59                         else { 
60                                 savegroups = groups;
61                                 splitAtDash(groups, Groups);
62                                 globaldata->Groups = Groups;
63                         }
64                                 
65                         string temp;
66                         temp = validParameter->validFile(parameters, "iters", false);                           if (temp == "not found") { temp = "10000"; }
67                         convert(temp, iters); 
68                         
69                         temp = validParameter->validFile(parameters, "cutoff", false);                          if (temp == "not found") { temp = "1.0"; }
70                         convert(temp, cutOff); 
71                         
72                         temp = validParameter->validFile(parameters, "step", false);                            if (temp == "not found") { temp = "0.01"; }
73                         convert(temp, step); 
74         
75                         userform = validParameter->validFile(parameters, "form", false);                        if (userform == "not found") { userform = "integral"; }
76                         
77                         delete validParameter;
78                         
79                         if (abort == false) {
80                         
81                                 matrix = globaldata->gMatrix;                           //get the distance matrix
82                                 setGroups();                                                            //set the groups to be analyzed
83
84                                 if(userform == "discrete"){
85                                         form = new DLibshuff(matrix, iters, step, cutOff);
86                                 }
87                                 else{
88                                         form = new SLibshuff(matrix, iters, cutOff);
89                                 }
90                         }
91                         
92                 }
93                 
94         }
95         catch(exception& e) {
96                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
97                 exit(1);
98         }
99         catch(...) {
100                 cout << "An unknown error has occurred in the LibShuffCommand class function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
101                 exit(1);
102         }       
103                         
104 }
105 //**********************************************************************************************************************
106
107 void LibShuffCommand::help(){
108         try {
109                 cout << "The libshuff command can only be executed after a successful read.dist command including a groupfile." << "\n";
110                 cout << "The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required." << "\n";
111                 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups." << "\n";
112                 cout << "The group names are separated by dashes.  The iters parameter allows you to specify how many random matrices you would like compared to your matrix." << "\n";
113                 cout << "The step parameter allows you to specify change in distance you would like between each output if you are using the discrete form." << "\n";
114                 cout << "The form parameter allows you to specify if you would like to analyze your matrix using the discrete or integral form. Your options are integral or discrete." << "\n";
115                 cout << "The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep)." << "\n";
116                 cout << "Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0)." << "\n";
117                 cout << "The default value for groups is all the groups in your groupfile, iters is 10000, cutoff is 1.0, form is integral and step is 0.01." << "\n";
118                 cout << "The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual." << "\n";
119                 cout << "Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters)." << "\n" << "\n";
120         }
121         catch(exception& e) {
122                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123                 exit(1);
124         }
125         catch(...) {
126                 cout << "An unknown error has occurred in the LibShuffCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127                 exit(1);
128         }       
129 }
130
131 //**********************************************************************************************************************
132
133 int LibShuffCommand::execute(){
134         try {
135                 
136                 if (abort == true) {    return 0;       }
137                 
138                 savedDXYValues = form->evaluateAll();
139                 savedMinValues = form->getSavedMins();
140                 
141                 pValueCounts.resize(numGroups);
142                 for(int i=0;i<numGroups;i++){
143                         pValueCounts[i].assign(numGroups, 0);
144                 }
145                 
146                 Progress* reading = new Progress();
147                 
148                 for(int i=0;i<numGroups-1;i++) {
149                         for(int j=i+1;j<numGroups;j++) {
150                                 reading->newLine(groupNames[i]+'-'+groupNames[j], iters);
151                                 for(int p=0;p<iters;p++) {              
152                                         form->randomizeGroups(i,j);
153                                         if(form->evaluatePair(i,j) >= savedDXYValues[i][j])     {       pValueCounts[i][j]++;   }
154                                         if(form->evaluatePair(j,i) >= savedDXYValues[j][i])     {       pValueCounts[j][i]++;   }
155                                         reading->update(p);                     
156                                 }
157                                 form->resetGroup(i);
158                                 form->resetGroup(j);
159                         }
160                 }
161                 reading->finish();
162                 delete reading;
163
164                 cout << endl;
165                 printSummaryFile();
166                 printCoverageFile();
167                 
168                 //clear out users groups
169                 globaldata->Groups.clear();
170                 delete form;
171                 
172                 //delete globaldata's copy of the gmatrix to free up memory
173                 delete globaldata->gMatrix;  globaldata->gMatrix = NULL;
174                 
175                 return 0;
176         }
177         catch(exception& e) {
178                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179                 exit(1);
180         }
181         catch(...) {
182                 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }       
185 }
186
187 //**********************************************************************************************************************
188
189 void LibShuffCommand::printCoverageFile() {
190         try {
191
192                 ofstream outCov;
193                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage";
194                 openOutputFile(summaryFile, outCov);
195                 outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
196                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
197                 
198                 map<double,vector<int> > allDistances;
199                 map<double,vector<int> >::iterator it;
200
201                 vector<vector<int> > indices(numGroups);
202                 int numIndices = numGroups * numGroups;
203                 
204                 int index = 0;
205                 for(int i=0;i<numGroups;i++){
206                         indices[i].assign(numGroups,0);
207                         for(int j=0;j<numGroups;j++){
208                                 indices[i][j] = index++;
209                                 for(int k=0;k<savedMinValues[i][j].size();k++){
210                                         if(allDistances[savedMinValues[i][j][k]].size() != 0){
211                                                 allDistances[savedMinValues[i][j][k]][indices[i][j]]++;
212                                         }
213                                         else{
214                                                 allDistances[savedMinValues[i][j][k]].assign(numIndices, 0);
215                                                 allDistances[savedMinValues[i][j][k]][indices[i][j]] = 1;
216                                         }
217                                 }
218                         }
219                 }
220                 it=allDistances.begin();
221                 
222                 cout << setprecision(8);
223
224                 vector<int> prevRow = it->second;
225                 it++;
226                 
227                 for(it;it!=allDistances.end();it++){
228                         for(int i=0;i<it->second.size();i++){
229                                 it->second[i] += prevRow[i];
230                         }
231                         prevRow = it->second;
232                 }
233                 
234                 vector<int> lastRow = allDistances.rbegin()->second;
235                 outCov << setprecision(8);
236                 
237                 outCov << "dist";
238                 for (int i = 0; i < numGroups; i++){
239                         outCov << '\t' << groupNames[i];
240                 }
241                 for (int i=0;i<numGroups;i++){
242                         for(int j=i+1;j<numGroups;j++){
243                                 outCov << '\t' << groupNames[i] << '-' << groupNames[j] << '\t';
244                                 outCov << groupNames[j] << '-' << groupNames[i];
245                         }
246                 }
247                 outCov << endl;
248                 
249                 for(it=allDistances.begin();it!=allDistances.end();it++){
250                         outCov << it->first << '\t';
251                         for(int i=0;i<numGroups;i++){
252                                 outCov << it->second[indices[i][i]]/(float)lastRow[indices[i][i]] << '\t';
253                         }
254                         for(int i=0;i<numGroups;i++){
255                                 for(int j=i+1;j<numGroups;j++){
256                                         outCov << it->second[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t';
257                                         outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t';
258                                 }
259                         }
260                         outCov << endl;
261                 }
262                 
263         }
264         catch(exception& e) {
265                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
266                 exit(1);
267         }
268         catch(...) {
269                 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270                 exit(1);
271         }       
272
273
274 //**********************************************************************************************************************
275
276 void LibShuffCommand::printSummaryFile() {
277         try {
278
279                 ofstream outSum;
280                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary";
281                 openOutputFile(summaryFile, outSum);
282
283                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
284                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
285                 
286                 cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
287                 outSum << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
288         
289                 int precision = (int)log10(iters);
290                 for(int i=0;i<numGroups;i++){
291                         for(int j=i+1;j<numGroups;j++){
292                                 if(pValueCounts[i][j]){
293                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
294                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
295                                 }
296                                 else{
297                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
298                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
299                                 }
300                                 if(pValueCounts[j][i]){
301                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
302                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
303                                 }
304                                 else{
305                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
306                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
307                                 }
308                         }
309                 }
310                 
311                 
312         }
313         catch(exception& e) {
314                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315                 exit(1);
316         }
317         catch(...) {
318                 cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319                 exit(1);
320         }       
321
322
323 //**********************************************************************************************************************
324
325 void LibShuffCommand::setGroups() {
326         try {
327                 //if the user has not entered specific groups to analyze then do them all
328                 if (globaldata->Groups.size() == 0) {
329                         numGroups = globaldata->gGroupmap->getNumGroups();
330                         for (int i=0; i < numGroups; i++) { 
331                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
332                         }
333                 } else {
334                         if (savegroups != "all") {
335                                 //check that groups are valid
336                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
337                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
338                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
339                                                 // erase the invalid group from globaldata->Groups
340                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
341                                         }
342                                 }
343                         
344                                 //if the user only entered invalid groups
345                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
346                                         numGroups = globaldata->gGroupmap->getNumGroups();
347                                         for (int i=0; i < numGroups; i++) { 
348                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
349                                         }
350                                         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; 
351                                 } else { numGroups = globaldata->Groups.size(); }
352                         } else { //users wants all groups
353                                 numGroups = globaldata->gGroupmap->getNumGroups();
354                                 globaldata->Groups.clear();
355                                 for (int i=0; i < numGroups; i++) { 
356                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
357                                 }
358                         }
359                 }
360
361                 //sort so labels match
362                 sort(globaldata->Groups.begin(), globaldata->Groups.end());
363                 
364                 //sort
365                 sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
366
367                 groupNames = globaldata->Groups;
368
369                 // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
370 //              for (int i=0; i<numGroups; i++) { 
371 //                      for (int l = 0; l < numGroups; l++) {
372 //                              //set group comparison labels
373 //                              groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
374 //                      }
375 //              }
376         }
377         catch(exception& e) {
378                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
379                 exit(1);
380         }
381         catch(...) {
382                 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
383                 exit(1);
384         }
385 }
386
387 /***********************************************************/