]> git.donarmstrong.com Git - mothur.git/blobdiff - pcacommand.cpp
paralellized summary.shared
[mothur.git] / pcacommand.cpp
index ef89ae50cd43ad16101b4288931881027d03dcd6..51e3c319f377b893224e1e2f2d7093b3665c0d8a 100644 (file)
@@ -12,7 +12,7 @@
 
 //**********************************************************************************************************************
 
-PCACommand::PCACommand(string option){
+PCACommand::PCACommand(string option)  {
        try {
                abort = false;
                
@@ -21,56 +21,52 @@ PCACommand::PCACommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"phylip","lt"};
+                       string Array[] =  {"phylip","outputdir", "inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
                        map<string, string> parameters = parser. getParameters();
                        
                        ValidParameters validParameter;
+                       map<string, string>::iterator it;
                
                        //check to make sure all parameters are valid for command
-                       for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                       for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
-                       
+                       //if the user changes the input directory command factory will send this info to us in the output parameter 
+                       string inputDir = validParameter.validFile(parameters, "inputdir", false);              
+                       if (inputDir == "not found"){   inputDir = "";          }
+                       else {
+                               string path;
+                               it = parameters.find("phylip");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
+                               }
+                       }
+
                        //required parameters
                        phylipfile = validParameter.validFile(parameters, "phylip", true);
                        if (phylipfile == "not open") { abort = true; }
                        else if (phylipfile == "not found") { phylipfile = ""; abort = true; }  
                        else {  filename = phylipfile;  }
                        
-                       //columnfile = validParameter.validFile(parameters, "column", true);
-                       //if (columnfile == "not open") { abort = true; }       
-                       //else if (columnfile == "not found") { columnfile = ""; }
-                       //else {  format = "column";    }
-                       
-                       //namefile = validParameter.validFile(parameters, "name", true);
-                       //if (namefile == "not open") { abort = true; } 
-                       //else if (namefile == "not found") { namefile = ""; }
-                       
+                       //if the user changes the output directory command factory will send this info to us in the output parameter 
+                       outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
+                               outputDir = ""; 
+                               outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
+                       }
                        
                        //error checking on files       
-                       if (phylipfile == "")   { mothurOut("You must provide a distance file before running the pca command."); mothurOutEndLine(); abort = true; }            
-                       //if ((phylipfile == "") && (columnfile == "")) { mothurOut("You must provide a distance file before running the pca command."); mothurOutEndLine(); abort = true; }
-                       //else if ((phylipfile != "") && (columnfile != "")) { mothurOut("You may not use both the column and the phylip parameters."); mothurOutEndLine(); abort = true; }
-                       
-                       //if (columnfile != "") {
-                       //      if (namefile == "") {  mothurOut("You need to provide a namefile if you are going to use the column format."); mothurOutEndLine(); abort = true; }
-                       //}
-                       
-                       string temp = validParameter.validFile(parameters, "lt", false);                                if (temp == "not found") { temp = "false"; }
-                       bool lt = isTrue(temp);
-                       
-                       if (lt)         {  matrix = 2;  }
-                       else            {  matrix = 1;  }
-
-
+                       if (phylipfile == "")   { m->mothurOut("You must provide a distance file before running the pca command."); m->mothurOutEndLine(); abort = true; }              
                }
 
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "PCACommand");
+               m->errorOut(e, "PCACommand", "PCACommand");
                exit(1);
        }
 }
@@ -78,10 +74,10 @@ PCACommand::PCACommand(string option){
 void PCACommand::help(){
        try {
        
-               mothurOut("The pca command..."); mothurOutEndLine();
+               m->mothurOut("The pca command..."); m->mothurOutEndLine();
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "help");
+               m->errorOut(e, "PCACommand", "help");
                exit(1);
        }
 }
