]> git.donarmstrong.com Git - mothur.git/blob - pcoacommand.cpp
added load.logfile command. changed summary.single output for subsample=t.
[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 //**********************************************************************************************************************
16 vector<string> PCOACommand::setParameters(){    
17         try {
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);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "PCOACommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string PCOACommand::getHelpString(){    
34         try {
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";
41                 return helpString;
42         }
43         catch(exception& e) {
44                 m->errorOut(e, "PCOACommand", "getHelpString");
45                 exit(1);
46         }
47 }
48
49 //**********************************************************************************************************************
50 string PCOACommand::getOutputFileNameTag(string type, string inputName=""){     
51         try {
52         string outputFileName = "";
53                 map<string, vector<string> >::iterator it;
54         
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"); }
58         else {
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;  }
62         }
63         return outputFileName;
64         }
65         catch(exception& e) {
66                 m->errorOut(e, "PCOACommand", "getOutputFileNameTag");
67                 exit(1);
68         }
69 }
70
71 //**********************************************************************************************************************
72 PCOACommand::PCOACommand(){     
73         try {
74                 abort = true; calledHelp = true; 
75                 setParameters();
76                 vector<string> tempOutNames;
77                 outputTypes["pcoa"] = tempOutNames;
78                 outputTypes["loadings"] = tempOutNames;
79         }
80         catch(exception& e) {
81                 m->errorOut(e, "PCOACommand", "PCOACommand");
82                 exit(1);
83         }
84 }
85 //**********************************************************************************************************************
86
87 PCOACommand::PCOACommand(string option)  {
88         try {
89                 abort = false; calledHelp = false;   
90                 
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;}
94                 
95                 else {
96                         vector<string> myArray = setParameters();
97                         
98                         OptionParser parser(option);
99                         map<string, string> parameters = parser. getParameters();
100                         
101                         ValidParameters validParameter;
102                         map<string, string>::iterator it;
103                 
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;  }
107                         }
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 = "";          }
111                         else {
112                                 string path;
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;           }
119                                 }
120                         }
121                         
122                         //initialize outputTypes
123                         vector<string> tempOutNames;
124                         outputTypes["pcoa"] = tempOutNames;
125                         outputTypes["loadings"] = tempOutNames;
126                         
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); } 
136                         
137                         filename = phylipfile;  
138                         
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"){  
141                                 outputDir = ""; 
142                                 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
143                         }
144                         
145                         string temp = validParameter.validFile(parameters, "metric", false);    if (temp == "not found"){       temp = "T";                             }
146                         metric = m->isTrue(temp); 
147                 }
148
149         }
150         catch(exception& e) {
151                 m->errorOut(e, "PCOACommand", "PCOACommand");
152                 exit(1);
153         }
154 }
155 //**********************************************************************************************************************
156 int PCOACommand::execute(){
157         try {
158         
159                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
160                 
161                 cout.setf(ios::fixed, ios::floatfield);
162                 cout.setf(ios::showpoint);
163                 cerr.setf(ios::fixed, ios::floatfield);
164                 cerr.setf(ios::showpoint);
165                 
166                 vector<string> names;
167                 vector<vector<double> > D;
168         
169                 fbase = outputDir + m->getRootName(m->getSimpleName(filename));
170                 
171                 ReadPhylipVector readFile(filename);
172                 names = readFile.read(D);
173                 
174                 if (m->control_pressed) { return 0; }
175         
176                 double offset = 0.0000;
177                 vector<double> d;
178                 vector<double> e;
179                 vector<vector<double> > G = D;
180                 //vector<vector<double> > copy_G;
181                                 
182                 m->mothurOut("\nProcessing...\n");
183                 
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;
190                 } 
191                 
192                 if (m->control_pressed) { return 0; }
193                 
194                 output(fbase, names, G, d);
195                 
196                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
197                 
198                 if (metric) {   
199                         
200                         for (int i = 1; i < 4; i++) {
201                                                         
202                                 vector< vector<double> > EuclidDists = linearCalc.calculateEuclidianDistance(G, i); //G is the pcoa file
203                                 
204                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
205                                 
206                                 double corr = linearCalc.calcPearson(EuclidDists, D); //G is the pcoa file, D is the users distance matrix
207                                 
208                                 m->mothurOut("Rsq " + toString(i) + " axis: " + toString(corr * corr)); m->mothurOutEndLine();
209                                 
210                                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
211                         }
212                 }
213                 
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();
218                 
219                 return 0;
220         }
221         catch(exception& e) {
222                 m->errorOut(e, "PCOACommand", "execute");
223                 exit(1);
224         }
225 }
226 /*********************************************************************************************************************************/
227
228 void PCOACommand::get_comment(istream& f, char begin, char end){
229         try {
230                 char d=f.get();
231                 while(d != end){        d = f.get();    }
232                 d = f.peek();
233         }
234         catch(exception& e) {
235                 m->errorOut(e, "PCOACommand", "get_comment");
236                 exit(1);
237         }
238 }       
239 /*********************************************************************************************************************************/
240
241 void PCOACommand::output(string fnameRoot, vector<string> name_list, vector<vector<double> >& G, vector<double> d) {
242         try {
243                 int rank = name_list.size();
244                 double dsum = 0.0000;
245                 for(int i=0;i<rank;i++){
246                         dsum += d[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;                      }
250                         }
251                 }
252                 
253                 ofstream pcaData;
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);
260                 
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);        
268                 
269                 pcaLoadings << "axis\tloading\n";
270                 for(int i=0;i<rank;i++){
271                         pcaLoadings << i+1 << '\t' << d[i] * 100.0 / dsum << endl;
272                 }
273                 
274                 pcaData << "group";
275                 for(int i=0;i<rank;i++){
276                         pcaData << '\t' << "axis" << i+1;
277                 }
278                 pcaData << endl;
279                 
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';
284                         }
285                         pcaData << endl;
286                 }
287         }
288         catch(exception& e) {
289                 m->errorOut(e, "PCOACommand", "output");
290                 exit(1);
291         }
292 }
293
294 /*********************************************************************************************************************************/
295