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; }
127 InputData* input = new InputData(sharedfile, "sharedfile");
128 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
129 string lastLabel = lookup[0]->getLabel();
131 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
132 set<string> processedLabels;
133 set<string> userLabels = labels;
136 string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + getOutputFileNameTag("summary");
137 m->openOutputFile(outputFileName, out);
138 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
139 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
140 out << "H\tpvalue\n";
144 int N = m->getNumGroups();
147 vector<groupRank> vec;
148 vector<string> groups = m->getGroups();
153 //merge all groups into a vector
160 //populate counts and ranSums vectors
161 for (int i=0;i<N;i++) {
165 for(int j;j<vec.size();j++) {
166 if (vec[j].group == group) {
168 sum = sum + vec[j].rank;
176 for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
178 H = (12 / (N*(N+1))) * tmp - (3*(N+1));
180 //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
182 //H = ss / ( (N * (N + 1))/12 );
184 //correction for ties?
186 //p-value calculation
190 catch(exception& e) {
191 m->errorOut(e, "KruskalWallisCommand", "execute");
195 //**********************************************************************************************************************
196 void KruskalWallisCommand::assignRank(vector<groupRank> &vec) {
199 double numRanks, avgRank, j;
200 vector<groupRank>::iterator it, oldit;
202 sort (vec.begin(), vec.end(), comparevalue);
206 while ( it != vec.end() ) {
209 if (!equalvalue(*it, *(it+1))) {
214 while(equalrank(*it, *(it+1))) {
219 numRanks = double (distance(oldit, it));
220 avgRank = j / numRanks;
222 (*oldit).rank = avgRank;
231 catch(exception& e) {
232 m->errorOut(e, "KruskalWallisCommand", "getRank");
237 //**********************************************************************************************************************
238 void KruskalWallisCommand::assignValue(vector<groupRank> &vec) {
241 //**********************************************************************************************************************
242 //**********************************************************************************************************************
243 //**********************************************************************************************************************