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; }
72 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
75 vector<string> myArray = setParameters();
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") {
110 //if there is a current phylip file, use it
111 phylipfile = m->getPhylipFile();
112 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
113 else { m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; }
114 }else { m->setPhylipFile(phylipfile); }
116 filename = phylipfile;
118 //if the user changes the output directory command factory will send this info to us in the output parameter
119 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
121 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
124 string temp = validParameter.validFile(parameters, "metric", false); if (temp == "not found"){ temp = "T"; }
125 metric = m->isTrue(temp);
129 catch(exception& e) {
130 m->errorOut(e, "PCOACommand", "PCOACommand");
134 //**********************************************************************************************************************
135 int PCOACommand::execute(){
138 if (abort == true) { if (calledHelp) { return 0; } return 2; }
140 cout.setf(ios::fixed, ios::floatfield);
141 cout.setf(ios::showpoint);
142 cerr.setf(ios::fixed, ios::floatfield);
143 cerr.setf(ios::showpoint);
145 vector<string> names;
146 vector<vector<double> > D;
148 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
150 ReadPhylipVector readFile(filename);
151 names = readFile.read(D);
153 if (m->control_pressed) { return 0; }
155 double offset = 0.0000;
158 vector<vector<double> > G = D;
159 //vector<vector<double> > copy_G;
161 m->mothurOut("\nProcessing...\n");
163 for(int count=0;count<2;count++){
164 linearCalc.recenter(offset, D, G); if (m->control_pressed) { return 0; }
165 linearCalc.tred2(G, d, e); if (m->control_pressed) { return 0; }
166 linearCalc.qtli(d, e, G); if (m->control_pressed) { return 0; }
167 offset = d[d.size()-1];
168 if(offset > 0.0) break;
171 if (m->control_pressed) { return 0; }
173 output(fbase, names, G, d);
175 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
179 for (int i = 1; i < 4; i++) {
181 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
183 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
185 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
187 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
189 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
193 m->mothurOutEndLine();
194 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
195 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
196 m->mothurOutEndLine();
200 catch(exception& e) {
201 m->errorOut(e, "PCOACommand", "execute");
205 /*********************************************************************************************************************************/
207 void PCOACommand::get_comment(istream& f, char begin, char end){
210 while(d != end){ d = f.get(); }
213 catch(exception& e) {
214 m->errorOut(e, "PCOACommand", "get_comment");
218 /*********************************************************************************************************************************/
220 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
222 int rank = name_list.size();
223 double dsum = 0.0000;
224 for(int i=0;i<rank;i++){
226 for(int j=0;j<rank;j++){
227 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
228 else { G[i][j] = 0.00000; }
232 ofstream pcaData((fnameRoot+"pcoa.axes").c_str(), ios::trunc);
233 pcaData.setf(ios::fixed, ios::floatfield);
234 pcaData.setf(ios::showpoint);
235 outputNames.push_back(fnameRoot+"pcoa.axes");
236 outputTypes["pcoa"].push_back(fnameRoot+"pcoa.axes");
238 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
239 pcaLoadings.setf(ios::fixed, ios::floatfield);
240 pcaLoadings.setf(ios::showpoint);
241 outputNames.push_back(fnameRoot+"pcoa.loadings");
242 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");
244 pcaLoadings << "axis\tloading\n";
245 for(int i=0;i<rank;i++){
246 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
250 for(int i=0;i<rank;i++){
251 pcaData << '\t' << "axis" << i+1;
255 for(int i=0;i<rank;i++){
256 pcaData << name_list[i] << '\t';
257 for(int j=0;j<rank;j++){
258 pcaData << G[i][j] << '\t';
263 catch(exception& e) {
264 m->errorOut(e, "PCOACommand", "output");
269 /*********************************************************************************************************************************/