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","pcoa-loadings",false,true,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");
48 //**********************************************************************************************************************
49 string PCOACommand::getOutputPattern(string type) {
53 if (type == "pcoa") { pattern = "[filename],pcoa.axes"; }
54 else if (type == "loadings") { pattern = "[filename],pcoa.loadings"; }
55 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
60 m->errorOut(e, "PCOACommand", "getOutputPattern");
66 //**********************************************************************************************************************
67 PCOACommand::PCOACommand(){
69 abort = true; calledHelp = true;
71 vector<string> tempOutNames;
72 outputTypes["pcoa"] = tempOutNames;
73 outputTypes["loadings"] = tempOutNames;
76 m->errorOut(e, "PCOACommand", "PCOACommand");
80 //**********************************************************************************************************************
82 PCOACommand::PCOACommand(string option) {
84 abort = false; calledHelp = false;
86 //allow user to run help
87 if(option == "help") { help(); abort = true; calledHelp = true; }
88 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
91 vector<string> myArray = setParameters();
93 OptionParser parser(option);
94 map<string, string> parameters = parser. getParameters();
96 ValidParameters validParameter;
97 map<string, string>::iterator it;
99 //check to make sure all parameters are valid for command
100 for (it = parameters.begin(); it != parameters.end(); it++) {
101 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //if the user changes the input directory command factory will send this info to us in the output parameter
104 string inputDir = validParameter.validFile(parameters, "inputdir", false);
105 if (inputDir == "not found"){ inputDir = ""; }
108 it = parameters.find("phylip");
109 //user has given a template file
110 if(it != parameters.end()){
111 path = m->hasPath(it->second);
112 //if the user has not given a path then, add inputdir. else leave path alone.
113 if (path == "") { parameters["phylip"] = inputDir + it->second; }
117 //initialize outputTypes
118 vector<string> tempOutNames;
119 outputTypes["pcoa"] = tempOutNames;
120 outputTypes["loadings"] = tempOutNames;
122 //required parameters
123 phylipfile = validParameter.validFile(parameters, "phylip", true);
124 if (phylipfile == "not open") { abort = true; }
125 else if (phylipfile == "not found") {
126 //if there is a current phylip file, use it
127 phylipfile = m->getPhylipFile();
128 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
129 else { m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; }
130 }else { m->setPhylipFile(phylipfile); }
132 filename = phylipfile;
134 //if the user changes the output directory command factory will send this info to us in the output parameter
135 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
137 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
140 string temp = validParameter.validFile(parameters, "metric", false); if (temp == "not found"){ temp = "T"; }
141 metric = m->isTrue(temp);
145 catch(exception& e) {
146 m->errorOut(e, "PCOACommand", "PCOACommand");
150 //**********************************************************************************************************************
151 int PCOACommand::execute(){
154 if (abort == true) { if (calledHelp) { return 0; } return 2; }
156 cout.setf(ios::fixed, ios::floatfield);
157 cout.setf(ios::showpoint);
158 cerr.setf(ios::fixed, ios::floatfield);
159 cerr.setf(ios::showpoint);
161 vector<string> names;
162 vector<vector<double> > D;
164 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
166 ReadPhylipVector readFile(filename);
167 names = readFile.read(D);
169 if (m->control_pressed) { return 0; }
171 double offset = 0.0000;
174 vector<vector<double> > G = D;
175 //vector<vector<double> > copy_G;
177 m->mothurOut("\nProcessing...\n");
179 for(int count=0;count<2;count++){
180 linearCalc.recenter(offset, D, G); if (m->control_pressed) { return 0; }
181 linearCalc.tred2(G, d, e); if (m->control_pressed) { return 0; }
182 linearCalc.qtli(d, e, G); if (m->control_pressed) { return 0; }
183 offset = d[d.size()-1];
184 if(offset > 0.0) break;
187 if (m->control_pressed) { return 0; }
189 output(fbase, names, G, d);
191 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
195 for (int i = 1; i < 4; i++) {
197 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
199 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
201 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
203 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
205 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
209 m->mothurOutEndLine();
210 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
211 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
212 m->mothurOutEndLine();
216 catch(exception& e) {
217 m->errorOut(e, "PCOACommand", "execute");
221 /*********************************************************************************************************************************/
223 void PCOACommand::get_comment(istream& f, char begin, char end){
226 while(d != end){ d = f.get(); }
229 catch(exception& e) {
230 m->errorOut(e, "PCOACommand", "get_comment");
234 /*********************************************************************************************************************************/
236 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
238 int rank = name_list.size();
239 double dsum = 0.0000;
240 for(int i=0;i<rank;i++){
242 for(int j=0;j<rank;j++){
243 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
244 else { G[i][j] = 0.00000; }
249 map<string, string> variables;
250 variables["[filename]"] = fnameRoot;
251 string pcoaDataFile = getOutputFileName("pcoa",variables);
252 m->openOutputFile(pcoaDataFile, pcaData);
253 pcaData.setf(ios::fixed, ios::floatfield);
254 pcaData.setf(ios::showpoint);
255 outputNames.push_back(pcoaDataFile);
256 outputTypes["pcoa"].push_back(pcoaDataFile);
258 ofstream pcaLoadings;
259 string loadingsFile = getOutputFileName("loadings",variables);
260 m->openOutputFile(loadingsFile, pcaLoadings);
261 pcaLoadings.setf(ios::fixed, ios::floatfield);
262 pcaLoadings.setf(ios::showpoint);
263 outputNames.push_back(loadingsFile);
264 outputTypes["loadings"].push_back(loadingsFile);
266 pcaLoadings << "axis\tloading\n";
267 for(int i=0;i<rank;i++){
268 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
272 for(int i=0;i<rank;i++){
273 pcaData << '\t' << "axis" << i+1;
277 for(int i=0;i<rank;i++){
278 pcaData << name_list[i] << '\t';
279 for(int j=0;j<rank;j++){
280 pcaData << G[i][j] << '\t';
285 catch(exception& e) {
286 m->errorOut(e, "PCOACommand", "output");
291 /*********************************************************************************************************************************/