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