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","lt"};
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; }
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, matrix, names, D);
113 double offset = 0.0000;
116 vector<vector<double> > G = D;
117 vector<vector<double> > copy_G;
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, int square_m, 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, int m, vector<string>& names, vector<vector<double> >& D){
267 ifstream f(fname.c_str());
269 cerr << "Error: Could not open " << fname << endl;
272 char test = f.peek();
275 read_mega(f, m, names, D);
278 read_phylip(f, m, names, D);
283 catch(exception& e) {
284 errorOut(e, "PCACommand", "read");
289 /*********************************************************************************************************************************/
290 double PCACommand::pythag(double a, double b){
291 return(pow(a*a+b*b,0.5));
293 /*********************************************************************************************************************************/
295 void PCACommand::matrix_mult(vector<vector<double> > first, vector<vector<double> > second, vector<vector<double> >& product){
297 int first_rows = first.size();
298 int first_cols = first[0].size();
299 int second_cols = second[0].size();
301 product.resize(first_rows);
302 for(int i=0;i<first_rows;i++){
303 product[i].resize(second_cols);
306 for(int i=0;i<first_rows;i++){
307 for(int j=0;j<second_cols;j++){
309 for(int k=0;k<first_cols;k++){
310 product[i][j] += first[i][k] * second[k][j];
315 catch(exception& e) {
316 errorOut(e, "PCACommand", "matrix_mult");
322 /*********************************************************************************************************************************/
324 void PCACommand::recenter(double offset, vector<vector<double> > D, vector<vector<double> >& G){
328 vector<vector<double> > A(rank);
329 vector<vector<double> > C(rank);
330 for(int i=0;i<rank;i++){
335 double scale = -1.0000 / (double) rank;
337 for(int i=0;i<rank;i++){
339 C[i][i] = 1.0000 + scale;
340 for(int j=i+1;j<rank;j++){
341 A[i][j] = A[j][i] = -0.5 * D[i][j] * D[i][j] + offset;
342 C[i][j] = C[j][i] = scale;
349 catch(exception& e) {
350 errorOut(e, "PCACommand", "recenter");
356 /*********************************************************************************************************************************/
358 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
360 void PCACommand::tred2(vector<vector<double> >& a, vector<double>& d, vector<double>& e){
362 double scale, hh, h, g, f;
369 for(int i=n-1;i>0;i--){
373 for(int k=0;k<l+1;k++){
374 scale += fabs(a[i][k]);
380 for(int k=0;k<l+1;k++){
382 h += a[i][k] * a[i][k];
385 g = (f >= 0.0 ? -sqrt(h) : sqrt(h));
390 for(int j=0;j<l+1;j++){
391 a[j][i] = a[i][j] / h;
393 for(int k=0;k<j+1;k++){
394 g += a[j][k] * a[i][k];
396 for(int k=j+1;k<l+1;k++){
397 g += a[k][j] * a[i][k];
403 for(int j=0;j<l+1;j++){
405 e[j] = g = e[j] - hh * f;
406 for(int k=0;k<j+1;k++){
407 a[j][k] -= (f * e[k] + g * a[i][k]);
422 for(int i=0;i<n;i++){
425 for(int j=0;j<l;j++){
427 for(int k=0;k<l;k++){
428 g += a[i][k] * a[k][j];
430 for(int k=0;k<l;k++){
431 a[k][j] -= g * a[k][i];
437 for(int j=0;j<l;j++){
438 a[j][i] = a[i][j] = 0.0;
442 catch(exception& e) {
443 errorOut(e, "PCACommand", "tred2");
449 /*********************************************************************************************************************************/
451 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
453 void PCACommand::qtli(vector<double>& d, vector<double>& e, vector<vector<double> >& z) {
456 double s, r, p, g, f, dd, c, b;
459 for(int i=1;i<=n;i++){
464 for(int l=0;l<n;l++){
468 dd = fabs(d[m]) + fabs(d[m+1]);
469 if(fabs(e[m])+dd == dd) break;
472 if(iter++ == 30) cerr << "Too many iterations in tqli\n";
473 g = (d[l+1]-d[l]) / (2.0 * e[l]);
475 g = d[m] - d[l] + e[l] / (g + SIGN(r,g));
481 e[i+1] = (r=pythag(f,g));
490 r = (d[i] - g) * s + 2.0 * c * b;
491 d[i+1] = g + ( p = s * r);
493 for(int k=0;k<n;k++){
495 z[k][i+1] = s * z[k][i] + c * f;
496 z[k][i] = c * z[k][i] - s * f;
499 if(r == 0.00 && i >= l) continue;
508 for(int i=0;i<n;i++){
510 for(int j=i;j<n;j++){
518 for(int j=0;j<n;j++){
526 catch(exception& e) {
527 errorOut(e, "PCACommand", "qtli");
532 /*********************************************************************************************************************************/
534 void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> > G, vector<double> d) {
536 int rank = name_list.size();
537 double dsum = 0.0000;
538 for(int i=0;i<rank;i++){
540 for(int j=0;j<rank;j++){
541 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
542 else { G[i][j] = 0.00000; }
546 ofstream pcaData((fnameRoot+"pca").c_str(), ios::trunc);
547 pcaData.setf(ios::fixed, ios::floatfield);
548 pcaData.setf(ios::showpoint);
550 ofstream pcaLoadings((fnameRoot+"pca.loadings").c_str(), ios::trunc);
551 pcaLoadings.setf(ios::fixed, ios::floatfield);
552 pcaLoadings.setf(ios::showpoint);
554 pcaLoadings << "axis\tloading\n";
555 for(int i=0;i<rank;i++){
556 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
559 pcaData << "SeqName";
560 for(int i=0;i<rank;i++){
561 pcaData << '\t' << "axis" << i+1;
565 for(int i=0;i<rank;i++){
566 pcaData << name_list[i] << '\t';
567 for(int j=0;j<rank;j++){
568 pcaData << G[i][j] << '\t';
573 catch(exception& e) {
574 errorOut(e, "PCACommand", "output");
579 /*********************************************************************************************************************************/
581 void PCACommand::print_matrix(vector<vector<double> > A) {
584 for(int i=0;i<rank;i++){
585 for(int j=0;j<rank;j++){
586 cout << A[i][j] << " ";
591 catch(exception& e) {
592 errorOut(e, "PCACommand", "print_matrix");
596 /*********************************************************************************************************************************/