]> git.donarmstrong.com Git - mothur.git/blobdiff - pcacommand.cpp
added sets to amova and homova commands. added oligos to make.contigs. added metadat...
[mothur.git] / pcacommand.cpp
index e273e5fe7079ffab790ccf5498e69675a0fafeef..2cb37bda8896f8f2cb1f64521f1a73232507f24b 100644 (file)
@@ -48,6 +48,28 @@ string PCACommand::getHelpString(){
                exit(1);
        }
 }
+//**********************************************************************************************************************
+string PCACommand::getOutputFileNameTag(string type, string inputName=""){     
+       try {
+        string outputFileName = "";
+               map<string, vector<string> >::iterator it;
+        
+        //is this a type this command creates
+        it = outputTypes.find(type);
+        if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
+        else {
+            if (type == "pca") {  outputFileName =  "pca.axes"; }
+            else if (type == "loadings") {  outputFileName =  "pca.loadings"; }
+            else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
+        }
+        return outputFileName;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PCACommand", "getOutputFileNameTag");
+               exit(1);
+       }
+}
+
 //**********************************************************************************************************************
 PCACommand::PCACommand(){      
        try {
@@ -117,12 +139,12 @@ PCACommand::PCACommand(string option)  {
                        sharedfile = validParameter.validFile(parameters, "shared", true);
                        if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
                        else if (sharedfile == "not found") { sharedfile = ""; }
-                       else {  mode = "sharedfile"; inputFile = sharedfile; }
+                       else {  mode = "sharedfile"; inputFile = sharedfile; m->setSharedFile(sharedfile); }
                        
                        relabundfile = validParameter.validFile(parameters, "relabund", true);
                        if (relabundfile == "not open") { relabundfile = ""; abort = true; }    
                        else if (relabundfile == "not found") { relabundfile = ""; }
-                       else {  mode = "relabund"; inputFile = relabundfile; }
+                       else {  mode = "relabund"; inputFile = relabundfile; m->setRelAbundFile(relabundfile); }
                        
                        
                        if ((sharedfile == "") && (relabundfile == "")) { 
@@ -157,7 +179,7 @@ PCACommand::PCACommand(string option)  {
                        groups = validParameter.validFile(parameters, "groups", false);                 
                        if (groups == "not found") { groups = "";  }
                        else { m->splitAtDash(groups, Groups);  }                       
-                       m->Groups = Groups;                     
+                       m->setGroups(Groups);                   
                        
                }
                
@@ -203,7 +225,7 @@ int PCACommand::execute(){
                //as long as you are not at the end of the file or done wih the lines you want
                while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
                        
-                       if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } delete input; for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }  lookupFloat.clear(); return 0;  }
+                       if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);  } delete input; for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }  lookupFloat.clear(); return 0;  }
                        
                        if(labels.count(lookupFloat[0]->getLabel()) == 1){
                                processedLabels.insert(lookupFloat[0]->getLabel());
@@ -236,7 +258,7 @@ int PCACommand::execute(){
                }
                
                
-               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } delete input; for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  } lookupFloat.clear(); return 0;  }
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } delete input; for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  } lookupFloat.clear(); return 0;  }
                
                //output error messages about any remaining user labels
                set<string>::iterator it;
@@ -264,7 +286,7 @@ int PCACommand::execute(){
                for (int i = 0; i < lookupFloat.size(); i++) {  if (lookupFloat[i] != NULL) {   delete lookupFloat[i];  } } lookupFloat.clear();
                delete input;
                
-               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -319,7 +341,6 @@ int PCACommand::process(vector<SharedRAbundFloatVector*>& lookupFloat){
                vector< vector<double> > matrix(numSamples);
                vector<double> colMeans(numOTUs);
                
-                               
                //fill matrix with shared relative abundances, re-center
                for (int i = 0; i < lookupFloat.size(); i++) {
                        matrix[i].resize(numOTUs, 0);
@@ -366,7 +387,7 @@ int PCACommand::process(vector<SharedRAbundFloatVector*>& lookupFloat){
                
                string fbase = outputDir + m->getRootName(m->getSimpleName(inputFile));
                string outputFileName = fbase + lookupFloat[0]->getLabel();
-               output(outputFileName, m->Groups, X, d);
+               output(outputFileName, m->getGroups(), X, d);
                
                if (metric) {   
                        
@@ -376,13 +397,13 @@ int PCACommand::process(vector<SharedRAbundFloatVector*>& lookupFloat){
                                
                                vector< vector<double> > PCAEuclidDists = linearCalc.calculateEuclidianDistance(X, i); //G is the pca file
                                
-                               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
+                               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
 
                                double corr = linearCalc.calcPearson(PCAEuclidDists, observedEuclideanDistance);
                                                                
                                m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
                                
-                               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
+                               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
                        }
                }
                
@@ -404,17 +425,19 @@ void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vecto
                        dsum += d[i];
                }
                
-               ofstream pcaData((fnameRoot+".pca.axes").c_str(), ios::trunc);
+               ofstream pcaData;
+        m->openOutputFile((fnameRoot+"."+getOutputFileNameTag("pca")), pcaData);
                pcaData.setf(ios::fixed, ios::floatfield);
                pcaData.setf(ios::showpoint);   
-               outputNames.push_back(fnameRoot+".pca.axes");
-               outputTypes["pca"].push_back(fnameRoot+".pca.axes");
+               outputNames.push_back(fnameRoot+"."+getOutputFileNameTag("pca"));
+               outputTypes["pca"].push_back(fnameRoot+"."+getOutputFileNameTag("pca"));
                
-               ofstream pcaLoadings((fnameRoot+".pca.loadings").c_str(), ios::trunc);
+               ofstream pcaLoadings;
+         m->openOutputFile((fnameRoot+"."+getOutputFileNameTag("loadings")), pcaLoadings);
                pcaLoadings.setf(ios::fixed, ios::floatfield);
                pcaLoadings.setf(ios::showpoint);
-               outputNames.push_back(fnameRoot+".pca.loadings");
-               outputTypes["loadings"].push_back(fnameRoot+".pca.loadings");   
+               outputNames.push_back(fnameRoot+"."+getOutputFileNameTag("loadings"));
+               outputTypes["loadings"].push_back(fnameRoot+"."+getOutputFileNameTag("loadings"));      
                
                pcaLoadings << "axis\tloading\n";
                for(int i=0;i<numEigenValues;i++){
@@ -427,7 +450,7 @@ void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vecto
                }
                pcaData << endl;
                
-               for(int i=0;i<numEigenValues;i++){
+               for(int i=0;i<name_list.size();i++){
                        pcaData << name_list[i] << '\t';
                        for(int j=0;j<numEigenValues;j++){
                                pcaData << G[i][j] << '\t';