5 * Created by Pat Schloss on 7/6/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "sensspeccommand.h"
12 //**********************************************************************************************************************
13 vector<string> SensSpecCommand::setParameters(){
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);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "SensSpecCommand", "setParameters");
35 //**********************************************************************************************************************
36 string SensSpecCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The sens.spec command....\n";
43 m->errorOut(e, "SensSpecCommand", "getHelpString");
47 //**********************************************************************************************************************
48 SensSpecCommand::SensSpecCommand(){
50 abort = true; calledHelp = true;
52 vector<string> tempOutNames;
53 outputTypes["sensspec"] = tempOutNames;
56 m->errorOut(e, "SensSpecCommand", "SensSpecCommand");
60 //***************************************************************************************************************
62 SensSpecCommand::SensSpecCommand(string option) {
65 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
73 vector<string> myArray = setParameters();
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["sensspec"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("list");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["list"] = inputDir + it->second; }
103 it = parameters.find("phylip");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["phylip"] = inputDir + it->second; }
111 it = parameters.find("column");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["column"] = inputDir + it->second; }
119 //it = parameters.find("name");
120 //user has given a template file
121 //if(it != parameters.end()){
122 //path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 //if (path == "") { parameters["name"] = inputDir + it->second; }
128 //check for required parameters
129 listFile = validParameter.validFile(parameters, "list", true);
130 if (listFile == "not found") {
131 listFile = m->getListFile();
132 if (listFile != "") { m->mothurOut("Using " + listFile + " as input file for the list parameter."); m->mothurOutEndLine(); }
133 else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
135 else if (listFile == "not open") { abort = true; }
137 phylipfile = validParameter.validFile(parameters, "phylip", true);
138 if (phylipfile == "not found") { phylipfile = ""; }
139 else if (phylipfile == "not open") { abort = true; }
140 else { distFile = phylipfile; format = "phylip"; }
142 columnfile = validParameter.validFile(parameters, "column", true);
143 if (columnfile == "not found") { columnfile = ""; }
144 else if (columnfile == "not open") { abort = true; }
145 else { distFile = columnfile; format = "column"; }
147 if ((phylipfile == "") && (columnfile == "")) { //is there are current file available for either of these?
148 //give priority to column, then phylip
149 columnfile = m->getColumnFile();
150 if (columnfile != "") { distFile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
152 phylipfile = m->getPhylipFile();
153 if (phylipfile != "") { distFile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
155 m->mothurOut("No valid current files. You must provide a phylip or column file."); m->mothurOutEndLine();
159 }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 //if the user changes the output directory command factory will send this info to us in the output parameter
163 outputDir = validParameter.validFile(parameters, "outputdir", false);
164 if (outputDir == "not found"){
166 outputDir += m->hasPath(listFile); //if user entered a file with a path then preserve it
169 //check for optional parameter and set defaults
170 // ...at some point should added some additional type checking...
171 temp = validParameter.validFile(parameters, "hard", false);
172 if (temp == "not found"){ hard = 0; }
173 else if(!m->isTrue(temp)) { hard = 0; }
174 else if(m->isTrue(temp)) { hard = 1; }
176 // temp = validParameter.validFile(parameters, "name", true);
177 // if (temp == "not found") { nameFile = ""; }
178 // else if(temp == "not open") { abort = true; }
179 // else { nameFile = temp; }
180 // cout << "name:\t" << nameFile << endl;
182 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "-1.00"; }
183 convert(temp, cutoff);
184 // cout << cutoff << endl;
186 temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; }
187 convert(temp, precision);
188 // cout << precision << endl;
190 lineLabel = validParameter.validFile(parameters, "label", false); if (lineLabel == "not found") { lineLabel = ""; }
192 sensSpecFileName = listFile.substr(0,listFile.find_last_of('.')) + ".sensspec";
195 catch(exception& e) {
196 m->errorOut(e, "SensSpecCommand", "SensSpecCommand");
200 //***************************************************************************************************************
202 int SensSpecCommand::execute(){
204 if (abort == true) { if (calledHelp) { return 0; } return 2; }
207 outputNames.push_back(sensSpecFileName); outputTypes["sensspec"].push_back(sensSpecFileName);
208 if(format == "phylip") { processPhylip(); }
209 else if(format == "column") { processColumn(); }
214 catch(exception& e) {
215 m->errorOut(e, "SensSpecCommand", "execute");
220 //***************************************************************************************************************
222 void SensSpecCommand::processPhylip(){
224 //probably need some checking to confirm that the names in the distance matrix are the same as those in the list file
226 ifstream inputListFile;
227 m->openInputFile(listFile, inputListFile);
229 string origCutoff = "";
231 if(cutoff == -1.00) { getCutoff = 1; }
232 else { origCutoff = toString(cutoff); cutoff += (0.49 / double(precision)); }
237 map<string, int> seqMap;
240 while(inputListFile){
241 inputListFile >> label >> numOTUs;
242 for(int i=0;i<numOTUs;i++){
243 inputListFile >> seqList;
244 int seqListLength = seqList.length();
246 for(int j=0;j<seqListLength;j++){
248 if(seqList[j] == ','){
253 seqName += seqList[j];
259 m->gobble(inputListFile);
261 int lNumSeqs = seqMap.size();
265 m->openInputFile(distFile, phylipFile);
266 phylipFile >> pNumSeqs;
267 if(pNumSeqs != lNumSeqs){ cout << "numSeq mismatch!" << endl; }
271 vector<int> otuIndices(lNumSeqs, -1);
279 if(label != "unique"){
281 convert(label, cutoff);
282 if(hard == 0){ cutoff += (0.49 / double(precision)); }
285 origCutoff = "unique";
290 cout << label << endl;
292 for(int i=0;i<lNumSeqs;i++){
293 phylipFile >> seqName;
294 otuIndices[i] = seqMap[seqName];
296 for(int j=0;j<i;j++){
297 phylipFile >> distance;
299 if(distance <= cutoff){
300 if(otuIndices[i] == otuIndices[j]) { truePositives++; }
301 else { falseNegatives++; }
304 if(otuIndices[i] == otuIndices[j]) { falsePositives++; }
305 else { trueNegatives++; }
311 outputStatistics(label, origCutoff);
313 inputListFile.close();
316 catch(exception& e) {
317 m->errorOut(e, "SensSpecCommand", "processPhylip");
322 //***************************************************************************************************************
324 void SensSpecCommand::processColumn(){
326 ifstream inputListFile;
327 m->openInputFile(listFile, inputListFile);
329 string origCutoff = "";
331 if(cutoff == -1.00) { getCutoff = 1; }
332 else { origCutoff = toString(cutoff); cutoff += (0.49 / double(precision)); }
334 set<string> seqPairSet;
336 string label, seqList;
340 while(inputListFile){
343 inputListFile >> label >> numOTUs;
344 for(int i=0;i<numOTUs;i++){
346 vector<string> seqNameVector;
348 inputListFile >> seqList;
349 int seqListLength = seqList.length();
351 for(int j=0;j<seqListLength;j++){
353 if(seqList[j] == ','){
354 seqNameVector.push_back(seqName);
358 seqName += seqList[j];
361 seqNameVector.push_back(seqName);
363 numSeqs += seqNameVector.size();
365 int numSeqsInOTU = seqNameVector.size();
366 for(int j=0;j<numSeqsInOTU;j++){
367 string seqPairString = "";
368 for(int k=0;k<j;k++){
369 if(seqNameVector[j] < seqNameVector[k]) { seqPairString = seqNameVector[j] + '\t' + seqNameVector[k]; }
370 else { seqPairString = seqNameVector[k] + '\t' + seqNameVector[j]; }
371 seqPairSet.insert(seqPairString);
375 m->gobble(inputListFile);
377 int numDists = (numSeqs * (numSeqs-1) / 2);
380 m->openInputFile(distFile, columnFile);
381 string seqNameA, seqNameB, seqPairString;
386 trueNegatives = numDists;
390 if(label != "unique"){
392 convert(label, cutoff);
393 if(hard == 0){ cutoff += (0.49 / double(precision)); }
396 origCutoff = "unique";
401 cout << label << endl;
404 columnFile >> seqNameA >> seqNameB >> distance;
405 if(seqNameA < seqNameB) { seqPairString = seqNameA + '\t' + seqNameB; }
406 else { seqPairString = seqNameB + '\t' + seqNameA; }
408 set<string>::iterator it = seqPairSet.find(seqPairString);
410 if(distance <= cutoff){
411 if(it != seqPairSet.end()){
413 seqPairSet.erase(it);
420 else if(it != seqPairSet.end()){
423 seqPairSet.erase(it);
426 m->gobble(columnFile);
428 falsePositives += seqPairSet.size();
430 outputStatistics(label, origCutoff);
433 catch(exception& e) {
434 m->errorOut(e, "SensSpecCommand", "processColumn");
439 //***************************************************************************************************************
441 void SensSpecCommand::setUpOutput(){
443 ofstream sensSpecFile;
444 m->openOutputFile(sensSpecFileName, sensSpecFile);
446 sensSpecFile << "label\tcutoff\ttp\ttn\tfp\tfn\tsensitivity\tspecificity\tppv\tnpv\tfdr\taccuracy\tmcc\tf1score\n";
448 sensSpecFile.close();
450 catch(exception& e) {
451 m->errorOut(e, "SensSpecCommand", "setUpOutput");
456 //***************************************************************************************************************
458 void SensSpecCommand::outputStatistics(string label, string cutoff){
460 double tp = (double) truePositives;
461 double fp = (double) falsePositives;
462 double tn = (double) trueNegatives;
463 double fn = (double) falseNegatives;
467 double pPrime = tp + fp;
468 double nPrime = tn + fn;
470 double sensitivity = tp / p;
471 double specificity = tn / n;
472 double positivePredictiveValue = tp / pPrime;
473 double negativePredictiveValue = tn / nPrime;
474 double falseDiscoveryRate = fp / pPrime;
476 double accuracy = (tp + tn) / (p + n);
477 double matthewsCorrCoef = (tp * tn - fp * fn) / sqrt(p * n * pPrime * nPrime); if(p == 0 || n == 0){ matthewsCorrCoef = 0; }
478 double f1Score = 2.0 * tp / (p + pPrime);
481 if(p == 0) { sensitivity = 0; matthewsCorrCoef = 0; }
482 if(n == 0) { specificity = 0; matthewsCorrCoef = 0; }
483 if(p + n == 0) { accuracy = 0; }
484 if(p + pPrime == 0) { f1Score = 0; }
485 if(pPrime == 0) { positivePredictiveValue = 0; falseDiscoveryRate = 0; matthewsCorrCoef = 0; }
486 if(nPrime == 0) { negativePredictiveValue = 0; matthewsCorrCoef = 0; }
488 ofstream sensSpecFile;
489 m->openOutputFileAppend(sensSpecFileName, sensSpecFile);
491 sensSpecFile << label << '\t' << cutoff << '\t';
492 sensSpecFile << truePositives << '\t' << trueNegatives << '\t' << falsePositives << '\t' << falseNegatives << '\t';
493 sensSpecFile << setprecision(4);
494 sensSpecFile << sensitivity << '\t' << specificity << '\t' << positivePredictiveValue << '\t' << negativePredictiveValue << '\t';
495 sensSpecFile << falseDiscoveryRate << '\t' << accuracy << '\t' << matthewsCorrCoef << '\t' << f1Score << endl;
497 sensSpecFile.close();
499 catch(exception& e) {
500 m->errorOut(e, "SensSpecCommand", "outputStatistics");
505 //***************************************************************************************************************