]> git.donarmstrong.com Git - mothur.git/blob - kruskalwalliscommand.cpp
cleaned up code2
[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         InputData* input = new InputData(sharedfile, "sharedfile");
128         vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
129                 string lastLabel = lookup[0]->getLabel();
130                 
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;
134
135         ofstream out;
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";
141         
142         //math goes here
143         
144         int N = lookUp.size();
145         double H;
146         double tmp = 0.0;
147         vector<groupRank> vec;
148         string group;
149         int count;
150         double sum;
151                 
152         //merge all groups into a vector
153         //rank function here
154         assignRank(vec);
155         
156         //populate counts and ranSums vectors
157         for (int i=0;i<N;i++) {
158             count = 0;
159             sum = 0;
160             //group = next group
161             for(int j;j<vec.size();j++) {
162                 if (vec[j].group == group) {
163                     count++;
164                     sum = sum + vec[j].rank;
165                 }
166             }
167             counts[i] = count;
168             rankSums[i] = sum;
169         }
170         
171         //test statistic
172         for (int i=0;i<N;i++) { tmp = tmp + (pow(rankSums[i],2) / counts[i]); }
173         
174         H = (12 / (N*(N+1))) * tmp - (3*(N+1));
175         
176         //ss = tmp - pow(accumulate(rankSums.begin(), rankSums.end(), 0), 2);
177         
178         //H = ss / ( (N * (N + 1))/12 );
179         
180         //correction for ties?
181         
182         //p-value calculation
183         
184                 return 0;
185         }
186         catch(exception& e) {
187                 m->errorOut(e, "KruskalWallisCommand", "execute");
188                 exit(1);
189         }
190 }
191 //**********************************************************************************************************************
192 void KruskalWallisCommand::assignRank(vector<groupRank> &vec) {
193     try {
194         double rank = 1;
195         double numRanks, avgRank, j;
196         vector<groupRank>::iterator it, oldit;
197
198         sort (vec.begin(), vec.end(), comparevalue);
199
200         it = vec.begin();
201
202         while ( it != vec.end() ) {
203             j = rank;
204             oldit = it;
205             if (!equalvalue(*it, *(it+1))) {
206                 (*it).rank = rank; 
207                 rank = rank+1; 
208                 it++; }
209             else {
210                 while(equalrank(*it, *(it+1))) {
211                     j = j + (j+1);
212                     rank++;
213                     it++;
214                 }
215                 numRanks = double (distance(oldit, it));
216                 avgRank = j / numRanks;
217                 while(oldit != it) {
218                     (*oldit).rank = avgRank;
219                     oldit++;
220                 }
221             }
222
223         }
224         
225
226     }
227     catch(exception& e) {
228                 m->errorOut(e, "KruskalWallisCommand", "getRank");
229                 exit(1);
230         }
231     
232 }
233 //**********************************************************************************************************************
234
235 //**********************************************************************************************************************
236 //**********************************************************************************************************************
237 //**********************************************************************************************************************