5 * Created by Sarah Westcott on 3/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
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. */
16 #include "libshuffcommand.h"
18 #include "slibshuff.h"
19 #include "dlibshuff.h"
21 //**********************************************************************************************************************
23 LibShuffCommand::LibShuffCommand(string option) {
25 globaldata = GlobalData::getInstance();
29 //allow user to run help
30 if(option == "help") { help(); abort = true; }
33 //valid paramters for this command
34 string Array[] = {"iters","groups","step","form","cutoff","outputdir","inputdir"};
35 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
37 OptionParser parser(option);
38 map<string, string> parameters = parser.getParameters();
40 ValidParameters validParameter;
42 //check to make sure all parameters are valid for command
43 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
44 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
47 //if the user changes the output directory command factory will send this info to us in the output parameter
48 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
50 outputDir += m->hasPath(globaldata->getPhylipFile()); //if user entered a file with a path then preserve it
53 //make sure the user has already run the read.dist command
54 if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) {
55 m->mothurOut("You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. "); m->mothurOutEndLine(); abort = true;;
58 //check for optional parameter and set defaults
59 // ...at some point should added some additional type checking...
60 groups = validParameter.validFile(parameters, "groups", false);
61 if (groups == "not found") { groups = ""; savegroups = groups; }
64 m->splitAtDash(groups, Groups);
65 globaldata->Groups = Groups;
69 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "10000"; }
72 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "1.0"; }
73 convert(temp, cutOff);
75 temp = validParameter.validFile(parameters, "step", false); if (temp == "not found") { temp = "0.01"; }
78 userform = validParameter.validFile(parameters, "form", false); if (userform == "not found") { userform = "integral"; }
82 matrix = globaldata->gMatrix; //get the distance matrix
83 setGroups(); //set the groups to be analyzed and sorts them
85 /********************************************************************************************/
86 //this is needed because when we read the matrix we sort it into groups in alphabetical order
87 //the rest of the command and the classes used in this command assume specific order
88 /********************************************************************************************/
89 matrix->setGroups(globaldata->gGroupmap->namesOfGroups);
91 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) { sizes.push_back(globaldata->gGroupmap->getNumSeqs(globaldata->gGroupmap->namesOfGroups[i])); }
92 matrix->setSizes(sizes);
95 if(userform == "discrete"){
96 form = new DLibshuff(matrix, iters, step, cutOff);
99 form = new SLibshuff(matrix, iters, cutOff);
106 catch(exception& e) {
107 m->errorOut(e, "LibShuffCommand", "LibShuffCommand");
111 //**********************************************************************************************************************
113 void LibShuffCommand::help(){
115 m->mothurOut("The libshuff command can only be executed after a successful read.dist command including a groupfile.\n");
116 m->mothurOut("The libshuff command parameters are groups, iters, step, form and cutoff. No parameters are required.\n");
117 m->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");
118 m->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");
119 m->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");
120 m->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");
121 m->mothurOut("The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n");
122 m->mothurOut("Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n");
123 m->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");
124 m->mothurOut("The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n");
125 m->mothurOut("Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n\n");
127 catch(exception& e) {
128 m->errorOut(e, "LibShuffCommand", "help");
133 //**********************************************************************************************************************
135 int LibShuffCommand::execute(){
138 if (abort == true) { return 0; }
140 savedDXYValues = form->evaluateAll();
141 savedMinValues = form->getSavedMins();
143 if (m->control_pressed) { delete form; globaldata->Groups.clear(); delete globaldata->gMatrix; globaldata->gMatrix = NULL; return 0; }
145 pValueCounts.resize(numGroups);
146 for(int i=0;i<numGroups;i++){
147 pValueCounts[i].assign(numGroups, 0);
150 if (m->control_pressed) { delete form; globaldata->Groups.clear(); delete globaldata->gMatrix; globaldata->gMatrix = NULL; return 0; }
152 Progress* reading = new Progress();
154 for(int i=0;i<numGroups-1;i++) {
155 for(int j=i+1;j<numGroups;j++) {
157 if (m->control_pressed) { delete form; globaldata->Groups.clear(); delete globaldata->gMatrix; globaldata->gMatrix = NULL; delete reading; return 0; }
159 reading->newLine(groupNames[i]+'-'+groupNames[j], iters);
160 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
161 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
163 for(int p=0;p<iters;p++) {
165 if (m->control_pressed) { delete form; globaldata->Groups.clear(); delete globaldata->gMatrix; globaldata->gMatrix = NULL; delete reading; return 0; }
167 form->randomizeGroups(spoti,spotj);
168 if(form->evaluatePair(spoti,spotj) >= savedDXYValues[spoti][spotj]) { pValueCounts[i][j]++; }
169 if(form->evaluatePair(spotj,spoti) >= savedDXYValues[spotj][spoti]) { pValueCounts[j][i]++; }
171 if (m->control_pressed) { delete form; globaldata->Groups.clear(); delete globaldata->gMatrix; globaldata->gMatrix = NULL; delete reading; return 0; }
175 form->resetGroup(spoti);
176 form->resetGroup(spotj);
180 if (m->control_pressed) { delete form; globaldata->Groups.clear(); delete globaldata->gMatrix; globaldata->gMatrix = NULL; delete reading; return 0; }
185 m->mothurOutEndLine();
189 //clear out users groups
190 globaldata->Groups.clear();
193 //delete globaldata's copy of the gmatrix to free up memory
194 delete globaldata->gMatrix; globaldata->gMatrix = NULL;
196 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
199 m->mothurOutEndLine();
200 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
201 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
202 m->mothurOutEndLine();
206 catch(exception& e) {
207 m->errorOut(e, "LibShuffCommand", "execute");
212 //**********************************************************************************************************************
214 int LibShuffCommand::printCoverageFile() {
218 summaryFile = outputDir + m->getRootName(m->getSimpleName(globaldata->getPhylipFile())) + "libshuff.coverage";
219 m->openOutputFile(summaryFile, outCov);
220 outputNames.push_back(summaryFile);
221 outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
222 //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
224 map<double,vector<int> > allDistances;
225 map<double,vector<int> >::iterator it;
227 vector<vector<int> > indices(numGroups);
228 int numIndices = numGroups * numGroups;
231 for(int i=0;i<numGroups;i++){
232 indices[i].assign(numGroups,0);
233 for(int j=0;j<numGroups;j++){
234 indices[i][j] = index++;
236 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
237 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
239 for(int k=0;k<savedMinValues[spoti][spotj].size();k++){
241 if(m->control_pressed) { outCov.close(); return 0; }
243 if(allDistances[savedMinValues[spoti][spotj][k]].size() != 0){
244 allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]]++;
247 allDistances[savedMinValues[spoti][spotj][k]].assign(numIndices, 0);
248 allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]] = 1;
253 it=allDistances.begin();
255 //cout << setprecision(8);
257 vector<int> prevRow = it->second;
260 for(;it!=allDistances.end();it++){
261 for(int i=0;i<it->second.size();i++){
262 it->second[i] += prevRow[i];
264 prevRow = it->second;
267 vector<int> lastRow = allDistances.rbegin()->second;
268 outCov << setprecision(8);
271 for (int i = 0; i < numGroups; i++){
272 outCov << '\t' << groupNames[i];
274 for (int i=0;i<numGroups;i++){
275 for(int j=i+1;j<numGroups;j++){
276 if(m->control_pressed) { outCov.close(); return 0; }
277 outCov << '\t' << groupNames[i] << '-' << groupNames[j] << '\t';
278 outCov << groupNames[j] << '-' << groupNames[i];
283 for(it=allDistances.begin();it!=allDistances.end();it++){
284 outCov << it->first << '\t';
285 for(int i=0;i<numGroups;i++){
286 outCov << it->second[indices[i][i]]/(float)lastRow[indices[i][i]] << '\t';
288 for(int i=0;i<numGroups;i++){
289 for(int j=i+1;j<numGroups;j++){
290 if(m->control_pressed) { outCov.close(); return 0; }
292 outCov << it->second[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t';
293 outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t';
302 catch(exception& e) {
303 m->errorOut(e, "LibShuffCommand", "printCoverageFile");
308 //**********************************************************************************************************************
310 int LibShuffCommand::printSummaryFile() {
314 summaryFile = outputDir + m->getRootName(m->getSimpleName(globaldata->getPhylipFile())) + "libshuff.summary";
315 m->openOutputFile(summaryFile, outSum);
316 outputNames.push_back(summaryFile);
318 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
319 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
321 cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
322 m->mothurOutJustToLog("Comparison\tdCXYScore\tSignificance"); m->mothurOutEndLine();
323 outSum << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
325 int precision = (int)log10(iters);
326 for(int i=0;i<numGroups;i++){
327 for(int j=i+1;j<numGroups;j++){
328 if(m->control_pressed) { outSum.close(); return 0; }
330 int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
331 int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
333 if(pValueCounts[i][j]){
334 cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
335 m->mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((pValueCounts[i][j]/(float)iters))); m->mothurOutEndLine();
336 outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
339 cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
340 m->mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((1/(float)iters))); m->mothurOutEndLine();
341 outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
343 if(pValueCounts[j][i]){
344 cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
345 m->mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((pValueCounts[j][i]/(float)iters))); m->mothurOutEndLine();
346 outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
349 cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
350 m->mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((1/(float)iters))); m->mothurOutEndLine();
351 outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
359 catch(exception& e) {
360 m->errorOut(e, "LibShuffCommand", "printSummaryFile");
365 //**********************************************************************************************************************
367 void LibShuffCommand::setGroups() {
369 //if the user has not entered specific groups to analyze then do them all
370 if (globaldata->Groups.size() == 0) {
371 numGroups = globaldata->gGroupmap->getNumGroups();
372 for (int i=0; i < numGroups; i++) {
373 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
376 if (savegroups != "all") {
377 //check that groups are valid
378 for (int i = 0; i < globaldata->Groups.size(); i++) {
379 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
380 m->mothurOut(globaldata->Groups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
381 // erase the invalid group from globaldata->Groups
382 globaldata->Groups.erase(globaldata->Groups.begin()+i);
386 //if the user only entered invalid groups
387 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
388 numGroups = globaldata->gGroupmap->getNumGroups();
389 for (int i=0; i < numGroups; i++) {
390 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
392 m->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."); m->mothurOutEndLine();
393 } else { numGroups = globaldata->Groups.size(); }
394 } else { //users wants all groups
395 numGroups = globaldata->gGroupmap->getNumGroups();
396 globaldata->Groups.clear();
397 for (int i=0; i < numGroups; i++) {
398 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
403 //sort so labels match
404 sort(globaldata->Groups.begin(), globaldata->Groups.end());
407 sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
409 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) { globaldata->gGroupmap->groupIndex[globaldata->gGroupmap->namesOfGroups[i]] = i; }
411 groupNames = globaldata->Groups;
414 catch(exception& e) {
415 m->errorOut(e, "LibShuffCommand", "setGroups");
420 /***********************************************************/