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); parameters.push_back(pgroups);
16 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,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::getOutputFileNameTag(string type, string inputName=""){
43 string outputFileName = "";
44 map<string, vector<string> >::iterator it;
46 //is this a type this command creates
47 it = outputTypes.find(type);
48 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
50 if (type == "summary") { outputFileName = "cooccurence.summary"; }
51 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
53 return outputFileName;
56 m->errorOut(e, "KruskalWallisCommand", "getOutputFileNameTag");
60 //**********************************************************************************************************************
61 KruskalWallisCommand::KruskalWallisCommand(){
63 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["summary"] = tempOutNames;
70 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
74 //**********************************************************************************************************************
75 KruskalWallisCommand::KruskalWallisCommand(string option) {
77 abort = false; calledHelp = false;
79 //allow user to run help
80 if(option == "help") { help(); abort = true; calledHelp = true; }
81 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
84 vector<string> myArray = setParameters();
86 OptionParser parser(option);
87 map<string,string> parameters = parser.getParameters();
88 map<string,string>::iterator it;
90 ValidParameters validParameter;
92 //check to make sure all parameters are valid for command
93 for (it = parameters.begin(); it != parameters.end(); it++) {
94 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
98 sharedfile = validParameter.validFile(parameters, "shared", true);
99 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
100 else if (sharedfile == "not found") {
101 //if there is a current shared file, use it
102 sharedfile = m->getSharedFile();
103 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
104 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
105 }else { m->setSharedFile(sharedfile); }
107 //if the user changes the output directory command factory will send this info to us in the output parameter
108 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(sharedfile); }
110 groups = validParameter.validFile(parameters, "groups", false);
111 if (groups == "not found") { groups = ""; }
113 m->splitAtDash(groups, Groups);
115 m->setGroups(Groups);
117 //if the user changes the input directory command factory will send this info to us in the output parameter
118 string inputDir = validParameter.validFile(parameters, "inputdir", false);
119 if (inputDir == "not found"){ inputDir = ""; }
122 it = parameters.find("shared");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["shared"] = inputDir + it->second; }
131 vector<string> tempOutNames;
132 outputTypes["summary"] = tempOutNames;
138 catch(exception& e) {
139 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
143 //**********************************************************************************************************************
144 int KruskalWallisCommand::execute(){
146 if (abort == true) { if (calledHelp) { return 0; } return 2; }
148 InputData* input = new InputData(sharedfile, "sharedfile");
149 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
150 string lastLabel = lookup[0]->getLabel();
153 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
154 set<string> processedLabels;
155 set<string> userLabels = labels;
158 string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + getOutputFileNameTag("summary");
159 m->openOutputFile(outputFileName, out);
160 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
161 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
162 out << "H\tpvalue\n";
166 int N = m->getNumGroups();
169 vector<groupRank> vec;
170 vector<string> groups = m->getGroups();
175 //merge all groups into a vector
182 //populate counts and ranSums vectors
183 for (int i=0;i<N;i++) {
187 for(int j;j<vec.size();j++) {
188 if (vec[j].group == group) {
190 sum = sum + vec[j].rank;
198 for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
200 H = (12 / (N*(N+1))) * tmp - (3*(N+1));
202 //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
204 //H = ss / ( (N * (N + 1))/12 );
206 //correction for ties?
208 //p-value calculation
212 catch(exception& e) {
213 m->errorOut(e, "KruskalWallisCommand", "execute");
217 //**********************************************************************************************************************
218 void KruskalWallisCommand::assignRank(vector<groupRank> &vec) {
221 double numRanks, avgRank, j;
222 vector<groupRank>::iterator it, oldit;
224 sort (vec.begin(), vec.end(), comparevalue);
228 while ( it != vec.end() ) {
231 if (!equalvalue(*it, *(it+1))) {
236 while(equalrank(*it, *(it+1))) {
241 numRanks = double (distance(oldit, it));
242 avgRank = j / numRanks;
244 (*oldit).rank = avgRank;
253 catch(exception& e) {
254 m->errorOut(e, "KruskalWallisCommand", "getRank");
259 //**********************************************************************************************************************
260 void KruskalWallisCommand::assignValue(vector<groupRank> &vec) {
263 //**********************************************************************************************************************
264 //**********************************************************************************************************************
265 //**********************************************************************************************************************