+
+void SensSpecCommand::outputStatistics(string label, string cutoff){
+ try{
+ double tp = (double) truePositives;
+ double fp = (double) falsePositives;
+ double tn = (double) trueNegatives;
+ double fn = (double) falseNegatives;
+
+ double p = tp + fn;
+ double n = fp + tn;
+ double pPrime = tp + fp;
+ double nPrime = tn + fn;
+
+ double sensitivity = tp / p;
+ double specificity = tn / n;
+ double positivePredictiveValue = tp / pPrime;
+ double negativePredictiveValue = tn / nPrime;
+ double falseDiscoveryRate = fp / pPrime;
+
+ double accuracy = (tp + tn) / (p + n);
+ double matthewsCorrCoef = (tp * tn - fp * fn) / sqrt(p * n * pPrime * nPrime); if(p == 0 || n == 0){ matthewsCorrCoef = 0; }
+ double f1Score = 2.0 * tp / (p + pPrime);
+
+
+ if(p == 0) { sensitivity = 0; matthewsCorrCoef = 0; }
+ if(n == 0) { specificity = 0; matthewsCorrCoef = 0; }
+ if(p + n == 0) { accuracy = 0; }
+ if(p + pPrime == 0) { f1Score = 0; }
+ if(pPrime == 0) { positivePredictiveValue = 0; falseDiscoveryRate = 0; matthewsCorrCoef = 0; }
+ if(nPrime == 0) { negativePredictiveValue = 0; matthewsCorrCoef = 0; }
+
+ ofstream sensSpecFile;
+ m->openOutputFileAppend(sensSpecFileName, sensSpecFile);
+
+ sensSpecFile << label << '\t' << cutoff << '\t';
+ sensSpecFile << truePositives << '\t' << trueNegatives << '\t' << falsePositives << '\t' << falseNegatives << '\t';
+ sensSpecFile << setprecision(4);
+ sensSpecFile << sensitivity << '\t' << specificity << '\t' << positivePredictiveValue << '\t' << negativePredictiveValue << '\t';
+ sensSpecFile << falseDiscoveryRate << '\t' << accuracy << '\t' << matthewsCorrCoef << '\t' << f1Score << endl;
+
+ sensSpecFile.close();
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SensSpecCommand", "outputStatistics");
+ exit(1);
+ }
+}
+//***************************************************************************************************************
+
+string SensSpecCommand::preProcessList(){
+ try {
+ set<string> uniqueNames;
+ //get unique names from distance file
+ if (format == "phylip") {
+
+ ifstream phylipFile;
+ m->openInputFile(distFile, phylipFile);
+ string numTest;
+ int pNumSeqs;
+ phylipFile >> numTest;
+
+ if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
+ else {
+ m->mothurConvert(numTest, pNumSeqs);
+ }
+ phylipFile >> pNumSeqs; m->gobble(phylipFile);
+
+ string seqName;
+ double distance;
+
+ for(int i=0;i<pNumSeqs;i++){
+
+ if (m->control_pressed) { return ""; }
+
+ phylipFile >> seqName;
+ uniqueNames.insert(seqName);
+
+ for(int j=0;j<i;j++){
+ phylipFile >> distance;
+ }
+ m->gobble(phylipFile);
+ }
+ phylipFile.close();
+ }else {
+ ifstream columnFile;
+ m->openInputFile(distFile, columnFile);
+ string seqNameA, seqNameB;
+ double distance;
+
+ while(columnFile){
+ if (m->control_pressed) { return ""; }
+ columnFile >> seqNameA >> seqNameB >> distance;
+ uniqueNames.insert(seqNameA); uniqueNames.insert(seqNameB);
+ m->gobble(columnFile);
+ }
+ columnFile.close();
+ }
+
+ //read list file, if numSeqs > unique names then remove redundant names
+ string newListFile = listFile + ".temp";
+ ofstream out;
+ m->openOutputFile(newListFile, out);
+ ifstream in;
+ m->openInputFile(listFile, in);
+
+ bool wroteSomething = false;
+
+ while(!in.eof()){
+
+ if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(newListFile); return ""; }
+
+ //read in list vector
+ ListVector list(in);
+
+ //listfile is already unique
+ if (list.getNumSeqs() == uniqueNames.size()) { in.close(); out.close(); m->mothurRemove(newListFile); return ""; }
+
+ //make a new list vector
+ ListVector newList;
+ newList.setLabel(list.getLabel());
+
+ //for each bin
+ for (int i = 0; i < list.getNumBins(); i++) {
+
+ //parse out names that are in accnos file
+ string binnames = list.get(i);
+ vector<string> bnames;
+ m->splitAtComma(binnames, bnames);
+
+ string newNames = "";
+ for (int i = 0; i < bnames.size(); i++) {
+ string name = bnames[i];
+ //if that name is in the .accnos file, add it
+ if (uniqueNames.count(name) != 0) { newNames += name + ","; }
+ }
+
+ //if there are names in this bin add to new list
+ if (newNames != "") {
+ newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
+ newList.push_back(newNames);
+ }
+ }
+
+ //print new listvector
+ if (newList.getNumBins() != 0) {
+ wroteSomething = true;
+ newList.print(out);
+ }
+
+ m->gobble(in);
+ }
+ in.close();
+ out.close();
+
+ if (wroteSomething) { return newListFile; }
+ return "";
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SensSpecCommand", "preProcessList");
+ exit(1);
+ }
+}
+
+
+//***************************************************************************************************************
+
+
+