2 * File: kruskalwalliscommand.cpp
5 * Created on June 26, 2012, 11:06 AM
7 #include "kruskalwalliscommand.h"
9 //**********************************************************************************************************************
10 vector<string> KruskalWallisCommand::setParameters(){
12 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
13 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
15 vector<string> myArray;
16 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
20 m->errorOut(e, "KruskalWallisCommand", "setParameters");
24 //**********************************************************************************************************************
25 string KruskalWallisCommand::getHelpString(){
27 string helpString = "";
28 helpString += "The kruskalwallis command parameter options are \n";
29 helpString += "Kruskal–Wallis one-way analysis of variance is a non-parametric method for testing whether samples originate from the same distribution.";
33 m->errorOut(e, "KruskalWallisCommand", "getHelpString");
37 //**********************************************************************************************************************
38 string KruskalWallisCommand::getOutputFileNameTag(string type, string inputName=""){
40 string outputFileName = "";
41 map<string, vector<string> >::iterator it;
43 //is this a type this command creates
44 it = outputTypes.find(type);
45 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
47 if (type == "summary") { outputFileName = "cooccurence.summary"; }
48 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
50 return outputFileName;
53 m->errorOut(e, "KruskalWallisCommand", "getOutputFileNameTag");
57 //**********************************************************************************************************************
58 KruskalWallisCommand::KruskalWallisCommand(){
60 abort = true; calledHelp = true;
62 vector<string> tempOutNames;
63 outputTypes["summary"] = tempOutNames;
67 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
71 //**********************************************************************************************************************
72 KruskalWallisCommand::KruskalWallisCommand(string option) {
74 abort = false; calledHelp = false;
76 //allow user to run help
77 if(option == "help") { help(); abort = true; calledHelp = true; }
78 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
81 vector<string> myArray = setParameters();
83 OptionParser parser(option);
84 map<string,string> parameters = parser.getParameters();
85 map<string,string>::iterator it;
87 ValidParameters validParameter;
89 //check to make sure all parameters are valid for command
90 for (it = parameters.begin(); it != parameters.end(); it++) {
91 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
95 //if the user changes the input directory command factory will send this info to us in the output parameter
96 string inputDir = validParameter.validFile(parameters, "inputdir", false);
97 if (inputDir == "not found"){ inputDir = ""; }
100 it = parameters.find("shared");
101 //user has given a template file
102 if(it != parameters.end()){
103 path = m->hasPath(it->second);
104 //if the user has not given a path then, add inputdir. else leave path alone.
105 if (path == "") { parameters["shared"] = inputDir + it->second; }
109 vector<string> tempOutNames;
110 outputTypes["summary"] = tempOutNames;
116 catch(exception& e) {
117 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
121 //**********************************************************************************************************************
122 int KruskalWallisCommand::execute(){
124 if (abort == true) { if (calledHelp) { return 0; } return 2; }
128 int N; //= thisLookUp.size();
131 vector<groupRank> vec;
136 //merge all groups into a vector
140 //populate counts and ranSums vectors
141 for (int i=0;i<N;i++) {
145 for(int j;j<vec.size();j++) {
146 if (vec[j].group == group) {
148 sum = sum + vec[j].rank;
156 for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
158 H = (12 / (N*(N+1))) * tmp - (3*(N+1));
160 //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
162 //H = ss / ( (N * (N + 1))/12 );
164 //correction for ties?
166 //p-value calculation
170 catch(exception& e) {
171 m->errorOut(e, "KruskalWallisCommand", "execute");
175 //**********************************************************************************************************************
176 void KruskalWallisCommand::assignRank(vector<groupRank> &vec) {
179 double numRanks, avgRank;
180 vector<groupRank>::iterator it, oldit;
182 sort (vec.begin(), vec.end(), comparevalue);
186 while ( it != vec.end() ) {
189 if (!equalvalue(*it, *it+1)) { *it->rank = rank; rank++; it++; }
191 while(equalrank(*it, *it+1)) {
196 numRanks = double (distance(oldit, it));
197 avgRank = j / numRanks;
199 *oldit->rank = avgRank;
207 catch(exception& e) {
208 m->errorOut(e, "KruskalWallisCommand", "getRank");
213 //**********************************************************************************************************************
215 //**********************************************************************************************************************
216 //**********************************************************************************************************************
217 //**********************************************************************************************************************