]> git.donarmstrong.com Git - mothur.git/blob - mantelcommand.cpp
added citation function to commands
[mothur.git] / mantelcommand.cpp
1 /*
2  *  mantelcommand.cpp
3  *  mothur
4  *
5  *  Created by westcott on 2/9/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "mantelcommand.h"
11 #include "readphylipvector.h"
12
13
14 //**********************************************************************************************************************
15 vector<string> MantelCommand::setParameters(){  
16         try {
17                 CommandParameter pphylip1("phylip1", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pphylip1);
18                 CommandParameter pphylip2("phylip2", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pphylip2);
19                 CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
20                 CommandParameter pmethod("method", "Multiple", "pearson-spearman-kendall", "pearson", "", "", "",false,false); parameters.push_back(pmethod);
21                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "MantelCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string MantelCommand::getHelpString(){  
35         try {
36                 string helpString = "";
37                 helpString += "Sokal, R. R., & Rohlf, F. J. (1995). Biometry, 3rd edn. New York: Freeman.\n";
38                 helpString += "The mantel command reads two distance matrices and calculates the mantel correlation coefficient.\n";
39                 helpString += "The mantel command parameters are phylip1, phylip2, iters and method.  The phylip1 and phylip2 parameters are required.  Matrices must be the same size and contain the same names.\n";
40                 helpString += "The method parameter allows you to select what method you would like to use. Options are pearson, spearman and kendall. Default=pearson.\n";
41                 helpString += "The iters parameter allows you to set number of randomization for the P value.  The default is 1000. \n";
42                 helpString += "The mantel command should be in the following format: mantel(phylip1=veg.dist, phylip2=env.dist).\n";
43                 helpString += "The mantel command outputs a .mantel file.\n";
44                 helpString += "Note: No spaces between parameter labels (i.e. phylip1), '=' and parameters (i.e. veg.dist).\n";
45                 return helpString;
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "MantelCommand", "getHelpString");
49                 exit(1);
50         }
51 }
52 //**********************************************************************************************************************
53 MantelCommand::MantelCommand(){ 
54         try {
55                 abort = true; calledHelp = true;
56                 setParameters();
57                 vector<string> tempOutNames;
58                 outputTypes["mantel"] = tempOutNames;
59         }
60         catch(exception& e) {
61                 m->errorOut(e, "MantelCommand", "MantelCommand");
62                 exit(1);
63         }
64 }
65 //**********************************************************************************************************************
66 MantelCommand::MantelCommand(string option)  {
67         try {
68                 abort = false; calledHelp = false;   
69                 
70                 //allow user to run help
71                 if(option == "help") { help(); abort = true; calledHelp = true; }
72                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73                 
74                 else {
75                         vector<string> myArray = setParameters();
76                         
77                         OptionParser parser(option);
78                         map<string, string> parameters = parser.getParameters();
79                         
80                         ValidParameters validParameter;
81                         map<string, string>::iterator it;
82                         
83                         //check to make sure all parameters are valid for command
84                         for (it = parameters.begin(); it != parameters.end(); it++) { 
85                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
86                         }
87                         
88                         vector<string> tempOutNames;
89                         outputTypes["mantel"] = tempOutNames;
90                         
91                         //if the user changes the input directory command factory will send this info to us in the output parameter 
92                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
93                         if (inputDir == "not found"){   inputDir = "";          }
94                         else {
95                                 string path;
96                                 it = parameters.find("phylip1");
97                                 //user has given a template file
98                                 if(it != parameters.end()){ 
99                                         path = m->hasPath(it->second);
100                                         //if the user has not given a path then, add inputdir. else leave path alone.
101                                         if (path == "") {       parameters["phylip1"] = inputDir + it->second;          }
102                                 }
103                                 
104                                 it = parameters.find("phylip2");
105                                 //user has given a template file
106                                 if(it != parameters.end()){ 
107                                         path = m->hasPath(it->second);
108                                         //if the user has not given a path then, add inputdir. else leave path alone.
109                                         if (path == "") {       parameters["phylip2"] = inputDir + it->second;          }
110                                 }
111                         }
112                         
113                         
114                         //check for required parameters
115                         phylipfile1 = validParameter.validFile(parameters, "phylip1", true);
116                         if (phylipfile1 == "not open") { phylipfile1 = ""; abort = true; }
117                         else if (phylipfile1 == "not found") { phylipfile1 = ""; m->mothurOut("phylip1 is a required parameter for the mantel command."); m->mothurOutEndLine(); abort = true;  }       
118                         
119                         phylipfile2 = validParameter.validFile(parameters, "phylip2", true);
120                         if (phylipfile2 == "not open") { phylipfile2 = ""; abort = true; }
121                         else if (phylipfile2 == "not found") { phylipfile2 = ""; m->mothurOut("phylip2 is a required parameter for the mantel command."); m->mothurOutEndLine(); abort = true;  }       
122                         
123                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(phylipfile1);    }
124                         
125                         method = validParameter.validFile(parameters, "method", false);         if (method == "not found"){     method = "pearson";             }
126                         
127                         string temp = validParameter.validFile(parameters, "iters", false);                     if (temp == "not found") { temp = "1000"; }
128                         convert(temp, iters);
129                         
130                         if ((method != "pearson") && (method != "spearman") && (method != "kendall")) { m->mothurOut(method + " is not a valid method. Valid methods are pearson, spearman, and kendall."); m->mothurOutEndLine(); abort = true; }
131                 }
132         }
133         catch(exception& e) {
134                 m->errorOut(e, "MantelCommand", "MantelCommand");               
135                 exit(1);
136         }
137 }
138 //**********************************************************************************************************************
139
140 int MantelCommand::execute(){
141         try {
142                 
143                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
144                 
145                 /***************************************************/
146                 //    reading distance files                                       //
147                 /***************************************************/
148                 
149                 //read phylip1
150                 ReadPhylipVector readMatrix(phylipfile1);
151                 vector< vector<double> > matrix1;
152                 vector<string> names1 = readMatrix.read(matrix1);
153                 
154                 if (m->control_pressed) { return 0; }
155                 
156                 //read phylip2
157                 ReadPhylipVector readMatrix2(phylipfile2);
158                 vector< vector<double> > matrix2;
159                 vector<string> names2 = readMatrix2.read(matrix2);
160                 
161                 if (m->control_pressed) { return 0; }
162                 
163                 //make sure matrix2 and matrix1 are in the same order
164                 if (names1 == names2) { //then everything is in same order and same size
165                 }else if (names1.size() != names2.size()) { //wrong size no need to order, abort
166                         m->mothurOut("[ERROR]: distance matrices are not the same size, aborting."); m->mothurOutEndLine();
167                         m->control_pressed = true;
168                 }else { //sizes are the same, but either the names are different or they are in different order
169                         m->mothurOut("[WARNING]: Names do not match between distance files. Comparing based on order in files."); m->mothurOutEndLine();
170                 }       
171                 
172                 if (m->control_pressed) { return 0; }
173                 
174                 /***************************************************/
175                 //    calculating mantel and signifigance                  //
176                 /***************************************************/
177                 
178                 //calc mantel coefficient
179                 LinearAlgebra linear;
180                 double mantel = 0.0;
181                 if (method == "pearson")                {  mantel = linear.calcPearson(matrix1, matrix2);       }
182                 else if (method == "spearman")  {  mantel = linear.calcSpearman(matrix1, matrix2);      }
183                 else if (method == "kendall")   {  mantel = linear.calcKendall(matrix1, matrix2);       }
184                 
185                 
186                 //calc signifigance
187                 int count = 0;
188                 for (int i = 0; i < iters; i++) {
189                         
190                         if (m->control_pressed) { return 0; }
191                         
192                         //randomize matrix2
193                         vector< vector<double> > matrix2Copy = matrix2;
194                         random_shuffle(matrix2Copy.begin(), matrix2Copy.end());
195                 
196                         //calc random mantel
197                         double randomMantel = 0.0;
198                         if (method == "pearson")                {  randomMantel = linear.calcPearson(matrix1, matrix2Copy);     }
199                         else if (method == "spearman")  {  randomMantel = linear.calcSpearman(matrix1, matrix2Copy);    }
200                         else if (method == "kendall")   {  randomMantel = linear.calcKendall(matrix1, matrix2Copy);     }
201                         
202                         if (randomMantel >= mantel) { count++; }
203                 }
204                 
205                 double pValue = count / (float) iters;
206                 
207                 if (m->control_pressed) { return 0; }
208                 
209                 string outputFile = outputDir + m->getRootName(m->getSimpleName(phylipfile1)) + "mantel";
210                 outputNames.push_back(outputFile); outputTypes["mantel"].push_back(outputFile);
211                 ofstream out;
212                 
213                 m->openOutputFile(outputFile, out);
214                 
215                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
216                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
217                 
218                 out << "Mantel\tpValue" << endl;
219                 out << mantel << '\t' << pValue << endl;
220                 
221                 out.close();
222         
223                 cout << "\nmantel = " << mantel << "\tpValue = " << pValue << endl;
224                 m->mothurOutJustToLog("\nmantel = " + toString(mantel) + "\tpValue = " + toString(pValue) + "\n"); 
225                 
226                 m->mothurOutEndLine();
227                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
228                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
229                 m->mothurOutEndLine();          
230                 
231                 return 0;
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "MantelCommand", "execute");     
235                 exit(1);
236         }
237 }
238
239 //**********************************************************************************************************************
240
241