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");
49 //**********************************************************************************************************************
50 string PCOACommand::getOutputFileNameTag(string type, string inputName=""){
52 string outputFileName = "";
53 map<string, vector<string> >::iterator it;
55 //is this a type this command creates
56 it = outputTypes.find(type);
57 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
59 if (type == "pcoa") { outputFileName = "pcoa.axes"; }
60 else if (type == "loadings") { outputFileName = "pcoa.loadings"; }
61 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
63 return outputFileName;
66 m->errorOut(e, "PCOACommand", "getOutputFileNameTag");
71 //**********************************************************************************************************************
72 PCOACommand::PCOACommand(){
74 abort = true; calledHelp = true;
76 vector<string> tempOutNames;
77 outputTypes["pcoa"] = tempOutNames;
78 outputTypes["loadings"] = tempOutNames;
81 m->errorOut(e, "PCOACommand", "PCOACommand");
85 //**********************************************************************************************************************
87 PCOACommand::PCOACommand(string option) {
89 abort = false; calledHelp = false;
91 //allow user to run help
92 if(option == "help") { help(); abort = true; calledHelp = true; }
93 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
96 vector<string> myArray = setParameters();
98 OptionParser parser(option);
99 map<string, string> parameters = parser. getParameters();
101 ValidParameters validParameter;
102 map<string, string>::iterator it;
104 //check to make sure all parameters are valid for command
105 for (it = parameters.begin(); it != parameters.end(); it++) {
106 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
110 if (inputDir == "not found"){ inputDir = ""; }
113 it = parameters.find("phylip");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["phylip"] = inputDir + it->second; }
122 //initialize outputTypes
123 vector<string> tempOutNames;
124 outputTypes["pcoa"] = tempOutNames;
125 outputTypes["loadings"] = tempOutNames;
127 //required parameters
128 phylipfile = validParameter.validFile(parameters, "phylip", true);
129 if (phylipfile == "not open") { abort = true; }
130 else if (phylipfile == "not found") {
131 //if there is a current phylip file, use it
132 phylipfile = m->getPhylipFile();
133 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
134 else { m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; }
135 }else { m->setPhylipFile(phylipfile); }
137 filename = phylipfile;
139 //if the user changes the output directory command factory will send this info to us in the output parameter
140 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
142 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
145 string temp = validParameter.validFile(parameters, "metric", false); if (temp == "not found"){ temp = "T"; }
146 metric = m->isTrue(temp);
150 catch(exception& e) {
151 m->errorOut(e, "PCOACommand", "PCOACommand");
155 //**********************************************************************************************************************
156 int PCOACommand::execute(){
159 if (abort == true) { if (calledHelp) { return 0; } return 2; }
161 cout.setf(ios::fixed, ios::floatfield);
162 cout.setf(ios::showpoint);
163 cerr.setf(ios::fixed, ios::floatfield);
164 cerr.setf(ios::showpoint);
166 vector<string> names;
167 vector<vector<double> > D;
169 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
171 ReadPhylipVector readFile(filename);
172 names = readFile.read(D);
174 if (m->control_pressed) { return 0; }
176 double offset = 0.0000;
179 vector<vector<double> > G = D;
180 //vector<vector<double> > copy_G;
182 m->mothurOut("\nProcessing...\n");
184 for(int count=0;count<2;count++){
185 linearCalc.recenter(offset, D, G); if (m->control_pressed) { return 0; }
186 linearCalc.tred2(G, d, e); if (m->control_pressed) { return 0; }
187 linearCalc.qtli(d, e, G); if (m->control_pressed) { return 0; }
188 offset = d[d.size()-1];
189 if(offset > 0.0) break;
192 if (m->control_pressed) { return 0; }
194 output(fbase, names, G, d);
196 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
200 for (int i = 1; i < 4; i++) {
202 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
204 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
206 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
208 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
210 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
214 m->mothurOutEndLine();
215 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
216 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
217 m->mothurOutEndLine();
221 catch(exception& e) {
222 m->errorOut(e, "PCOACommand", "execute");
226 /*********************************************************************************************************************************/
228 void PCOACommand::get_comment(istream& f, char begin, char end){
231 while(d != end){ d = f.get(); }
234 catch(exception& e) {
235 m->errorOut(e, "PCOACommand", "get_comment");
239 /*********************************************************************************************************************************/
241 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
243 int rank = name_list.size();
244 double dsum = 0.0000;
245 for(int i=0;i<rank;i++){
247 for(int j=0;j<rank;j++){
248 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
249 else { G[i][j] = 0.00000; }
254 string pcoaDataFile = fnameRoot+getOutputFileNameTag("pcoa");
255 m->openOutputFile(pcoaDataFile, pcaData);
256 pcaData.setf(ios::fixed, ios::floatfield);
257 pcaData.setf(ios::showpoint);
258 outputNames.push_back(pcoaDataFile);
259 outputTypes["pcoa"].push_back(pcoaDataFile);
261 ofstream pcaLoadings;
262 string loadingsFile = fnameRoot+getOutputFileNameTag("loadings");
263 m->openOutputFile(loadingsFile, pcaLoadings);
264 pcaLoadings.setf(ios::fixed, ios::floatfield);
265 pcaLoadings.setf(ios::showpoint);
266 outputNames.push_back(loadingsFile);
267 outputTypes["loadings"].push_back(loadingsFile);
269 pcaLoadings << "axis\tloading\n";
270 for(int i=0;i<rank;i++){
271 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
275 for(int i=0;i<rank;i++){
276 pcaData << '\t' << "axis" << i+1;
280 for(int i=0;i<rank;i++){
281 pcaData << name_list[i] << '\t';
282 for(int j=0;j<rank;j++){
283 pcaData << G[i][j] << '\t';
288 catch(exception& e) {
289 m->errorOut(e, "PCOACommand", "output");
294 /*********************************************************************************************************************************/