6 * Created by westcott on 1/4/10.
7 * Copyright 2010 Schloss Lab. All rights reserved.
11 #include "pcoacommand.h"
12 #include "readphylipvector.h"
14 //**********************************************************************************************************************
15 vector<string> PCOACommand::getValidParameters(){
17 string Array[] = {"phylip", "metric","outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "PCOACommand", "getValidParameters");
26 //**********************************************************************************************************************
27 PCOACommand::PCOACommand(){
30 //initialize outputTypes
31 vector<string> tempOutNames;
32 outputTypes["pcoa"] = tempOutNames;
33 outputTypes["loadings"] = tempOutNames;
36 m->errorOut(e, "PCOACommand", "PCOACommand");
40 //**********************************************************************************************************************
41 vector<string> PCOACommand::getRequiredParameters(){
43 string Array[] = {"phylip"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48 m->errorOut(e, "PCOACommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> PCOACommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "PCOACommand", "getRequiredFiles");
63 //**********************************************************************************************************************
65 PCOACommand::PCOACommand(string option) {
69 //allow user to run help
70 if(option == "help") { help(); abort = true; }
73 //valid paramters for this command
74 string Array[] = {"phylip","metric","outputdir", "inputdir"};
75 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
77 OptionParser parser(option);
78 map<string, string> parameters = parser. getParameters();
80 ValidParameters validParameter;
81 map<string, string>::iterator it;
83 //check to make sure all parameters are valid for command
84 for (it = parameters.begin(); it != parameters.end(); it++) {
85 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //if the user changes the input directory command factory will send this info to us in the output parameter
88 string inputDir = validParameter.validFile(parameters, "inputdir", false);
89 if (inputDir == "not found"){ inputDir = ""; }
92 it = parameters.find("phylip");
93 //user has given a template file
94 if(it != parameters.end()){
95 path = m->hasPath(it->second);
96 //if the user has not given a path then, add inputdir. else leave path alone.
97 if (path == "") { parameters["phylip"] = inputDir + it->second; }
101 //initialize outputTypes
102 vector<string> tempOutNames;
103 outputTypes["pcoa"] = tempOutNames;
104 outputTypes["loadings"] = tempOutNames;
106 //required parameters
107 phylipfile = validParameter.validFile(parameters, "phylip", true);
108 if (phylipfile == "not open") { abort = true; }
109 else if (phylipfile == "not found") { phylipfile = ""; abort = true; }
110 else { filename = phylipfile; }
112 //if the user changes the output directory command factory will send this info to us in the output parameter
113 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
115 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
118 //error checking on files
119 if (phylipfile == "") { m->mothurOut("You must provide a distance file before running the pcoa command."); m->mothurOutEndLine(); abort = true; }
121 string temp = validParameter.validFile(parameters, "metric", false); if (temp == "not found"){ temp = "T"; }
122 metric = m->isTrue(temp);
126 catch(exception& e) {
127 m->errorOut(e, "PCOACommand", "PCOACommand");
131 //**********************************************************************************************************************
132 void PCOACommand::help(){
135 m->mothurOut("The pcoa command parameters are phylip and metric"); m->mothurOutEndLine();
136 m->mothurOut("The phylip parameter allows you to enter your distance file."); m->mothurOutEndLine();
137 m->mothurOut("The metric parameter allows indicate you if would like the pearson correlation coefficient calculated. Default=True"); m->mothurOutEndLine();
138 m->mothurOut("Example pcoa(phylip=yourDistanceFile).\n");
139 m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n\n");
141 catch(exception& e) {
142 m->errorOut(e, "PCOACommand", "help");
146 //**********************************************************************************************************************
147 PCOACommand::~PCOACommand(){}
148 //**********************************************************************************************************************
149 int PCOACommand::execute(){
152 if (abort == true) { return 0; }
154 cout.setf(ios::fixed, ios::floatfield);
155 cout.setf(ios::showpoint);
156 cerr.setf(ios::fixed, ios::floatfield);
157 cerr.setf(ios::showpoint);
159 vector<string> names;
160 vector<vector<double> > D;
162 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
164 ReadPhylipVector readFile(filename);
165 names = readFile.read(D);
167 if (m->control_pressed) { return 0; }
169 double offset = 0.0000;
172 vector<vector<double> > G = D;
173 vector<vector<double> > copy_G;
175 m->mothurOut("\nProcessing...\n\n");
177 for(int count=0;count<2;count++){
178 recenter(offset, D, G); if (m->control_pressed) { return 0; }
179 linearCalc.tred2(G, d, e); if (m->control_pressed) { return 0; }
180 linearCalc.qtli(d, e, G); if (m->control_pressed) { return 0; }
181 offset = d[d.size()-1];
182 if(offset > 0.0) break;
185 if (m->control_pressed) { return 0; }
187 output(fbase, names, G, d);
189 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
193 for (int i = 1; i < 4; i++) {
195 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
197 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
199 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
201 m->mothurOut("Pearson's coefficient using " + toString(i) + " axis: " + toString(corr)); m->mothurOutEndLine();
203 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
207 m->mothurOutEndLine();
208 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
209 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
210 m->mothurOutEndLine();
214 catch(exception& e) {
215 m->errorOut(e, "PCOACommand", "execute");
219 /*********************************************************************************************************************************/
221 void PCOACommand::get_comment(istream& f, char begin, char end){
224 while(d != end){ d = f.get(); }
227 catch(exception& e) {
228 m->errorOut(e, "PCOACommand", "get_comment");
232 /*********************************************************************************************************************************/
234 void PCOACommand::recenter(double offset, vector<vector<double> > D, vector<vector<double> >& G){
238 vector<vector<double> > A(rank);
239 vector<vector<double> > C(rank);
240 for(int i=0;i<rank;i++){
245 double scale = -1.0000 / (double) rank;
247 for(int i=0;i<rank;i++){
249 C[i][i] = 1.0000 + scale;
250 for(int j=i+1;j<rank;j++){
251 A[i][j] = A[j][i] = -0.5 * D[i][j] * D[i][j] + offset;
252 C[i][j] = C[j][i] = scale;
256 A = linearCalc.matrix_mult(C,A);
257 G = linearCalc.matrix_mult(A,C);
259 catch(exception& e) {
260 m->errorOut(e, "PCOACommand", "recenter");
266 /*********************************************************************************************************************************/
268 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
270 int rank = name_list.size();
271 double dsum = 0.0000;
272 for(int i=0;i<rank;i++){
274 for(int j=0;j<rank;j++){
275 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
276 else { G[i][j] = 0.00000; }
280 ofstream pcaData((fnameRoot+"pcoa.axes").c_str(), ios::trunc);
281 pcaData.setf(ios::fixed, ios::floatfield);
282 pcaData.setf(ios::showpoint);
283 outputNames.push_back(fnameRoot+"pcoa.axes");
284 outputTypes["pcoa"].push_back(fnameRoot+"pcoa.axes");
286 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
287 pcaLoadings.setf(ios::fixed, ios::floatfield);
288 pcaLoadings.setf(ios::showpoint);
289 outputNames.push_back(fnameRoot+"pcoa.loadings");
290 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");
292 pcaLoadings << "axis\tloading\n";
293 for(int i=0;i<rank;i++){
294 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
298 for(int i=0;i<rank;i++){
299 pcaData << '\t' << "axis" << i+1;
303 for(int i=0;i<rank;i++){
304 pcaData << name_list[i] << '\t';
305 for(int j=0;j<rank;j++){
306 pcaData << G[i][j] << '\t';
311 catch(exception& e) {
312 m->errorOut(e, "PCOACommand", "output");
317 /*********************************************************************************************************************************/