X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=libshuffcommand.cpp;h=e7e1aa77a49005f553c2657271a7d1ecd6dbac12;hb=0470f6d037aacb3563c3f7010708120a4a67d4e6;hp=bd0166213bcf26dde642856b4004031d3480110a;hpb=2d2fbc80f9359b19873ba3e63970b58f4f8f49f3;p=mothur.git diff --git a/libshuffcommand.cpp b/libshuffcommand.cpp index bd01662..e7e1aa7 100644 --- a/libshuffcommand.cpp +++ b/libshuffcommand.cpp @@ -7,46 +7,89 @@ * */ +/* This class is designed to implement an integral form of the Cramer-von Mises statistic. + you may refer to the "Integration of Microbial Ecology and Statistics: A Test To Compare Gene Libraries" + paper in Applied and Environmental Microbiology, Sept. 2004, p. 5485-5492 0099-2240/04/$8.00+0 + DOI: 10.1128/AEM.70.9.5485-5492.2004 Copyright 2004 American Society for Microbiology for more information. */ + + #include "libshuffcommand.h" +#include "libshuff.h" +#include "slibshuff.h" +#include "dlibshuff.h" //********************************************************************************************************************** - -LibShuffCommand::LibShuffCommand(){ +LibShuffCommand::LibShuffCommand(string option){ try { + srand( (unsigned)time( NULL ) ); + globaldata = GlobalData::getInstance(); - convert(globaldata->getCutOff(), cutOff); - convert(globaldata->getIters(), iters); - convert(globaldata->getStep(), step); - form = globaldata->getForm(); - matrix = globaldata->gMatrix; - coverageFile = getRootName(globaldata->getPhylipFile()) + "coverage"; - summaryFile = getRootName(globaldata->getPhylipFile()) + "slsummary"; - openOutputFile(coverageFile, out); - openOutputFile(summaryFile, outSum); + abort = false; + Groups.clear(); - //set the groups to be analyzed - setGroups(); - - //file headers for coverage file - out << "D" << '\t'; - for (int i = 0; i < groupComb.size(); i++) { - out << "C" + groupComb[i] << '\t'; - } - for (int i = 0; i < numGroups; i++) { - for (int j = 0; j < numGroups; j++) { - //don't output AA to AA - if (i != j) { - out << "Delta" + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'; + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"iters","groups","step","form","cutoff"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + parser = new OptionParser(); + parser->parse(option, parameters); delete parser; + + ValidParameters* validParameter = new ValidParameters(); + + //check to make sure all parameters are valid for command + for (it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter->isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + //make sure the user has already run the read.dist command + if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) { + cout << "You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. " << endl; abort = true;; + } + + //check for optional parameter and set defaults + // ...at some point should added some additional type checking... + groups = validParameter->validFile(parameters, "groups", false); + if (groups == "not found") { groups = ""; savegroups = groups; } + else { + savegroups = groups; + splitAtDash(groups, Groups); + globaldata->Groups = Groups; + } + + string temp; + temp = validParameter->validFile(parameters, "iters", false); if (temp == "not found") { temp = "10000"; } + convert(temp, iters); + + temp = validParameter->validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "1.0"; } + convert(temp, cutOff); + + temp = validParameter->validFile(parameters, "step", false); if (temp == "not found") { temp = "0.01"; } + convert(temp, step); + + userform = validParameter->validFile(parameters, "form", false); if (userform == "not found") { userform = "integral"; } + + delete validParameter; + + if (abort == false) { + + matrix = globaldata->gMatrix; //get the distance matrix + setGroups(); //set the groups to be analyzed + + if(userform == "discrete"){ + form = new DLibshuff(matrix, iters, step, cutOff); + } + else{ + form = new SLibshuff(matrix, iters, cutOff); } } + } - out << endl; - - numComp = numGroups*numGroups; - - coverage = new Coverage(); } catch(exception& e) { @@ -59,161 +102,75 @@ LibShuffCommand::LibShuffCommand(){ } } - //********************************************************************************************************************** -LibShuffCommand::~LibShuffCommand(){ - delete coverage; +void LibShuffCommand::help(){ + try { + cout << "The libshuff command can only be executed after a successful read.dist command including a groupfile." << "\n"; + cout << "The libshuff command parameters are groups, iters, step, form and cutoff. No parameters are required." << "\n"; + 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"; + 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"; + 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"; + 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"; + cout << "The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep)." << "\n"; + cout << "Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0)." << "\n"; + 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"; + cout << "The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual." << "\n"; + cout << "Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters)." << "\n" << "\n"; + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the LibShuffCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } } //********************************************************************************************************************** int LibShuffCommand::execute(){ try { - //deltaValues[0] = scores for the difference between AA and AB. - //cValues[0][0] = AA, cValues[0][1] = AB, cValues[0][2] = AC, cValues[1][0] = BA, cValues[1][1] = BB... - vector dist; - int next; - sumDelta.resize(numComp-numGroups, 0.0); + if (abort == true) { return 0; } - float D = 0.0; - - /*****************************/ - //get values for users matrix - /*****************************/ - matrix->setBounds(); + savedDXYValues = form->evaluateAll(); + savedMinValues = form->getSavedMins(); - if (form != "discrete") { matrix->getDist(dist); next = 1; } -//cout << "Distances" << endl; -//for (int i = 0; i < dist.size(); i++) { cout << dist[i] << " "; } -//cout << endl; - - //get values for users matrix - while (D <= cutOff) { - //clear out old Values - deltaValues.clear(); - coverage->getValues(matrix, D, cValues); - - //find delta values - int count = 0; - for (int i = 0; i < numGroups; i++) { - for (int j = 0; j < numGroups; j++) { - //don't save AA to AA - if (i != j) { - //(Caa - Cab)^2 - deltaValues.push_back( (abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j])) ); - sumDelta[count] += deltaValues[count]; - count++; - } - } - } - - printCoverageFile(D); - - //check form - if (form != "discrete") { - if (next == dist.size()) { break; } - else { D = dist[next]; next++; } - }else { D += step; } - - + pValueCounts.resize(numGroups); + for(int i=0;iGroups[i] + "-" + globaldata->Groups[j] << '\t'; - } - } - } - cout << endl; + Progress* reading = new Progress(); - for (int i = 0; i < sumDelta.size(); i++) { - cout << setprecision(6) << sumDelta[i] << '\t'; - } - cout << endl; - - /*******************************************************************************/ - //create and score random matrixes finding the sumDelta values for summary file - /******************************************************************************/ - - //initialize rsumDelta - rsumDelta.resize(numComp-numGroups); - for (int l = 0; l < rsumDelta.size(); l++) { - for (int w = 0; w < iters; w++) { - rsumDelta[l].push_back(0.0); - } - } - - - for (int m = 0; m < iters; m++) { - //generate random matrix in getValues - //values for random matrix - cout << "Iteration " << m+1 << endl; - D = 0.0; - next = 1; - - while (D <= cutOff) { - coverage->getValues(matrix, D, cValues, "random"); - - //find delta values - int count = 0; - for (int i = 0; i < numGroups; i++) { - for (int j = 0; j < numGroups; j++) { - //don't save AA to AA - if (i != j) { - //(Caa - Cab)^2 - rsumDelta[count][m] += ((abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j]))); -//cout << "iter " << m << " box " << i << j << " delta = " << ((abs(cValues[i][i]-cValues[i][j]) * abs(cValues[i][i]-cValues[i][j]))) << endl; - count++; - } - } + for(int i=0;inewLine(groupNames[i]+'-'+groupNames[j], iters); + for(int p=0;prandomizeGroups(i,j); + if(form->evaluatePair(i,j) >= savedDXYValues[i][j]) { pValueCounts[i][j]++; } + if(form->evaluatePair(j,i) >= savedDXYValues[j][i]) { pValueCounts[j][i]++; } + reading->update(p); } - - //check form - if (form != "discrete") { - if (next == dist.size()) { break; } - else { D = dist[next]; next++; } - }else { D += step; } - - - //clear out old Values - cValues.clear(); + form->resetGroup(i); + form->resetGroup(j); } -cout << "random sum delta for iter " << m << endl; -for (int i = 0; i < rsumDelta.size(); i++) { - cout << setprecision(6) << rsumDelta[i][m] << '\t'; -} -cout << endl; - } - - /**********************************************************/ - //find the signifigance of the user matrix' sumdelta values - /**********************************************************/ - - for (int t = 0; t < rsumDelta.size(); t++) { - //sort rsumDelta t - sort(rsumDelta[t].begin(), rsumDelta[t].end()); - - //the index of the score higher than yours is returned - //so if you have 1000 random matrices the index returned is 100 - //then there are 900 matrices with a score greater then you. - //giving you a signifigance of 0.900 - int index = findIndex(sumDelta[t], t); - - //the signifigance is the number of trees with the users score or higher - sumDeltaSig.push_back((iters-index)/(float)iters); + reading->finish(); + delete reading; - } - + cout << endl; printSummaryFile(); + printCoverageFile(); //clear out users groups globaldata->Groups.clear(); + delete form; + + //delete globaldata's copy of the gmatrix to free up memory + delete globaldata->gMatrix; globaldata->gMatrix = NULL; return 0; } @@ -226,28 +183,83 @@ cout << endl; exit(1); } } + //********************************************************************************************************************** -void LibShuffCommand::printCoverageFile(float d) { + +void LibShuffCommand::printCoverageFile() { try { - //format output - out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint); + + ofstream outCov; + summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage"; + openOutputFile(summaryFile, outCov); + outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint); + cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); + + map > allDistances; + map >::iterator it; + + vector > indices(numGroups); + int numIndices = numGroups * numGroups; - out << setprecision(6) << d << '\t'; + int index = 0; + for(int i=0;i prevRow = it->second; + it++; + + for(it;it!=allDistances.end();it++){ + for(int i=0;isecond.size();i++){ + it->second[i] += prevRow[i]; + } + prevRow = it->second; + } - //print out coverage values - for (int i = 0; i < numGroups; i++) { - for (int j = 0; j < numGroups; j++) { - out << cValues[i][j] << '\t'; + vector lastRow = allDistances.rbegin()->second; + outCov << setprecision(8); + + outCov << "dist"; + for (int i = 0; i < numGroups; i++){ + outCov << '\t' << groupNames[i]; + } + for (int i=0;ifirst << '\t'; + for(int i=0;isecond[indices[i][i]]/(float)lastRow[indices[i][i]] << '\t'; + } + for(int i=0;isecond[indices[i][j]]/(float)lastRow[indices[i][j]] << '\t'; + outCov << it->second[indices[j][i]]/(float)lastRow[indices[j][i]] << '\t'; + } + } + outCov << endl; } - out << endl; - } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printCoverageFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; @@ -258,33 +270,45 @@ void LibShuffCommand::printCoverageFile(float d) { exit(1); } } + //********************************************************************************************************************** + void LibShuffCommand::printSummaryFile() { try { - //format output + + ofstream outSum; + summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary"; + openOutputFile(summaryFile, outSum); + outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint); + cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); - for (int i = 0; i < numGroups; i++) { - for (int j = 0; j < numGroups; j++) { - //don't output AA to AA - if (i != j) { - outSum << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'; - cout << "Delta " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'<< "DeltaSig " + globaldata->Groups[i] + "-" + globaldata->Groups[j] << '\t'; + cout << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl; + outSum << setw(20) << left << "Comparison" << '\t' << setprecision(8) << "dCXYScore" << '\t' << "Significance" << endl; + + int precision = (int)log10(iters); + for(int i=0;igetIters().length()) << sumDeltaSig[i] << '\t'; - cout << setprecision(6) << sumDelta[i] << '\t' << setprecision(globaldata->getIters().length()) << sumDeltaSig[i] << '\t'; - } - outSum << endl; - cout << endl; - } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function printSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; @@ -297,6 +321,7 @@ void LibShuffCommand::printSummaryFile() { } //********************************************************************************************************************** + void LibShuffCommand::setGroups() { try { //if the user has not entered specific groups to analyze then do them all @@ -305,14 +330,14 @@ void LibShuffCommand::setGroups() { for (int i=0; i < numGroups; i++) { globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]); } - }else { - if (globaldata->getGroups() != "all") { + } else { + if (savegroups != "all") { //check that groups are valid for (int i = 0; i < globaldata->Groups.size(); i++) { if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) { cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl; // erase the invalid group from globaldata->Groups - globaldata->Groups.erase (globaldata->Groups.begin()+i); + globaldata->Groups.erase(globaldata->Groups.begin()+i); } } @@ -323,8 +348,8 @@ void LibShuffCommand::setGroups() { globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]); } 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; - }else { numGroups = globaldata->Groups.size(); } - }else { //users wants all groups + } else { numGroups = globaldata->Groups.size(); } + } else { //users wants all groups numGroups = globaldata->gGroupmap->getNumGroups(); globaldata->Groups.clear(); for (int i=0; i < numGroups; i++) { @@ -332,17 +357,22 @@ void LibShuffCommand::setGroups() { } } } - + //sort so labels match sort(globaldata->Groups.begin(), globaldata->Groups.end()); + //sort + sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end()); + + groupNames = globaldata->Groups; + // number of comparisons i.e. with groups A,B,C = AA, AB, AC, BA, BB, BC...; - for (int i=0; iGroups[i] + "-" + globaldata->Groups[l]); - } - } +// for (int i=0; iGroups[i] + "-" + globaldata->Groups[l]); +// } +// } } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; @@ -353,23 +383,5 @@ void LibShuffCommand::setGroups() { exit(1); } } -/***********************************************************/ -int LibShuffCommand::findIndex(float score, int index) { - try{ - for (int i = 0; i < rsumDelta[index].size(); i++) { - if (rsumDelta[index][i] >= score) { return i; } - } - return rsumDelta[index].size(); - } - catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the LibShuffCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the LibShuffCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } -} /***********************************************************/ -