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