]> git.donarmstrong.com Git - mothur.git/blob - libshuffcommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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                         OptionParser parser(option);
41                         map<string, string> parameters = parser.getParameters();
42                         
43                         ValidParameters validParameter;
44                 
45                         //check to make sure all parameters are valid for command
46                         for (map<string,string>::iterator 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                         if (abort == false) {
78                         
79                                 matrix = globaldata->gMatrix;                           //get the distance matrix
80                                 setGroups();                                                            //set the groups to be analyzed
81
82                                 if(userform == "discrete"){
83                                         form = new DLibshuff(matrix, iters, step, cutOff);
84                                 }
85                                 else{
86                                         form = new SLibshuff(matrix, iters, cutOff);
87                                 }
88                         }
89                         
90                 }
91                 
92         }
93         catch(exception& e) {
94                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95                 exit(1);
96         }
97         catch(...) {
98                 cout << "An unknown error has occurred in the LibShuffCommand class function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99                 exit(1);
100         }       
101                         
102 }
103 //**********************************************************************************************************************
104
105 void LibShuffCommand::help(){
106         try {
107                 cout << "The libshuff command can only be executed after a successful read.dist command including a groupfile." << "\n";
108                 cout << "The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required." << "\n";
109                 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";
110                 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";
111                 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";
112                 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";
113                 cout << "The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep)." << "\n";
114                 cout << "Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0)." << "\n";
115                 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";
116                 cout << "The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual." << "\n";
117                 cout << "Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters)." << "\n" << "\n";
118         }
119         catch(exception& e) {
120                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121                 exit(1);
122         }
123         catch(...) {
124                 cout << "An unknown error has occurred in the LibShuffCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
125                 exit(1);
126         }       
127 }
128
129 //**********************************************************************************************************************
130
131 int LibShuffCommand::execute(){
132         try {
133                 
134                 if (abort == true) {    return 0;       }
135                 
136                 savedDXYValues = form->evaluateAll();
137                 savedMinValues = form->getSavedMins();
138                 
139                 pValueCounts.resize(numGroups);
140                 for(int i=0;i<numGroups;i++){
141                         pValueCounts[i].assign(numGroups, 0);
142                 }
143                 
144                 Progress* reading = new Progress();
145                 
146                 for(int i=0;i<numGroups-1;i++) {
147                         for(int j=i+1;j<numGroups;j++) {
148                                 reading->newLine(groupNames[i]+'-'+groupNames[j], iters);
149                                 for(int p=0;p<iters;p++) {              
150                                         form->randomizeGroups(i,j);
151                                         if(form->evaluatePair(i,j) >= savedDXYValues[i][j])     {       pValueCounts[i][j]++;   }
152                                         if(form->evaluatePair(j,i) >= savedDXYValues[j][i])     {       pValueCounts[j][i]++;   }
153                                         reading->update(p);                     
154                                 }
155                                 form->resetGroup(i);
156                                 form->resetGroup(j);
157                         }
158                 }
159                 reading->finish();
160                 delete reading;
161
162                 cout << endl;
163                 printSummaryFile();
164                 printCoverageFile();
165                 
166                 //clear out users groups
167                 globaldata->Groups.clear();
168                 delete form;
169                 
170                 //delete globaldata's copy of the gmatrix to free up memory
171                 delete globaldata->gMatrix;  globaldata->gMatrix = NULL;
172                 
173                 return 0;
174         }
175         catch(exception& e) {
176                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177                 exit(1);
178         }
179         catch(...) {
180                 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181                 exit(1);
182         }       
183 }
184
185 //**********************************************************************************************************************
186
187 void LibShuffCommand::printCoverageFile() {
188         try {
189
190                 ofstream outCov;
191                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage";
192                 openOutputFile(summaryFile, outCov);
193                 outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
194                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
195                 
196                 map<double,vector<int> > allDistances;
197                 map<double,vector<int> >::iterator it;
198
199                 vector<vector<int> > indices(numGroups);
200                 int numIndices = numGroups * numGroups;
201                 
202                 int index = 0;
203                 for(int i=0;i<numGroups;i++){
204                         indices[i].assign(numGroups,0);
205                         for(int j=0;j<numGroups;j++){
206                                 indices[i][j] = index++;
207                                 for(int k=0;k<savedMinValues[i][j].size();k++){
208                                         if(allDistances[savedMinValues[i][j][k]].size() != 0){
209                                                 allDistances[savedMinValues[i][j][k]][indices[i][j]]++;
210                                         }
211                                         else{
212                                                 allDistances[savedMinValues[i][j][k]].assign(numIndices, 0);
213                                                 allDistances[savedMinValues[i][j][k]][indices[i][j]] = 1;
214                                         }
215                                 }
216                         }
217                 }
218                 it=allDistances.begin();
219                 
220                 cout << setprecision(8);
221
222                 vector<int> prevRow = it->second;
223                 it++;
224                 
225                 for(it;it!=allDistances.end();it++){
226                         for(int i=0;i<it->second.size();i++){
227                                 it->second[i] += prevRow[i];
228                         }
229                         prevRow = it->second;
230                 }
231                 
232                 vector<int> lastRow = allDistances.rbegin()->second;
233                 outCov << setprecision(8);
234                 
235                 outCov << "dist";
236                 for (int i = 0; i < numGroups; i++){
237                         outCov << '\t' << groupNames[i];
238                 }
239                 for (int i=0;i<numGroups;i++){
240                         for(int j=i+1;j<numGroups;j++){
241                                 outCov << '\t' << groupNames[i] << '-' << groupNames[j] << '\t';
242                                 outCov << groupNames[j] << '-' << groupNames[i];
243                         }
244                 }
245                 outCov << endl;
246                 
247                 for(it=allDistances.begin();it!=allDistances.end();it++){
248                         outCov << it->first << '\t';
249                         for(int i=0;i<numGroups;i++){
250                                 outCov << it->second[indices[i][i]]/(float)lastRow[indices[i][i]] << '\t';
251                         }
252                         for(int i=0;i<numGroups;i++){
253                                 for(int j=i+1;j<numGroups;j++){
254                                         outCov << it->second[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t';
255                                         outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t';
256                                 }
257                         }
258                         outCov << endl;
259                 }
260                 
261         }
262         catch(exception& e) {
263                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
264                 exit(1);
265         }
266         catch(...) {
267                 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268                 exit(1);
269         }       
270
271
272 //**********************************************************************************************************************
273
274 void LibShuffCommand::printSummaryFile() {
275         try {
276
277                 ofstream outSum;
278                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary";
279                 openOutputFile(summaryFile, outSum);
280
281                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
282                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
283                 
284                 cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
285                 outSum << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
286         
287                 int precision = (int)log10(iters);
288                 for(int i=0;i<numGroups;i++){
289                         for(int j=i+1;j<numGroups;j++){
290                                 if(pValueCounts[i][j]){
291                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
292                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
293                                 }
294                                 else{
295                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
296                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
297                                 }
298                                 if(pValueCounts[j][i]){
299                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
300                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
301                                 }
302                                 else{
303                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
304                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
305                                 }
306                         }
307                 }
308                 
309                 
310         }
311         catch(exception& e) {
312                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. 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 printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317                 exit(1);
318         }       
319
320
321 //**********************************************************************************************************************
322
323 void LibShuffCommand::setGroups() {
324         try {
325                 //if the user has not entered specific groups to analyze then do them all
326                 if (globaldata->Groups.size() == 0) {
327                         numGroups = globaldata->gGroupmap->getNumGroups();
328                         for (int i=0; i < numGroups; i++) { 
329                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
330                         }
331                 } else {
332                         if (savegroups != "all") {
333                                 //check that groups are valid
334                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
335                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
336                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
337                                                 // erase the invalid group from globaldata->Groups
338                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
339                                         }
340                                 }
341                         
342                                 //if the user only entered invalid groups
343                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
344                                         numGroups = globaldata->gGroupmap->getNumGroups();
345                                         for (int i=0; i < numGroups; i++) { 
346                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
347                                         }
348                                         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; 
349                                 } else { numGroups = globaldata->Groups.size(); }
350                         } else { //users wants all groups
351                                 numGroups = globaldata->gGroupmap->getNumGroups();
352                                 globaldata->Groups.clear();
353                                 for (int i=0; i < numGroups; i++) { 
354                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
355                                 }
356                         }
357                 }
358
359                 //sort so labels match
360                 sort(globaldata->Groups.begin(), globaldata->Groups.end());
361                 
362                 //sort
363                 sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
364
365                 groupNames = globaldata->Groups;
366
367                 // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
368 //              for (int i=0; i<numGroups; i++) { 
369 //                      for (int l = 0; l < numGroups; l++) {
370 //                              //set group comparison labels
371 //                              groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
372 //                      }
373 //              }
374         }
375         catch(exception& e) {
376                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377                 exit(1);
378         }
379         catch(...) {
380                 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
381                 exit(1);
382         }
383 }
384
385 /***********************************************************/