]> git.donarmstrong.com Git - mothur.git/blob - kruskalwalliscommand.cpp
cleaned up code
[mothur.git] / kruskalwalliscommand.cpp
1 /* 
2  * File:   kruskalwalliscommand.cpp
3  * Author: kiverson
4  *
5  * Created on June 26, 2012, 11:06 AM
6  */
7
8 #include "kruskalwalliscommand.h"
9
10 //**********************************************************************************************************************
11 vector<string> KruskalWallisCommand::setParameters(){   
12         try {
13                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
14                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
15                 
16                 vector<string> myArray;
17                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "KruskalWallisCommand", "setParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 string KruskalWallisCommand::getHelpString(){   
27         try {
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.";
31                 return helpString;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "KruskalWallisCommand", "getHelpString");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 string KruskalWallisCommand::getOutputFileNameTag(string type, string inputName=""){    
40         try {
41         string outputFileName = "";
42                 map<string, vector<string> >::iterator it;
43         
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"); }
47         else {
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;  }
50         }
51         return outputFileName;
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "KruskalWallisCommand", "getOutputFileNameTag");
55                 exit(1);
56         }
57 }
58 //**********************************************************************************************************************
59 KruskalWallisCommand::KruskalWallisCommand(){   
60         try {
61                 abort = true; calledHelp = true; 
62                 setParameters();
63         vector<string> tempOutNames;
64                 outputTypes["summary"] = tempOutNames;
65
66         }
67         catch(exception& e) {
68                 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
69                 exit(1);
70         }
71 }
72 //**********************************************************************************************************************
73 KruskalWallisCommand::KruskalWallisCommand(string option) {
74         try {
75                 abort = false; calledHelp = false;   
76                                 
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;}
80                 
81                 else {
82                         vector<string> myArray = setParameters();
83                         
84                         OptionParser parser(option);
85                         map<string,string> parameters = parser.getParameters();
86                         map<string,string>::iterator it;
87                         
88                         ValidParameters validParameter;
89                         
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;  }
93                         }
94
95                         
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 = "";          }
99                         else {
100                                 string path;
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;           }
107                                 }
108                         }
109                 
110             vector<string> tempOutNames;
111             outputTypes["summary"] = tempOutNames;
112
113
114                 }
115
116         }
117         catch(exception& e) {
118                 m->errorOut(e, "KruskalWallisCommand", "KruskalWallisCommand");
119                 exit(1);
120         }
121 }
122 //**********************************************************************************************************************
123 int KruskalWallisCommand::execute(){
124         try {
125                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
126         
127         //math goes here
128         
129         int N; //= thisLookUp.size();
130         double H;
131         double tmp = 0.0;
132         vector<groupRank> vec;
133         string group;
134         int count;
135         double sum;
136                 
137         //merge all groups into a vector
138         //rank function here
139         assignRank(vec);
140         
141         //populate counts and ranSums vectors
142         for (int i=0;i<N;i++) {
143             count = 0;
144             sum = 0;
145             //group = next group
146             for(int j;j<vec.size();j++) {
147                 if (vec[j].group == group) {
148                     count++;
149                     sum = sum + vec[j].rank;
150                 }
151             }
152             counts[i] = count;
153             rankSums[i] = sum;
154         }
155         
156         //test statistic
157         for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
158         
159         H = (12 / (N*(N+1))) * tmp - (3*(N+1));
160         
161         //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
162         
163         //H = ss / ( (N * (N + 1))/12 );
164         
165         //correction for ties?
166         
167         //p-value calculation
168         
169                 return 0;
170         }
171         catch(exception& e) {
172                 m->errorOut(e, "KruskalWallisCommand", "execute");
173                 exit(1);
174         }
175 }
176 //**********************************************************************************************************************
177 void KruskalWallisCommand::assignRank(vector<groupRank> &vec) {
178     try {
179         double rank = 1;
180         double numRanks, avgRank, j;
181         vector<groupRank>::iterator it, oldit;
182
183         sort (vec.begin(), vec.end(), comparevalue);
184
185         it = vec.begin();
186
187         while ( it != vec.end() ) {
188             j = rank;
189             oldit = it;
190             if (!equalvalue(*it, *(it+1))) {
191                 (*it).rank = rank; 
192                 rank = rank+1; 
193                 it++; }
194             else {
195                 while(equalrank(*it, *(it+1))) {
196                     j = j + (j+1);
197                     rank++;
198                     it++;
199                 }
200                 numRanks = double (distance(oldit, it));
201                 avgRank = j / numRanks;
202                 while(oldit != it) {
203                     (*oldit).rank = avgRank;
204                     oldit++;
205                 }
206             }
207
208         }
209         
210
211     }
212     catch(exception& e) {
213                 m->errorOut(e, "KruskalWallisCommand", "getRank");
214                 exit(1);
215         }
216     
217 }
218 //**********************************************************************************************************************
219
220 //**********************************************************************************************************************
221 //**********************************************************************************************************************
222 //**********************************************************************************************************************