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(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["pcoa"] = tempOutNames;
32 outputTypes["loadings"] = tempOutNames;
35 m->errorOut(e, "PCOACommand", "PCOACommand");
39 //**********************************************************************************************************************
40 vector<string> PCOACommand::getRequiredParameters(){
42 string Array[] = {"phylip"};
43 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
47 m->errorOut(e, "PCOACommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> PCOACommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "PCOACommand", "getRequiredFiles");
62 //**********************************************************************************************************************
64 PCOACommand::PCOACommand(string option) {
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
72 //valid paramters for this command
73 string Array[] = {"phylip","metric","outputdir", "inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
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") { phylipfile = ""; abort = true; }
109 else { filename = phylipfile; }
111 //if the user changes the output directory command factory will send this info to us in the output parameter
112 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
114 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
117 //error checking on files
118 if (phylipfile == "") { m->mothurOut("You must provide a distance file before running the pcoa command."); m->mothurOutEndLine(); abort = true; }
120 string temp = validParameter.validFile(parameters, "metric", false); if (temp == "not found"){ temp = "T"; }
121 metric = m->isTrue(temp);
125 catch(exception& e) {
126 m->errorOut(e, "PCOACommand", "PCOACommand");
130 //**********************************************************************************************************************
131 void PCOACommand::help(){
134 m->mothurOut("The pcoa command parameters are phylip and metric"); m->mothurOutEndLine();
135 m->mothurOut("The phylip parameter allows you to enter your distance file."); m->mothurOutEndLine();
136 m->mothurOut("The metric parameter allows indicate you if would like the pearson correlation coefficient calculated. Default=True"); m->mothurOutEndLine();
137 m->mothurOut("Example pcoa(phylip=yourDistanceFile).\n");
138 m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n\n");
140 catch(exception& e) {
141 m->errorOut(e, "PCOACommand", "help");
145 //**********************************************************************************************************************
146 PCOACommand::~PCOACommand(){}
147 //**********************************************************************************************************************
148 int PCOACommand::execute(){
151 if (abort == true) { if (calledHelp) { return 0; } return 2; }
153 cout.setf(ios::fixed, ios::floatfield);
154 cout.setf(ios::showpoint);
155 cerr.setf(ios::fixed, ios::floatfield);
156 cerr.setf(ios::showpoint);
158 vector<string> names;
159 vector<vector<double> > D;
161 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
163 ReadPhylipVector readFile(filename);
164 names = readFile.read(D);
166 if (m->control_pressed) { return 0; }
168 double offset = 0.0000;
171 vector<vector<double> > G = D;
172 vector<vector<double> > copy_G;
174 m->mothurOut("\nProcessing...\n\n");
176 for(int count=0;count<2;count++){
177 recenter(offset, D, G); if (m->control_pressed) { return 0; }
178 linearCalc.tred2(G, d, e); if (m->control_pressed) { return 0; }
179 linearCalc.qtli(d, e, G); if (m->control_pressed) { return 0; }
180 offset = d[d.size()-1];
181 if(offset > 0.0) break;
184 if (m->control_pressed) { return 0; }
186 output(fbase, names, G, d);
188 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
192 for (int i = 1; i < 4; i++) {
194 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
196 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
198 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
200 m->mothurOut("Pearson's coefficient using " + toString(i) + " axis: " + toString(corr)); m->mothurOutEndLine();
202 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
206 m->mothurOutEndLine();
207 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
208 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
209 m->mothurOutEndLine();
213 catch(exception& e) {
214 m->errorOut(e, "PCOACommand", "execute");
218 /*********************************************************************************************************************************/
220 void PCOACommand::get_comment(istream& f, char begin, char end){
223 while(d != end){ d = f.get(); }
226 catch(exception& e) {
227 m->errorOut(e, "PCOACommand", "get_comment");
231 /*********************************************************************************************************************************/
233 void PCOACommand::recenter(double offset, vector<vector<double> > D, vector<vector<double> >& G){
237 vector<vector<double> > A(rank);
238 vector<vector<double> > C(rank);
239 for(int i=0;i<rank;i++){
244 double scale = -1.0000 / (double) rank;
246 for(int i=0;i<rank;i++){
248 C[i][i] = 1.0000 + scale;
249 for(int j=i+1;j<rank;j++){
250 A[i][j] = A[j][i] = -0.5 * D[i][j] * D[i][j] + offset;
251 C[i][j] = C[j][i] = scale;
255 A = linearCalc.matrix_mult(C,A);
256 G = linearCalc.matrix_mult(A,C);
258 catch(exception& e) {
259 m->errorOut(e, "PCOACommand", "recenter");
265 /*********************************************************************************************************************************/
267 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
269 int rank = name_list.size();
270 double dsum = 0.0000;
271 for(int i=0;i<rank;i++){
273 for(int j=0;j<rank;j++){
274 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
275 else { G[i][j] = 0.00000; }
279 ofstream pcaData((fnameRoot+"pcoa.axes").c_str(), ios::trunc);
280 pcaData.setf(ios::fixed, ios::floatfield);
281 pcaData.setf(ios::showpoint);
282 outputNames.push_back(fnameRoot+"pcoa.axes");
283 outputTypes["pcoa"].push_back(fnameRoot+"pcoa.axes");
285 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
286 pcaLoadings.setf(ios::fixed, ios::floatfield);
287 pcaLoadings.setf(ios::showpoint);
288 outputNames.push_back(fnameRoot+"pcoa.loadings");
289 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");
291 pcaLoadings << "axis\tloading\n";
292 for(int i=0;i<rank;i++){
293 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
297 for(int i=0;i<rank;i++){
298 pcaData << '\t' << "axis" << i+1;
302 for(int i=0;i<rank;i++){
303 pcaData << name_list[i] << '\t';
304 for(int j=0;j<rank;j++){
305 pcaData << G[i][j] << '\t';
310 catch(exception& e) {
311 m->errorOut(e, "PCOACommand", "output");
316 /*********************************************************************************************************************************/