1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
package pp2.prediction.knn;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Vector;
import pp2.go.DAG;
import pp2.go.Relations;
import pp2.tools.Tools;
/**
* represents one function prediction target which is given in the BLAST results file
* @author Thomas Hopf
*
*/
public class PredictionTarget {
public static final int NUM_ONTOLOGIES = 3;
public static final int BIOLOGICAL_PROCESS = 0;
public static final int MOLECULAR_FUNCTION = 1;
public static final int CELLULAR_COMPONENT = 2;
public static final double MAX_EVALUE = 1000;
private static String newline = System.getProperty("line.separator");
private static String tab = "\t";
private String targetID;
private Vector<BlastHit> blastHits = new Vector<BlastHit>();
/*
* getters and setters
*/
public void setTargetID(String targetID) {
this.targetID = targetID;
}
public String getTargetID()
{
return targetID;
}
public void addBlastHit(String hitLine, boolean deleteDuplicates)
{
if(deleteDuplicates)
{
String newID = hitLine.split("\t")[0];
for(int i=0; i<blastHits.size(); i++)
{
if(blastHits.get(i).getHitID().equals(newID))
blastHits.remove(i);
}
}
blastHits.add(new BlastHit(hitLine));
}
public Vector<BlastHit> getBlastHits()
{
return blastHits;
}
/*
* methods
*/
private int ontologyToIndex(String ontology)
{
if(ontology.equals("molecular_function"))
return MOLECULAR_FUNCTION;
else {
if(ontology.equals("biological_process"))
return PredictionTarget.BIOLOGICAL_PROCESS;
else
return PredictionTarget.CELLULAR_COMPONENT;
}
}
// private double limitRange(double value)
// {
// return Math.max(1E-308, Math.min(value, 1));
// }
private double getExponent(double eValue)
{
if(eValue <= 0)
return MAX_EVALUE;
else
return -Math.log(eValue);
}
private String indexToOntology(int index)
{
switch(index)
{
case BIOLOGICAL_PROCESS: return "B";
case MOLECULAR_FUNCTION: return "M";
case CELLULAR_COMPONENT: return "C";
default: return "-";
}
}
public void predict(Relations relations, boolean useEValueThreshold, double eValueTreshold, int k, BufferedWriter naiveWriter, BufferedWriter leafWriter, double[] distribution) throws Exception
{
// System.out.println("predicting " + targetID);
// System.out.print(".");
// if we don't have any BLAST hits for the current target, predicting anything is pointless...
if(blastHits.size() == 0)
return;
// sort result list in ascending order by e-Value
Collections.sort(blastHits);
// create a vector containing only the relevant Blast hits, i.e. those with an e-Value
// below the threshold or the first k nearest neighbors
Vector<BlastHit> blastHitsTmp = new Vector<BlastHit>(blastHits.size());
int j=0;
while(j < blastHits.size() && blastHits.get(j).getEValue() <= eValueTreshold && j < k)
{
blastHitsTmp.add(blastHits.get(j));
j++;
}
// calculate the global quality score of this hit (i.e. the quantile of mean(E-Values) + 2*stddev(E-values)
double quantile = 1;
if(distribution != null)
{
// first, calculate mean
double mean = 0;
for(BlastHit b: blastHitsTmp)
mean += getExponent(b.getEValue());
mean /= blastHitsTmp.size();
//System.out.println("mean " + mean);
// second, calculate standard deviation
double stdDev = 0;
for(BlastHit b: blastHitsTmp)
stdDev += (getExponent(b.getEValue()) - mean) * (getExponent(b.getEValue()) - mean);
// unbiased estimator: normalize by N-1
stdDev = Math.sqrt(stdDev / (blastHitsTmp.size() - 1));
//System.out.println("sd " + stdDev);
// calculate quality score as mean+2*stddev
double qualityScore = mean + 2*stdDev;
//System.out.println("score " + qualityScore);
// find which quantile the quality score corresponds to. This is the global quality score of the target
int i = 0;
while(i<100 && qualityScore >= distribution[i])
i++;
quantile = (i + 1)/(double)100;
//System.out.println("quantile :" + quantile);
}
// System.out.println("before: " + blastHits.size() + " => after: " + blastHitsTmp.size());
// for(BlastHit b: blastHits)
// {
// System.out.println(b.getHitID() + ": " + b.getEValue());
// }
/*
* step 1: get a list of all unique GO terms of hits for this prediction and construct the DAG
*
* TODO: this is a bit redundant since DAG.java also converts the String[] parameter to a HashSet
*/
HashSet<String> allGoTerms = new HashSet<String>();
int totalNumGoTerms = 0;
for(BlastHit b : blastHitsTmp)
for(String s : b.getGoTerms())
{
allGoTerms.add("GO:" + s);
totalNumGoTerms++;
}
DAG dag = new DAG(relations, (String [])allGoTerms.toArray(new String[0]));
HashSet<BlastHit> normalizationHits = new HashSet<BlastHit>();
// check whether there are any valid nodes or if we lost everything due to filtering cellular component annotations.
// if we did, it makes no sense to make a prediction
if(dag.getLeaves().length <= 0)
{
System.out.println("no DAG nodes for " + targetID + " after CC filtering, skip");
return;
}
/*
* step 2: add annotations to the DAG by iterating over all evidence (= BLAST hits)
*/
// once again, iterate over all BLAST hits
for(BlastHit b : blastHitsTmp)
{
// iterate over all GO terms of the current hit
for(String s: b.getGoTerms())
{
//b.activateOntologySupport(relations.getType("GO:"+s));
// attach to corresponding nodes in the DAG
DAGNode n = null;
if((n = dag.getDAGNode("GO:" + s)) != null)
{
n.addHit(b);
// update which GO ontologies are supported by the hit, do only those that weren't filtered before
String r = relations.getType("GO:"+s);
if(r == null) // TODO: remove me
System.err.println("error! invalid: GO:"+s + " for target " + this.targetID);
b.activateOntologySupport(r);
// also add evidence to all "ancestors" in the DAG
for(String ancestor: dag.getAncestors("GO:"+s))
dag.getDAGNode(ancestor).addHit(b); // here no additional "GO:" is necessary because the getAncestors() method already returns correct IDs
}
}
}
/*
* step 3: calculate normalization values
*/
double[] eValueSum = new double[NUM_ONTOLOGIES];
double[] eValueSumSquared = new double[NUM_ONTOLOGIES];
double[] eValueSumLog = new double[NUM_ONTOLOGIES];
double[] eValueSumLogSquared = new double[NUM_ONTOLOGIES];
double[] bitScoreSum = new double[NUM_ONTOLOGIES];
double[] bitScoreSumSquared = new double[NUM_ONTOLOGIES];
int[] unweightedSum = new int[NUM_ONTOLOGIES];
for(BlastHit b: blastHitsTmp)
{
//double exponent = -Math.log(limitRange(b.getEValue()));
double exponent = getExponent(b.getEValue());
//System.out.println(exponent);
double exponentLog = Math.log(exponent);
for(int i=0; i<NUM_ONTOLOGIES; i++)
{
if(b.supports(i))
{
unweightedSum[i]++;
eValueSum[i] += exponent;
eValueSumSquared[i] += exponent*exponent;
eValueSumLog[i] += exponentLog;
eValueSumLogSquared[i] += exponentLog*exponentLog;
bitScoreSum[i] += b.getBitScore();
bitScoreSumSquared[i] += b.getBitScore();
}
}
}
/*
* step 4: score all nodes in the DAG
*/
naiveWriter.write(">" + targetID + newline);
for(String s: dag.getDAGNodes().keySet())
{
DAGNode d = dag.getDAGNodes().get(s);
double eValueNom = 0, eValueLogNom = 0, bitScoreNom = 0;
int unweightedNom = 0;
for(BlastHit b: d.getSupportingHits())
{
unweightedNom++;
//double exponent = -Math.log(limitRange(b.getEValue()));
double exponent = getExponent(b.getEValue());
eValueNom += exponent;
eValueLogNom += Math.log(exponent);
bitScoreNom += b.getBitScore();
}
int index = ontologyToIndex(relations.getType(s));
// System.out.println(index + " -> " + eValueSum[index]);
// System.out.print(index);
d.setUnweightedSCore((double)unweightedNom / (unweightedSum[index] > 0 ? unweightedSum[index] : 1));
d.seteValueScore(eValueNom / (eValueSum[index] > 1 ? eValueSum[index] : 1));
// d.seteValueSquaredScore(eValueNom / (eValueSumSquared[index] > 1 ? eValueSumSquared[index] : 1));
d.seteValueLogScore(eValueLogNom / (eValueSumLog[index] > 1 ? eValueSumLog[index] : 1));
// d.seteValueLogSquaredScore(eValueLogNom / (eValueSumLogSquared[index] > 1 ? eValueSumLogSquared[index] : 1));
d.setBitScoreScore(bitScoreNom / (bitScoreSum[index] > 1 ? bitScoreSum[index] : 1));
// d.setBitScoreSquaredScore(bitScoreNom / (bitScoreSumSquared[index] > 1 ? bitScoreSumSquared[index] : 1));
naiveWriter.write(s + tab + indexToOntology(index) + tab + Tools.round(d.getUnweightedScore()) + tab + Tools.round(d.geteValueScore()) + tab + Tools.round(d.geteValueLogScore()));
if(distribution != null)
naiveWriter.write(tab + Tools.round(d.getUnweightedScore() * quantile) + tab + Tools.round(d.geteValueScore() * quantile) + tab + Tools.round(d.geteValueLogScore() * quantile));
naiveWriter.write(newline);
// System.out.println(s + ": " + d.geteValueScore() + "\t" + "\t" + d.getUnweightedScore() + "\t" + relations.getType(s) + "\t" + eValueSum[index]);
// ontologies
}
naiveWriter.write("//" + newline); // + Character.LINE_SEPARATOR);
// TODO : 1 - score?
// TODO: unweighted kNN
// TODO: bitscore macht keinen sinn da direkt abhängig von E-value...http://www.ncbi.nlm.nih.gov/BLAST/tutorial/Altschul-1.html
/*
* step 5: score leaves and output
*/
// System.out.println("biological_process: " + unweightedSum[BIOLOGICAL_PROCESS] + ", " + eValueSum[BIOLOGICAL_PROCESS]);
// System.out.println("molecular_function: " + unweightedSum[MOLECULAR_FUNCTION] + ", " + eValueSum[MOLECULAR_FUNCTION]);
// System.out.println("cellular_component: " + unweightedSum[CELLULAR_COMPONENT] + ", " + eValueSum[CELLULAR_COMPONENT]);
leafWriter.write(">" + targetID + newline);
for(String l: dag.getLeaves())
{
DAGNode currentNode = dag.getDAGNodes().get(l);
double leafScoreEValue = currentNode.geteValueScore();
double leafScoreEValueLog = currentNode.geteValueLogScore();
double leafScoreUnweighted = currentNode.getUnweightedScore();
for(String m: dag.getAncestors(l))
{
leafScoreEValue += dag.getDAGNodes().get(m).geteValueScore();
leafScoreEValueLog += dag.getDAGNodes().get(m).geteValueLogScore();
leafScoreUnweighted += dag.getDAGNodes().get(m).getUnweightedScore();
}
int N = dag.getAncestors(l).length + 1;
currentNode.setLeafScoreEValue(leafScoreEValue / N);
currentNode.setLeafScoreEValueLog(leafScoreEValueLog / N);
currentNode.setLeafScoreUnweighted(leafScoreUnweighted / N);
// System.out.println("->" + l + " " + dag.getDAGNodes().get(l).getSupportingHits().size() + " " + dag.getDAGNodes().get(l).geteValueScore() + " " + dag.getDAGNodes().get(l).getLeafScoreEValue() + " " + relations.getType(l));
leafWriter.write(l + tab + indexToOntology(ontologyToIndex(relations.getType(l))) + tab + Tools.round(currentNode.getLeafScoreUnweighted()) + tab + Tools.round(currentNode.getLeafScoreEValue()) + tab + Tools.round(currentNode.getLeafScoreEValueLog()));
if(distribution != null)
leafWriter.write(tab + Tools.round(currentNode.getLeafScoreUnweighted() * quantile) + tab + Tools.round(currentNode.getLeafScoreEValue() * quantile) + tab + Tools.round(currentNode.getLeafScoreEValueLog() * quantile));
leafWriter.write(newline);
}
leafWriter.write("//" + newline);
// if(relations.getType(l).equals("biological_process"))
// ; //continue;
//
// System.out.println("->" + l + " " + dag.getDAGNodes().get(l).getSupportingHits().size() + " " + dag.getDAGNodes().get(l).geteValueScore() + " " + relations.getType(l));
// System.out.print("\t");
// for(String m: dag.getAncestors(l))
// {
// //System.out.print(" | " + m + "="+dag.getDAGNodes().get(m).geteValueScore());
// System.out.print(" | " + m + "="+dag.getDAGNodes().get(m).getSupportingHits().size());
// }
// System.out.println();
// }
}
}
|