6 * Created by westcott on 1/4/10.
7 * Copyright 2010 Schloss Lab. All rights reserved.
11 #include "pcacommand.h"
13 //**********************************************************************************************************************
14 vector<string> PCACommand::getValidParameters(){
16 string Array[] = {"phylip", "outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "PCACommand", "getValidParameters");
25 //**********************************************************************************************************************
26 PCACommand::PCACommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["pcoa"] = tempOutNames;
31 outputTypes["loadings"] = tempOutNames;
34 m->errorOut(e, "PCACommand", "PCACommand");
38 //**********************************************************************************************************************
39 vector<string> PCACommand::getRequiredParameters(){
41 string Array[] = {"phylip"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "PCACommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> PCACommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "PCACommand", "getRequiredFiles");
61 //**********************************************************************************************************************
63 PCACommand::PCACommand(string option) {
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"phylip","outputdir", "inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string, string> parameters = parser. getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //if the user changes the input directory command factory will send this info to us in the output parameter
86 string inputDir = validParameter.validFile(parameters, "inputdir", false);
87 if (inputDir == "not found"){ inputDir = ""; }
90 it = parameters.find("phylip");
91 //user has given a template file
92 if(it != parameters.end()){
93 path = m->hasPath(it->second);
94 //if the user has not given a path then, add inputdir. else leave path alone.
95 if (path == "") { parameters["phylip"] = inputDir + it->second; }
99 //initialize outputTypes
100 vector<string> tempOutNames;
101 outputTypes["pcoa"] = tempOutNames;
102 outputTypes["loadings"] = tempOutNames;
104 //required parameters
105 phylipfile = validParameter.validFile(parameters, "phylip", true);
106 if (phylipfile == "not open") { abort = true; }
107 else if (phylipfile == "not found") { phylipfile = ""; abort = true; }
108 else { filename = phylipfile; }
110 //if the user changes the output directory command factory will send this info to us in the output parameter
111 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
113 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
116 //error checking on files
117 if (phylipfile == "") { m->mothurOut("You must provide a distance file before running the pca command."); m->mothurOutEndLine(); abort = true; }
121 catch(exception& e) {
122 m->errorOut(e, "PCACommand", "PCACommand");
126 //**********************************************************************************************************************
127 void PCACommand::help(){
130 m->mothurOut("The pca command..."); m->mothurOutEndLine();
132 catch(exception& e) {
133 m->errorOut(e, "PCACommand", "help");
137 //**********************************************************************************************************************
138 PCACommand::~PCACommand(){}
139 //**********************************************************************************************************************
140 int PCACommand::execute(){
143 if (abort == true) { return 0; }
145 cout.setf(ios::fixed, ios::floatfield);
146 cout.setf(ios::showpoint);
147 cerr.setf(ios::fixed, ios::floatfield);
148 cerr.setf(ios::showpoint);
150 vector<string> names;
151 vector<vector<double> > D;
153 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
155 read(filename, names, D);
157 if (m->control_pressed) { return 0; }
159 double offset = 0.0000;
162 vector<vector<double> > G = D;
163 vector<vector<double> > copy_G;
164 //int rank = D.size();
166 m->mothurOut("\nProcessing...\n");
168 for(int count=0;count<2;count++){
169 recenter(offset, D, G); if (m->control_pressed) { return 0; }
170 tred2(G, d, e); if (m->control_pressed) { return 0; }
171 qtli(d, e, G); if (m->control_pressed) { return 0; }
172 offset = d[d.size()-1];
173 if(offset > 0.0) break;
176 if (m->control_pressed) { return 0; }
178 output(fbase, names, G, d);
180 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
182 m->mothurOutEndLine();
183 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
184 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
185 m->mothurOutEndLine();
189 catch(exception& e) {
190 m->errorOut(e, "PCACommand", "execute");
194 /*********************************************************************************************************************************/
196 inline double SIGN(const double a, const double b)
198 return b>=0 ? (a>=0 ? a:-a) : (a>=0 ? -a:a);
201 /*********************************************************************************************************************************/
203 void PCACommand::get_comment(istream& f, char begin, char end){
206 while(d != end){ d = f.get(); }
209 catch(exception& e) {
210 m->errorOut(e, "PCACommand", "get_comment");
215 /*********************************************************************************************************************************/
217 int PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list, vector<vector<double> >& d){
225 name_list.resize(rank);
228 for(int i=0;i<rank;i++)
230 for(int i=0;i<rank;i++) {
232 // cout << i << "\t" << name_list[i] << endl;
233 for(int j=0;j<rank;j++) {
234 if (m->control_pressed) { return 0; }
237 if (d[i][j] == -0.0000)
242 else if(square_m == 2){
243 for(int i=0;i<rank;i++){
248 for(int i=1;i<rank;i++){
251 for(int j=0;j<i;j++){
252 if (m->control_pressed) { return 0; }
254 if (d[i][j] == -0.0000)
263 catch(exception& e) {
264 m->errorOut(e, "PCACommand", "read_phylip");
270 /*********************************************************************************************************************************/
272 void PCACommand::read(string fname, vector<string>& names, vector<vector<double> >& D){
275 m->openInputFile(fname, f);
277 //check whether matrix is square
283 f >> numSeqs >> name;
285 while((d=f.get()) != EOF){
287 //is d a number meaning its square
293 //is d a line return meaning its lower triangle
301 //reopen to get back to beginning
302 m->openInputFile(fname, f);
303 read_phylip(f, q, names, D);
305 catch(exception& e) {
306 m->errorOut(e, "PCACommand", "read");
311 /*********************************************************************************************************************************/
313 double PCACommand::pythag(double a, double b) { return(pow(a*a+b*b,0.5)); }
315 /*********************************************************************************************************************************/
317 void PCACommand::matrix_mult(vector<vector<double> > first, vector<vector<double> > second, vector<vector<double> >& product){
319 int first_rows = first.size();
320 int first_cols = first[0].size();
321 int second_cols = second[0].size();
323 product.resize(first_rows);
324 for(int i=0;i<first_rows;i++){
325 product[i].resize(second_cols);
328 for(int i=0;i<first_rows;i++){
329 for(int j=0;j<second_cols;j++){
331 for(int k=0;k<first_cols;k++){
332 product[i][j] += first[i][k] * second[k][j];
337 catch(exception& e) {
338 m->errorOut(e, "PCACommand", "matrix_mult");
344 /*********************************************************************************************************************************/
346 void PCACommand::recenter(double offset, vector<vector<double> > D, vector<vector<double> >& G){
350 vector<vector<double> > A(rank);
351 vector<vector<double> > C(rank);
352 for(int i=0;i<rank;i++){
357 double scale = -1.0000 / (double) rank;
359 for(int i=0;i<rank;i++){
361 C[i][i] = 1.0000 + scale;
362 for(int j=i+1;j<rank;j++){
363 A[i][j] = A[j][i] = -0.5 * D[i][j] * D[i][j] + offset;
364 C[i][j] = C[j][i] = scale;
371 catch(exception& e) {
372 m->errorOut(e, "PCACommand", "recenter");
378 /*********************************************************************************************************************************/
380 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
382 void PCACommand::tred2(vector<vector<double> >& a, vector<double>& d, vector<double>& e){
384 double scale, hh, h, g, f;
391 for(int i=n-1;i>0;i--){
395 for(int k=0;k<l+1;k++){
396 scale += fabs(a[i][k]);
402 for(int k=0;k<l+1;k++){
404 h += a[i][k] * a[i][k];
407 g = (f >= 0.0 ? -sqrt(h) : sqrt(h));
412 for(int j=0;j<l+1;j++){
413 a[j][i] = a[i][j] / h;
415 for(int k=0;k<j+1;k++){
416 g += a[j][k] * a[i][k];
418 for(int k=j+1;k<l+1;k++){
419 g += a[k][j] * a[i][k];
425 for(int j=0;j<l+1;j++){
427 e[j] = g = e[j] - hh * f;
428 for(int k=0;k<j+1;k++){
429 a[j][k] -= (f * e[k] + g * a[i][k]);
444 for(int i=0;i<n;i++){
447 for(int j=0;j<l;j++){
449 for(int k=0;k<l;k++){
450 g += a[i][k] * a[k][j];
452 for(int k=0;k<l;k++){
453 a[k][j] -= g * a[k][i];
459 for(int j=0;j<l;j++){
460 a[j][i] = a[i][j] = 0.0;
464 catch(exception& e) {
465 m->errorOut(e, "PCACommand", "tred2");
471 /*********************************************************************************************************************************/
473 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
475 void PCACommand::qtli(vector<double>& d, vector<double>& e, vector<vector<double> >& z) {
478 double s, r, p, g, f, dd, c, b;
481 for(int i=1;i<=n;i++){
486 for(int l=0;l<n;l++){
490 dd = fabs(d[m]) + fabs(d[m+1]);
491 if(fabs(e[m])+dd == dd) break;
494 if(iter++ == 30) cerr << "Too many iterations in tqli\n";
495 g = (d[l+1]-d[l]) / (2.0 * e[l]);
497 g = d[m] - d[l] + e[l] / (g + SIGN(r,g));
503 e[i+1] = (r=pythag(f,g));
512 r = (d[i] - g) * s + 2.0 * c * b;
513 d[i+1] = g + ( p = s * r);
515 for(int k=0;k<n;k++){
517 z[k][i+1] = s * z[k][i] + c * f;
518 z[k][i] = c * z[k][i] - s * f;
521 if(r == 0.00 && i >= l) continue;
530 for(int i=0;i<n;i++){
532 for(int j=i;j<n;j++){
540 for(int j=0;j<n;j++){
548 catch(exception& e) {
549 m->errorOut(e, "PCACommand", "qtli");
554 /*********************************************************************************************************************************/
556 void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> > G, vector<double> d) {
558 int rank = name_list.size();
559 double dsum = 0.0000;
560 for(int i=0;i<rank;i++){
562 for(int j=0;j<rank;j++){
563 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
564 else { G[i][j] = 0.00000; }
568 ofstream pcaData((fnameRoot+"pcoa").c_str(), ios::trunc);
569 pcaData.setf(ios::fixed, ios::floatfield);
570 pcaData.setf(ios::showpoint);
571 outputNames.push_back(fnameRoot+"pcoa");
572 outputTypes["pcoa"].push_back(fnameRoot+"pcoa");
574 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
575 pcaLoadings.setf(ios::fixed, ios::floatfield);
576 pcaLoadings.setf(ios::showpoint);
577 outputNames.push_back(fnameRoot+"pcoa.loadings");
578 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");
580 pcaLoadings << "axis\tloading\n";
581 for(int i=0;i<rank;i++){
582 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
586 for(int i=0;i<rank;i++){
587 pcaData << '\t' << "axis" << i+1;
591 for(int i=0;i<rank;i++){
592 pcaData << name_list[i] << '\t';
593 for(int j=0;j<rank;j++){
594 pcaData << G[i][j] << '\t';
599 catch(exception& e) {
600 m->errorOut(e, "PCACommand", "output");
605 /*********************************************************************************************************************************/