| 1 |
import java.io.BufferedReader; |
| 2 |
import java.io.FileNotFoundException; |
| 3 |
import java.io.FileReader; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.Hashtable; |
| 6 |
import java.util.Vector; |
| 7 |
|
| 8 |
/** |
| 9 |
* TODO |
| 10 |
* implement all methods |
| 11 |
* include GO Parser |
| 12 |
* |
| 13 |
* @author Yannick, Ariane |
| 14 |
* |
| 15 |
*/ |
| 16 |
public class Validator { |
| 17 |
|
| 18 |
private String valFile; |
| 19 |
private String parentFile = "GO_Table.txt"; |
| 20 |
private Hashtable<String, String[]> parentTable; |
| 21 |
|
| 22 |
private double rec; |
| 23 |
private double prec; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor |
| 27 |
* @param validationFile |
| 28 |
* @throws IOException |
| 29 |
* @throws FileNotFoundException |
| 30 |
*/ |
| 31 |
public Validator(String validationFile) throws FileNotFoundException, IOException{ |
| 32 |
valFile = validationFile; |
| 33 |
parentTable = createParentTable(); |
| 34 |
rec = 0; |
| 35 |
prec = 0; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* this method does the validation job |
| 40 |
* including *reading the validationFile |
| 41 |
* *comparing GO Terms |
| 42 |
* *calculate Performance of results |
| 43 |
*/ |
| 44 |
public void validate() throws FileNotFoundException, IOException{ |
| 45 |
|
| 46 |
BufferedReader valFileReader = new BufferedReader(new FileReader(valFile)); |
| 47 |
String line; |
| 48 |
int counter = 0; |
| 49 |
while((line = valFileReader.readLine()) != null){ |
| 50 |
String[] tempLine = line.substring(1).split("\t"); |
| 51 |
String[] ids = tempLine[0].split(","); |
| 52 |
String[] GOs = tempLine[1].split(","); |
| 53 |
|
| 54 |
counter++; |
| 55 |
|
| 56 |
Vector<String> idsWithHull = getHull(ids); |
| 57 |
Vector<String> GOsWithHull = getHull(GOs); |
| 58 |
|
| 59 |
calcPosNegs(idsWithHull, GOsWithHull); |
| 60 |
|
| 61 |
} |
| 62 |
calcPerformanceII(counter); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* returns the Hull for a specific term |
| 67 |
* @param terms |
| 68 |
* @return |
| 69 |
*/ |
| 70 |
|
| 71 |
private Vector<String> getHull(String[] terms){ |
| 72 |
|
| 73 |
Vector<String> termsWithHull = new Vector<String>(); |
| 74 |
|
| 75 |
for(String term : terms){ |
| 76 |
String[] termParents = parentTable.get(term); |
| 77 |
termsWithHull.add(term); |
| 78 |
|
| 79 |
if(termParents != null){ |
| 80 |
for(String par:termParents){ |
| 81 |
if(!par.equals("GO:0003674") && !par.equals("GO:0008150") && !par.equals("GO:0005575") && !termsWithHull.contains(par)){ |
| 82 |
termsWithHull.add(par); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
return termsWithHull; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* saves the GO_Parent file into a Hastable |
| 93 |
* @return |
| 94 |
* @throws FileNotFoundException |
| 95 |
* @throws IOException |
| 96 |
*/ |
| 97 |
private Hashtable<String, String[]> createParentTable() throws FileNotFoundException, IOException{ |
| 98 |
|
| 99 |
Hashtable<String, String[]> returnTable = new Hashtable<String, String[]>(); |
| 100 |
|
| 101 |
BufferedReader parentFileReader = new BufferedReader(new FileReader(parentFile)); |
| 102 |
|
| 103 |
String inline; |
| 104 |
while((inline = parentFileReader.readLine()) != null){ |
| 105 |
String[] tempinline = inline.split("\t"); |
| 106 |
String GO = tempinline[0]; |
| 107 |
String[] parents = tempinline[1].split(","); |
| 108 |
|
| 109 |
returnTable.put(GO, parents); |
| 110 |
} |
| 111 |
return returnTable; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* calculates the TP, FP, FN and the first Part for the Performance |
| 116 |
* @param idsWithHull |
| 117 |
* @param GOsWithHull |
| 118 |
*/ |
| 119 |
private void calcPosNegs(Vector<String> idsWithHull, Vector<String> GOsWithHull){ |
| 120 |
|
| 121 |
int TP = 0; |
| 122 |
int FP = 0; |
| 123 |
int FN = 0; |
| 124 |
|
| 125 |
Vector<String> toRemove = new Vector<String>(); |
| 126 |
|
| 127 |
for(String a : idsWithHull){ |
| 128 |
if (GOsWithHull.contains(a)){ |
| 129 |
TP++; |
| 130 |
toRemove.add(a); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
for(String tr : toRemove){ |
| 135 |
idsWithHull.remove(tr); |
| 136 |
GOsWithHull.remove(tr); |
| 137 |
} |
| 138 |
|
| 139 |
FP = GOsWithHull.size(); |
| 140 |
FN = idsWithHull.size(); |
| 141 |
|
| 142 |
calcPerformanceI(TP, FP, FN); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* calculates the first Step for Recall and Precision |
| 148 |
* @param TP |
| 149 |
* @param FP |
| 150 |
* @param FN |
| 151 |
*/ |
| 152 |
private void calcPerformanceI(int TP, int FP, int FN){ |
| 153 |
double recall = (TP / (TP + FN)); |
| 154 |
double precision = (TP / (TP + FP)); |
| 155 |
|
| 156 |
prec += precision; |
| 157 |
rec += recall; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* calculates the performance of Recall and Precision |
| 162 |
* @param counter |
| 163 |
*/ |
| 164 |
private void calcPerformanceII(int counter){ |
| 165 |
|
| 166 |
double precision = prec / (double)counter; |
| 167 |
double recall = rec / (double)counter; |
| 168 |
|
| 169 |
System.out.println("Precision: " + precision + "\t" + "Recall: " + recall); |
| 170 |
} |
| 171 |
|
| 172 |
} |