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"
23 //**********************************************************************************************************************
24 vector<string> LibShuffCommand::setParameters(){
26 CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pphylip);
27 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
28 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
29 CommandParameter piters("iters", "Number", "", "10000", "", "", "",false,false); parameters.push_back(piters);
30 CommandParameter pstep("step", "Number", "", "0.01", "", "", "",false,false); parameters.push_back(pstep);
31 CommandParameter pcutoff("cutoff", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pcutoff);
32 CommandParameter pform("form", "Multiple", "discrete-integral", "integral", "", "", "",false,false); parameters.push_back(pform);
33 CommandParameter psim("sim", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psim);
34 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
35 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
37 vector<string> myArray;
38 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
42 m->errorOut(e, "LibShuffCommand", "setParameters");
46 //**********************************************************************************************************************
47 string LibShuffCommand::getHelpString(){
49 string helpString = "";
50 helpString += "The libshuff command parameters are phylip, group, sim, groups, iters, step, form and cutoff. phylip and group parameters are required, unless you have valid current files.\n";
51 helpString += "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";
52 helpString += "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";
53 helpString += "The step parameter allows you to specify change in distance you would like between each output if you are using the discrete form.\n";
54 helpString += "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";
55 helpString += "The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n";
56 helpString += "Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n";
57 helpString += "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";
58 helpString += "The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n";
59 helpString += "Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n";
63 m->errorOut(e, "LibShuffCommand", "getHelpString");
67 //**********************************************************************************************************************
68 LibShuffCommand::LibShuffCommand(){
70 abort = true; calledHelp = true;
72 vector<string> tempOutNames;
73 outputTypes["coverage"] = tempOutNames;
74 outputTypes["libshuffsummary"] = tempOutNames;
77 m->errorOut(e, "LibShuffCommand", "LibShuffCommand");
82 //**********************************************************************************************************************
83 LibShuffCommand::LibShuffCommand(string option) {
85 abort = false; calledHelp = false;
87 //allow user to run help
88 if(option == "help") { help(); abort = true; calledHelp = true; }
89 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
92 vector<string> myArray = setParameters();
94 OptionParser parser(option);
95 map<string, string> parameters = parser.getParameters();
96 map<string,string>::iterator it;
98 ValidParameters validParameter;
100 //check to make sure all parameters are valid for command
101 for (it = parameters.begin(); it != parameters.end(); it++) {
102 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
105 //initialize outputTypes
106 vector<string> tempOutNames;
107 outputTypes["coverage"] = tempOutNames;
108 outputTypes["libshuffsummary"] = tempOutNames;
110 string inputDir = validParameter.validFile(parameters, "inputdir", false);
111 if (inputDir == "not found"){ inputDir = ""; }
114 it = parameters.find("phylip");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["phylip"] = inputDir + it->second; }
122 it = parameters.find("group");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["group"] = inputDir + it->second; }
131 //check for required parameters
132 phylipfile = validParameter.validFile(parameters, "phylip", true);
133 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
134 else if (phylipfile == "not found") {
135 phylipfile = m->getPhylipFile();
136 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
138 m->mothurOut("You must provide a phylip file."); m->mothurOutEndLine();
143 //check for required parameters
144 groupfile = validParameter.validFile(parameters, "group", true);
145 if (groupfile == "not open") { groupfile = ""; abort = true; }
146 else if (groupfile == "not found") {
147 groupfile = m->getGroupFile();
148 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
150 m->mothurOut("You must provide a group file."); m->mothurOutEndLine();
155 //if the user changes the output directory command factory will send this info to us in the output parameter
156 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
158 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
161 //check for optional parameter and set defaults
162 // ...at some point should added some additional type checking...
163 groups = validParameter.validFile(parameters, "groups", false);
164 if (groups == "not found") { groups = ""; savegroups = groups; }
167 m->splitAtDash(groups, Groups);
172 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "10000"; }
173 convert(temp, iters);
175 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "1.0"; }
176 convert(temp, cutOff);
178 temp = validParameter.validFile(parameters, "step", false); if (temp == "not found") { temp = "0.01"; }
181 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
182 sim = m->isTrue(temp);
184 userform = validParameter.validFile(parameters, "form", false); if (userform == "not found") { userform = "integral"; }
189 catch(exception& e) {
190 m->errorOut(e, "LibShuffCommand", "LibShuffCommand");
194 //**********************************************************************************************************************
196 int LibShuffCommand::execute(){
199 if (abort == true) { if (calledHelp) { return 0; } return 2; }
202 groupMap = new GroupMap(groupfile);
203 int error = groupMap->readMap();
204 if (error == 1) { delete groupMap; return 0; }
207 m->openInputFile(phylipfile, in);
208 matrix = new FullMatrix(in, groupMap, sim); //reads the matrix file
211 if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
213 //if files don't match...
214 if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {
215 m->mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences."); m->mothurOutEndLine();
216 //create new group file
217 if(outputDir == "") { outputDir += m->hasPath(groupfile); }
219 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "editted.groups";
220 outputNames.push_back(newGroupFile);
222 m->openOutputFile(newGroupFile, outGroups);
224 for (int i = 0; i < matrix->getNumSeqs(); i++) {
225 if (m->control_pressed) { delete groupMap; delete matrix; outGroups.close(); remove(newGroupFile.c_str()); return 0; }
227 Names temp = matrix->getRowInfo(i);
228 outGroups << temp.seqName << '\t' << temp.groupName << endl;
232 m->mothurOut(newGroupFile + " is a new group file containing only the sequence that are in your distance file. I will read this file instead."); m->mothurOutEndLine();
236 groupfile = newGroupFile;
238 groupMap = new GroupMap(groupfile);
241 if (m->control_pressed) { delete groupMap; delete matrix; remove(newGroupFile.c_str()); return 0; }
245 setGroups(); //set the groups to be analyzed and sorts them
247 /********************************************************************************************/
248 //this is needed because when we read the matrix we sort it into groups in alphabetical order
249 //the rest of the command and the classes used in this command assume specific order
250 /********************************************************************************************/
251 matrix->setGroups(groupMap->namesOfGroups);
253 for (int i = 0; i < groupMap->namesOfGroups.size(); i++) { sizes.push_back(groupMap->getNumSeqs(groupMap->namesOfGroups[i])); }
254 matrix->setSizes(sizes);
257 if(userform == "discrete"){
258 form = new DLibshuff(matrix, iters, step, cutOff);
261 form = new SLibshuff(matrix, iters, cutOff);
264 savedDXYValues = form->evaluateAll();
265 savedMinValues = form->getSavedMins();
267 if (m->control_pressed) { delete form; m->Groups.clear(); delete matrix; delete groupMap; return 0; }
269 pValueCounts.resize(numGroups);
270 for(int i=0;i<numGroups;i++){
271 pValueCounts[i].assign(numGroups, 0);
274 if (m->control_pressed) { outputTypes.clear(); delete form; m->Groups.clear(); delete matrix; delete groupMap; return 0; }
276 Progress* reading = new Progress();
278 for(int i=0;i<numGroups-1;i++) {
279 for(int j=i+1;j<numGroups;j++) {
281 if (m->control_pressed) { outputTypes.clear(); delete form; m->Groups.clear(); delete matrix; delete groupMap; delete reading; return 0; }
283 reading->newLine(groupNames[i]+'-'+groupNames[j], iters);
284 int spoti = groupMap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
285 int spotj = groupMap->groupIndex[groupNames[j]];
287 for(int p=0;p<iters;p++) {
289 if (m->control_pressed) { outputTypes.clear(); delete form; m->Groups.clear(); delete matrix; delete groupMap; delete reading; return 0; }
291 form->randomizeGroups(spoti,spotj);
292 if(form->evaluatePair(spoti,spotj) >= savedDXYValues[spoti][spotj]) { pValueCounts[i][j]++; }
293 if(form->evaluatePair(spotj,spoti) >= savedDXYValues[spotj][spoti]) { pValueCounts[j][i]++; }
295 if (m->control_pressed) { outputTypes.clear(); delete form; m->Groups.clear(); delete matrix; delete groupMap; delete reading; return 0; }
299 form->resetGroup(spoti);
300 form->resetGroup(spotj);
304 if (m->control_pressed) { outputTypes.clear(); delete form; m->Groups.clear(); delete matrix; delete groupMap; delete reading; return 0; }
309 m->mothurOutEndLine();
313 //clear out users groups
317 delete matrix; delete groupMap;
319 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
321 m->mothurOutEndLine();
322 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
323 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
324 m->mothurOutEndLine();
328 catch(exception& e) {
329 m->errorOut(e, "LibShuffCommand", "execute");
334 //**********************************************************************************************************************
336 int LibShuffCommand::printCoverageFile() {
340 summaryFile = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "libshuff.coverage";
341 m->openOutputFile(summaryFile, outCov);
342 outputNames.push_back(summaryFile); outputTypes["coverage"].push_back(summaryFile);
343 outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
344 //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
346 map<double,vector<int> > allDistances;
347 map<double,vector<int> >::iterator it;
349 vector<vector<int> > indices(numGroups);
350 int numIndices = numGroups * numGroups;
353 for(int i=0;i<numGroups;i++){
354 indices[i].assign(numGroups,0);
355 for(int j=0;j<numGroups;j++){
356 indices[i][j] = index++;
358 int spoti = groupMap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
359 int spotj = groupMap->groupIndex[groupNames[j]];
361 for(int k=0;k<savedMinValues[spoti][spotj].size();k++){
363 if(m->control_pressed) { outCov.close(); return 0; }
365 if(allDistances[savedMinValues[spoti][spotj][k]].size() != 0){
366 allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]]++;
369 allDistances[savedMinValues[spoti][spotj][k]].assign(numIndices, 0);
370 allDistances[savedMinValues[spoti][spotj][k]][indices[i][j]] = 1;
375 it=allDistances.begin();
377 //cout << setprecision(8);
379 vector<int> prevRow = it->second;
382 for(;it!=allDistances.end();it++){
383 for(int i=0;i<it->second.size();i++){
384 it->second[i] += prevRow[i];
386 prevRow = it->second;
389 vector<int> lastRow = allDistances.rbegin()->second;
390 outCov << setprecision(8);
393 for (int i = 0; i < numGroups; i++){
394 outCov << '\t' << groupNames[i];
396 for (int i=0;i<numGroups;i++){
397 for(int j=i+1;j<numGroups;j++){
398 if(m->control_pressed) { outCov.close(); return 0; }
399 outCov << '\t' << groupNames[i] << '-' << groupNames[j] << '\t';
400 outCov << groupNames[j] << '-' << groupNames[i];
405 for(it=allDistances.begin();it!=allDistances.end();it++){
406 outCov << it->first << '\t';
407 for(int i=0;i<numGroups;i++){
408 outCov << it->second[indices[i][i]]/(float)lastRow[indices[i][i]] << '\t';
410 for(int i=0;i<numGroups;i++){
411 for(int j=i+1;j<numGroups;j++){
412 if(m->control_pressed) { outCov.close(); return 0; }
414 outCov << it->second[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t';
415 outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t';
424 catch(exception& e) {
425 m->errorOut(e, "LibShuffCommand", "printCoverageFile");
430 //**********************************************************************************************************************
432 int LibShuffCommand::printSummaryFile() {
436 summaryFile = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "libshuff.summary";
437 m->openOutputFile(summaryFile, outSum);
438 outputNames.push_back(summaryFile); outputTypes["libshuffsummary"].push_back(summaryFile);
440 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
441 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
443 cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
444 m->mothurOutJustToLog("Comparison\tdCXYScore\tSignificance"); m->mothurOutEndLine();
445 outSum << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl;
447 int precision = (int)log10(iters);
448 for(int i=0;i<numGroups;i++){
449 for(int j=i+1;j<numGroups;j++){
450 if(m->control_pressed) { outSum.close(); return 0; }
452 int spoti = groupMap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
453 int spotj = groupMap->groupIndex[groupNames[j]];
455 if(pValueCounts[i][j]){
456 cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
457 m->mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((pValueCounts[i][j]/(float)iters))); m->mothurOutEndLine();
458 outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
461 cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
462 m->mothurOutJustToLog(groupNames[i]+"-"+groupNames[j] + "\t" + toString(savedDXYValues[spoti][spotj]) + "\t" + toString((1/(float)iters))); m->mothurOutEndLine();
463 outSum << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << '<' <<setprecision(precision) << 1/(float)iters << endl;
465 if(pValueCounts[j][i]){
466 cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
467 m->mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((pValueCounts[j][i]/(float)iters))); m->mothurOutEndLine();
468 outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << setprecision (precision) << pValueCounts[j][i]/(float)iters << endl;
471 cout << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
472 m->mothurOutJustToLog(groupNames[j]+"-"+groupNames[i] + "\t" + toString(savedDXYValues[spotj][spoti]) + "\t" + toString((1/(float)iters))); m->mothurOutEndLine();
473 outSum << setw(20) << left << groupNames[j]+'-'+groupNames[i] << '\t' << setprecision(8) << savedDXYValues[spotj][spoti] << '\t' << '<' <<setprecision (precision) << 1/(float)iters << endl;
481 catch(exception& e) {
482 m->errorOut(e, "LibShuffCommand", "printSummaryFile");
487 //**********************************************************************************************************************
489 void LibShuffCommand::setGroups() {
491 //if the user has not entered specific groups to analyze then do them all
492 if (m->Groups.size() == 0) {
493 numGroups = groupMap->getNumGroups();
494 for (int i=0; i < numGroups; i++) {
495 m->Groups.push_back(groupMap->namesOfGroups[i]);
498 if (savegroups != "all") {
499 //check that groups are valid
500 for (int i = 0; i < m->Groups.size(); i++) {
501 if (groupMap->isValidGroup(m->Groups[i]) != true) {
502 m->mothurOut(m->Groups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
503 // erase the invalid group from globaldata->Groups
504 m->Groups.erase(m->Groups.begin()+i);
508 //if the user only entered invalid groups
509 if ((m->Groups.size() == 0) || (m->Groups.size() == 1)) {
510 numGroups = groupMap->getNumGroups();
511 for (int i=0; i < numGroups; i++) {
512 m->Groups.push_back(groupMap->namesOfGroups[i]);
514 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();
515 } else { numGroups = m->Groups.size(); }
516 } else { //users wants all groups
517 numGroups = groupMap->getNumGroups();
519 for (int i=0; i < numGroups; i++) {
520 m->Groups.push_back(groupMap->namesOfGroups[i]);
525 //sort so labels match
526 sort(m->Groups.begin(), m->Groups.end());
529 sort(groupMap->namesOfGroups.begin(), groupMap->namesOfGroups.end());
531 for (int i = 0; i < groupMap->namesOfGroups.size(); i++) { groupMap->groupIndex[groupMap->namesOfGroups[i]] = i; }
533 groupNames = m->Groups;
536 catch(exception& e) {
537 m->errorOut(e, "LibShuffCommand", "setGroups");
542 /***********************************************************/