@@ -101,39 +97,44 @@ int PCACommand::execute(){
                vector<string> names;
                vector<vector<double> > D;
        
-               fbase = filename;
-               if(fbase.find_last_of(".")!=string::npos){
-                       fbase.erase(fbase.find_last_of(".")+1); 
-               }
-               else{
-                       fbase += ".";
-               }
-               read(filename, matrix, names, D);
+               fbase = outputDir + m->getRootName(m->getSimpleName(filename));
+               
+               read(filename, names, D);
+               
+               if (m->control_pressed) { return 0; }
        
                double offset = 0.0000;
                vector<double> d;
                vector<double> e;
                vector<vector<double> > G = D;
                vector<vector<double> > copy_G;
-               int rank = D.size();
+               //int rank = D.size();
                
-               cout << "\nProcessing...\n";
+               m->mothurOut("\nProcessing...\n");
                
                for(int count=0;count<2;count++){
-                       recenter(offset, D, G);   
-                       tred2(G, d, e);
-                       qtli(d, e, G);
+                       recenter(offset, D, G);         if (m->control_pressed) { return 0; }
+                       tred2(G, d, e);                         if (m->control_pressed) { return 0; }
+                       qtli(d, e, G);                          if (m->control_pressed) { return 0; }
                        offset = d[d.size()-1];
                        if(offset > 0.0) break;
                } 
                
+               if (m->control_pressed) { return 0; }
                
                output(fbase, names, G, d);
                
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
+               
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
+               m->mothurOutEndLine();
+               
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "execute");
+               m->errorOut(e, "PCACommand", "execute");
                exit(1);
        }
 }
@@ -153,66 +154,14 @@ void PCACommand::get_comment(istream& f, char begin, char end){
                d = f.peek();
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "get_comment");
+               m->errorOut(e, "PCACommand", "get_comment");
                exit(1);
        }
 }      
 
 /*********************************************************************************************************************************/
 
-void PCACommand::read_mega(istream& f, int square_m, vector<string>& name_list, vector<vector<double> >& d){
-       try {
-               get_comment(f, '#', '\n');
-               
-               char test = f.peek();
-               
-               while(test == '!'){                                                             //get header comments
-                       get_comment(f, '!', ';');
-                       while(isspace(test=f.get()))            {;}
-                       f.putback(test);
-                       test = f.peek();
-               }
-               while(test != '\n'){                                                    //get sequence names
-                       get_comment(f, '[', ']');
-                       char d = f.get();
-                       d = f.get();
-                       if(d == '#'){
-                               string name;
-                               f >> name;
-                               name_list.push_back(name);
-                               while(isspace(test=f.get()))            {;}
-                               f.putback(test);
-                       }
-                       else{
-                               break;
-                       }
-               }
-               int rank = name_list.size();
-               d.resize(rank);
-               for(int i=0;i<rank;i++){                d[i].resize(rank);              }
-               
-               d[0][0] = 0.0000;
-               get_comment(f, '[', ']');
-               for(int i=1;i<rank;i++){
-                       get_comment(f, '[', ']');
-                       d[i][i]=0.0000;
-                       for(int j=0;j<i;j++){
-                               f >> d[i][j];
-                               if (d[i][j] == -0.0000)
-                                       d[i][j] = 0.0000;
-                               d[j][i]=d[i][j];
-                       }
-               }
-       }
-       catch(exception& e) {
-               errorOut(e, "PCACommand", "read_mega");
-               exit(1);
-       }
-}
-
-/*********************************************************************************************************************************/
-
-void PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list, vector<vector<double> >& d){
+int PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list, vector<vector<double> >& d){
        try {
                //     int count1=0;
                //     int count2=0;
@@ -229,6 +178,8 @@ void PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list
                                f >> name_list[i];
                                //                      cout << i << "\t" << name_list[i] << endl;
                                for(int j=0;j<rank;j++) {
+                                       if (m->control_pressed) { return 0; }
+                                       
                                        f >> d[i][j];
                                        if (d[i][j] == -0.0000)
                                                d[i][j] = 0.0000;
@@ -245,6 +196,7 @@ void PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list
                                f >> name_list[i];
                                d[i][i]=0.0000;
                                for(int j=0;j<i;j++){
+                                       if (m->control_pressed) { return 0; }
                                        f >> d[i][j];
                                        if (d[i][j] == -0.0000)
                                                d[i][j] = 0.0000;
@@ -252,9 +204,11 @@ void PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list
                                }
                        }
                }
+               
+               return 0;
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "read_phylip");
+               m->errorOut(e, "PCACommand", "read_phylip");
                exit(1);
        }
 
