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