2 * File: kruskalwalliscommand.cpp
5 * Created on June 26, 2012, 11:06 AM
8 #include "kruskalwalliscommand.h"
10 //**********************************************************************************************************************
11 vector<string> KruskalWallisCommand::setParameters(){
13 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
14 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
15 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false,true); parameters.push_back(pgroups);
16 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none","summary",false,true,true); parameters.push_back(pshared);
18 vector<string> myArray;
19 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
23 m->errorOut(e, "KruskalWallisCommand", "setParameters");
27 //**********************************************************************************************************************
28 string KruskalWallisCommand::getHelpString(){
30 string helpString = "";
31 helpString += "The kruskalwallis command parameter options are \n";
32 helpString += "Kruskal–Wallis one-way analysis of variance is a non-parametric method for testing whether samples originate from the same distribution.";
36 m->errorOut(e, "KruskalWallisCommand", "getHelpString");
40 //**********************************************************************************************************************
41 string KruskalWallisCommand::getOutputPattern(string type) {
45 if (type == "summary") { pattern = "[filename],cooccurence.summary"; }
46 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
51 m->errorOut(e, "KruskalWallisCommand", "getOutputPattern");
55 //**********************************************************************************************************************
56 KruskalWallisCommand::KruskalWallisCommand(){
58 abort = true; calledHelp = true;
60 vector<string> tempOutNames;
61 outputTypes["summary"] = tempOutNames;
65 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
69 //**********************************************************************************************************************
70 KruskalWallisCommand::KruskalWallisCommand(string option) {
72 abort = false; calledHelp = false;
74 //allow user to run help
75 if(option == "help") { help(); abort = true; calledHelp = true; }
76 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
79 vector<string> myArray = setParameters();
81 OptionParser parser(option);
82 map<string,string> parameters = parser.getParameters();
83 map<string,string>::iterator it;
85 ValidParameters validParameter;
87 //check to make sure all parameters are valid for command
88 for (it = parameters.begin(); it != parameters.end(); it++) {
89 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
93 sharedfile = validParameter.validFile(parameters, "shared", true);
94 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
95 else if (sharedfile == "not found") {
96 //if there is a current shared file, use it
97 sharedfile = m->getSharedFile();
98 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
99 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
100 }else { m->setSharedFile(sharedfile); }
102 //if the user changes the output directory command factory will send this info to us in the output parameter
103 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(sharedfile); }
105 groups = validParameter.validFile(parameters, "groups", false);
106 if (groups == "not found") { groups = ""; }
108 m->splitAtDash(groups, Groups);
110 m->setGroups(Groups);
112 //if the user changes the input directory command factory will send this info to us in the output parameter
113 string inputDir = validParameter.validFile(parameters, "inputdir", false);
114 if (inputDir == "not found"){ inputDir = ""; }
117 it = parameters.find("shared");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["shared"] = inputDir + it->second; }
126 vector<string> tempOutNames;
127 outputTypes["summary"] = tempOutNames;
133 catch(exception& e) {
134 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
138 //**********************************************************************************************************************
139 int KruskalWallisCommand::execute(){
141 if (abort == true) { if (calledHelp) { return 0; } return 2; }
143 InputData* input = new InputData(sharedfile, "sharedfile");
144 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
145 string lastLabel = lookup[0]->getLabel();
148 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
149 set<string> processedLabels;
150 set<string> userLabels = labels;
153 map<string,string> variables;
154 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
155 string outputFileName = getOutputFileName("summary",variables);
156 m->openOutputFile(outputFileName, out);
157 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
158 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
159 out << "H\tpvalue\n";
163 int N = m->getNumGroups();
166 vector<groupRank> vec;
167 vector<string> groups = m->getGroups();
172 //merge all groups into a vector
179 //populate counts and ranSums vectors
180 for (int i=0;i<N;i++) {
184 for(int j;j<vec.size();j++) {
185 if (vec[j].group == group) {
187 sum = sum + vec[j].rank;
195 for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
197 H = (12 / (N*(N+1))) * tmp - (3*(N+1));
199 //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
201 //H = ss / ( (N * (N + 1))/12 );
203 //correction for ties?
205 //p-value calculation
209 catch(exception& e) {
210 m->errorOut(e, "KruskalWallisCommand", "execute");
214 //**********************************************************************************************************************
215 void KruskalWallisCommand::assignRank(vector<groupRank> &vec) {
218 double numRanks, avgRank, j;
219 vector<groupRank>::iterator it, oldit;
221 sort (vec.begin(), vec.end(), comparevalue);
225 while ( it != vec.end() ) {
228 if (!equalvalue(*it, *(it+1))) {
233 while(equalrank(*it, *(it+1))) {
238 numRanks = double (distance(oldit, it));
239 avgRank = j / numRanks;
241 (*oldit).rank = avgRank;
250 catch(exception& e) {
251 m->errorOut(e, "KruskalWallisCommand", "getRank");
256 //**********************************************************************************************************************
257 void KruskalWallisCommand::assignValue(vector<groupRank> &vec) {
260 //**********************************************************************************************************************
261 //**********************************************************************************************************************
262 //**********************************************************************************************************************