]> git.donarmstrong.com Git - mothur.git/blob - pcoacommand.cpp
changing command name classify.shared to classifyrf.shared
[mothur.git] / pcoacommand.cpp
1
2 /*
3  *  pcacommand.cpp
4  *  Mothur
5  *
6  *  Created by westcott on 1/4/10.
7  *  Copyright 2010 Schloss Lab. All rights reserved.
8  *
9  */
10
11 #include "pcoacommand.h"
12 #include "readphylipvector.h"
13
14
15 //**********************************************************************************************************************
16 vector<string> PCOACommand::setParameters(){    
17         try {
18                 CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none","pcoa-loadings",false,true,true); parameters.push_back(pphylip);
19                 CommandParameter pmetric("metric", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pmetric);
20                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
21                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "PCOACommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string PCOACommand::getHelpString(){    
34         try {
35                 string helpString = "";
36                 helpString += "The pcoa command parameters are phylip and metric"; 
37                 helpString += "The phylip parameter allows you to enter your distance file.";
38                 helpString += "The metric parameter allows indicate you if would like the pearson correlation coefficient calculated. Default=True"; 
39                 helpString += "Example pcoa(phylip=yourDistanceFile).\n";
40                 helpString += "Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "PCOACommand", "getHelpString");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49 string PCOACommand::getOutputPattern(string type) {
50     try {
51         string pattern = "";
52         
53         if (type == "pcoa") {  pattern = "[filename],pcoa.axes"; } 
54         else if (type == "loadings") {  pattern = "[filename],pcoa.loadings"; } 
55         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
56         
57         return pattern;
58     }
59     catch(exception& e) {
60         m->errorOut(e, "PCOACommand", "getOutputPattern");
61         exit(1);
62     }
63 }
64
65
66 //**********************************************************************************************************************
67 PCOACommand::PCOACommand(){     
68         try {
69                 abort = true; calledHelp = true; 
70                 setParameters();
71                 vector<string> tempOutNames;
72                 outputTypes["pcoa"] = tempOutNames;
73                 outputTypes["loadings"] = tempOutNames;
74         }
75         catch(exception& e) {
76                 m->errorOut(e, "PCOACommand", "PCOACommand");
77                 exit(1);
78         }
79 }
80 //**********************************************************************************************************************
81
82 PCOACommand::PCOACommand(string option)  {
83         try {
84                 abort = false; calledHelp = false;   
85                 
86                 //allow user to run help
87                 if(option == "help") { help(); abort = true; calledHelp = true; }
88                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
89                 
90                 else {
91                         vector<string> myArray = setParameters();
92                         
93                         OptionParser parser(option);
94                         map<string, string> parameters = parser. getParameters();
95                         
96                         ValidParameters validParameter;
97                         map<string, string>::iterator it;
98                 
99                         //check to make sure all parameters are valid for command
100                         for (it = parameters.begin(); it != parameters.end(); it++) { 
101                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
102                         }
103                         //if the user changes the input directory command factory will send this info to us in the output parameter 
104                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
105                         if (inputDir == "not found"){   inputDir = "";          }
106                         else {
107                                 string path;
108                                 it = parameters.find("phylip");
109                                 //user has given a template file
110                                 if(it != parameters.end()){ 
111                                         path = m->hasPath(it->second);
112                                         //if the user has not given a path then, add inputdir. else leave path alone.
113                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
114                                 }
115                         }
116                         
117                         //initialize outputTypes
118                         vector<string> tempOutNames;
119                         outputTypes["pcoa"] = tempOutNames;
120                         outputTypes["loadings"] = tempOutNames;
121                         
122                         //required parameters
123                         phylipfile = validParameter.validFile(parameters, "phylip", true);
124                         if (phylipfile == "not open") { abort = true; }
125                         else if (phylipfile == "not found") {                   
126                                 //if there is a current phylip file, use it
127                                 phylipfile = m->getPhylipFile(); 
128                                 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
129                                 else {  m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; }
130                         }else { m->setPhylipFile(phylipfile); } 
131                         
132                         filename = phylipfile;  
133                         
134                         //if the user changes the output directory command factory will send this info to us in the output parameter 
135                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
136                                 outputDir = ""; 
137                                 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
138                         }
139                         
140                         string temp = validParameter.validFile(parameters, "metric", false);    if (temp == "not found"){       temp = "T";                             }
141                         metric = m->isTrue(temp); 
142                 }
143
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "PCOACommand", "PCOACommand");
147                 exit(1);
148         }
149 }
150 //**********************************************************************************************************************
151 int PCOACommand::execute(){
152         try {
153         
154                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
155                 
156                 cout.setf(ios::fixed, ios::floatfield);
157                 cout.setf(ios::showpoint);
158                 cerr.setf(ios::fixed, ios::floatfield);
159                 cerr.setf(ios::showpoint);
160                 
161                 vector<string> names;
162                 vector<vector<double> > D;
163         
164                 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
165                 
166                 ReadPhylipVector readFile(filename);
167                 names = readFile.read(D);
168                 
169                 if (m->control_pressed) { return 0; }
170         
171                 double offset = 0.0000;
172                 vector<double> d;
173                 vector<double> e;
174                 vector<vector<double> > G = D;
175                 //vector<vector<double> > copy_G;
176                                 
177                 m->mothurOut("\nProcessing...\n");
178                 
179                 for(int count=0;count<2;count++){
180                         linearCalc.recenter(offset, D, G);              if (m->control_pressed) { return 0; }
181                         linearCalc.tred2(G, d, e);                              if (m->control_pressed) { return 0; }
182                         linearCalc.qtli(d, e, G);                               if (m->control_pressed) { return 0; }
183                         offset = d[d.size()-1];
184                         if(offset > 0.0) break;
185                 } 
186                 
187                 if (m->control_pressed) { return 0; }
188                 
189                 output(fbase, names, G, d);
190                 
191                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
192                 
193                 if (metric) {   
194                         
195                         for (int i = 1; i < 4; i++) {
196                                                         
197                                 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
198                                 
199                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
200                                 
201                                 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
202                                 
203                                 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
204                                 
205                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
206                         }
207                 }
208                 
209                 m->mothurOutEndLine();
210                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
211                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
212                 m->mothurOutEndLine();
213                 
214                 return 0;
215         }
216         catch(exception& e) {
217                 m->errorOut(e, "PCOACommand", "execute");
218                 exit(1);
219         }
220 }
221 /*********************************************************************************************************************************/
222
223 void PCOACommand::get_comment(istream& f, char begin, char end){
224         try {
225                 char d=f.get();
226                 while(d != end){        d = f.get();    }
227                 d = f.peek();
228         }
229         catch(exception& e) {
230                 m->errorOut(e, "PCOACommand", "get_comment");
231                 exit(1);
232         }
233 }       
234 /*********************************************************************************************************************************/
235
236 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
237         try {
238                 int rank = name_list.size();
239                 double dsum = 0.0000;
240                 for(int i=0;i<rank;i++){
241                         dsum += d[i];
242                         for(int j=0;j<rank;j++){
243                                 if(d[j] >= 0)   {       G[i][j] *= pow(d[j],0.5);       }
244                                 else                    {       G[i][j] = 0.00000;                      }
245                         }
246                 }
247                 
248                 ofstream pcaData;
249         map<string, string> variables; 
250         variables["[filename]"] = fnameRoot;
251         string pcoaDataFile = getOutputFileName("pcoa",variables);
252         m->openOutputFile(pcoaDataFile, pcaData);
253                 pcaData.setf(ios::fixed, ios::floatfield);
254                 pcaData.setf(ios::showpoint);   
255                 outputNames.push_back(pcoaDataFile);
256                 outputTypes["pcoa"].push_back(pcoaDataFile);
257                 
258                 ofstream pcaLoadings;
259         string loadingsFile = getOutputFileName("loadings",variables);
260         m->openOutputFile(loadingsFile, pcaLoadings);
261                 pcaLoadings.setf(ios::fixed, ios::floatfield);
262                 pcaLoadings.setf(ios::showpoint);
263                 outputNames.push_back(loadingsFile);
264                 outputTypes["loadings"].push_back(loadingsFile);        
265                 
266                 pcaLoadings << "axis\tloading\n";
267                 for(int i=0;i<rank;i++){
268                         pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
269                 }
270                 
271                 pcaData << "group";
272                 for(int i=0;i<rank;i++){
273                         pcaData << '\t' << "axis" << i+1;
274                 }
275                 pcaData << endl;
276                 
277                 for(int i=0;i<rank;i++){
278                         pcaData << name_list[i] << '\t';
279                         for(int j=0;j<rank;j++){
280                                 pcaData << G[i][j] << '\t';
281                         }
282                         pcaData << endl;
283                 }
284         }
285         catch(exception& e) {
286                 m->errorOut(e, "PCOACommand", "output");
287                 exit(1);
288         }
289 }
290
291 /*********************************************************************************************************************************/
292