| 1 |
package pp2.prediction.knn; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.BufferedWriter; |
| 5 |
import java.io.IOException; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.Collections; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Map; |
| 12 |
|
| 13 |
import pp2.tools.Tools; |
| 14 |
|
| 15 |
/** |
| 16 |
* Determines distribution of confidence score prefactor |
| 17 |
* among all targets to make the score comparable on a |
| 18 |
* global level |
| 19 |
* @author Stefanie Kaufmann |
| 20 |
* |
| 21 |
*/ |
| 22 |
public class DetermineDistribution { |
| 23 |
|
| 24 |
/** |
| 25 |
* Determines distribution of confidence score prefactor |
| 26 |
* among all targets to make the score comparable on a |
| 27 |
* global level |
| 28 |
* @param blastresults File containing the tabular blast |
| 29 |
* results |
| 30 |
* @param outputFile Name of the output file |
| 31 |
* @param k Parameter k, describes the number of best hits |
| 32 |
* that will be incorporated into the calculation |
| 33 |
* @param eThresh Parameter e-Value threshold, describes |
| 34 |
* maximal e-Value that should be used, higher values |
| 35 |
* are discarded |
| 36 |
*/ |
| 37 |
public void determineDistribution(String blastresults, String outputFile, |
| 38 |
int k, double eThresh) { |
| 39 |
try { |
| 40 |
//eThresh = logValue(eThresh); |
| 41 |
BufferedReader blast = Tools.openFile(blastresults); |
| 42 |
BufferedWriter output = Tools.writeToFile(outputFile); |
| 43 |
String line; |
| 44 |
// List<Double> values = new ArrayList<Double>(); |
| 45 |
List<Double> factors = new ArrayList<Double>(); |
| 46 |
Map<String,Double> swissprotToFactors = new HashMap<String,Double>(); |
| 47 |
int counter = 0; |
| 48 |
String template = ""; |
| 49 |
|
| 50 |
int numTargets = 0; |
| 51 |
int x = 0; |
| 52 |
|
| 53 |
while((line = blast.readLine()) != null) { |
| 54 |
counter++; |
| 55 |
if(line.startsWith("//")) { |
| 56 |
List<Double> values = new ArrayList<Double>(swissprotToFactors.values()); |
| 57 |
//take only best k hits and remove bad e-Values |
| 58 |
Collections.sort(values); |
| 59 |
if(k > 0 && k < values.size()) { |
| 60 |
values = values.subList(0, k); |
| 61 |
} |
| 62 |
if(eThresh > 0) { |
| 63 |
|
| 64 |
Iterator<Double> iterator = values.iterator(); |
| 65 |
while(iterator.hasNext()) |
| 66 |
{ |
| 67 |
Double v = iterator.next(); |
| 68 |
if(v >= eThresh) { |
| 69 |
iterator.remove(); |
| 70 |
} |
| 71 |
} |
| 72 |
// Exception, Erklärung: http://tech.puredanger.com/2009/02/02/java-concurrency-bugs-concurrentmodificationexception/ |
| 73 |
/*for(Double v : values) { |
| 74 |
if(v >= eThresh) { |
| 75 |
values.remove(v); |
| 76 |
} |
| 77 |
}*/ |
| 78 |
} |
| 79 |
|
| 80 |
int c = 0; |
| 81 |
for(Double v : values) { |
| 82 |
if(v == 0) |
| 83 |
c++; |
| 84 |
} |
| 85 |
if(c == values.size()) |
| 86 |
x++; |
| 87 |
|
| 88 |
//calculate prefactor based on average and standard deviation |
| 89 |
double avg = calculateAvg(values); |
| 90 |
double stddev = calculateStdDev(values, avg); |
| 91 |
factors.add(prefactor(stddev, avg)); |
| 92 |
//new target |
| 93 |
} else if(line.startsWith(">")) { |
| 94 |
swissprotToFactors = new HashMap<String, Double>(); |
| 95 |
numTargets++; |
| 96 |
} else if(!line.isEmpty() && line.split("\t").length > 1){ |
| 97 |
//parse e-Value for current hit and save it |
| 98 |
String[] content = line.split("\t"); |
| 99 |
template = content[1]; |
| 100 |
String e = content[2].toUpperCase(); |
| 101 |
if(e.startsWith("E")) { |
| 102 |
e = 1.0+e; |
| 103 |
} |
| 104 |
double eValue = Double.parseDouble(e); |
| 105 |
swissprotToFactors.put(template, eValue); |
| 106 |
} |
| 107 |
} |
| 108 |
System.out.println(" Done parsing..."); |
| 109 |
blast.close(); |
| 110 |
//sort prefactors |
| 111 |
Collections.sort(factors); |
| 112 |
System.out.println(" Done sorting..."); |
| 113 |
//divide set of prefactors into 100 parts |
| 114 |
int divisionSize = (int)(factors.size()/100.0); |
| 115 |
|
| 116 |
System.out.println(" Division size: " + divisionSize); |
| 117 |
|
| 118 |
for(int i=1; i<101; i++) { |
| 119 |
output.write(factors.get(i*divisionSize)+"\n"); |
| 120 |
} |
| 121 |
output.flush(); |
| 122 |
output.close(); |
| 123 |
|
| 124 |
System.out.println(" Number of targets: " + numTargets); |
| 125 |
System.out.println(" Targets with all eVals == 0: " + x); |
| 126 |
System.out.println(" Max number problematic quantiles: " + (x / (double)divisionSize)); |
| 127 |
|
| 128 |
} catch (IOException e) { |
| 129 |
e.printStackTrace(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Calculates average for a list of e-Values |
| 135 |
* @param eValues List of e-Values |
| 136 |
* @return Average of those e-Values |
| 137 |
*/ |
| 138 |
public double calculateAvg(List<Double> eValues) { |
| 139 |
double sum = 0; |
| 140 |
for(double e : eValues) { |
| 141 |
double log = logValue(e); |
| 142 |
sum += log; |
| 143 |
} |
| 144 |
return sum/(double)eValues.size(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Calculates standard deviation for a list of e-Values |
| 149 |
* @param eValues List of e-Values |
| 150 |
* @param avg Average of those e-Values |
| 151 |
* @return Standard deviation of e-Values |
| 152 |
*/ |
| 153 |
public double calculateStdDev(List<Double> eValues, double avg) { |
| 154 |
double sum = 0; |
| 155 |
for(double e : eValues) { |
| 156 |
double log = logValue(e); |
| 157 |
sum += (log-avg)*(log-avg); |
| 158 |
} |
| 159 |
//if there is only one e-Value (=1 hit), standard deviation is 0 |
| 160 |
if(eValues.size() == 1) { |
| 161 |
return 0; |
| 162 |
} |
| 163 |
return Math.sqrt(sum/(double)(eValues.size()-1)); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Calculates the negative logarithm of the e-Value |
| 168 |
* @param eValue The e-Value |
| 169 |
* @return Negative logarithm of the e-Value |
| 170 |
*/ |
| 171 |
public double logValue(double eValue) { |
| 172 |
if(eValue == 0) { |
| 173 |
return 1000; |
| 174 |
} |
| 175 |
|
| 176 |
if(-Math.log(eValue)>1000) |
| 177 |
System.out.println(-Math.log(eValue)); |
| 178 |
|
| 179 |
return -Math.log(eValue); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Calculates the prefactor for the confidence score |
| 184 |
* based on standard deviation and average |
| 185 |
* @param stddev Standard deviation of e-Values of all |
| 186 |
* hits for current target |
| 187 |
* @param avg Average of e-Values of all hits for current |
| 188 |
* target |
| 189 |
* @return Prefactor for global comparison of confidence |
| 190 |
* score |
| 191 |
*/ |
| 192 |
public double prefactor(double stddev, double avg) { |
| 193 |
return avg+2*stddev; |
| 194 |
} |
| 195 |
|
| 196 |
public static void printUsage() { |
| 197 |
System.err.println(" Usage: DetermineDistribution -i blastfile" + |
| 198 |
"-o outputfile (-k k | -e e-value-threshold)"); |
| 199 |
System.exit(1); |
| 200 |
} |
| 201 |
|
| 202 |
public static void main(String[] args) { |
| 203 |
String blastFile = ""; |
| 204 |
String outputFile = ""; |
| 205 |
int k = 0; |
| 206 |
double e = 0; |
| 207 |
for(int i=0; i<args.length; i+=2) { |
| 208 |
if(args[i].equals("-i")) { |
| 209 |
blastFile = args[i+1]; |
| 210 |
} else if(args[i].equals("-o")) { |
| 211 |
outputFile = args[i+1]; |
| 212 |
} else if(args[i].equals("-k")) { |
| 213 |
k = Integer.parseInt(args[i+1]); |
| 214 |
} else if(args[i].equals("-e")) { |
| 215 |
e = Double.parseDouble(args[i+1]); |
| 216 |
} else { |
| 217 |
printUsage(); |
| 218 |
} |
| 219 |
} |
| 220 |
if(!blastFile.isEmpty() && !outputFile.isEmpty() |
| 221 |
&& (k > 0 || e > 0)) { |
| 222 |
new DetermineDistribution().determineDistribution(blastFile, outputFile, k, e); |
| 223 |
} else { |
| 224 |
printUsage(); |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
} |