@@ -262,34 +216,49 @@ void PCACommand::read_phylip(istream& f, int square_m, vector<string>& name_list
 
 /*********************************************************************************************************************************/
 
-void PCACommand::read(string fname, int m, vector<string>& names, vector<vector<double> >& D){
+void PCACommand::read(string fname, vector<string>& names, vector<vector<double> >& D){
        try {
-               ifstream f(fname.c_str());
-               if(!f) {
-                       cerr << "Error: Could not open " << fname << endl;
-                       exit(1);
-               }
-               char test = f.peek();
+               ifstream f;
+               m->openInputFile(fname, f);
+                       
+               //check whether matrix is square
+               char d;
+               int q = 1;
+               int numSeqs;
+               string name;
                
-               if(test == '#'){
-                       read_mega(f, m, names, D);
-               }
-               else{
-                       read_phylip(f, m, names, D);
+               f >> numSeqs >> name; 
+               
+               while((d=f.get()) != EOF){
+                       
+                       //is d a number meaning its square
+                       if(isalnum(d)){ 
+                               q = 1; 
+                               break; 
+                       }
+                       
+                       //is d a line return meaning its lower triangle
+                       if(d == '\n'){
+                               q = 2;
+                               break;
+                       }
                }
+               f.close();
                
-               int rank = D.size();
+               //reopen to get back to beginning
+               m->openInputFile(fname, f);                     
+               read_phylip(f, q, names, D);
        }
-       catch(exception& e) {
-               errorOut(e, "PCACommand", "read");
+               catch(exception& e) {
+               m->errorOut(e, "PCACommand", "read");
                exit(1);
        }
 }
 
 /*********************************************************************************************************************************/
-double PCACommand::pythag(double a, double b){
-    return(pow(a*a+b*b,0.5));
-}
+
+double PCACommand::pythag(double a, double b)  {       return(pow(a*a+b*b,0.5));       }
+
 /*********************************************************************************************************************************/
 
 void PCACommand::matrix_mult(vector<vector<double> > first, vector<vector<double> > second, vector<vector<double> >& product){
@@ -313,7 +282,7 @@ void PCACommand::matrix_mult(vector<vector<double> > first, vector<vector<double
                }
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "matrix_mult");
+               m->errorOut(e, "PCACommand", "matrix_mult");
                exit(1);
        }
 
@@ -347,7 +316,7 @@ void PCACommand::recenter(double offset, vector<vector<double> > D, vector<vecto
                matrix_mult(A,C,G);
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "recenter");
+               m->errorOut(e, "PCACommand", "recenter");
                exit(1);
        }
 
@@ -440,7 +409,7 @@ void PCACommand::tred2(vector<vector<double> >& a, vector<double>& d, vector<dou
                }
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "tred2");
+               m->errorOut(e, "PCACommand", "tred2");
                exit(1);
        }
 
@@ -524,7 +493,7 @@ void PCACommand::qtli(vector<double>& d, vector<double>& e, vector<vector<double
                }
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "qtli");
+               m->errorOut(e, "PCACommand", "qtli");
                exit(1);
        }
 }
@@ -543,20 +512,22 @@ void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vecto
                        }
                }
                
-               ofstream pcaData((fnameRoot+"pca").c_str(), ios::trunc);
+               ofstream pcaData((fnameRoot+"pcoa").c_str(), ios::trunc);
                pcaData.setf(ios::fixed, ios::floatfield);
                pcaData.setf(ios::showpoint);   
+               outputNames.push_back(fnameRoot+"pcoa");
                
-               ofstream pcaLoadings((fnameRoot+"pca.loadings").c_str(), ios::trunc);
+               ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
                pcaLoadings.setf(ios::fixed, ios::floatfield);
-               pcaLoadings.setf(ios::showpoint);       
+               pcaLoadings.setf(ios::showpoint);
+               outputNames.push_back(fnameRoot+"pcoa.loadings");       
                
                pcaLoadings << "axis\tloading\n";
                for(int i=0;i<rank;i++){
                        pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
                }
                
-               pcaData << "SeqName";
+               pcaData << "group";
                for(int i=0;i<rank;i++){
                        pcaData << '\t' << "axis" << i+1;
                }
@@ -571,27 +542,10 @@ void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vecto
                }
        }
        catch(exception& e) {
-               errorOut(e, "PCACommand", "output");
+               m->errorOut(e, "PCACommand", "output");
                exit(1);
        }
 }
 
 /*********************************************************************************************************************************/
 
-void PCACommand::print_matrix(vector<vector<double> > A) {
-       try {
-               int rank = A.size();
-               for(int i=0;i<rank;i++){
-                       for(int j=0;j<rank;j++){
-                               cout << A[i][j] << "  ";
-                       }
-                       cout << endl;
-               }
-       }
-       catch(exception& e) {
-               errorOut(e, "PCACommand", "print_matrix");
-               exit(1);
-       }
-}
-/*********************************************************************************************************************************/
-