]> git.donarmstrong.com Git - mothur.git/blob - pcacommand.cpp
added citation function to commands
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
74                 
75                 else {
76                         vector<string> myArray = setParameters();
77                         
78                         OptionParser parser(option);
79                         map<string, string> parameters = parser. getParameters();
80                         
81                         ValidParameters validParameter;
82                         map<string, string>::iterator it;
83                         
84                         //check to make sure all parameters are valid for command
85                         for (it = parameters.begin(); it != parameters.end(); it++) { 
86                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
87                         }
88         
89                         //initialize outputTypes
90                         vector<string> tempOutNames;
91                         outputTypes["pca"] = tempOutNames;
92                         outputTypes["loadings"] = tempOutNames;
93                         
94                         //if the user changes the input directory command factory will send this info to us in the output parameter 
95                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
96                         if (inputDir == "not found"){   inputDir = "";          }
97                         else {
98                                 string path;
99                                 it = parameters.find("shared");
100                                 //user has given a template file
101                                 if(it != parameters.end()){ 
102                                         path = m->hasPath(it->second);
103                                         //if the user has not given a path then, add inputdir. else leave path alone.
104                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
105                                 }
106                                 
107                                 it = parameters.find("relabund");
108                                 //user has given a template file
109                                 if(it != parameters.end()){ 
110                                         path = m->hasPath(it->second);
111                                         //if the user has not given a path then, add inputdir. else leave path alone.
112                                         if (path == "") {       parameters["relabund"] = inputDir + it->second;         }
113                                 }
114                         }
115                         
116                         //check for required parameters
117                         sharedfile = validParameter.validFile(parameters, "shared", true);
118                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
119                         else if (sharedfile == "not found") { sharedfile = ""; }
120                         else {  mode = "sharedfile"; inputFile = sharedfile; }
121                         
122                         relabundfile = validParameter.validFile(parameters, "relabund", true);
123                         if (relabundfile == "not open") { relabundfile = ""; abort = true; }    
124                         else if (relabundfile == "not found") { relabundfile = ""; }
125                         else {  mode = "relabund"; inputFile = relabundfile; }
126                         
127                         
128                         if ((sharedfile == "") && (relabundfile == "")) { 
129                                 //is there are current file available for any of these?
130                                 //give priority to shared, then list, then rabund, then sabund
131                                 //if there is a current shared file, use it
132                                 sharedfile = m->getSharedFile(); 
133                                 if (sharedfile != "") { inputFile = sharedfile; mode = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
134                                 else { 
135                                         relabundfile = m->getRelAbundFile(); 
136                                         if (relabundfile != "") { inputFile = relabundfile; mode = "relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
137                                         else { 
138                                                 m->mothurOut("No valid current files. You must provide a relabund or shared file."); m->mothurOutEndLine(); 
139                                                 abort = true;
140                                         }
141                                 }
142                         }
143                                 
144                         //if the user changes the output directory command factory will send this info to us in the output parameter 
145                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
146                                 outputDir = ""; 
147                                 outputDir += m->hasPath(inputFile); //if user entered a file with a path then preserve it       
148                         }
149                                                 
150                         string temp = validParameter.validFile(parameters, "metric", false);    if (temp == "not found"){       temp = "T";                             }
151                         metric = m->isTrue(temp); 
152                         
153                         label = validParameter.validFile(parameters, "label", false);                   
154                         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(); } }
155                         else { m->splitAtDash(label, labels); }
156                         
157                         groups = validParameter.validFile(parameters, "groups", false);                 
158                         if (groups == "not found") { groups = "";  }
159                         else { m->splitAtDash(groups, Groups);  }                       
160                         m->Groups = Groups;                     
161                         
162                 }
163                 
164         }
165         catch(exception& e) {
166                 m->errorOut(e, "PCACommand", "PCACommand");
167                 exit(1);
168         }
169 }
170 //**********************************************************************************************************************
171 int PCACommand::execute(){
172         try {
173                 
174                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
175                 
176                 cout.setf(ios::fixed, ios::floatfield);
177                 cout.setf(ios::showpoint);
178                 cerr.setf(ios::fixed, ios::floatfield);
179                 cerr.setf(ios::showpoint);
180                 
181                 //get first line of shared file
182                 vector< vector<double> > matrix;
183                 InputData* input;
184                 if (mode == "sharedfile")                       {  
185                         input = new InputData(inputFile, "sharedfile");
186                 }else if (mode == "relabund")   { 
187                         input = new InputData(inputFile, "relabund");
188                 }else {  m->mothurOut("[ERROR]: filetype not recognized."); m->mothurOutEndLine();  return 0; }
189                 
190                 vector<SharedRAbundFloatVector*> lookupFloat = input->getSharedRAbundFloatVectors();
191                 string lastLabel = lookupFloat[0]->getLabel();
192                         
193                 set<string> processedLabels;
194                 set<string> userLabels = labels;
195                 
196                 //if the user gave no labels, then use the first one read
197                 if (labels.size() == 0) { 
198                         label = lastLabel;  
199                         
200                         process(lookupFloat);
201                 }
202                 
203                 //as long as you are not at the end of the file or done wih the lines you want
204                 while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
205                         
206                         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                         
208                         if(labels.count(lookupFloat[0]->getLabel()) == 1){
209                                 processedLabels.insert(lookupFloat[0]->getLabel());
210                                 userLabels.erase(lookupFloat[0]->getLabel());
211                                 
212                                 process(lookupFloat);
213                         }
214                         
215                         if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
216                                 string saveLabel = lookupFloat[0]->getLabel();
217                                 
218                                 for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  }  lookupFloat.clear();
219                                 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
220                                 
221                                 process(lookupFloat);
222                                 
223                                 processedLabels.insert(lookupFloat[0]->getLabel());
224                                 userLabels.erase(lookupFloat[0]->getLabel());
225                                 
226                                 //restore real lastlabel to save below
227                                 lookupFloat[0]->setLabel(saveLabel);
228                         }
229                         
230                         lastLabel = lookupFloat[0]->getLabel();                 
231                         
232                         //get next line to process
233                         //prevent memory leak
234                         for (int i = 0; i < lookupFloat.size(); i++) {  delete lookupFloat[i];  } lookupFloat.clear();
235                         lookupFloat = input->getSharedRAbundFloatVectors();
236                 }
237                 
238                 
239                 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                 
241                 //output error messages about any remaining user labels
242                 set<string>::iterator it;
243                 bool needToRun = false;
244                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
245                         m->mothurOut("Your file does not include the label " + *it); 
246                         if (processedLabels.count(lastLabel) != 1) {
247                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
248                                 needToRun = true;
249                         }else {
250                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
251                         }
252                 }
253                 
254                 //run last label if you need to
255                 if (needToRun == true)  {
256                         for (int i = 0; i < lookupFloat.size(); i++) {  if (lookupFloat[i] != NULL) {   delete lookupFloat[i];  } }  lookupFloat.clear();
257                         lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
258                         
259                         process(lookupFloat);
260                         
261                         for (int i = 0; i < lookupFloat.size(); i++) {  if (lookupFloat[i] != NULL) {   delete lookupFloat[i];  } } lookupFloat.clear();
262                 }       
263                 
264                 for (int i = 0; i < lookupFloat.size(); i++) {  if (lookupFloat[i] != NULL) {   delete lookupFloat[i];  } } lookupFloat.clear();
265                 delete input;
266                 
267                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
268                 
269                 m->mothurOutEndLine();
270                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
271                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
272                 m->mothurOutEndLine();
273                 
274                 return 0;
275         }
276         catch(exception& e) {
277                 m->errorOut(e, "PCACommand", "execute");
278                 exit(1);
279         }
280 }
281
282 /**********************************************************************************************************************
283 vector< vector<double> > PCACommand::createMatrix(vector<SharedRAbundFloatVector*> lookupFloat){
284         try {
285                 vector< vector<double> > matrix; matrix.resize(lookupFloat.size());
286                 
287                 //fill matrix with shared files relative abundances
288                 for (int i = 0; i < lookupFloat.size(); i++) {
289                         for (int j = 0; j < lookupFloat[i]->getNumBins(); j++) {
290                                 matrix[i].push_back(lookupFloat[i]->getAbundance(j));
291                         }
292                 }
293                 
294                 vector< vector<double> > transposeMatrix; transposeMatrix.resize(matrix[0].size());
295                 for (int i = 0; i < transposeMatrix.size(); i++) {
296                         for (int j = 0; j < matrix.size(); j++) {
297                                 transposeMatrix[i].push_back(matrix[j][i]);
298                         }
299                 }
300                 
301                 matrix = linearCalc.matrix_mult(matrix, transposeMatrix);
302                 
303                 return matrix;
304         }
305         catch(exception& e) {
306                 m->errorOut(e, "PCACommand", "createMatrix");   
307                 exit(1);
308         }
309 }*/
310 //**********************************************************************************************************************
311
312 int PCACommand::process(vector<SharedRAbundFloatVector*>& lookupFloat){
313         try {
314                 m->mothurOut("\nProcessing " + lookupFloat[0]->getLabel()); m->mothurOutEndLine();
315         
316                 int numOTUs = lookupFloat[0]->getNumBins();
317                 int numSamples = lookupFloat.size();
318                 
319                 vector< vector<double> > matrix(numSamples);
320                 vector<double> colMeans(numOTUs);
321                 
322                                 
323                 //fill matrix with shared relative abundances, re-center
324                 for (int i = 0; i < lookupFloat.size(); i++) {
325                         matrix[i].resize(numOTUs, 0);
326                         
327                         for (int j = 0; j < numOTUs; j++) {
328                                 matrix[i][j] = lookupFloat[i]->getAbundance(j);
329                                 colMeans[j] += matrix[i][j];
330                         }
331                 }
332                 
333
334                 for(int j=0;j<numOTUs;j++){
335                         colMeans[j] = colMeans[j] / (double)numSamples;
336                 }
337                 
338                 vector<vector<double> > centered = matrix;
339                 for(int i=0;i<numSamples;i++){
340                         for(int j=0;j<numOTUs;j++){
341                                 centered[i][j] = centered[i][j] - colMeans[j];                          
342                         }
343                 }
344
345                 
346                 vector< vector<double> > transpose(numOTUs);
347                 for (int i = 0; i < numOTUs; i++) {
348                         transpose[i].resize(numSamples, 0);
349                         
350                         for (int j = 0; j < numSamples; j++) {
351                                 transpose[i][j] = centered[j][i];
352                         }
353                 }
354
355                 vector<vector<double> > crossProduct = linearCalc.matrix_mult(transpose, centered);     
356                 
357                 vector<double> d;
358                 vector<double> e;
359
360                 linearCalc.tred2(crossProduct, d, e);           if (m->control_pressed) { return 0; }
361                 linearCalc.qtli(d, e, crossProduct);            if (m->control_pressed) { return 0; }
362                 
363                 vector<vector<double> > X = linearCalc.matrix_mult(centered, crossProduct);
364                 
365                 if (m->control_pressed) { return 0; }
366                 
367                 string fbase = outputDir + m->getRootName(m->getSimpleName(inputFile));
368                 string outputFileName = fbase + lookupFloat[0]->getLabel();
369                 output(outputFileName, m->Groups, X, d);
370                 
371                 if (metric) {   
372                         
373                         vector<vector<double> > observedEuclideanDistance = linearCalc.getObservedEuclideanDistance(centered);
374                         
375                         for (int i = 1; i < 4; i++) {
376                                 
377                                 vector< vector<double> > PCAEuclidDists = linearCalc.calculateEuclidianDistance(X, i); //G is the pca file
378                                 
379                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
380
381                                 double corr = linearCalc.calcPearson(PCAEuclidDists, observedEuclideanDistance);
382                                                                 
383                                 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
384                                 
385                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
386                         }
387                 }
388                 
389                 return 0;
390         }
391         catch(exception& e) {
392                 m->errorOut(e, "PCACommand", "process");        
393                 exit(1);
394         }
395 }
396 /*********************************************************************************************************************************/
397
398 void PCACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
399         try {
400
401                 int numEigenValues = d.size();
402                 double dsum = 0.0000;
403                 for(int i=0;i<numEigenValues;i++){
404                         dsum += d[i];
405                 }
406                 
407                 ofstream pcaData((fnameRoot+".pca.axes").c_str(), ios::trunc);
408                 pcaData.setf(ios::fixed, ios::floatfield);
409                 pcaData.setf(ios::showpoint);   
410                 outputNames.push_back(fnameRoot+".pca.axes");
411                 outputTypes["pca"].push_back(fnameRoot+".pca.axes");
412                 
413                 ofstream pcaLoadings((fnameRoot+".pca.loadings").c_str(), ios::trunc);
414                 pcaLoadings.setf(ios::fixed, ios::floatfield);
415                 pcaLoadings.setf(ios::showpoint);
416                 outputNames.push_back(fnameRoot+".pca.loadings");
417                 outputTypes["loadings"].push_back(fnameRoot+".pca.loadings");   
418                 
419                 pcaLoadings << "axis\tloading\n";
420                 for(int i=0;i<numEigenValues;i++){
421                         pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
422                 }
423                 
424                 pcaData << "group";
425                 for(int i=0;i<numEigenValues;i++){
426                         pcaData << '\t' << "axis" << i+1;
427                 }
428                 pcaData << endl;
429                 
430                 for(int i=0;i<numEigenValues;i++){
431                         pcaData << name_list[i] << '\t';
432                         for(int j=0;j<numEigenValues;j++){
433                                 pcaData << G[i][j] << '\t';
434                         }
435                         pcaData << endl;
436                 }
437         }
438         catch(exception& e) {
439                 m->errorOut(e, "PCACommand", "output");
440                 exit(1);
441         }
442 }
443 /*********************************************************************************************************************************/
444
445