6 * Created by westcott on 1/4/10.
7 * Copyright 2010 Schloss Lab. All rights reserved.
11 #include "pcoacommand.h"
12 #include "readphylipvector.h"
15 //**********************************************************************************************************************
16 vector<string> PCOACommand::setParameters(){
18 CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pphylip);
19 CommandParameter pmetric("metric", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pmetric);
20 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "PCOACommand", "setParameters");
32 //**********************************************************************************************************************
33 string PCOACommand::getHelpString(){
35 string helpString = "";
36 helpString += "The pcoa command parameters are phylip and metric";
37 helpString += "The phylip parameter allows you to enter your distance file.";
38 helpString += "The metric parameter allows indicate you if would like the pearson correlation coefficient calculated. Default=True";
39 helpString += "Example pcoa(phylip=yourDistanceFile).\n";
40 helpString += "Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n";
44 m->errorOut(e, "PCOACommand", "getHelpString");
50 //**********************************************************************************************************************
51 PCOACommand::PCOACommand(){
53 abort = true; calledHelp = true;
55 vector<string> tempOutNames;
56 outputTypes["pcoa"] = tempOutNames;
57 outputTypes["loadings"] = tempOutNames;
60 m->errorOut(e, "PCOACommand", "PCOACommand");
64 //**********************************************************************************************************************
66 PCOACommand::PCOACommand(string option) {
68 abort = false; calledHelp = false;
70 //allow user to run help
71 if(option == "help") { help(); abort = true; calledHelp = true; }
74 vector<string> myArray = setParameters();
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") {
109 //if there is a current phylip file, use it
110 phylipfile = m->getPhylipFile();
111 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
112 else { m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; }
115 filename = phylipfile;
117 //if the user changes the output directory command factory will send this info to us in the output parameter
118 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
120 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
123 string temp = validParameter.validFile(parameters, "metric", false); if (temp == "not found"){ temp = "T"; }
124 metric = m->isTrue(temp);
128 catch(exception& e) {
129 m->errorOut(e, "PCOACommand", "PCOACommand");
133 //**********************************************************************************************************************
134 int PCOACommand::execute(){
137 if (abort == true) { if (calledHelp) { return 0; } return 2; }
139 cout.setf(ios::fixed, ios::floatfield);
140 cout.setf(ios::showpoint);
141 cerr.setf(ios::fixed, ios::floatfield);
142 cerr.setf(ios::showpoint);
144 vector<string> names;
145 vector<vector<double> > D;
147 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
149 ReadPhylipVector readFile(filename);
150 names = readFile.read(D);
152 if (m->control_pressed) { return 0; }
154 double offset = 0.0000;
157 vector<vector<double> > G = D;
158 //vector<vector<double> > copy_G;
160 m->mothurOut("\nProcessing...\n");
162 for(int count=0;count<2;count++){
163 linearCalc.recenter(offset, D, G); if (m->control_pressed) { return 0; }
164 linearCalc.tred2(G, d, e); if (m->control_pressed) { return 0; }
165 linearCalc.qtli(d, e, G); if (m->control_pressed) { return 0; }
166 offset = d[d.size()-1];
167 if(offset > 0.0) break;
170 if (m->control_pressed) { return 0; }
172 output(fbase, names, G, d);
174 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
178 for (int i = 1; i < 4; i++) {
180 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
182 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
184 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
186 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
188 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
192 m->mothurOutEndLine();
193 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
194 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
195 m->mothurOutEndLine();
199 catch(exception& e) {
200 m->errorOut(e, "PCOACommand", "execute");
204 /*********************************************************************************************************************************/
206 void PCOACommand::get_comment(istream& f, char begin, char end){
209 while(d != end){ d = f.get(); }
212 catch(exception& e) {
213 m->errorOut(e, "PCOACommand", "get_comment");
217 /*********************************************************************************************************************************/
219 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
221 int rank = name_list.size();
222 double dsum = 0.0000;
223 for(int i=0;i<rank;i++){
225 for(int j=0;j<rank;j++){
226 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
227 else { G[i][j] = 0.00000; }
231 ofstream pcaData((fnameRoot+"pcoa.axes").c_str(), ios::trunc);
232 pcaData.setf(ios::fixed, ios::floatfield);
233 pcaData.setf(ios::showpoint);
234 outputNames.push_back(fnameRoot+"pcoa.axes");
235 outputTypes["pcoa"].push_back(fnameRoot+"pcoa.axes");
237 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
238 pcaLoadings.setf(ios::fixed, ios::floatfield);
239 pcaLoadings.setf(ios::showpoint);
240 outputNames.push_back(fnameRoot+"pcoa.loadings");
241 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");
243 pcaLoadings << "axis\tloading\n";
244 for(int i=0;i<rank;i++){
245 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
249 for(int i=0;i<rank;i++){
250 pcaData << '\t' << "axis" << i+1;
254 for(int i=0;i<rank;i++){
255 pcaData << name_list[i] << '\t';
256 for(int j=0;j<rank;j++){
257 pcaData << G[i][j] << '\t';
262 catch(exception& e) {
263 m->errorOut(e, "PCOACommand", "output");
268 /*********************************************************************************************************************************/