]> git.donarmstrong.com Git - mothur.git/blob - libshuffcommand.cpp
fixed bug in 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 /* 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                 
26                 globaldata = GlobalData::getInstance();
27                 abort = false;
28                 Groups.clear();
29                 
30                 
31                 //allow user to run help
32                 if(option == "help") { help(); abort = true; }
33                 
34                 else {
35                         //valid paramters for this command
36                         string Array[] =  {"iters","groups","step","form","cutoff"};
37                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
38                         
39                         OptionParser parser(option);
40                         map<string, string> parameters = parser.getParameters();
41                         
42                         ValidParameters validParameter;
43                 
44                         //check to make sure all parameters are valid for command
45                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
46                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
47                         }
48                         
49                         //make sure the user has already run the read.dist command
50                         if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) {
51                                 mothurOut("You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. "); mothurOutEndLine(); abort = true;; 
52                         }
53                                                 
54                         //check for optional parameter and set defaults
55                         // ...at some point should added some additional type checking...
56                         groups = validParameter.validFile(parameters, "groups", false);                 
57                         if (groups == "not found") { groups = ""; savegroups = groups; }
58                         else { 
59                                 savegroups = groups;
60                                 splitAtDash(groups, Groups);
61                                 globaldata->Groups = Groups;
62                         }
63                                 
64                         string temp;
65                         temp = validParameter.validFile(parameters, "iters", false);                            if (temp == "not found") { temp = "10000"; }
66                         convert(temp, iters); 
67                         
68                         temp = validParameter.validFile(parameters, "cutoff", false);                           if (temp == "not found") { temp = "1.0"; }
69                         convert(temp, cutOff); 
70                         
71                         temp = validParameter.validFile(parameters, "step", false);                             if (temp == "not found") { temp = "0.01"; }
72                         convert(temp, step); 
73         
74                         userform = validParameter.validFile(parameters, "form", false);                 if (userform == "not found") { userform = "integral"; }
75                         
76                         if (abort == false) {
77                         
78                                 matrix = globaldata->gMatrix;                           //get the distance matrix
79                                 setGroups();                                                            //set the groups to be analyzed and sorts them
80                                 
81                                 /********************************************************************************************/
82                                 //this is needed because when we read the matrix we sort it into groups in alphabetical order
83                                 //the rest of the command and the classes used in this command assume specific order
84                                 /********************************************************************************************/
85                                 matrix->setGroups(globaldata->gGroupmap->namesOfGroups);
86                                 vector<int> sizes;
87                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {  sizes.push_back(globaldata->gGroupmap->getNumSeqs(globaldata->gGroupmap->namesOfGroups[i]));  }
88                                 matrix->setSizes(sizes);
89                                 
90
91                                 if(userform == "discrete"){
92                                         form = new DLibshuff(matrix, iters, step, cutOff);
93                                 }
94                                 else{
95                                         form = new SLibshuff(matrix, iters, cutOff);
96                                 }
97                         }
98                         
99                 }
100                 
101         }
102         catch(exception& e) {
103                 errorOut(e, "LibShuffCommand", "LibShuffCommand");
104                 exit(1);
105         }
106 }
107 //**********************************************************************************************************************
108
109 void LibShuffCommand::help(){
110         try {
111                 mothurOut("The libshuff command can only be executed after a successful read.dist command including a groupfile.\n");
112                 mothurOut("The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required.\n");
113                 mothurOut("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");
114                 mothurOut("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");
115                 mothurOut("The step parameter allows you to specify change in distance you would like between each output if you are using the discrete form.\n");
116                 mothurOut("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");
117                 mothurOut("The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n");
118                 mothurOut("Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n");
119                 mothurOut("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");
120                 mothurOut("The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n");
121                 mothurOut("Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n\n");
122         }
123         catch(exception& e) {
124                 errorOut(e, "LibShuffCommand", "help");
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                                 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
150                                 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
151         
152                                 for(int p=0;p<iters;p++) {              
153                                         form->randomizeGroups(spoti,spotj); 
154                                         if(form->evaluatePair(spoti,spotj) >= savedDXYValues[spoti][spotj])     {       pValueCounts[i][j]++;   }
155                                         if(form->evaluatePair(spotj,spoti) >= savedDXYValues[spotj][spoti])     {       pValueCounts[j][i]++;   }
156                                         reading->update(p);                     
157                                 }
158                                 form->resetGroup(spoti);
159                                 form->resetGroup(spotj);
160                         }
161                 }
162                 reading->finish();
163                 delete reading;
164
165                 mothurOutEndLine();
166                 printSummaryFile();
167                 printCoverageFile();
168                 
169                 //clear out users groups
170                 globaldata->Groups.clear();
171                 delete form;
172                 
173                 //delete globaldata's copy of the gmatrix to free up memory
174                 delete globaldata->gMatrix;  globaldata->gMatrix = NULL;
175                 
176                 return 0;
177         }
178         catch(exception& e) {
179                 errorOut(e, "LibShuffCommand", "execute");
180                 exit(1);
181         }
182 }
183
184 //**********************************************************************************************************************
185
186 void LibShuffCommand::printCoverageFile() {
187         try {
188
189                 ofstream outCov;
190                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage";
191                 openOutputFile(summaryFile, outCov);
192                 outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
193                 //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
194                 
195                 map<double,vector<int> > allDistances;
196                 map<double,vector<int> >::iterator it;
197
198                 vector<vector<int> > indices(numGroups);
199                 int numIndices = numGroups * numGroups;
200                 
201                 int index = 0;
202                 for(int i=0;i<numGroups;i++){
203                         indices[i].assign(numGroups,0);
204                         for(int j=0;j<numGroups;j++){
205                                 indices[i][j] = index++;
206                                 
207                                 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
208                                 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
209                                 
210                                 for(int k=0;k<savedMinValues[spoti][spotj].size();k++){
211                                         if(allDistances[savedMinValues[spoti][spotj][k]].size() != 0){
212                                                 allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]]++;
213                                         }
214                                         else{
215                                                 allDistances[savedMinValues[spoti][spotj][k]].assign(numIndices, 0);
216                                                 allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]] = 1;
217                                         }
218                                 }
219                         }
220                 }
221                 it=allDistances.begin();
222                 
223                 //cout << setprecision(8);
224
225                 vector<int> prevRow = it->second;
226                 it++;
227                 
228                 for(;it!=allDistances.end();it++){
229                         for(int i=0;i<it->second.size();i++){
230                                 it->second[i] += prevRow[i];
231                         }
232                         prevRow = it->second;
233                 }
234                 
235                 vector<int> lastRow = allDistances.rbegin()->second;
236                 outCov << setprecision(8);
237                 
238                 outCov << "dist";
239                 for (int i = 0; i < numGroups; i++){
240                         outCov << '\t' << groupNames[i];
241                 }
242                 for (int i=0;i<numGroups;i++){
243                         for(int j=i+1;j<numGroups;j++){
244                                 outCov << '\t' << groupNames[i] << '-' << groupNames[j] << '\t';
245                                 outCov << groupNames[j] << '-' << groupNames[i];
246                         }
247                 }
248                 outCov << endl;
249                 
250                 for(it=allDistances.begin();it!=allDistances.end();it++){
251                         outCov << it->first << '\t';
252                         for(int i=0;i<numGroups;i++){
253                                 outCov << it->second[indices[i][i]]/(float)lastRow[indices[i][i]] << '\t';
254                         }
255                         for(int i=0;i<numGroups;i++){
256                                 for(int j=i+1;j<numGroups;j++){
257                                         outCov << it->second[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t';
258                                         outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t';
259                                 }
260                         }
261                         outCov << endl;
262                 }
263                 outCov.close();
264         }
265         catch(exception& e) {
266                 errorOut(e, "LibShuffCommand", "printCoverageFile");
267                 exit(1);
268         }
269
270
271 //**********************************************************************************************************************
272
273 void LibShuffCommand::printSummaryFile() {
274         try {
275
276                 ofstream outSum;
277                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary";
278                 openOutputFile(summaryFile, outSum);
279
280                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
281                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
282                 
283                 cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
284                 mothurOutJustToLog("Comparison\tdCXYScore\tSignificance"); mothurOutEndLine();
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                                 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
291                                 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
292                                 
293                                 if(pValueCounts[i][j]){
294                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
295                                         mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((pValueCounts[i][j]/(float)iters))); mothurOutEndLine();
296                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
297                                 }
298                                 else{
299                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
300                                         mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((1/(float)iters))); mothurOutEndLine();
301                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
302                                 }
303                                 if(pValueCounts[j][i]){
304                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
305                                         mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((pValueCounts[j][i]/(float)iters))); mothurOutEndLine();
306                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
307                                 }
308                                 else{
309                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
310                                         mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((1/(float)iters))); mothurOutEndLine();
311                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
312                                 }
313                         }
314                 }
315                 
316                 outSum.close();
317         }
318         catch(exception& e) {
319                 errorOut(e, "LibShuffCommand", "printSummaryFile");
320                 exit(1);
321         }
322
323
324 //**********************************************************************************************************************
325
326 void LibShuffCommand::setGroups() {
327         try {
328                 //if the user has not entered specific groups to analyze then do them all
329                 if (globaldata->Groups.size() == 0) {
330                         numGroups = globaldata->gGroupmap->getNumGroups();
331                         for (int i=0; i < numGroups; i++) { 
332                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
333                         }
334                 } else {
335                         if (savegroups != "all") {
336                                 //check that groups are valid
337                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
338                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
339                                                 mothurOut(globaldata->Groups[i] + " is not a valid group, and will be disregarded."); mothurOutEndLine();
340                                                 // erase the invalid group from globaldata->Groups
341                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
342                                         }
343                                 }
344                         
345                                 //if the user only entered invalid groups
346                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
347                                         numGroups = globaldata->gGroupmap->getNumGroups();
348                                         for (int i=0; i < numGroups; i++) { 
349                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
350                                         }
351                                         mothurOut("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."); mothurOutEndLine();
352                                 } else { numGroups = globaldata->Groups.size(); }
353                         } else { //users wants all groups
354                                 numGroups = globaldata->gGroupmap->getNumGroups();
355                                 globaldata->Groups.clear();
356                                 for (int i=0; i < numGroups; i++) { 
357                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
358                                 }
359                         }
360                 }
361
362                 //sort so labels match
363                 sort(globaldata->Groups.begin(), globaldata->Groups.end());
364                 
365                 //sort
366                 sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
367                 
368                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {  globaldata->gGroupmap->groupIndex[globaldata->gGroupmap->namesOfGroups[i]] = i;  }
369
370                 groupNames = globaldata->Groups;
371
372         }
373         catch(exception& e) {
374                 errorOut(e, "LibShuffCommand", "setGroups");
375                 exit(1);
376         }
377 }
378
379 /***********************************************************/