5 * Created by westcott on 1/7/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "pcacommand.h"
11 #include "inputdata.h"
13 //**********************************************************************************************************************
14 vector<string> PCACommand::setParameters(){
16 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
17 CommandParameter prelabund("relabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prelabund);
18 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
19 CommandParameter pmetric("metric", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pmetric);
20 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "PCACommand", "setParameters");
33 //**********************************************************************************************************************
34 string PCACommand::getHelpString(){
36 string helpString = "";
37 helpString += "The pca command parameters are shared, relabund, label, groups and metric. shared or relabund is required unless you have a valid current file.";
38 helpString += "The label parameter is used to analyze specific labels in your input. Default is the first label in your shared or relabund file. Multiple labels may be separated by dashes.\n";
39 helpString += "The groups parameter allows you to specify which groups you would like analyzed. Groupnames are separated by dashes.\n";
40 helpString += "The metric parameter allows indicate you if would like the pearson correlation coefficient calculated. Default=True";
41 helpString += "Example pca(groups=yourGroups).\n";
42 helpString += "Example pca(groups=A-B-C).\n";
43 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
47 m->errorOut(e, "PCACommand", "getHelpString");
51 //**********************************************************************************************************************
52 PCACommand::PCACommand(){
54 abort = true; calledHelp = true;
56 vector<string> tempOutNames;
57 outputTypes["pca"] = tempOutNames;
58 outputTypes["loadings"] = tempOutNames;
61 m->errorOut(e, "PCACommand", "PCACommand");
65 //**********************************************************************************************************************
67 PCACommand::PCACommand(string option) {
69 abort = false; calledHelp = false;
71 //allow user to run help
72 if(option == "help") { help(); 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; }
88 //initialize outputTypes
89 vector<string> tempOutNames;
90 outputTypes["pca"] = tempOutNames;
91 outputTypes["loadings"] = tempOutNames;
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("shared");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["shared"] = inputDir + it->second; }
106 it = parameters.find("relabund");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["relabund"] = inputDir + it->second; }
115 //check for required parameters
116 sharedfile = validParameter.validFile(parameters, "shared", true);
117 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
118 else if (sharedfile == "not found") { sharedfile = ""; }
119 else { mode = "sharedfile"; inputFile = sharedfile; }
121 relabundfile = validParameter.validFile(parameters, "relabund", true);
122 if (relabundfile == "not open") { relabundfile = ""; abort = true; }
123 else if (relabundfile == "not found") { relabundfile = ""; }
124 else { mode = "relabund"; inputFile = relabundfile; }
127 if ((sharedfile == "") && (relabundfile == "")) {
128 //is there are current file available for any of these?
129 //give priority to shared, then list, then rabund, then sabund
130 //if there is a current shared file, use it
131 sharedfile = m->getSharedFile();
132 if (sharedfile != "") { inputFile = sharedfile; mode = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
134 relabundfile = m->getRelAbundFile();
135 if (relabundfile != "") { inputFile = relabundfile; mode = "relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
137 m->mothurOut("No valid current files. You must provide a relabund or shared file."); m->mothurOutEndLine();
143 //if the user changes the output directory command factory will send this info to us in the output parameter
144 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
146 outputDir += m->hasPath(inputFile); //if user entered a file with a path then preserve it
149 string temp = validParameter.validFile(parameters, "metric", false); if (temp == "not found"){ temp = "T"; }
150 metric = m->isTrue(temp);
152 label = validParameter.validFile(parameters, "label", false);
153 if (label == "not found") { label = ""; if(labels.size() == 0) { m->mothurOut("You did not provide a label, I will use the first label in your inputfile."); m->mothurOutEndLine(); } }
154 else { m->splitAtDash(label, labels); }
156 groups = validParameter.validFile(parameters, "groups", false);
157 if (groups == "not found") { groups = ""; }
158 else { m->splitAtDash(groups, Groups); }
164 catch(exception& e) {
165 m->errorOut(e, "PCACommand", "PCACommand");
169 //**********************************************************************************************************************
170 int PCACommand::execute(){
173 if (abort == true) { if (calledHelp) { return 0; } return 2; }
175 cout.setf(ios::fixed, ios::floatfield);
176 cout.setf(ios::showpoint);
177 cerr.setf(ios::fixed, ios::floatfield);
178 cerr.setf(ios::showpoint);
180 //get first line of shared file
181 vector< vector<double> > matrix;
183 if (mode == "shared") {
184 input = new InputData(inputFile, "sharedfile");
185 }else if (mode == "relabund") {
186 input = new InputData(inputFile, "relabund");
187 }else { m->mothurOut("[ERROR]: filetype not recognized."); m->mothurOutEndLine(); return 0; }
189 vector<SharedRAbundFloatVector*> lookupFloat = input->getSharedRAbundFloatVectors();
190 string lastLabel = lookupFloat[0]->getLabel();
192 set<string> processedLabels;
193 set<string> userLabels = labels;
195 //if the user gave no labels, then use the first one read
196 if (labels.size() == 0) {
199 process(lookupFloat);
202 //as long as you are not at the end of the file or done wih the lines you want
203 while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
205 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete input; for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } lookupFloat.clear(); return 0; }
207 if(labels.count(lookupFloat[0]->getLabel()) == 1){
208 processedLabels.insert(lookupFloat[0]->getLabel());
209 userLabels.erase(lookupFloat[0]->getLabel());
211 process(lookupFloat);
214 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
215 string saveLabel = lookupFloat[0]->getLabel();
217 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } lookupFloat.clear();
218 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
220 process(lookupFloat);
222 processedLabels.insert(lookupFloat[0]->getLabel());
223 userLabels.erase(lookupFloat[0]->getLabel());
225 //restore real lastlabel to save below
226 lookupFloat[0]->setLabel(saveLabel);
229 lastLabel = lookupFloat[0]->getLabel();
231 //get next line to process
232 //prevent memory leak
233 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } lookupFloat.clear();
234 lookupFloat = input->getSharedRAbundFloatVectors();
238 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete input; for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } lookupFloat.clear(); return 0; }
240 //output error messages about any remaining user labels
241 set<string>::iterator it;
242 bool needToRun = false;
243 for (it = userLabels.begin(); it != userLabels.end(); it++) {
244 m->mothurOut("Your file does not include the label " + *it);
245 if (processedLabels.count(lastLabel) != 1) {
246 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
249 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
253 //run last label if you need to
254 if (needToRun == true) {
255 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } } lookupFloat.clear();
256 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
258 process(lookupFloat);
260 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } } lookupFloat.clear();
263 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } } lookupFloat.clear();
266 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
268 m->mothurOutEndLine();
269 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
270 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
271 m->mothurOutEndLine();
275 catch(exception& e) {
276 m->errorOut(e, "PCACommand", "execute");
280 /**********************************************************************************************************************
281 vector< vector<double> > PCACommand::createMatrix(vector<SharedRAbundFloatVector*> lookupFloat){
283 vector< vector<double> > matrix; matrix.resize(lookupFloat.size());
285 //fill matrix with shared files relative abundances
286 for (int i = 0; i < lookupFloat.size(); i++) {
287 for (int j = 0; j < lookupFloat[i]->getNumBins(); j++) {
288 matrix[i].push_back(lookupFloat[i]->getAbundance(j));
292 vector< vector<double> > transposeMatrix; transposeMatrix.resize(matrix[0].size());
293 for (int i = 0; i < transposeMatrix.size(); i++) {
294 for (int j = 0; j < matrix.size(); j++) {
295 transposeMatrix[i].push_back(matrix[j][i]);
299 matrix = linearCalc.matrix_mult(matrix, transposeMatrix);
303 catch(exception& e) {
304 m->errorOut(e, "PCACommand", "createMatrix");
308 //**********************************************************************************************************************
309 int PCACommand::process(vector<SharedRAbundFloatVector*>& lookupFloat){
311 m->mothurOut("\nProcessing " + lookupFloat[0]->getLabel()); m->mothurOutEndLine();
313 vector< vector<double> > matrix; matrix.resize(lookupFloat.size());
316 string temp = outputDir + "matrix.transpose.out";
317 m->openOutputFile(temp, out);
318 out << "matrix" << endl;
320 //fill matrix with shared files relative abundances
321 for (int i = 0; i < lookupFloat.size(); i++) {
322 for (int j = 0; j < lookupFloat[i]->getNumBins(); j++) {
323 matrix[i].push_back(lookupFloat[i]->getAbundance(j));
324 out << lookupFloat[i]->getAbundance(j) << '\t';
328 out << endl << endl << "transpose" << endl;
329 vector< vector<double> > transposeMatrix; transposeMatrix.resize(matrix[0].size());
330 for (int i = 0; i < transposeMatrix.size(); i++) {
331 for (int j = 0; j < matrix.size(); j++) {
332 transposeMatrix[i].push_back(matrix[j][i]);
333 out << matrix[j][i] << '\t';
338 matrix = linearCalc.matrix_mult(matrix, transposeMatrix);
340 out << endl << endl << "matrix mult" << endl;
341 for (int i = 0; i < matrix.size(); i++) {
342 for (int j = 0; j < matrix[i].size(); j++) {
343 out << matrix[i][j] << '\t';
350 double offset = 0.0000;
353 vector<vector<double> > G = matrix;
354 //vector<vector<double> > copy_G;
356 for(int count=0;count<2;count++){
357 linearCalc.recenter(offset, matrix, G); if (m->control_pressed) { return 0; }
358 linearCalc.tred2(G, d, e); if (m->control_pressed) { return 0; }
359 linearCalc.qtli(d, e, G); if (m->control_pressed) { return 0; }
360 offset = d[d.size()-1];
361 if(offset > 0.0) break;
364 if (m->control_pressed) { return 0; }
366 string fbase = outputDir + m->getRootName(m->getSimpleName(inputFile));
367 string outputFileName = fbase + lookupFloat[0]->getLabel();
368 output(outputFileName, m->Groups, G, d);
372 for (int i = 1; i < 4; i++) {
374 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pca file
376 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
378 double corr = linearCalc.calcPearson(EuclidDists, matrix); //G is the pca file, D is the users distance matrix
380 m->mothurOut("Pearson's coefficient using " + toString(i) + " axis: " + toString(corr)); m->mothurOutEndLine();
382 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
384 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
390 catch(exception& e) {
391 m->errorOut(e, "PCACommand", "process");
395 /*********************************************************************************************************************************/
397 void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
399 int rank = name_list.size();
400 double dsum = 0.0000;
401 for(int i=0;i<rank;i++){
403 for(int j=0;j<rank;j++){
404 if(d[j] >= 0) { G[i][j] *= pow(d[j],0.5); }
405 else { G[i][j] = 0.00000; }
409 ofstream pcaData((fnameRoot+".pca.axes").c_str(), ios::trunc);
410 pcaData.setf(ios::fixed, ios::floatfield);
411 pcaData.setf(ios::showpoint);
412 outputNames.push_back(fnameRoot+".pca.axes");
413 outputTypes["pca"].push_back(fnameRoot+".pca.axes");
415 ofstream pcaLoadings((fnameRoot+".pca.loadings").c_str(), ios::trunc);
416 pcaLoadings.setf(ios::fixed, ios::floatfield);
417 pcaLoadings.setf(ios::showpoint);
418 outputNames.push_back(fnameRoot+".pca.loadings");
419 outputTypes["loadings"].push_back(fnameRoot+".pca.loadings");
421 pcaLoadings << "axis\tloading\n";
422 for(int i=0;i<rank;i++){
423 pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
427 for(int i=0;i<rank;i++){
428 pcaData << '\t' << "axis" << i+1;
432 for(int i=0;i<rank;i++){
433 pcaData << name_list[i] << '\t';
434 for(int j=0;j<rank;j++){
435 pcaData << G[i][j] << '\t';
440 catch(exception& e) {
441 m->errorOut(e, "PCACommand", "output");
445 /*********************************************************************************************************************************/