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