]> git.donarmstrong.com Git - mothur.git/blob - libshuffcommand.cpp
fixed some bugs and added mgcluster command
[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                 globaldata = GlobalData::getInstance();
26                 abort = false;
27                 Groups.clear();
28                 
29                 
30                 //allow user to run help
31                 if(option == "help") { help(); abort = true; }
32                 
33                 else {
34                         //valid paramters for this command
35                         string Array[] =  {"iters","groups","step","form","cutoff"};
36                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
37                         
38                         OptionParser parser(option);
39                         map<string, string> parameters = parser.getParameters();
40                         
41                         ValidParameters validParameter;
42                 
43                         //check to make sure all parameters are valid for command
44                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
45                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
46                         }
47                         
48                         //make sure the user has already run the read.dist command
49                         if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) {
50                                 mothurOut("You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. "); mothurOutEndLine(); abort = true;; 
51                         }
52                                                 
53                         //check for optional parameter and set defaults
54                         // ...at some point should added some additional type checking...
55                         groups = validParameter.validFile(parameters, "groups", false);                 
56                         if (groups == "not found") { groups = ""; savegroups = groups; }
57                         else { 
58                                 savegroups = groups;
59                                 splitAtDash(groups, Groups);
60                                 globaldata->Groups = Groups;
61                         }
62                                 
63                         string temp;
64                         temp = validParameter.validFile(parameters, "iters", false);                            if (temp == "not found") { temp = "10000"; }
65                         convert(temp, iters); 
66                         
67                         temp = validParameter.validFile(parameters, "cutoff", false);                           if (temp == "not found") { temp = "1.0"; }
68                         convert(temp, cutOff); 
69                         
70                         temp = validParameter.validFile(parameters, "step", false);                             if (temp == "not found") { temp = "0.01"; }
71                         convert(temp, step); 
72         
73                         userform = validParameter.validFile(parameters, "form", false);                 if (userform == "not found") { userform = "integral"; }
74                         
75                         if (abort == false) {
76                 
77                                 matrix = globaldata->gMatrix;                           //get the distance matrix
78                                 setGroups();                                                            //set the groups to be analyzed and sorts them
79         
80                                 /********************************************************************************************/
81                                 //this is needed because when we read the matrix we sort it into groups in alphabetical order
82                                 //the rest of the command and the classes used in this command assume specific order
83                                 /********************************************************************************************/
84                                 matrix->setGroups(globaldata->gGroupmap->namesOfGroups);
85                                 vector<int> sizes;
86                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {   sizes.push_back(globaldata->gGroupmap->getNumSeqs(globaldata->gGroupmap->namesOfGroups[i]));  }
87                                 matrix->setSizes(sizes);
88                         
89
90                                 if(userform == "discrete"){
91                                         form = new DLibshuff(matrix, iters, step, cutOff);
92                                 }
93                                 else{
94                                         form = new SLibshuff(matrix, iters, cutOff);
95                                 }
96                         }
97                         
98                 }
99                 
100         }
101         catch(exception& e) {
102                 errorOut(e, "LibShuffCommand", "LibShuffCommand");
103                 exit(1);
104         }
105 }
106 //**********************************************************************************************************************
107
108 void LibShuffCommand::help(){
109         try {
110                 mothurOut("The libshuff command can only be executed after a successful read.dist command including a groupfile.\n");
111                 mothurOut("The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required.\n");
112                 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");
113                 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");
114                 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");
115                 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");
116                 mothurOut("The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n");
117                 mothurOut("Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n");
118                 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");
119                 mothurOut("The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n");
120                 mothurOut("Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n\n");
121         }
122         catch(exception& e) {
123                 errorOut(e, "LibShuffCommand", "help");
124                 exit(1);
125         }
126 }
127
128 //**********************************************************************************************************************
129
130 int LibShuffCommand::execute(){
131         try {
132                 
133                 if (abort == true) {    return 0;       }
134         
135                 savedDXYValues = form->evaluateAll();
136                 savedMinValues = form->getSavedMins();
137         
138                 pValueCounts.resize(numGroups);
139                 for(int i=0;i<numGroups;i++){
140                         pValueCounts[i].assign(numGroups, 0);
141                 }
142                 
143                 Progress* reading = new Progress();
144                 
145                 for(int i=0;i<numGroups-1;i++) {
146                         for(int j=i+1;j<numGroups;j++) {
147                                 reading->newLine(groupNames[i]+'-'+groupNames[j], iters);
148                                 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
149                                 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
150         
151                                 for(int p=0;p<iters;p++) {              
152                                         form->randomizeGroups(spoti,spotj); 
153                                         if(form->evaluatePair(spoti,spotj) >= savedDXYValues[spoti][spotj])     {       pValueCounts[i][j]++;   }
154                                         if(form->evaluatePair(spotj,spoti) >= savedDXYValues[spotj][spoti])     {       pValueCounts[j][i]++;   }
155                                         reading->update(p);                     
156                                 }
157                                 form->resetGroup(spoti);
158                                 form->resetGroup(spotj);
159                         }
160                 }
161                 reading->finish();
162                 delete reading;
163
164                 mothurOutEndLine();
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                 errorOut(e, "LibShuffCommand", "execute");
179                 exit(1);
180         }
181 }
182
183 //**********************************************************************************************************************
184
185 void LibShuffCommand::printCoverageFile() {
186         try {
187
188                 ofstream outCov;
189                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage";
190                 openOutputFile(summaryFile, outCov);
191                 outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
192                 //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
193                 
194                 map<double,vector<int> > allDistances;
195                 map<double,vector<int> >::iterator it;
196
197                 vector<vector<int> > indices(numGroups);
198                 int numIndices = numGroups * numGroups;
199                 
200                 int index = 0;
201                 for(int i=0;i<numGroups;i++){
202                         indices[i].assign(numGroups,0);
203                         for(int j=0;j<numGroups;j++){
204                                 indices[i][j] = index++;
205                                 
206                                 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
207                                 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
208                                 
209                                 for(int k=0;k<savedMinValues[spoti][spotj].size();k++){
210                                         if(allDistances[savedMinValues[spoti][spotj][k]].size() != 0){
211                                                 allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]]++;
212                                         }
213                                         else{
214                                                 allDistances[savedMinValues[spoti][spotj][k]].assign(numIndices, 0);
215                                                 allDistances[savedMinValues[spoti][spotj][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!=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                 outCov.close();
263         }
264         catch(exception& e) {
265                 errorOut(e, "LibShuffCommand", "printCoverageFile");
266                 exit(1);
267         }
268
269
270 //**********************************************************************************************************************
271
272 void LibShuffCommand::printSummaryFile() {
273         try {
274
275                 ofstream outSum;
276                 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary";
277                 openOutputFile(summaryFile, outSum);
278
279                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
280                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
281                 
282                 cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
283                 mothurOutJustToLog("Comparison\tdCXYScore\tSignificance"); mothurOutEndLine();
284                 outSum << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
285         
286                 int precision = (int)log10(iters);
287                 for(int i=0;i<numGroups;i++){
288                         for(int j=i+1;j<numGroups;j++){
289                                 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
290                                 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
291                                 
292                                 if(pValueCounts[i][j]){
293                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
294                                         mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((pValueCounts[i][j]/(float)iters))); mothurOutEndLine();
295                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
296                                 }
297                                 else{
298                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
299                                         mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((1/(float)iters))); mothurOutEndLine();
300                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
301                                 }
302                                 if(pValueCounts[j][i]){
303                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
304                                         mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((pValueCounts[j][i]/(float)iters))); mothurOutEndLine();
305                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
306                                 }
307                                 else{
308                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
309                                         mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((1/(float)iters))); mothurOutEndLine();
310                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
311                                 }
312                         }
313                 }
314                 
315                 outSum.close();
316         }
317         catch(exception& e) {
318                 errorOut(e, "LibShuffCommand", "printSummaryFile");
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                                                 mothurOut(globaldata->Groups[i] + " is not a valid group, and will be disregarded."); mothurOutEndLine();
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                                         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();
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                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {  globaldata->gGroupmap->groupIndex[globaldata->gGroupmap->namesOfGroups[i]] = i;  }
368
369                 groupNames = globaldata->Groups;
370
371         }
372         catch(exception& e) {
373                 errorOut(e, "LibShuffCommand", "setGroups");
374                 exit(1);
375         }
376 }
377
378 /***********************************************************/