]> git.donarmstrong.com Git - mothur.git/blob - mantelcommand.cpp
added load.logfile command. changed summary.single output for subsample=t.
[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 string MantelCommand::getOutputFileNameTag(string type, string inputName=""){   
54         try {
55         string outputFileName = "";
56                 map<string, vector<string> >::iterator it;
57         
58         //is this a type this command creates
59         it = outputTypes.find(type);
60         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
61         else {
62             if (type == "mantel")             {   outputFileName =  "mantel";       }
63             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
64         }
65         return outputFileName;
66         }
67         catch(exception& e) {
68                 m->errorOut(e, "MantelCommand", "getOutputFileNameTag");
69                 exit(1);
70         }
71 }
72 //**********************************************************************************************************************
73 MantelCommand::MantelCommand(){ 
74         try {
75                 abort = true; calledHelp = true;
76                 setParameters();
77                 vector<string> tempOutNames;
78                 outputTypes["mantel"] = tempOutNames;
79         }
80         catch(exception& e) {
81                 m->errorOut(e, "MantelCommand", "MantelCommand");
82                 exit(1);
83         }
84 }
85 //**********************************************************************************************************************
86 MantelCommand::MantelCommand(string option)  {
87         try {
88                 abort = false; calledHelp = false;   
89                 
90                 //allow user to run help
91                 if(option == "help") { help(); abort = true; calledHelp = true; }
92                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
93                 
94                 else {
95                         vector<string> myArray = setParameters();
96                         
97                         OptionParser parser(option);
98                         map<string, string> parameters = parser.getParameters();
99                         
100                         ValidParameters validParameter;
101                         map<string, string>::iterator it;
102                         
103                         //check to make sure all parameters are valid for command
104                         for (it = parameters.begin(); it != parameters.end(); it++) { 
105                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
106                         }
107                         
108                         vector<string> tempOutNames;
109                         outputTypes["mantel"] = tempOutNames;
110                         
111                         //if the user changes the input directory command factory will send this info to us in the output parameter 
112                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
113                         if (inputDir == "not found"){   inputDir = "";          }
114                         else {
115                                 string path;
116                                 it = parameters.find("phylip1");
117                                 //user has given a template file
118                                 if(it != parameters.end()){ 
119                                         path = m->hasPath(it->second);
120                                         //if the user has not given a path then, add inputdir. else leave path alone.
121                                         if (path == "") {       parameters["phylip1"] = inputDir + it->second;          }
122                                 }
123                                 
124                                 it = parameters.find("phylip2");
125                                 //user has given a template file
126                                 if(it != parameters.end()){ 
127                                         path = m->hasPath(it->second);
128                                         //if the user has not given a path then, add inputdir. else leave path alone.
129                                         if (path == "") {       parameters["phylip2"] = inputDir + it->second;          }
130                                 }
131                         }
132                         
133                         
134                         //check for required parameters
135                         phylipfile1 = validParameter.validFile(parameters, "phylip1", true);
136                         if (phylipfile1 == "not open") { phylipfile1 = ""; abort = true; }
137                         else if (phylipfile1 == "not found") { phylipfile1 = ""; m->mothurOut("phylip1 is a required parameter for the mantel command."); m->mothurOutEndLine(); abort = true;  }       
138                         
139                         phylipfile2 = validParameter.validFile(parameters, "phylip2", true);
140                         if (phylipfile2 == "not open") { phylipfile2 = ""; abort = true; }
141                         else if (phylipfile2 == "not found") { phylipfile2 = ""; m->mothurOut("phylip2 is a required parameter for the mantel command."); m->mothurOutEndLine(); abort = true;  }       
142                         
143                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(phylipfile1);    }
144                         
145                         method = validParameter.validFile(parameters, "method", false);         if (method == "not found"){     method = "pearson";             }
146                         
147                         string temp = validParameter.validFile(parameters, "iters", false);                     if (temp == "not found") { temp = "1000"; }
148                         m->mothurConvert(temp, iters);
149                         
150                         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; }
151                 }
152         }
153         catch(exception& e) {
154                 m->errorOut(e, "MantelCommand", "MantelCommand");               
155                 exit(1);
156         }
157 }
158 //**********************************************************************************************************************
159
160 int MantelCommand::execute(){
161         try {
162                 
163                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
164                 
165                 /***************************************************/
166                 //    reading distance files                                       //
167                 /***************************************************/
168                 
169                 //read phylip1
170                 ReadPhylipVector readMatrix(phylipfile1);
171                 vector< vector<double> > matrix1;
172                 vector<string> names1 = readMatrix.read(matrix1);
173                 
174                 if (m->control_pressed) { return 0; }
175                 
176                 //read phylip2
177                 ReadPhylipVector readMatrix2(phylipfile2);
178                 vector< vector<double> > matrix2;
179                 vector<string> names2 = readMatrix2.read(matrix2);
180                 
181                 if (m->control_pressed) { return 0; }
182                 
183                 //make sure matrix2 and matrix1 are in the same order
184                 if (names1 == names2) { //then everything is in same order and same size
185                 }else if (names1.size() != names2.size()) { //wrong size no need to order, abort
186                         m->mothurOut("[ERROR]: distance matrices are not the same size, aborting."); m->mothurOutEndLine();
187                         m->control_pressed = true;
188                 }else { //sizes are the same, but either the names are different or they are in different order
189                         m->mothurOut("[WARNING]: Names do not match between distance files. Comparing based on order in files."); m->mothurOutEndLine();
190                 }       
191                 
192                 if (m->control_pressed) { return 0; }
193                 
194                 /***************************************************/
195                 //    calculating mantel and signifigance                  //
196                 /***************************************************/
197                 
198                 //calc mantel coefficient
199                 LinearAlgebra linear;
200                 double mantel = 0.0;
201                 if (method == "pearson")                {  mantel = linear.calcPearson(matrix1, matrix2);       }
202                 else if (method == "spearman")  {  mantel = linear.calcSpearman(matrix1, matrix2);      }
203                 else if (method == "kendall")   {  mantel = linear.calcKendall(matrix1, matrix2);       }
204                 
205                 
206                 //calc signifigance
207                 int count = 0;
208                 for (int i = 0; i < iters; i++) {
209                         
210                         if (m->control_pressed) { return 0; }
211                         
212                         //randomize matrix2
213                         vector< vector<double> > matrix2Copy = matrix2;
214                         random_shuffle(matrix2Copy.begin(), matrix2Copy.end());
215                 
216                         //calc random mantel
217                         double randomMantel = 0.0;
218                         if (method == "pearson")                {  randomMantel = linear.calcPearson(matrix1, matrix2Copy);     }
219                         else if (method == "spearman")  {  randomMantel = linear.calcSpearman(matrix1, matrix2Copy);    }
220                         else if (method == "kendall")   {  randomMantel = linear.calcKendall(matrix1, matrix2Copy);     }
221                         
222                         if (randomMantel >= mantel) { count++; }
223                 }
224                 
225                 double pValue = count / (float) iters;
226                 
227                 if (m->control_pressed) { return 0; }
228                 
229                 string outputFile = outputDir + m->getRootName(m->getSimpleName(phylipfile1)) + getOutputFileNameTag("mantel");
230                 outputNames.push_back(outputFile); outputTypes["mantel"].push_back(outputFile);
231                 ofstream out;
232                 
233                 m->openOutputFile(outputFile, out);
234                 
235                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
236                 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
237                 
238                 out << "Mantel\tpValue" << endl;
239                 out << mantel << '\t' << pValue << endl;
240                 
241                 out.close();
242         
243                 cout << "\nmantel = " << mantel << "\tpValue = " << pValue << endl;
244                 m->mothurOutJustToLog("\nmantel = " + toString(mantel) + "\tpValue = " + toString(pValue) + "\n"); 
245                 
246                 m->mothurOutEndLine();
247                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
248                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
249                 m->mothurOutEndLine();          
250                 
251                 return 0;
252         }
253         catch(exception& e) {
254                 m->errorOut(e, "MantelCommand", "execute");     
255                 exit(1);
256         }
257 }
258
259 //**********************************************************************************************************************
260
261