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);
16 vector<string> myArray;
17 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
21 m->errorOut(e, "KruskalWallisCommand", "setParameters");
25 //**********************************************************************************************************************
26 string KruskalWallisCommand::getHelpString(){
28 string helpString = "";
29 helpString += "The kruskalwallis command parameter options are \n";
30 helpString += "Kruskal–Wallis one-way analysis of variance is a non-parametric method for testing whether samples originate from the same distribution.";
34 m->errorOut(e, "KruskalWallisCommand", "getHelpString");
38 //**********************************************************************************************************************
39 string KruskalWallisCommand::getOutputFileNameTag(string type, string inputName=""){
41 string outputFileName = "";
42 map<string, vector<string> >::iterator it;
44 //is this a type this command creates
45 it = outputTypes.find(type);
46 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
48 if (type == "summary") { outputFileName = "cooccurence.summary"; }
49 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
51 return outputFileName;
54 m->errorOut(e, "KruskalWallisCommand", "getOutputFileNameTag");
58 //**********************************************************************************************************************
59 KruskalWallisCommand::KruskalWallisCommand(){
61 abort = true; calledHelp = true;
63 vector<string> tempOutNames;
64 outputTypes["summary"] = tempOutNames;
68 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
72 //**********************************************************************************************************************
73 KruskalWallisCommand::KruskalWallisCommand(string option) {
75 abort = false; calledHelp = false;
77 //allow user to run help
78 if(option == "help") { help(); abort = true; calledHelp = true; }
79 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
82 vector<string> myArray = setParameters();
84 OptionParser parser(option);
85 map<string,string> parameters = parser.getParameters();
86 map<string,string>::iterator it;
88 ValidParameters validParameter;
90 //check to make sure all parameters are valid for command
91 for (it = parameters.begin(); it != parameters.end(); it++) {
92 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
96 //if the user changes the input directory command factory will send this info to us in the output parameter
97 string inputDir = validParameter.validFile(parameters, "inputdir", false);
98 if (inputDir == "not found"){ inputDir = ""; }
101 it = parameters.find("shared");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["shared"] = inputDir + it->second; }
110 vector<string> tempOutNames;
111 outputTypes["summary"] = tempOutNames;
117 catch(exception& e) {
118 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
122 //**********************************************************************************************************************
123 int KruskalWallisCommand::execute(){
125 if (abort == true) { if (calledHelp) { return 0; } return 2; }
129 int N; //= thisLookUp.size();
132 vector<groupRank> vec;
137 //merge all groups into a vector
141 //populate counts and ranSums vectors
142 for (int i=0;i<N;i++) {
146 for(int j;j<vec.size();j++) {
147 if (vec[j].group == group) {
149 sum = sum + vec[j].rank;
157 for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
159 H = (12 / (N*(N+1))) * tmp - (3*(N+1));
161 //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
163 //H = ss / ( (N * (N + 1))/12 );
165 //correction for ties?
167 //p-value calculation
171 catch(exception& e) {
172 m->errorOut(e, "KruskalWallisCommand", "execute");
176 //**********************************************************************************************************************
177 void KruskalWallisCommand::assignRank(vector<groupRank> &vec) {
180 double numRanks, avgRank, j;
181 vector<groupRank>::iterator it, oldit;
183 sort (vec.begin(), vec.end(), comparevalue);
187 while ( it != vec.end() ) {
190 if (!equalvalue(*it, *(it+1))) {
195 while(equalrank(*it, *(it+1))) {
200 numRanks = double (distance(oldit, it));
201 avgRank = j / numRanks;
203 (*oldit).rank = avgRank;
212 catch(exception& e) {
213 m->errorOut(e, "KruskalWallisCommand", "getRank");
218 //**********************************************************************************************************************
220 //**********************************************************************************************************************
221 //**********************************************************************************************************************
222 //**********************************************************************************************************************