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){
26 globaldata = GlobalData::getInstance();
31 //allow user to run help
32 if(option == "help") { help(); abort = true; }
35 //valid paramters for this command
36 string Array[] = {"iters","groups","step","form","cutoff"};
37 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
39 OptionParser parser(option);
40 map<string, string> parameters = parser.getParameters();
42 ValidParameters validParameter;
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; }
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;;
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; }
60 splitAtDash(groups, Groups);
61 globaldata->Groups = Groups;
65 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "10000"; }
68 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "1.0"; }
69 convert(temp, cutOff);
71 temp = validParameter.validFile(parameters, "step", false); if (temp == "not found") { temp = "0.01"; }
74 userform = validParameter.validFile(parameters, "form", false); if (userform == "not found") { userform = "integral"; }
78 matrix = globaldata->gMatrix; //get the distance matrix
79 setGroups(); //set the groups to be analyzed
81 if(userform == "discrete"){
82 form = new DLibshuff(matrix, iters, step, cutOff);
85 form = new SLibshuff(matrix, iters, cutOff);
93 errorOut(e, "LibShuffCommand", "LibShuffCommand");
97 //**********************************************************************************************************************
99 void LibShuffCommand::help(){
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");
113 catch(exception& e) {
114 errorOut(e, "LibShuffCommand", "help");
119 //**********************************************************************************************************************
121 int LibShuffCommand::execute(){
124 if (abort == true) { return 0; }
126 savedDXYValues = form->evaluateAll();
127 savedMinValues = form->getSavedMins();
129 pValueCounts.resize(numGroups);
130 for(int i=0;i<numGroups;i++){
131 pValueCounts[i].assign(numGroups, 0);
134 Progress* reading = new Progress();
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]++; }
156 //clear out users groups
157 globaldata->Groups.clear();
160 //delete globaldata's copy of the gmatrix to free up memory
161 delete globaldata->gMatrix; globaldata->gMatrix = NULL;
165 catch(exception& e) {
166 errorOut(e, "LibShuffCommand", "execute");
171 //**********************************************************************************************************************
173 void LibShuffCommand::printCoverageFile() {
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);
182 map<double,vector<int> > allDistances;
183 map<double,vector<int> >::iterator it;
185 vector<vector<int> > indices(numGroups);
186 int numIndices = numGroups * numGroups;
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]]++;
198 allDistances[savedMinValues[i][j][k]].assign(numIndices, 0);
199 allDistances[savedMinValues[i][j][k]][indices[i][j]] = 1;
204 it=allDistances.begin();
206 //cout << setprecision(8);
208 vector<int> prevRow = it->second;
211 for(it;it!=allDistances.end();it++){
212 for(int i=0;i<it->second.size();i++){
213 it->second[i] += prevRow[i];
215 prevRow = it->second;
218 vector<int> lastRow = allDistances.rbegin()->second;
219 outCov << setprecision(8);
222 for (int i = 0; i < numGroups; i++){
223 outCov << '\t' << groupNames[i];
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];
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';
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';
248 catch(exception& e) {
249 errorOut(e, "LibShuffCommand", "printCoverageFile");
254 //**********************************************************************************************************************
256 void LibShuffCommand::printSummaryFile() {
260 summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary";
261 openOutputFile(summaryFile, outSum);
263 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
264 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
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;
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;
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;
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;
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;
298 catch(exception& e) {
299 errorOut(e, "LibShuffCommand", "printSummaryFile");
304 //**********************************************************************************************************************
306 void LibShuffCommand::setGroups() {
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]);
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);
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]);
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]);
342 //sort so labels match
343 sort(globaldata->Groups.begin(), globaldata->Groups.end());
346 sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
348 groupNames = globaldata->Groups;
351 catch(exception& e) {
352 errorOut(e, "LibShuffCommand", "setGroups");
357 /***********************************************************/