6 * Created by westcott on 1/4/10.
7 * Copyright 2010 Schloss Lab. All rights reserved.
11 #include "pcacommand.h"
13 //**********************************************************************************************************************
15 PCACommand::PCACommand(string option){
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"phylip"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string, string> parameters = parser. getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 phylipfile = validParameter.validFile(parameters, "phylip", true);
39 if (phylipfile == "not open") { abort = true; }
40 else if (phylipfile == "not found") { phylipfile = ""; abort = true; }
41 else { filename = phylipfile; }
43 //columnfile = validParameter.validFile(parameters, "column", true);
44 //if (columnfile == "not open") { abort = true; }
45 //else if (columnfile == "not found") { columnfile = ""; }
46 //else { format = "column"; }
48 //namefile = validParameter.validFile(parameters, "name", true);
49 //if (namefile == "not open") { abort = true; }
50 //else if (namefile == "not found") { namefile = ""; }
53 //error checking on files
54 if (phylipfile == "") { mothurOut("You must provide a distance file before running the pca command."); mothurOutEndLine(); abort = true; }
55 //if ((phylipfile == "") && (columnfile == "")) { mothurOut("You must provide a distance file before running the pca command."); mothurOutEndLine(); abort = true; }
56 //else if ((phylipfile != "") && (columnfile != "")) { mothurOut("You may not use both the column and the phylip parameters."); mothurOutEndLine(); abort = true; }
58 //if (columnfile != "") {
59 // if (namefile == "") { mothurOut("You need to provide a namefile if you are going to use the column format."); mothurOutEndLine(); abort = true; }
62 //string temp = validParameter.validFile(parameters, "lt", false); if (temp == "not found") { temp = "false"; }
63 //bool lt = isTrue(temp);
65 //if (lt) { matrix = 2; }
66 //else { matrix = 1; }
73 errorOut(e, "PCACommand", "PCACommand");
77 //**********************************************************************************************************************
78 void PCACommand::help(){
81 mothurOut("The pca command..."); mothurOutEndLine();
84 errorOut(e, "PCACommand", "help");
88 //**********************************************************************************************************************
89 PCACommand::~PCACommand(){}
90 //**********************************************************************************************************************
91 int PCACommand::execute(){
94 if (abort == true) { return 0; }
96 cout.setf(ios::fixed, ios::floatfield);
97 cout.setf(ios::showpoint);
98 cerr.setf(ios::fixed, ios::floatfield);
99 cerr.setf(ios::showpoint);
101 vector<string> names;
102 vector<vector<double> > D;
105 if(fbase.find_last_of(".")!=string::npos){
106 fbase.erase(fbase.find_last_of(".")+1);
111 read(filename, names, D);
113 double offset = 0.0000;
116 vector<vector<double> > G = D;
117 vector<vector<double> > copy_G;
118 //int rank = D.size();
120 cout << "\nProcessing...\n";
122 for(int count=0;count<2;count++){
123 recenter(offset, D, G);
126 offset = d[d.size()-1];
127 if(offset > 0.0) break;
131 output(fbase, names, G, d);
135 catch(exception& e) {
136 errorOut(e, "PCACommand", "execute");
140 /*********************************************************************************************************************************/
142 inline double SIGN(const double a, const double b)
144 return b>=0 ? (a>=0 ? a:-a) : (a>=0 ? -a:a);
147 /*********************************************************************************************************************************/
149 void PCACommand::get_comment(istream& f, char begin, char end){
152 while(d != end){ d = f.get(); }
155 catch(exception& e) {
156 errorOut(e, "PCACommand", "get_comment");
161 /*********************************************************************************************************************************/
163 void PCACommand::read_mega(istream& f, vector<string>& name_list, vector<vector<double> >& d){
165 get_comment(f, '#', '\n');
167 char test = f.peek();
169 while(test == '!'){ //get header comments
170 get_comment(f, '!', ';');
171 while(isspace(test=f.get())) {;}
175 while(test != '\n'){ //get sequence names
176 get_comment(f, '[', ']');
182 name_list.push_back(name);
183 while(isspace(test=f.get())) {;}
190 int rank = name_list.size();
192 for(int i=0;i<rank;i++){ d[i].resize(rank); }
195 get_comment(f, '[', ']');
196 for(int i=1;i<rank;i++){
197 get_comment(f, '[', ']');
199 for(int j=0;j<i;j++){
201 if (d[i][j] == -0.0000)
207 catch(exception& e) {
208 errorOut(e, "PCACommand", "read_mega");
213 /*********************************************************************************************************************************/
215 void PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list, vector<vector<double> >& d){
223 name_list.resize(rank);
226 for(int i=0;i<rank;i++)
228 for(int i=0;i<rank;i++) {
230 // cout << i << "\t" << name_list[i] << endl;
231 for(int j=0;j<rank;j++) {
233 if (d[i][j] == -0.0000)
238 else if(square_m == 2){
239 for(int i=0;i<rank;i++){
244 for(int i=1;i<rank;i++){
247 for(int j=0;j<i;j++){
249 if (d[i][j] == -0.0000)
256 catch(exception& e) {
257 errorOut(e, "PCACommand", "read_phylip");
263 /*********************************************************************************************************************************/
265 void PCACommand::read(string fname, vector<string>& names, vector<vector<double> >& D){
268 openInputFile(fname, f);
270 char test = f.peek();
273 read_mega(f, names, D);
276 //check whether matrix is square
282 f >> numSeqs >> name;
284 while((d=f.get()) != EOF){
286 //is d a number meaning its square
292 //is d a line return meaning its lower triangle
300 //reopen to get back to beginning
301 openInputFile(fname, f);
302 read_phylip(f, m, names, D);
305 //int rank = D.size();
307 catch(exception& e) {
308 errorOut(e, "PCACommand", "read");
313 /*********************************************************************************************************************************/
314 double PCACommand::pythag(double a, double b){
315 return(pow(a*a+b*b,0.5));
317 /*********************************************************************************************************************************/
319 void PCACommand::matrix_mult(vector<vector<double> > first, vector<vector<double> > second, vector<vector<double> >& product){
321 int first_rows = first.size();
322 int first_cols = first[0].size();
323 int second_cols = second[0].size();
325 product.resize(first_rows);
326 for(int i=0;i<first_rows;i++){
327 product[i].resize(second_cols);
330 for(int i=0;i<first_rows;i++){
331 for(int j=0;j<second_cols;j++){
333 for(int k=0;k<first_cols;k++){
334 product[i][j] += first[i][k] * second[k][j];
339 catch(exception& e) {
340 errorOut(e, "PCACommand", "matrix_mult");
346 /*********************************************************************************************************************************/
348 void PCACommand::recenter(double offset, vector<vector<double> > D, vector<vector<double> >& G){
352 vector<vector<double> > A(rank);
353 vector<vector<double> > C(rank);
354 for(int i=0;i<rank;i++){
359 double scale = -1.0000 / (double) rank;
361 for(int i=0;i<rank;i++){
363 C[i][i] = 1.0000 + scale;
364 for(int j=i+1;j<rank;j++){
365 A[i][j] = A[j][i] = -0.5 * D[i][j] * D[i][j] + offset;
366 C[i][j] = C[j][i] = scale;
373 catch(exception& e) {
374 errorOut(e, "PCACommand", "recenter");
380 /*********************************************************************************************************************************/
382 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
384 void PCACommand::tred2(vector<vector<double> >& a, vector<double>& d, vector<double>& e){
386 double scale, hh, h, g, f;
393 for(int i=n-1;i>0;i--){
397 for(int k=0;k<l+1;k++){
398 scale += fabs(a[i][k]);
404 for(int k=0;k<l+1;k++){
406 h += a[i][k] * a[i][k];
409 g = (f >= 0.0 ? -sqrt(h) : sqrt(h));
414 for(int j=0;j<l+1;j++){
415 a[j][i] = a[i][j] / h;
417 for(int k=0;k<j+1;k++){
418 g += a[j][k] * a[i][k];
420 for(int k=j+1;k<l+1;k++){
421 g += a[k][j] * a[i][k];
427 for(int j=0;j<l+1;j++){
429 e[j] = g = e[j] - hh * f;
430 for(int k=0;k<j+1;k++){
431 a[j][k] -= (f * e[k] + g * a[i][k]);
446 for(int i=0;i<n;i++){
449 for(int j=0;j<l;j++){
451 for(int k=0;k<l;k++){
452 g += a[i][k] * a[k][j];
454 for(int k=0;k<l;k++){
455 a[k][j] -= g * a[k][i];
461 for(int j=0;j<l;j++){
462 a[j][i] = a[i][j] = 0.0;
466 catch(exception& e) {
467 errorOut(e, "PCACommand", "tred2");
473 /*********************************************************************************************************************************/
475 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
477 void PCACommand::qtli(vector<double>& d, vector<double>& e, vector<vector<double> >& z) {
480 double s, r, p, g, f, dd, c, b;
483 for(int i=1;i<=n;i++){
488 for(int l=0;l<n;l++){
492 dd = fabs(d[m]) + fabs(d[m+1]);
493 if(fabs(e[m])+dd == dd) break;
496 if(iter++ == 30) cerr << "Too many iterations in tqli\n";
497 g = (d[l+1]-d[l]) / (2.0 * e[l]);
499 g = d[m] - d[l] + e[l] / (g + SIGN(r,g));
505 e[i+1] = (r=pythag(f,g));
514 r = (d[i] - g) * s + 2.0 * c * b;
515 d[i+1] = g + ( p = s * r);
517 for(int k=0;k<n;k++){
519 z[k][i+1] = s * z[k][i] + c * f;
520 z[k][i] = c * z[k][i] - s * f;
523 if(r == 0.00 && i >= l) continue;
532 for(int i=0;i<n;i++){
534 for(int j=i;j<n;j++){
542 for(int j=0;j<n;j++){
550 catch(exception& e) {
551 errorOut(e, "PCACommand", "qtli");
556 /*********************************************************************************************************************************/
558 void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> > G, vector<double> d) {
560 int rank = name_list.size();
561 double dsum = 0.0000;
562 for(int i=0;i<rank;i++){
564 for(int j=0;j<rank;j++){
565 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
566 else { G[i][j] = 0.00000; }
570 ofstream pcaData((fnameRoot+"pca").c_str(), ios::trunc);
571 pcaData.setf(ios::fixed, ios::floatfield);
572 pcaData.setf(ios::showpoint);
574 ofstream pcaLoadings((fnameRoot+"pca.loadings").c_str(), ios::trunc);
575 pcaLoadings.setf(ios::fixed, ios::floatfield);
576 pcaLoadings.setf(ios::showpoint);
578 pcaLoadings << "axis\tloading\n";
579 for(int i=0;i<rank;i++){
580 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
583 pcaData << "SeqName";
584 for(int i=0;i<rank;i++){
585 pcaData << '\t' << "axis" << i+1;
589 for(int i=0;i<rank;i++){
590 pcaData << name_list[i] << '\t';
591 for(int j=0;j<rank;j++){
592 pcaData << G[i][j] << '\t';
597 catch(exception& e) {
598 errorOut(e, "PCACommand", "output");
603 /*********************************************************************************************************************************/
605 void PCACommand::print_matrix(vector<vector<double> > A) {
608 for(int i=0;i<rank;i++){
609 for(int j=0;j<rank;j++){
610 cout << A[i][j] << " ";
615 catch(exception& e) {
616 errorOut(e, "PCACommand", "print_matrix");
620 /*********************************************************************************************************************************/