]> git.donarmstrong.com Git - mothur.git/blob - pcacommand.cpp
pat's fixes to pca command
[mothur.git] / pcacommand.cpp
1 /*
2  *  pcacommand.cpp
3  *  mothur
4  *
5  *  Created by westcott on 1/7/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "pcacommand.h"
11 #include "inputdata.h"
12
13 //**********************************************************************************************************************
14 vector<string> PCACommand::setParameters(){     
15         try {
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);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "PCACommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string PCACommand::getHelpString(){     
35         try {
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 you to indicate 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";
44                 return helpString;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "PCACommand", "getHelpString");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 PCACommand::PCACommand(){       
53         try {
54                 abort = true; calledHelp = true; 
55                 setParameters();
56                 vector<string> tempOutNames;
57                 outputTypes["pca"] = tempOutNames;
58                 outputTypes["loadings"] = tempOutNames;
59         }
60         catch(exception& e) {
61                 m->errorOut(e, "PCACommand", "PCACommand");
62                 exit(1);
63         }
64 }
65 //**********************************************************************************************************************
66
67 PCACommand::PCACommand(string option)  {
68         try {
69                 abort = false; calledHelp = false;   
70                 
71                 //allow user to run help
72                 if(option == "help") { help(); abort = true; calledHelp = true; }
73                 
74                 else {
75                         vector<string> myArray = setParameters();
76                         
77                         OptionParser parser(option);
78                         map<string, string> parameters = parser. getParameters();
79                         
80                         ValidParameters validParameter;
81                         map<string, string>::iterator it;
82                         
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;  }
86                         }
87         
88                         //initialize outputTypes
89                         vector<string> tempOutNames;
90                         outputTypes["pca"] = tempOutNames;
91                         outputTypes["loadings"] = tempOutNames;
92                         
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 = "";          }
96                         else {
97                                 string path;
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;           }
104                                 }
105                                 
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;         }
112                                 }
113                         }
114                         
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; }
120                         
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; }
125                         
126                         
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(); }
133                                 else { 
134                                         relabundfile = m->getRelAbundFile(); 
135                                         if (relabundfile != "") { inputFile = relabundfile; mode = "relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
136                                         else { 
137                                                 m->mothurOut("No valid current files. You must provide a relabund or shared file."); m->mothurOutEndLine(); 
138                                                 abort = true;
139                                         }
140                                 }
141                         }
142                                 
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"){  
145                                 outputDir = ""; 
146                                 outputDir += m->hasPath(inputFile); //if user entered a file with a path then preserve it       
147                         }
148                                                 
149                         string temp = validParameter.validFile(parameters, "metric", false);    if (temp == "not found"){       temp = "T";                             }
150                         metric = m->isTrue(temp); 
151                         
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); }
155                         
156                         groups = validParameter.validFile(parameters, "groups", false);                 
157                         if (groups == "not found") { groups = "";  }
158                         else { m->splitAtDash(groups, Groups);  }                       
159                         m->Groups = Groups;                     
160                         
161                 }
162                 
163         }
164         catch(exception& e) {
165                 m->errorOut(e, "PCACommand", "PCACommand");
166                 exit(1);
167         }
168 }
169 //**********************************************************************************************************************
170 int PCACommand::execute(){
171         try {
172                 
173                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
174                 
175                 cout.setf(ios::fixed, ios::floatfield);
176                 cout.setf(ios::showpoint);
177                 cerr.setf(ios::fixed, ios::floatfield);
178                 cerr.setf(ios::showpoint);
179                 
180                 //get first line of shared file
181                 vector< vector<double> > matrix;
182                 InputData* input;
183                 if (mode == "sharedfile")                       {  
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; }
188                 
189                 vector<SharedRAbundFloatVector*> lookupFloat = input->getSharedRAbundFloatVectors();
190                 string lastLabel = lookupFloat[0]->getLabel();
191                         
192                 set<string> processedLabels;
193                 set<string> userLabels = labels;
194                 
195                 //if the user gave no labels, then use the first one read
196                 if (labels.size() == 0) { 
197                         label = lastLabel;  
198                         
199                         process(lookupFloat);
200                 }
201                 
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)) {
204                         
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;  }
206                         
207                         if(labels.count(lookupFloat[0]->getLabel()) == 1){
208                                 processedLabels.insert(lookupFloat[0]->getLabel());
209                                 userLabels.erase(lookupFloat[0]->getLabel());
210                                 
211                                 process(lookupFloat);
212                         }
213                         
214                         if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
215                                 string saveLabel = lookupFloat[0]->getLabel();
216                                 
217                                 for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }  lookupFloat.clear();
218                                 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
219                                 
220                                 process(lookupFloat);
221                                 
222                                 processedLabels.insert(lookupFloat[0]->getLabel());
223                                 userLabels.erase(lookupFloat[0]->getLabel());
224                                 
225                                 //restore real lastlabel to save below
226                                 lookupFloat[0]->setLabel(saveLabel);
227                         }
228                         
229                         lastLabel = lookupFloat[0]->getLabel();                 
230                         
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();
235                 }
236                 
237                 
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;  }
239                 
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();
247                                 needToRun = true;
248                         }else {
249                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
250                         }
251                 }
252                 
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);
257                         
258                         process(lookupFloat);
259                         
260                         for (int i = 0; i < lookupFloat.size(); i++) {  if (lookupFloat[i] != NULL) {   delete lookupFloat[i];  } } lookupFloat.clear();
261                 }       
262                 
263                 for (int i = 0; i < lookupFloat.size(); i++) {  if (lookupFloat[i] != NULL) {   delete lookupFloat[i];  } } lookupFloat.clear();
264                 delete input;
265                 
266                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
267                 
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();
272                 
273                 return 0;
274         }
275         catch(exception& e) {
276                 m->errorOut(e, "PCACommand", "execute");
277                 exit(1);
278         }
279 }
280
281 /**********************************************************************************************************************
282 vector< vector<double> > PCACommand::createMatrix(vector<SharedRAbundFloatVector*> lookupFloat){
283         try {
284                 vector< vector<double> > matrix; matrix.resize(lookupFloat.size());
285                 
286                 //fill matrix with shared files relative abundances
287                 for (int i = 0; i < lookupFloat.size(); i++) {
288                         for (int j = 0; j < lookupFloat[i]->getNumBins(); j++) {
289                                 matrix[i].push_back(lookupFloat[i]->getAbundance(j));
290                         }
291                 }
292                 
293                 vector< vector<double> > transposeMatrix; transposeMatrix.resize(matrix[0].size());
294                 for (int i = 0; i < transposeMatrix.size(); i++) {
295                         for (int j = 0; j < matrix.size(); j++) {
296                                 transposeMatrix[i].push_back(matrix[j][i]);
297                         }
298                 }
299                 
300                 matrix = linearCalc.matrix_mult(matrix, transposeMatrix);
301                 
302                 return matrix;
303         }
304         catch(exception& e) {
305                 m->errorOut(e, "PCACommand", "createMatrix");   
306                 exit(1);
307         }
308 }*/
309 //**********************************************************************************************************************
310
311 int PCACommand::process(vector<SharedRAbundFloatVector*>& lookupFloat){
312         try {
313                 m->mothurOut("\nProcessing " + lookupFloat[0]->getLabel()); m->mothurOutEndLine();
314         
315                 int numOTUs = lookupFloat[0]->getNumBins();
316                 int numSamples = lookupFloat.size();
317                 
318                 vector< vector<double> > matrix(numSamples);
319                 vector<double> colMeans(numOTUs);
320                 
321                                 
322                 //fill matrix with shared relative abundances, re-center
323                 for (int i = 0; i < lookupFloat.size(); i++) {
324                         matrix[i].resize(numOTUs, 0);
325                         
326                         for (int j = 0; j < numOTUs; j++) {
327                                 matrix[i][j] = lookupFloat[i]->getAbundance(j);
328                                 colMeans[j] += matrix[i][j];
329                         }
330                 }
331                 
332
333                 for(int j=0;j<numOTUs;j++){
334                         colMeans[j] = colMeans[j] / (double)numSamples;
335                 }
336                 
337                 vector<vector<double> > centered = matrix;
338                 for(int i=0;i<numSamples;i++){
339                         for(int j=0;j<numOTUs;j++){
340                                 centered[i][j] = centered[i][j] - colMeans[j];                          
341                         }
342                 }
343
344                 
345                 vector< vector<double> > transpose(numOTUs);
346                 for (int i = 0; i < numOTUs; i++) {
347                         transpose[i].resize(numSamples, 0);
348                         
349                         for (int j = 0; j < numSamples; j++) {
350                                 transpose[i][j] = centered[j][i];
351                         }
352                 }
353
354                 vector<vector<double> > crossProduct = linearCalc.matrix_mult(transpose, centered);     
355                 
356                 vector<double> d;
357                 vector<double> e;
358
359                 linearCalc.tred2(crossProduct, d, e);           if (m->control_pressed) { return 0; }
360                 linearCalc.qtli(d, e, crossProduct);            if (m->control_pressed) { return 0; }
361                 
362                 vector<vector<double> > X = linearCalc.matrix_mult(centered, crossProduct);
363                 
364                 if (m->control_pressed) { return 0; }
365                 
366                 string fbase = outputDir + m->getRootName(m->getSimpleName(inputFile));
367                 string outputFileName = fbase + lookupFloat[0]->getLabel();
368                 output(outputFileName, m->Groups, X, d);
369                 
370                 if (metric) {   
371                         
372                         vector<vector<double> > observedEuclideanDistance = linearCalc.getObservedEuclideanDistance(centered);
373                         
374                         for (int i = 1; i < 4; i++) {
375                                 
376                                 vector< vector<double> > PCAEuclidDists = linearCalc.calculateEuclidianDistance(X, i); //G is the pca file
377                                 
378                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
379
380                                 double corr = linearCalc.calcPearson(PCAEuclidDists, observedEuclideanDistance);
381                                                                 
382                                 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
383                                 
384                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
385                         }
386                 }
387                 
388                 return 0;
389         }
390         catch(exception& e) {
391                 m->errorOut(e, "PCACommand", "process");        
392                 exit(1);
393         }
394 }
395 /*********************************************************************************************************************************/
396
397 void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
398         try {
399
400                 int numEigenValues = d.size();
401                 double dsum = 0.0000;
402                 for(int i=0;i<numEigenValues;i++){
403                         dsum += d[i];
404                 }
405                 
406                 ofstream pcaData((fnameRoot+".pca.axes").c_str(), ios::trunc);
407                 pcaData.setf(ios::fixed, ios::floatfield);
408                 pcaData.setf(ios::showpoint);   
409                 outputNames.push_back(fnameRoot+".pca.axes");
410                 outputTypes["pca"].push_back(fnameRoot+".pca.axes");
411                 
412                 ofstream pcaLoadings((fnameRoot+".pca.loadings").c_str(), ios::trunc);
413                 pcaLoadings.setf(ios::fixed, ios::floatfield);
414                 pcaLoadings.setf(ios::showpoint);
415                 outputNames.push_back(fnameRoot+".pca.loadings");
416                 outputTypes["loadings"].push_back(fnameRoot+".pca.loadings");   
417                 
418                 pcaLoadings << "axis\tloading\n";
419                 for(int i=0;i<numEigenValues;i++){
420                         pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
421                 }
422                 
423                 pcaData << "group";
424                 for(int i=0;i<numEigenValues;i++){
425                         pcaData << '\t' << "axis" << i+1;
426                 }
427                 pcaData << endl;
428                 
429                 for(int i=0;i<numEigenValues;i++){
430                         pcaData << name_list[i] << '\t';
431                         for(int j=0;j<numEigenValues;j++){
432                                 pcaData << G[i][j] << '\t';
433                         }
434                         pcaData << endl;
435                 }
436         }
437         catch(exception& e) {
438                 m->errorOut(e, "PCACommand", "output");
439                 exit(1);
440         }
441 }
442 /*********************************************************************************************************************************/
443
444