]> git.donarmstrong.com Git - mothur.git/blob - sensspeccommand.cpp
working on current change
[mothur.git] / sensspeccommand.cpp
1 /*
2  *  sensspeccommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 7/6/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "sensspeccommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> SensSpecCommand::setParameters(){        
14         try {
15                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
16                 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
17                 //CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
18                 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pcolumn);
19                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
20                 CommandParameter pcutoff("cutoff", "Number", "", "-1.00", "", "", "",false,false); parameters.push_back(pcutoff);
21                 CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
22                 CommandParameter phard("hard", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(phard);
23                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
25                 
26                 vector<string> myArray;
27                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
28                 return myArray;
29         }
30         catch(exception& e) {
31                 m->errorOut(e, "SensSpecCommand", "setParameters");
32                 exit(1);
33         }
34 }
35 //**********************************************************************************************************************
36 string SensSpecCommand::getHelpString(){        
37         try {
38                 string helpString = "";
39                 helpString += "The sens.spec command....\n";
40                 return helpString;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "SensSpecCommand", "getHelpString");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 SensSpecCommand::SensSpecCommand(){     
49         try {
50                 abort = true; calledHelp = true; 
51                 setParameters();
52                 vector<string> tempOutNames;
53                 outputTypes["sensspec"] = tempOutNames;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "SensSpecCommand", "SensSpecCommand");
57                 exit(1);
58         }
59 }
60 //***************************************************************************************************************
61
62 SensSpecCommand::SensSpecCommand(string option)  {
63         try {
64                 
65                 abort = false; calledHelp = false;   
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; calledHelp = true; }
69                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
70                 
71                 else {
72                         string temp;
73                         
74                         vector<string> myArray = setParameters();
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                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["sensspec"] = tempOutNames;
90                         
91                         //if the user changes the input directory command factory will send this info to us in the output parameter 
92                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
93                         if (inputDir == "not found"){   inputDir = "";          }
94                         else {
95                                 string path;
96                                 it = parameters.find("list");
97                                 //user has given a template file
98                                 if(it != parameters.end()){ 
99                                         path = m->hasPath(it->second);
100                                         //if the user has not given a path then, add inputdir. else leave path alone.
101                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
102                                 }
103                                 
104                                 it = parameters.find("phylip");
105                                 //user has given a template file
106                                 if(it != parameters.end()){ 
107                                         path = m->hasPath(it->second);
108                                         //if the user has not given a path then, add inputdir. else leave path alone.
109                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
110                                 }
111                                 
112                                 it = parameters.find("column");
113                                 //user has given a template file
114                                 if(it != parameters.end()){ 
115                                         path = m->hasPath(it->second);
116                                         //if the user has not given a path then, add inputdir. else leave path alone.
117                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
118                                 }
119                                 
120                                 //it = parameters.find("name");
121                                 //user has given a template file
122                                 //if(it != parameters.end()){ 
123                                         //path = m->hasPath(it->second);
124                                         //if the user has not given a path then, add inputdir. else leave path alone.
125                                         //if (path == "") {     parameters["name"] = inputDir + it->second;             }
126                                 //}
127                                 
128                         }
129                         //check for required parameters
130                         listFile = validParameter.validFile(parameters, "list", true);
131                         if (listFile == "not found") {          
132                                 listFile = m->getListFile(); 
133                                 if (listFile != "") { m->mothurOut("Using " + listFile + " as input file for the list parameter."); m->mothurOutEndLine(); }
134                                 else {  m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
135                         }
136                         else if (listFile == "not open") { abort = true; }      
137                         else { m->setListFile(listFile); }
138                         
139                         phylipfile = validParameter.validFile(parameters, "phylip", true);
140                         if (phylipfile == "not found") { phylipfile = "";  }
141                         else if (phylipfile == "not open") { abort = true; }    
142                         else { distFile = phylipfile; format = "phylip"; m->setPhylipFile(phylipfile);  }
143                         
144                         columnfile = validParameter.validFile(parameters, "column", true);
145                         if (columnfile == "not found") { columnfile = ""; }
146                         else if (columnfile == "not open") { abort = true; }    
147                         else { distFile = columnfile; format = "column";   m->setColumnFile(columnfile); }
148                         
149                         if ((phylipfile == "") && (columnfile == "")) { //is there are current file available for either of these?
150                                 //give priority to column, then phylip
151                                 columnfile = m->getColumnFile(); 
152                                 if (columnfile != "") {  distFile = columnfile; format = "column";  m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
153                                 else { 
154                                         phylipfile = m->getPhylipFile(); 
155                                         if (phylipfile != "") {  distFile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
156                                         else { 
157                                                 m->mothurOut("No valid current files. You must provide a phylip or column file."); m->mothurOutEndLine(); 
158                                                 abort = true;
159                                         }
160                                 }
161                         }else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a sens.spec command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
162                         
163                         
164                         //if the user changes the output directory command factory will send this info to us in the output parameter 
165                         outputDir = validParameter.validFile(parameters, "outputdir", false);
166                         if (outputDir == "not found"){  
167                                 outputDir = ""; 
168                                 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it        
169                         }
170
171                         //check for optional parameter and set defaults
172                         // ...at some point should added some additional type checking...
173                         temp = validParameter.validFile(parameters, "hard", false);
174                         if (temp == "not found"){       hard = 0;       }
175                         else if(!m->isTrue(temp))       {       hard = 0;       }
176                         else if(m->isTrue(temp))        {       hard = 1;       }
177                         
178 //                      temp = validParameter.validFile(parameters, "name", true);
179 //                      if (temp == "not found")        {       nameFile = "";          }
180 //                      else if(temp == "not open")     {       abort = true;           }
181 //                      else                                            {       nameFile = temp;        }
182 //                      cout << "name:\t" << nameFile << endl;
183
184                         temp = validParameter.validFile(parameters, "cutoff", false);           if (temp == "not found") { temp = "-1.00"; }
185                         convert(temp, cutoff);  
186 //                      cout << cutoff << endl;
187                         
188                         temp = validParameter.validFile(parameters, "precision", false);        if (temp == "not found") { temp = "100"; }
189                         convert(temp, precision);  
190 //                      cout << precision << endl;
191                         
192                         lineLabel = validParameter.validFile(parameters, "label", false);       if (lineLabel == "not found") { lineLabel = ""; }
193                         
194                         sensSpecFileName = outputDir + m->getRootName(m->getSimpleName(listFile)) + ".sensspec";
195                 }
196         }
197         catch(exception& e) {
198                 m->errorOut(e, "SensSpecCommand", "SensSpecCommand");
199                 exit(1);
200         }
201 }
202 //***************************************************************************************************************
203
204 int SensSpecCommand::execute(){
205         try{
206                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
207
208                 setUpOutput();
209                 outputNames.push_back(sensSpecFileName); outputTypes["sensspec"].push_back(sensSpecFileName);
210                 if(format == "phylip")          {       processPhylip();        }
211                 else if(format == "column")     {       processColumn();        }
212                 
213                 m->mothurOutEndLine();
214                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
215                 m->mothurOut(sensSpecFileName); m->mothurOutEndLine();  
216                 m->mothurOutEndLine();
217                 
218                 
219                 return 0;       
220         }
221         catch(exception& e) {
222                 m->errorOut(e, "SensSpecCommand", "execute");
223                 exit(1);
224         }
225 }
226
227 //***************************************************************************************************************
228
229 void SensSpecCommand::processPhylip(){
230         try{
231                 //probably need some checking to confirm that the names in the distance matrix are the same as those in the list file
232                 
233                 ifstream inputListFile;
234                 m->openInputFile(listFile, inputListFile);
235                 
236                 string origCutoff = "";
237                 bool getCutoff = 0;
238                 if(cutoff == -1.00)     {       getCutoff = 1;                                                                                                                  }
239                 else                            {       origCutoff = toString(cutoff);  cutoff += (0.49 / double(precision));   }
240                 
241                 string label;
242                 int numOTUs;
243
244                 map<string, int> seqMap;
245                 string seqList;
246                 
247                 while(inputListFile){
248                         inputListFile >> label >> numOTUs;
249                         for(int i=0;i<numOTUs;i++){
250                                 inputListFile >> seqList;
251                                 int seqListLength = seqList.length();
252                                 string seqName = "";
253                                 for(int j=0;j<seqListLength;j++){
254                                         
255                                         if(seqList[j] == ','){
256                                                 seqMap[seqName] = i;
257                                                 seqName = "";
258                                         }
259                                         else{
260                                                 seqName += seqList[j];
261                                         }
262                                         
263                                 }
264                                 seqMap[seqName] = i;
265                         }
266                         m->gobble(inputListFile);
267                 
268                         int lNumSeqs = seqMap.size();
269                         int pNumSeqs = 0;
270
271                         ifstream phylipFile;
272                         m->openInputFile(distFile, phylipFile);
273                         phylipFile >> pNumSeqs;
274                         if(pNumSeqs != lNumSeqs){       cout << "numSeq mismatch!" << endl;     }
275                         
276                         string seqName;
277                         double distance;
278                         vector<int> otuIndices(lNumSeqs, -1);
279                                 
280                         truePositives = 0;
281                         falsePositives = 0;
282                         trueNegatives = 0;
283                         falseNegatives = 0;
284                         
285                         if(getCutoff == 1){
286                                 if(label != "unique"){
287                                         origCutoff = label;
288                                         convert(label, cutoff);
289                                         if(hard == 0){  cutoff += (0.49 / double(precision));   }               
290                                 }
291                                 else{
292                                         origCutoff = "unique";
293                                         cutoff = 0.0000;
294                                 }
295                         }
296                                    
297                         cout << label << endl;
298                         
299                         for(int i=0;i<lNumSeqs;i++){
300                                 phylipFile >> seqName;
301                                 otuIndices[i] = seqMap[seqName];
302                                 
303                                 for(int j=0;j<i;j++){
304                                         phylipFile >> distance;
305                                         
306                                         if(distance <= cutoff){
307                                                 if(otuIndices[i] == otuIndices[j])      {       truePositives++;        }
308                                                 else                                                            {       falseNegatives++;       }
309                                         }
310                                         else{
311                                                 if(otuIndices[i] == otuIndices[j])      {       falsePositives++;       }
312                                                 else                                                            {       trueNegatives++;        }
313                                         }
314                                 }
315                         }
316                         phylipFile.close();
317                         
318                         outputStatistics(label, origCutoff);
319                 }
320                 inputListFile.close();
321
322         }
323         catch(exception& e) {
324                 m->errorOut(e, "SensSpecCommand", "processPhylip");
325                 exit(1);
326         }
327 }
328
329 //***************************************************************************************************************
330
331 void SensSpecCommand::processColumn(){
332         try{            
333                 ifstream inputListFile;
334                 m->openInputFile(listFile, inputListFile);
335                 
336                 string origCutoff = "";
337                 bool getCutoff = 0;
338                 if(cutoff == -1.00)     {       getCutoff = 1;                                                                                                                  }
339                 else                            {       origCutoff = toString(cutoff);  cutoff += (0.49 / double(precision));   }
340                 
341                 set<string> seqPairSet;
342                 
343                 string label, seqList;
344                 int numOTUs;
345                 int numSeqs;
346                 
347                 while(inputListFile){
348                         numSeqs = 0;
349                         
350                         inputListFile >> label >> numOTUs;
351                         for(int i=0;i<numOTUs;i++){
352                                 
353                                 vector<string> seqNameVector;
354                                 
355                                 inputListFile >> seqList;
356                                 int seqListLength = seqList.length();
357                                 string seqName = "";
358                                 for(int j=0;j<seqListLength;j++){
359                                         
360                                         if(seqList[j] == ','){
361                                                 seqNameVector.push_back(seqName);
362                                                 seqName = "";
363                                         }
364                                         else{
365                                                 seqName += seqList[j];
366                                         }
367                                 }
368                                 seqNameVector.push_back(seqName);
369
370                                 numSeqs += seqNameVector.size();
371                                 
372                                 int numSeqsInOTU = seqNameVector.size();
373                                 for(int j=0;j<numSeqsInOTU;j++){
374                                         string seqPairString = "";                              
375                                         for(int k=0;k<j;k++){
376                                                 if(seqNameVector[j] < seqNameVector[k]) {       seqPairString = seqNameVector[j] + '\t' + seqNameVector[k];     }
377                                                 else                                                                    {       seqPairString = seqNameVector[k] + '\t' + seqNameVector[j];     }
378                                                 seqPairSet.insert(seqPairString);
379                                         }
380                                 }
381                         }
382                         m->gobble(inputListFile);
383                         
384                         int numDists = (numSeqs * (numSeqs-1) / 2);
385
386                         ifstream columnFile;
387                         m->openInputFile(distFile, columnFile);
388                         string seqNameA, seqNameB, seqPairString;
389                         double distance;
390                         
391                         truePositives = 0;
392                         falsePositives = 0;
393                         trueNegatives = numDists;
394                         falseNegatives = 0;
395                         
396                         if(getCutoff == 1){
397                                 if(label != "unique"){
398                                         origCutoff = label;
399                                         convert(label, cutoff);
400                                         if(hard == 0){  cutoff += (0.49 / double(precision));   }               
401                                 }
402                                 else{
403                                         origCutoff = "unique";
404                                         cutoff = 0.0000;
405                                 }
406                         }
407                         
408                         cout << label << endl;
409                         
410                         while(columnFile){
411                                 columnFile >> seqNameA >> seqNameB >> distance;
412                                 if(seqNameA < seqNameB) {       seqPairString = seqNameA + '\t' + seqNameB;     }
413                                 else                                    {       seqPairString = seqNameB + '\t' + seqNameA;     }
414
415                                 set<string>::iterator it = seqPairSet.find(seqPairString);
416                         
417                                 if(distance <= cutoff){
418                                         if(it != seqPairSet.end()){
419                                                 truePositives++;
420                                                 seqPairSet.erase(it);   
421                                         }
422                                         else{
423                                                 falseNegatives++;
424                                         }
425                                         trueNegatives--;
426                                 }
427                                 else if(it != seqPairSet.end()){        
428                                         falsePositives++;
429                                         trueNegatives--;
430                                         seqPairSet.erase(it);   
431                                 }
432                                 
433                                 m->gobble(columnFile);
434                         }
435                         falsePositives += seqPairSet.size();
436                         
437                         outputStatistics(label, origCutoff);
438                 }
439         }
440         catch(exception& e) {
441                 m->errorOut(e, "SensSpecCommand", "processColumn");
442                 exit(1);
443         }
444 }
445
446 //***************************************************************************************************************
447
448 void SensSpecCommand::setUpOutput(){
449         try{            
450                 ofstream sensSpecFile;
451                 m->openOutputFile(sensSpecFileName, sensSpecFile);
452                 
453                 sensSpecFile << "label\tcutoff\ttp\ttn\tfp\tfn\tsensitivity\tspecificity\tppv\tnpv\tfdr\taccuracy\tmcc\tf1score\n";
454
455                 sensSpecFile.close();
456         }
457         catch(exception& e) {
458                 m->errorOut(e, "SensSpecCommand", "setUpOutput");
459                 exit(1);
460         }
461 }
462
463 //***************************************************************************************************************
464
465 void SensSpecCommand::outputStatistics(string label, string cutoff){
466         try{            
467                 double tp = (double) truePositives;
468                 double fp = (double) falsePositives;
469                 double tn = (double) trueNegatives;
470                 double fn = (double) falseNegatives;
471                 
472                 double p = tp + fn;
473                 double n = fp + tn;
474                 double pPrime = tp + fp;
475                 double nPrime = tn + fn;
476                 
477                 double sensitivity = tp / p;    
478                 double specificity = tn / n;
479                 double positivePredictiveValue = tp / pPrime;
480                 double negativePredictiveValue = tn / nPrime;
481                 double falseDiscoveryRate = fp / pPrime;
482                 
483                 double accuracy = (tp + tn) / (p + n);
484                 double matthewsCorrCoef = (tp * tn - fp * fn) / sqrt(p * n * pPrime * nPrime);  if(p == 0 || n == 0){   matthewsCorrCoef = 0;   }
485                 double f1Score = 2.0 * tp / (p + pPrime);
486                 
487                 
488                 if(p == 0)                      {       sensitivity = 0;        matthewsCorrCoef = 0;   }
489                 if(n == 0)                      {       specificity = 0;        matthewsCorrCoef = 0;   }
490                 if(p + n == 0)          {       accuracy = 0;                                                           }
491                 if(p + pPrime == 0)     {       f1Score = 0;                                                            }
492                 if(pPrime == 0)         {       positivePredictiveValue = 0;    falseDiscoveryRate = 0; matthewsCorrCoef = 0;   }
493                 if(nPrime == 0)         {       negativePredictiveValue = 0;    matthewsCorrCoef = 0;                                                   }
494                 
495                 ofstream sensSpecFile;
496                 m->openOutputFileAppend(sensSpecFileName, sensSpecFile);
497                 
498                 sensSpecFile << label << '\t' << cutoff << '\t';
499                 sensSpecFile << truePositives << '\t' << trueNegatives << '\t' << falsePositives << '\t' << falseNegatives << '\t';
500                 sensSpecFile << setprecision(4);
501                 sensSpecFile << sensitivity << '\t' << specificity << '\t' << positivePredictiveValue << '\t' << negativePredictiveValue << '\t';
502                 sensSpecFile << falseDiscoveryRate << '\t' << accuracy << '\t' << matthewsCorrCoef << '\t' << f1Score << endl;
503                 
504                 sensSpecFile.close();
505         }
506         catch(exception& e) {
507                 m->errorOut(e, "SensSpecCommand", "outputStatistics");
508                 exit(1);
509         }
510 }
511
512 //***************************************************************************************************************
513
514
515