]> git.donarmstrong.com Git - mothur.git/blob - pcoacommand.cpp
added getCommandInfoCommand for gui
[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",false,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
50 //**********************************************************************************************************************
51 PCOACommand::PCOACommand(){     
52         try {
53                 abort = true; calledHelp = true; 
54                 setParameters();
55                 vector<string> tempOutNames;
56                 outputTypes["pcoa"] = tempOutNames;
57                 outputTypes["loadings"] = tempOutNames;
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "PCOACommand", "PCOACommand");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65
66 PCOACommand::PCOACommand(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                 
73                 else {
74                         vector<string> myArray = setParameters();
75                         
76                         OptionParser parser(option);
77                         map<string, string> parameters = parser. getParameters();
78                         
79                         ValidParameters validParameter;
80                         map<string, string>::iterator it;
81                 
82                         //check to make sure all parameters are valid for command
83                         for (it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         //if the user changes the input directory command factory will send this info to us in the output parameter 
87                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
88                         if (inputDir == "not found"){   inputDir = "";          }
89                         else {
90                                 string path;
91                                 it = parameters.find("phylip");
92                                 //user has given a template file
93                                 if(it != parameters.end()){ 
94                                         path = m->hasPath(it->second);
95                                         //if the user has not given a path then, add inputdir. else leave path alone.
96                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
97                                 }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["pcoa"] = tempOutNames;
103                         outputTypes["loadings"] = tempOutNames;
104                         
105                         //required parameters
106                         phylipfile = validParameter.validFile(parameters, "phylip", true);
107                         if (phylipfile == "not open") { abort = true; }
108                         else if (phylipfile == "not found") {                   
109                                 //if there is a current phylip file, use it
110                                 phylipfile = m->getPhylipFile(); 
111                                 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
112                                 else {  m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; }
113                         }       
114                         
115                         filename = phylipfile;  
116                         
117                         //if the user changes the output directory command factory will send this info to us in the output parameter 
118                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
119                                 outputDir = ""; 
120                                 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
121                         }
122                         
123                         string temp = validParameter.validFile(parameters, "metric", false);    if (temp == "not found"){       temp = "T";                             }
124                         metric = m->isTrue(temp); 
125                 }
126
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "PCOACommand", "PCOACommand");
130                 exit(1);
131         }
132 }
133 //**********************************************************************************************************************
134 int PCOACommand::execute(){
135         try {
136         
137                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
138                 
139                 cout.setf(ios::fixed, ios::floatfield);
140                 cout.setf(ios::showpoint);
141                 cerr.setf(ios::fixed, ios::floatfield);
142                 cerr.setf(ios::showpoint);
143                 
144                 vector<string> names;
145                 vector<vector<double> > D;
146         
147                 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
148                 
149                 ReadPhylipVector readFile(filename);
150                 names = readFile.read(D);
151                 
152                 if (m->control_pressed) { return 0; }
153         
154                 double offset = 0.0000;
155                 vector<double> d;
156                 vector<double> e;
157                 vector<vector<double> > G = D;
158                 //vector<vector<double> > copy_G;
159                                 
160                 m->mothurOut("\nProcessing...\n");
161                 
162                 for(int count=0;count<2;count++){
163                         linearCalc.recenter(offset, D, G);              if (m->control_pressed) { return 0; }
164                         linearCalc.tred2(G, d, e);                              if (m->control_pressed) { return 0; }
165                         linearCalc.qtli(d, e, G);                               if (m->control_pressed) { return 0; }
166                         offset = d[d.size()-1];
167                         if(offset > 0.0) break;
168                 } 
169                 
170                 if (m->control_pressed) { return 0; }
171                 
172                 output(fbase, names, G, d);
173                 
174                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
175                 
176                 if (metric) {   
177                         
178                         for (int i = 1; i < 4; i++) {
179                                                         
180                                 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
181                                 
182                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
183                                 
184                                 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
185                                 
186                                 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
187                                 
188                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
189                         }
190                 }
191                 
192                 m->mothurOutEndLine();
193                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
194                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
195                 m->mothurOutEndLine();
196                 
197                 return 0;
198         }
199         catch(exception& e) {
200                 m->errorOut(e, "PCOACommand", "execute");
201                 exit(1);
202         }
203 }
204 /*********************************************************************************************************************************/
205
206 void PCOACommand::get_comment(istream& f, char begin, char end){
207         try {
208                 char d=f.get();
209                 while(d != end){        d = f.get();    }
210                 d = f.peek();
211         }
212         catch(exception& e) {
213                 m->errorOut(e, "PCOACommand", "get_comment");
214                 exit(1);
215         }
216 }       
217 /*********************************************************************************************************************************/
218
219 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
220         try {
221                 int rank = name_list.size();
222                 double dsum = 0.0000;
223                 for(int i=0;i<rank;i++){
224                         dsum += d[i];
225                         for(int j=0;j<rank;j++){
226                                 if(d[j] >= 0)   {       G[i][j] *= pow(d[j],0.5);       }
227                                 else                    {       G[i][j] = 0.00000;                      }
228                         }
229                 }
230                 
231                 ofstream pcaData((fnameRoot+"pcoa.axes").c_str(), ios::trunc);
232                 pcaData.setf(ios::fixed, ios::floatfield);
233                 pcaData.setf(ios::showpoint);   
234                 outputNames.push_back(fnameRoot+"pcoa.axes");
235                 outputTypes["pcoa"].push_back(fnameRoot+"pcoa.axes");
236                 
237                 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
238                 pcaLoadings.setf(ios::fixed, ios::floatfield);
239                 pcaLoadings.setf(ios::showpoint);
240                 outputNames.push_back(fnameRoot+"pcoa.loadings");
241                 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");   
242                 
243                 pcaLoadings << "axis\tloading\n";
244                 for(int i=0;i<rank;i++){
245                         pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
246                 }
247                 
248                 pcaData << "group";
249                 for(int i=0;i<rank;i++){
250                         pcaData << '\t' << "axis" << i+1;
251                 }
252                 pcaData << endl;
253                 
254                 for(int i=0;i<rank;i++){
255                         pcaData << name_list[i] << '\t';
256                         for(int j=0;j<rank;j++){
257                                 pcaData << G[i][j] << '\t';
258                         }
259                         pcaData << endl;
260                 }
261         }
262         catch(exception& e) {
263                 m->errorOut(e, "PCOACommand", "output");
264                 exit(1);
265         }
266 }
267
268 /*********************************************************************************************************************************/
269