2 * File: kruskalwalliscommand.cpp
5 * Created on June 26, 2012, 11:06 AM
7 #include "kruskalwalliscommand.h"
9 //**********************************************************************************************************************
16 //**********************************************************************************************************************
17 vector<string> KruskalWallisCommand::setParameters(){
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "KruskalWallisCommand", "setParameters");
31 //**********************************************************************************************************************
32 string KruskalWallisCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The kruskalwallis command parameter options are \n";
36 helpString += "Kruskal–Wallis one-way analysis of variance is a non-parametric method for testing whether samples originate from the same distribution.";
40 m->errorOut(e, "KruskalWallisCommand", "getHelpString");
44 //**********************************************************************************************************************
45 string KruskalWallisCommand::getOutputFileNameTag(string type, string inputName=""){
47 string outputFileName = "";
48 map<string, vector<string> >::iterator it;
50 //is this a type this command creates
51 it = outputTypes.find(type);
52 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
54 if (type == "summary") { outputFileName = "cooccurence.summary"; }
55 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
57 return outputFileName;
60 m->errorOut(e, "KruskalWallisCommand", "getOutputFileNameTag");
64 //**********************************************************************************************************************
65 KruskalWallisCommand::KruskalWallisCommand(){
67 abort = true; calledHelp = true;
69 vector<string> tempOutNames;
70 outputTypes["summary"] = tempOutNames;
74 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
78 //**********************************************************************************************************************
79 KruskalWallisCommand::KruskalWallisCommand(string option) {
81 abort = false; calledHelp = false;
83 //allow user to run help
84 if(option == "help") { help(); abort = true; calledHelp = true; }
85 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
88 vector<string> myArray = setParameters();
90 OptionParser parser(option);
91 map<string,string> parameters = parser.getParameters();
92 map<string,string>::iterator it;
94 ValidParameters validParameter;
96 //check to make sure all parameters are valid for command
97 for (it = parameters.begin(); it != parameters.end(); it++) {
98 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
102 //if the user changes the input directory command factory will send this info to us in the output parameter
103 string inputDir = validParameter.validFile(parameters, "inputdir", false);
104 if (inputDir == "not found"){ inputDir = ""; }
107 it = parameters.find("shared");
108 //user has given a template file
109 if(it != parameters.end()){
110 path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["shared"] = inputDir + it->second; }
116 vector<string> tempOutNames;
117 outputTypes["summary"] = tempOutNames;
123 catch(exception& e) {
124 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
128 //**********************************************************************************************************************
129 int KruskalWallisCommand::execute(){
131 if (abort == true) { if (calledHelp) { return 0; } return 2; }
139 //merge all groups into a vector
143 for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
145 H = (12 / (N*(N+1))) * tmp - (3*(N+1));
147 //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
149 //H = ss / ( (N * (N + 1))/12 );
151 //correction for ties?
153 //p-value calculation
159 catch(exception& e) {
160 m->errorOut(e, "KruskalWallisCommand", "execute");
164 //**********************************************************************************************************************
165 multimap<double,double> KruskalWallisCommand::getRank(vector<groupRank> vec) {
167 multimap<double,double> rankMap;
173 sort (vec.begin(), vec.end());
175 for (int i=0;i<vec.size();i++) {
176 if (vec[i] != previous) { rankMap[rank] = vec[i]; }
177 else {tie = tie + rank; tiecount++;}
182 catch(exception& e) {
183 m->errorOut(e, "KruskalWallisCommand", "getRank");
188 //**********************************************************************************************************************
190 //**********************************************************************************************************************
191 //**********************************************************************************************************************
192 //**********************************************************************************************************************