]> git.donarmstrong.com Git - mothur.git/blobdiff - pcacommand.cpp
pat's differences before v.1.8
[mothur.git] / pcacommand.cpp
index 09324c9d3eaf3dd5a525a44cf233abe7df6e7285..c17053e29cb31812f681333078199c9abaea02fa 100644 (file)
@@ -21,51 +21,47 @@ PCACommand::PCACommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"phylip"};
+                       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 = 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 += 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;  }
-
-
                }
 
        }
@@ -101,13 +97,16 @@ 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 += ".";
-               }
+               //fbase = filename;
+               //if(fbase.find_last_of(".")!=string::npos){
+               //      fbase.erase(fbase.find_last_of(".")+1); 
+               //}
+               //else{
+               //      fbase += ".";
+               //}
+               
+               fbase = outputDir + getRootName(getSimpleName(filename));
+               
                read(filename, names, D);
        
                double offset = 0.0000;
@@ -160,58 +159,6 @@ void PCACommand::get_comment(istream& f, char begin, char end){
 
 /*********************************************************************************************************************************/
 
-void PCACommand::read_mega(istream& f, 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){
        try {
                //     int count1=0;
@@ -266,54 +213,45 @@ void PCACommand::read(string fname, vector<string>& names, vector<vector<double>
        try {
                ifstream f;
                openInputFile(fname, f);
+                       
+               //check whether matrix is square
+               char d;
+               int m = 1;
+               int numSeqs;
+               string name;
                
-               char test = f.peek();
+               f >> numSeqs >> name; 
                
-               if(test == '#'){
-                       read_mega(f, names, D);
-               }
-               else{
-                       //check whether matrix is square
-                       char d;
-                       int m = 1;
-                       int numSeqs;
-                       string name;
+               while((d=f.get()) != EOF){
                        
-                       f >> numSeqs >> name; 
-                       
-                       while((d=f.get()) != EOF){
-                               
-                               //is d a number meaning its square
-                               if(isalnum(d)){ 
-                                       m = 1; 
-                                       break; 
-                               }
-                               
-                               //is d a line return meaning its lower triangle
-                               if(d == '\n'){
-                                       m = 2;
-                                       break;
-                               }
+                       //is d a number meaning its square
+                       if(isalnum(d)){ 
+                               m = 1; 
+                               break; 
                        }
-                       f.close();
                        
-                       //reopen to get back to beginning
-                       openInputFile(fname, f);                        
-                       read_phylip(f, m, names, D);
+                       //is d a line return meaning its lower triangle
+                       if(d == '\n'){
+                               m = 2;
+                               break;
+                       }
                }
+               f.close();
                
-               //int rank = D.size();
+               //reopen to get back to beginning
+               openInputFile(fname, f);                        
+               read_phylip(f, m, names, D);
        }
-       catch(exception& e) {
+               catch(exception& e) {
                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){
@@ -580,7 +518,7 @@ void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vecto
                        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;
                }
@@ -602,20 +540,3 @@ void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vecto
 
 /*********************************************************************************************************************************/
 
-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);
-       }
-}
-/*********************************************************************************************************************************/
-