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(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["pcoa"] = tempOutNames;
32 outputTypes["loadings"] = tempOutNames;
35 m->errorOut(e, "PCACommand", "PCACommand");
39 //**********************************************************************************************************************
40 vector<string> PCACommand::getRequiredParameters(){
42 string Array[] = {"phylip"};
43 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
47 m->errorOut(e, "PCACommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> PCACommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "PCACommand", "getRequiredFiles");
62 //**********************************************************************************************************************
64 PCACommand::PCACommand(string option) {
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"phylip","outputdir", "inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string, string> parameters = parser. getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
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; }
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 = ""; }
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; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["pcoa"] = tempOutNames;
103 outputTypes["loadings"] = tempOutNames;
105 //required parameters
106 phylipfile = validParameter.validFile(parameters, "phylip", true);
107 if (phylipfile == "not open") { abort = true; }
108 else if (phylipfile == "not found") { phylipfile = ""; abort = true; }
109 else { filename = phylipfile; }
111 //if the user changes the output directory command factory will send this info to us in the output parameter
112 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
114 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
117 //error checking on files
118 if (phylipfile == "") { m->mothurOut("You must provide a distance file before running the pca command."); m->mothurOutEndLine(); abort = true; }
122 catch(exception& e) {
123 m->errorOut(e, "PCACommand", "PCACommand");
127 //**********************************************************************************************************************
128 void PCACommand::help(){
131 m->mothurOut("The pca command..."); m->mothurOutEndLine();
133 catch(exception& e) {
134 m->errorOut(e, "PCACommand", "help");
138 //**********************************************************************************************************************
139 PCACommand::~PCACommand(){}
140 //**********************************************************************************************************************
141 int PCACommand::execute(){
144 if (abort == true) { return 0; }
146 cout.setf(ios::fixed, ios::floatfield);
147 cout.setf(ios::showpoint);
148 cerr.setf(ios::fixed, ios::floatfield);
149 cerr.setf(ios::showpoint);
151 vector<string> names;
152 vector<vector<double> > D;
154 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
156 read(filename, names, D);
158 if (m->control_pressed) { return 0; }
160 double offset = 0.0000;
163 vector<vector<double> > G = D;
164 vector<vector<double> > copy_G;
165 //int rank = D.size();
167 m->mothurOut("\nProcessing...\n");
169 for(int count=0;count<2;count++){
170 recenter(offset, D, G); if (m->control_pressed) { return 0; }
171 tred2(G, d, e); if (m->control_pressed) { return 0; }
172 qtli(d, e, G); if (m->control_pressed) { return 0; }
173 offset = d[d.size()-1];
174 if(offset > 0.0) break;
177 if (m->control_pressed) { return 0; }
179 output(fbase, names, G, d);
181 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
183 m->mothurOutEndLine();
184 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
185 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
186 m->mothurOutEndLine();
190 catch(exception& e) {
191 m->errorOut(e, "PCACommand", "execute");
195 /*********************************************************************************************************************************/
197 inline double SIGN(const double a, const double b)
199 return b>=0 ? (a>=0 ? a:-a) : (a>=0 ? -a:a);
202 /*********************************************************************************************************************************/
204 void PCACommand::get_comment(istream& f, char begin, char end){
207 while(d != end){ d = f.get(); }
210 catch(exception& e) {
211 m->errorOut(e, "PCACommand", "get_comment");
216 /*********************************************************************************************************************************/
218 int PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list, vector<vector<double> >& d){
226 name_list.resize(rank);
229 for(int i=0;i<rank;i++)
231 for(int i=0;i<rank;i++) {
233 // cout << i << "\t" << name_list[i] << endl;
234 for(int j=0;j<rank;j++) {
235 if (m->control_pressed) { return 0; }
238 if (d[i][j] == -0.0000)
243 else if(square_m == 2){
244 for(int i=0;i<rank;i++){
249 for(int i=1;i<rank;i++){
252 for(int j=0;j<i;j++){
253 if (m->control_pressed) { return 0; }
255 if (d[i][j] == -0.0000)
264 catch(exception& e) {
265 m->errorOut(e, "PCACommand", "read_phylip");
271 /*********************************************************************************************************************************/
273 void PCACommand::read(string fname, vector<string>& names, vector<vector<double> >& D){
276 m->openInputFile(fname, f);
278 //check whether matrix is square
284 f >> numSeqs >> name;
286 while((d=f.get()) != EOF){
288 //is d a number meaning its square
294 //is d a line return meaning its lower triangle
302 //reopen to get back to beginning
303 m->openInputFile(fname, f);
304 read_phylip(f, q, names, D);
306 catch(exception& e) {
307 m->errorOut(e, "PCACommand", "read");
312 /*********************************************************************************************************************************/
314 double PCACommand::pythag(double a, double b) { return(pow(a*a+b*b,0.5)); }
316 /*********************************************************************************************************************************/
318 void PCACommand::matrix_mult(vector<vector<double> > first, vector<vector<double> > second, vector<vector<double> >& product){
320 int first_rows = first.size();
321 int first_cols = first[0].size();
322 int second_cols = second[0].size();
324 product.resize(first_rows);
325 for(int i=0;i<first_rows;i++){
326 product[i].resize(second_cols);
329 for(int i=0;i<first_rows;i++){
330 for(int j=0;j<second_cols;j++){
332 for(int k=0;k<first_cols;k++){
333 product[i][j] += first[i][k] * second[k][j];
338 catch(exception& e) {
339 m->errorOut(e, "PCACommand", "matrix_mult");
345 /*********************************************************************************************************************************/
347 void PCACommand::recenter(double offset, vector<vector<double> > D, vector<vector<double> >& G){
351 vector<vector<double> > A(rank);
352 vector<vector<double> > C(rank);
353 for(int i=0;i<rank;i++){
358 double scale = -1.0000 / (double) rank;
360 for(int i=0;i<rank;i++){
362 C[i][i] = 1.0000 + scale;
363 for(int j=i+1;j<rank;j++){
364 A[i][j] = A[j][i] = -0.5 * D[i][j] * D[i][j] + offset;
365 C[i][j] = C[j][i] = scale;
372 catch(exception& e) {
373 m->errorOut(e, "PCACommand", "recenter");
379 /*********************************************************************************************************************************/
381 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
383 void PCACommand::tred2(vector<vector<double> >& a, vector<double>& d, vector<double>& e){
385 double scale, hh, h, g, f;
392 for(int i=n-1;i>0;i--){
396 for(int k=0;k<l+1;k++){
397 scale += fabs(a[i][k]);
403 for(int k=0;k<l+1;k++){
405 h += a[i][k] * a[i][k];
408 g = (f >= 0.0 ? -sqrt(h) : sqrt(h));
413 for(int j=0;j<l+1;j++){
414 a[j][i] = a[i][j] / h;
416 for(int k=0;k<j+1;k++){
417 g += a[j][k] * a[i][k];
419 for(int k=j+1;k<l+1;k++){
420 g += a[k][j] * a[i][k];
426 for(int j=0;j<l+1;j++){
428 e[j] = g = e[j] - hh * f;
429 for(int k=0;k<j+1;k++){
430 a[j][k] -= (f * e[k] + g * a[i][k]);
445 for(int i=0;i<n;i++){
448 for(int j=0;j<l;j++){
450 for(int k=0;k<l;k++){
451 g += a[i][k] * a[k][j];
453 for(int k=0;k<l;k++){
454 a[k][j] -= g * a[k][i];
460 for(int j=0;j<l;j++){
461 a[j][i] = a[i][j] = 0.0;
465 catch(exception& e) {
466 m->errorOut(e, "PCACommand", "tred2");
472 /*********************************************************************************************************************************/
474 // This function is taken from Numerical Recipes in C++ by Press et al., 2nd edition, pg. 479
476 void PCACommand::qtli(vector<double>& d, vector<double>& e, vector<vector<double> >& z) {
479 double s, r, p, g, f, dd, c, b;
482 for(int i=1;i<=n;i++){
487 for(int l=0;l<n;l++){
491 dd = fabs(d[m]) + fabs(d[m+1]);
492 if(fabs(e[m])+dd == dd) break;
495 if(iter++ == 30) cerr << "Too many iterations in tqli\n";
496 g = (d[l+1]-d[l]) / (2.0 * e[l]);
498 g = d[m] - d[l] + e[l] / (g + SIGN(r,g));
504 e[i+1] = (r=pythag(f,g));
513 r = (d[i] - g) * s + 2.0 * c * b;
514 d[i+1] = g + ( p = s * r);
516 for(int k=0;k<n;k++){
518 z[k][i+1] = s * z[k][i] + c * f;
519 z[k][i] = c * z[k][i] - s * f;
522 if(r == 0.00 && i >= l) continue;
531 for(int i=0;i<n;i++){
533 for(int j=i;j<n;j++){
541 for(int j=0;j<n;j++){
549 catch(exception& e) {
550 m->errorOut(e, "PCACommand", "qtli");
555 /*********************************************************************************************************************************/
557 void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> > G, vector<double> d) {
559 int rank = name_list.size();
560 double dsum = 0.0000;
561 for(int i=0;i<rank;i++){
563 for(int j=0;j<rank;j++){
564 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
565 else { G[i][j] = 0.00000; }
569 ofstream pcaData((fnameRoot+"pcoa").c_str(), ios::trunc);
570 pcaData.setf(ios::fixed, ios::floatfield);
571 pcaData.setf(ios::showpoint);
572 outputNames.push_back(fnameRoot+"pcoa");
573 outputTypes["pcoa"].push_back(fnameRoot+"pcoa");
575 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
576 pcaLoadings.setf(ios::fixed, ios::floatfield);
577 pcaLoadings.setf(ios::showpoint);
578 outputNames.push_back(fnameRoot+"pcoa.loadings");
579 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");
581 pcaLoadings << "axis\tloading\n";
582 for(int i=0;i<rank;i++){
583 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
587 for(int i=0;i<rank;i++){
588 pcaData << '\t' << "axis" << i+1;
592 for(int i=0;i<rank;i++){
593 pcaData << name_list[i] << '\t';
594 for(int j=0;j<rank;j++){
595 pcaData << G[i][j] << '\t';
600 catch(exception& e) {
601 m->errorOut(e, "PCACommand", "output");
606 /*********************************************************************************************************************************/