]> git.donarmstrong.com Git - mothur.git/blob - libshuffcommand.cpp
added an alignreport option to screen seqs
[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                                 cout << "You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. " << endl; 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                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
94                 exit(1);
95         }
96         catch(...) {
97                 cout << "An unknown error has occurred in the LibShuffCommand class function LibShuffCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
98                 exit(1);
99         }       
100                         
101 }
102 //**********************************************************************************************************************
103
104 void LibShuffCommand::help(){
105         try {
106                 cout << "The libshuff command can only be executed after a successful read.dist command including a groupfile." << "\n";
107                 cout << "The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required." << "\n";
108                 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";
109                 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";
110                 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";
111                 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";
112                 cout << "The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep)." << "\n";
113                 cout << "Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0)." << "\n";
114                 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";
115                 cout << "The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual." << "\n";
116                 cout << "Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters)." << "\n" << "\n";
117         }
118         catch(exception& e) {
119                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the LibShuffCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
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                                 for(int p=0;p<iters;p++) {              
149                                         form->randomizeGroups(i,j);
150                                         if(form->evaluatePair(i,j) >= savedDXYValues[i][j])     {       pValueCounts[i][j]++;   }
151                                         if(form->evaluatePair(j,i) >= savedDXYValues[j][i])     {       pValueCounts[j][i]++;   }
152                                         reading->update(p);                     
153                                 }
154                                 form->resetGroup(i);
155                                 form->resetGroup(j);
156                         }
157                 }
158                 reading->finish();
159                 delete reading;
160
161                 cout << endl;
162                 printSummaryFile();
163                 printCoverageFile();
164                 
165                 //clear out users groups
166                 globaldata->Groups.clear();
167                 delete form;
168                 
169                 //delete globaldata's copy of the gmatrix to free up memory
170                 delete globaldata->gMatrix;  globaldata->gMatrix = NULL;
171                 
172                 return 0;
173         }
174         catch(exception& e) {
175                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176                 exit(1);
177         }
178         catch(...) {
179                 cout << "An unknown error has occurred in the LibShuffCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
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                                 for(int k=0;k<savedMinValues[i][j].size();k++){
207                                         if(allDistances[savedMinValues[i][j][k]].size() != 0){
208                                                 allDistances[savedMinValues[i][j][k]][indices[i][j]]++;
209                                         }
210                                         else{
211                                                 allDistances[savedMinValues[i][j][k]].assign(numIndices, 0);
212                                                 allDistances[savedMinValues[i][j][k]][indices[i][j]] = 1;
213                                         }
214                                 }
215                         }
216                 }
217                 it=allDistances.begin();
218                 
219                 cout << setprecision(8);
220
221                 vector<int> prevRow = it->second;
222                 it++;
223                 
224                 for(it;it!=allDistances.end();it++){
225                         for(int i=0;i<it->second.size();i++){
226                                 it->second[i] += prevRow[i];
227                         }
228                         prevRow = it->second;
229                 }
230                 
231                 vector<int> lastRow = allDistances.rbegin()->second;
232                 outCov << setprecision(8);
233                 
234                 outCov << "dist";
235                 for (int i = 0; i < numGroups; i++){
236                         outCov << '\t' << groupNames[i];
237                 }
238                 for (int i=0;i<numGroups;i++){
239                         for(int j=i+1;j<numGroups;j++){
240                                 outCov << '\t' << groupNames[i] << '-' << groupNames[j] << '\t';
241                                 outCov << groupNames[j] << '-' << groupNames[i];
242                         }
243                 }
244                 outCov << endl;
245                 
246                 for(it=allDistances.begin();it!=allDistances.end();it++){
247                         outCov << it->first << '\t';
248                         for(int i=0;i<numGroups;i++){
249                                 outCov << it->second[indices[i][i]]/(float)lastRow[indices[i][i]] << '\t';
250                         }
251                         for(int i=0;i<numGroups;i++){
252                                 for(int j=i+1;j<numGroups;j++){
253                                         outCov << it->second[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t';
254                                         outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t';
255                                 }
256                         }
257                         outCov << endl;
258                 }
259                 
260         }
261         catch(exception& e) {
262                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263                 exit(1);
264         }
265         catch(...) {
266                 cout << "An unknown error has occurred in the LibShuffCommand class function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
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                 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                                 if(pValueCounts[i][j]){
290                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
291                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
292                                 }
293                                 else{
294                                         cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
295                                         outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[i][j] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
296                                 }
297                                 if(pValueCounts[j][i]){
298                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
299                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
300                                 }
301                                 else{
302                                         cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
303                                         outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[j][i] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
304                                 }
305                         }
306                 }
307                 
308                 
309         }
310         catch(exception& e) {
311                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312                 exit(1);
313         }
314         catch(...) {
315                 cout << "An unknown error has occurred in the LibShuffCommand class function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316                 exit(1);
317         }       
318
319
320 //**********************************************************************************************************************
321
322 void LibShuffCommand::setGroups() {
323         try {
324                 //if the user has not entered specific groups to analyze then do them all
325                 if (globaldata->Groups.size() == 0) {
326                         numGroups = globaldata->gGroupmap->getNumGroups();
327                         for (int i=0; i < numGroups; i++) { 
328                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
329                         }
330                 } else {
331                         if (savegroups != "all") {
332                                 //check that groups are valid
333                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
334                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
335                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
336                                                 // erase the invalid group from globaldata->Groups
337                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
338                                         }
339                                 }
340                         
341                                 //if the user only entered invalid groups
342                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
343                                         numGroups = globaldata->gGroupmap->getNumGroups();
344                                         for (int i=0; i < numGroups; i++) { 
345                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
346                                         }
347                                         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; 
348                                 } else { numGroups = globaldata->Groups.size(); }
349                         } else { //users wants all groups
350                                 numGroups = globaldata->gGroupmap->getNumGroups();
351                                 globaldata->Groups.clear();
352                                 for (int i=0; i < numGroups; i++) { 
353                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
354                                 }
355                         }
356                 }
357
358                 //sort so labels match
359                 sort(globaldata->Groups.begin(), globaldata->Groups.end());
360                 
361                 //sort
362                 sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
363
364                 groupNames = globaldata->Groups;
365
366                 // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...;
367 //              for (int i=0; i<numGroups; i++) { 
368 //                      for (int l = 0; l < numGroups; l++) {
369 //                              //set group comparison labels
370 //                              groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
371 //                      }
372 //              }
373         }
374         catch(exception& e) {
375                 cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
376                 exit(1);
377         }
378         catch(...) {
379                 cout << "An unknown error has occurred in the LibShuffCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
380                 exit(1);
381         }
382 }
383
384 /***********************************************************/