]> git.donarmstrong.com Git - mothur.git/blob - pcoacommand.cpp
3cd844a36d5a5c253786b4eb29745388b042d2d4
[mothur.git] / pcoacommand.cpp
1
2 /*
3  *  pcacommand.cpp
4  *  Mothur
5  *
6  *  Created by westcott on 1/4/10.
7  *  Copyright 2010 Schloss Lab. All rights reserved.
8  *
9  */
10
11 #include "pcoacommand.h"
12 #include "readphylipvector.h"
13
14 //**********************************************************************************************************************
15 vector<string> PCOACommand::getValidParameters(){       
16         try {
17                 string Array[] =  {"phylip", "metric","outputdir","inputdir"};
18                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
19                 return myArray;
20         }
21         catch(exception& e) {
22                 m->errorOut(e, "PCOACommand", "getValidParameters");
23                 exit(1);
24         }
25 }
26 //**********************************************************************************************************************
27 PCOACommand::PCOACommand(){     
28         try {
29                 abort = true; calledHelp = true; 
30                 vector<string> tempOutNames;
31                 outputTypes["pcoa"] = tempOutNames;
32                 outputTypes["loadings"] = tempOutNames;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "PCOACommand", "PCOACommand");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 vector<string> PCOACommand::getRequiredParameters(){    
41         try {
42                 string Array[] =  {"phylip"};
43                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
44                 return myArray;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "PCOACommand", "getRequiredParameters");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 vector<string> PCOACommand::getRequiredFiles(){ 
53         try {
54                 vector<string> myArray;
55                 return myArray;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "PCOACommand", "getRequiredFiles");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63
64 PCOACommand::PCOACommand(string option)  {
65         try {
66                 abort = false; calledHelp = false;   
67                 
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; calledHelp = true; }
70                 
71                 else {
72                         //valid paramters for this command
73                         string Array[] =  {"phylip","metric","outputdir", "inputdir"};
74                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string, string> parameters = parser. getParameters();
78                         
79                         ValidParameters validParameter;
80                         map<string, string>::iterator it;
81                 
82                         //check to make sure all parameters are valid for command
83                         for (it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         //if the user changes the input directory command factory will send this info to us in the output parameter 
87                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
88                         if (inputDir == "not found"){   inputDir = "";          }
89                         else {
90                                 string path;
91                                 it = parameters.find("phylip");
92                                 //user has given a template file
93                                 if(it != parameters.end()){ 
94                                         path = m->hasPath(it->second);
95                                         //if the user has not given a path then, add inputdir. else leave path alone.
96                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
97                                 }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["pcoa"] = tempOutNames;
103                         outputTypes["loadings"] = tempOutNames;
104                         
105                         //required parameters
106                         phylipfile = validParameter.validFile(parameters, "phylip", true);
107                         if (phylipfile == "not open") { abort = true; }
108                         else if (phylipfile == "not found") { phylipfile = ""; abort = true; }  
109                         else {  filename = phylipfile;  }
110                         
111                         //if the user changes the output directory command factory will send this info to us in the output parameter 
112                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
113                                 outputDir = ""; 
114                                 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
115                         }
116                         
117                         //error checking on files       
118                         if (phylipfile == "")   { m->mothurOut("You must provide a distance file before running the pcoa command."); m->mothurOutEndLine(); abort = true; }             
119                 
120                         string temp = validParameter.validFile(parameters, "metric", false);    if (temp == "not found"){       temp = "T";                             }
121                         metric = m->isTrue(temp); 
122                 }
123
124         }
125         catch(exception& e) {
126                 m->errorOut(e, "PCOACommand", "PCOACommand");
127                 exit(1);
128         }
129 }
130 //**********************************************************************************************************************
131 void PCOACommand::help(){
132         try {
133         
134                 m->mothurOut("The pcoa command parameters are phylip and metric"); m->mothurOutEndLine();
135                 m->mothurOut("The phylip parameter allows you to enter your distance file."); m->mothurOutEndLine();
136                 m->mothurOut("The metric parameter allows indicate you if would like the pearson correlation coefficient calculated. Default=True"); m->mothurOutEndLine();
137                 m->mothurOut("Example pcoa(phylip=yourDistanceFile).\n");
138                 m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n\n");
139         }
140         catch(exception& e) {
141                 m->errorOut(e, "PCOACommand", "help");
142                 exit(1);
143         }
144 }
145 //**********************************************************************************************************************
146 PCOACommand::~PCOACommand(){}
147 //**********************************************************************************************************************
148 int PCOACommand::execute(){
149         try {
150         
151                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
152                 
153                 cout.setf(ios::fixed, ios::floatfield);
154                 cout.setf(ios::showpoint);
155                 cerr.setf(ios::fixed, ios::floatfield);
156                 cerr.setf(ios::showpoint);
157                 
158                 vector<string> names;
159                 vector<vector<double> > D;
160         
161                 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
162                 
163                 ReadPhylipVector readFile(filename);
164                 names = readFile.read(D);
165                 
166                 if (m->control_pressed) { return 0; }
167         
168                 double offset = 0.0000;
169                 vector<double> d;
170                 vector<double> e;
171                 vector<vector<double> > G = D;
172                 //vector<vector<double> > copy_G;
173                                 
174                 m->mothurOut("\nProcessing...\n\n");
175                 
176                 for(int count=0;count<2;count++){
177                         linearCalc.recenter(offset, D, G);              if (m->control_pressed) { return 0; }
178                         linearCalc.tred2(G, d, e);                              if (m->control_pressed) { return 0; }
179                         linearCalc.qtli(d, e, G);                               if (m->control_pressed) { return 0; }
180                         offset = d[d.size()-1];
181                         if(offset > 0.0) break;
182                 } 
183                 
184                 if (m->control_pressed) { return 0; }
185                 
186                 output(fbase, names, G, d);
187                 
188                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
189                 
190                 if (metric) {   
191                         
192                         for (int i = 1; i < 4; i++) {
193                                                         
194                                 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
195                                 
196                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
197                                 
198                                 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
199                                 
200                                 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
201                                 
202                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
203                         }
204                 }
205                 
206                 m->mothurOutEndLine();
207                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
208                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
209                 m->mothurOutEndLine();
210                 
211                 return 0;
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "PCOACommand", "execute");
215                 exit(1);
216         }
217 }
218 /*********************************************************************************************************************************/
219
220 void PCOACommand::get_comment(istream& f, char begin, char end){
221         try {
222                 char d=f.get();
223                 while(d != end){        d = f.get();    }
224                 d = f.peek();
225         }
226         catch(exception& e) {
227                 m->errorOut(e, "PCOACommand", "get_comment");
228                 exit(1);
229         }
230 }       
231 /*********************************************************************************************************************************/
232
233 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
234         try {
235                 int rank = name_list.size();
236                 double dsum = 0.0000;
237                 for(int i=0;i<rank;i++){
238                         dsum += d[i];
239                         for(int j=0;j<rank;j++){
240                                 if(d[j] >= 0)   {       G[i][j] *= pow(d[j],0.5);       }
241                                 else                    {       G[i][j] = 0.00000;                      }
242                         }
243                 }
244                 
245                 ofstream pcaData((fnameRoot+"pcoa.axes").c_str(), ios::trunc);
246                 pcaData.setf(ios::fixed, ios::floatfield);
247                 pcaData.setf(ios::showpoint);   
248                 outputNames.push_back(fnameRoot+"pcoa.axes");
249                 outputTypes["pcoa"].push_back(fnameRoot+"pcoa.axes");
250                 
251                 ofstream pcaLoadings((fnameRoot+"pcoa.loadings").c_str(), ios::trunc);
252                 pcaLoadings.setf(ios::fixed, ios::floatfield);
253                 pcaLoadings.setf(ios::showpoint);
254                 outputNames.push_back(fnameRoot+"pcoa.loadings");
255                 outputTypes["loadings"].push_back(fnameRoot+"pcoa.loadings");   
256                 
257                 pcaLoadings << "axis\tloading\n";
258                 for(int i=0;i<rank;i++){
259                         pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
260                 }
261                 
262                 pcaData << "group";
263                 for(int i=0;i<rank;i++){
264                         pcaData << '\t' << "axis" << i+1;
265                 }
266                 pcaData << endl;
267                 
268                 for(int i=0;i<rank;i++){
269                         pcaData << name_list[i] << '\t';
270                         for(int j=0;j<rank;j++){
271                                 pcaData << G[i][j] << '\t';
272                         }
273                         pcaData << endl;
274                 }
275         }
276         catch(exception& e) {
277                 m->errorOut(e, "PCOACommand", "output");
278                 exit(1);
279         }
280 }
281
282 /*********************************************************************************************************************************/
283