| 1 |
package pp2.prediction.knn; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.BufferedWriter; |
| 5 |
import java.io.IOException; |
| 6 |
|
| 7 |
import pp2.go.Relations; |
| 8 |
import pp2.tools.Tools; |
| 9 |
|
| 10 |
/** |
| 11 |
* performs all operations necessary to predict a list of targets |
| 12 |
* |
| 13 |
* @author Thomas Hopf |
| 14 |
* |
| 15 |
*/ |
| 16 |
public class KNNKernel { |
| 17 |
|
| 18 |
// holds all relations in the GO ontology tree |
| 19 |
private Relations relations; |
| 20 |
|
| 21 |
public KNNKernel(String closureFile, String termFile, boolean noCellularComponent) |
| 22 |
{ |
| 23 |
relations = new Relations(closureFile, termFile, noCellularComponent); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* fetches the next target with all BLAST hits from the reader |
| 28 |
* @param reader |
| 29 |
* @return |
| 30 |
*/ |
| 31 |
private PredictionTarget getNextTarget(BufferedReader reader, boolean eliminateDuplicateHits) |
| 32 |
{ |
| 33 |
PredictionTarget nextTarget = new PredictionTarget(); |
| 34 |
|
| 35 |
try { |
| 36 |
String line = null; |
| 37 |
while((line = reader.readLine()) != null) |
| 38 |
{ |
| 39 |
// return current target when we hit the "end-of-target"-line "//" |
| 40 |
if(line.equals("//")) |
| 41 |
return nextTarget; |
| 42 |
|
| 43 |
// get ID of current target |
| 44 |
if(line.startsWith(">")) |
| 45 |
nextTarget.setTargetID(line.substring(1)); |
| 46 |
|
| 47 |
// if line starts with "#", this is one of the BLAST hits in the database |
| 48 |
if(line.startsWith("#")) |
| 49 |
nextTarget.addBlastHit(line.substring(1), eliminateDuplicateHits); |
| 50 |
} |
| 51 |
|
| 52 |
} catch(Exception e) |
| 53 |
{ |
| 54 |
e.printStackTrace(); |
| 55 |
} |
| 56 |
|
| 57 |
return null; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* carries out the prediction of all targets in file containing multiple targets |
| 63 |
* @param targetFile |
| 64 |
*/ |
| 65 |
public void predictTargetList(String targetFile, String outFile, boolean eliminateDuplicateHits, boolean useEValueThreshold, double eValueTreshold, int k, String distributionFile) |
| 66 |
{ |
| 67 |
try { |
| 68 |
BufferedReader in = Tools.openFile(targetFile); |
| 69 |
BufferedWriter naiveWriter = Tools.writeToFile(outFile + ".naive"); |
| 70 |
BufferedWriter leaveWriter = Tools.writeToFile(outFile + ".leaves"); |
| 71 |
|
| 72 |
// read the distribution of global quality scores |
| 73 |
double[] distribution = null; |
| 74 |
if(distributionFile != null) |
| 75 |
{ |
| 76 |
distribution = new double[100]; |
| 77 |
int i = 0; |
| 78 |
BufferedReader distReader = Tools.openFile(distributionFile); |
| 79 |
|
| 80 |
String line = null; |
| 81 |
while((line = distReader.readLine()) != null) |
| 82 |
{ |
| 83 |
distribution[i] = Double.parseDouble(line); |
| 84 |
i++; |
| 85 |
} |
| 86 |
if(i != 100) |
| 87 |
System.err.println("warning: distribution file does not contain exactly 100 quantile lines."); |
| 88 |
|
| 89 |
distReader.close(); |
| 90 |
} |
| 91 |
|
| 92 |
// the current target for which to perform a prediction |
| 93 |
PredictionTarget target = null; |
| 94 |
|
| 95 |
int numPredicted = 0; |
| 96 |
// iterate over all targets |
| 97 |
while((target = getNextTarget(in, eliminateDuplicateHits)) != null) |
| 98 |
{ |
| 99 |
// skip double // error in blast output file |
| 100 |
if(target.getTargetID() == null) |
| 101 |
continue; |
| 102 |
|
| 103 |
//System.err.println(">" + target.getTargetID()); |
| 104 |
try { |
| 105 |
target.predict(relations, useEValueThreshold, eValueTreshold, k, naiveWriter, leaveWriter, distribution); |
| 106 |
} catch(Exception e) |
| 107 |
{ |
| 108 |
System.err.println("an error occured while predicting target " + target.getTargetID()); |
| 109 |
e.printStackTrace(); |
| 110 |
} |
| 111 |
|
| 112 |
numPredicted++; |
| 113 |
} |
| 114 |
|
| 115 |
System.out.println(" predicted " + numPredicted + " sequences."); |
| 116 |
in.close(); |
| 117 |
naiveWriter.close(); |
| 118 |
leaveWriter.close(); |
| 119 |
} catch(IOException e) |
| 120 |
{ |
| 121 |
e.printStackTrace(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
} |