| 1 |
package pp2.go; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.HashMap; |
| 6 |
import java.util.HashSet; |
| 7 |
|
| 8 |
/** |
| 9 |
* example constructor: |
| 10 |
* Relations test = new Relations("./../data/deductiveClosureGO.txt","./../data/goterm.txt",true); |
| 11 |
* noCC stands for no Cellular Component (for CAFA, true, since CC is ignored) |
| 12 |
* @author Mark Heron |
| 13 |
* |
| 14 |
*/ |
| 15 |
public class Relations { |
| 16 |
|
| 17 |
HashMap<String, String[]> ahnen; |
| 18 |
HashMap<String, String> type; |
| 19 |
boolean noCC; |
| 20 |
|
| 21 |
public Relations(String closure, String terms, boolean noCC) { |
| 22 |
|
| 23 |
BufferedReader input; |
| 24 |
|
| 25 |
this.noCC = noCC; |
| 26 |
|
| 27 |
// create type |
| 28 |
type = new HashMap<String, String>(); |
| 29 |
input = pp2.tools.Tools.openFile(terms); |
| 30 |
|
| 31 |
try { |
| 32 |
String line; |
| 33 |
while((line = input.readLine()) != null) { |
| 34 |
|
| 35 |
String[] split = line.split("\t"); |
| 36 |
if(split.length > 4) { |
| 37 |
|
| 38 |
String gokey = split[3]; |
| 39 |
String typevalue = split[2]; |
| 40 |
|
| 41 |
type.put(gokey, typevalue); |
| 42 |
} |
| 43 |
} |
| 44 |
input.close(); |
| 45 |
} catch (Exception e) { |
| 46 |
e.printStackTrace(); |
| 47 |
} |
| 48 |
|
| 49 |
// create ancestors/parents |
| 50 |
ahnen = new HashMap<String, String[]>(); |
| 51 |
HashMap<String, HashSet<String>> ahnen_tmp = new HashMap<String, HashSet<String>>(); |
| 52 |
|
| 53 |
input = pp2.tools.Tools.openFile(closure); |
| 54 |
|
| 55 |
try { |
| 56 |
String line; |
| 57 |
while((line = input.readLine()) != null) { |
| 58 |
|
| 59 |
if(line.startsWith("GO:")) { |
| 60 |
|
| 61 |
String[] split = line.split("\t"); |
| 62 |
String key = split[0]; |
| 63 |
String value = split[2]; |
| 64 |
if(getType(key).length() > 0 && getType(value).length() > 0 ) { |
| 65 |
if(isTypeofInterst(key) && isTypeofInterst(value)) { |
| 66 |
if(ahnen_tmp.containsKey(key)) { |
| 67 |
ahnen_tmp.get(key).add(value); |
| 68 |
} else { |
| 69 |
HashSet<String> ll = new HashSet<String>(); |
| 70 |
ll.add(value); |
| 71 |
ahnen_tmp.put(key, ll); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
input.close(); |
| 78 |
} catch (Exception e) { |
| 79 |
e.printStackTrace(); |
| 80 |
} |
| 81 |
|
| 82 |
for (String key : ahnen_tmp.keySet()) { |
| 83 |
String[] values = new String[0]; |
| 84 |
values = ahnen_tmp.get(key).toArray(values); |
| 85 |
ahnen.put(key, values); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public String[] getAncestors(String goterm) { |
| 93 |
if(ahnen.containsKey(goterm)) { |
| 94 |
return ahnen.get(goterm); |
| 95 |
} else { |
| 96 |
return new String[0]; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public String getType(String goterm) { |
| 103 |
if (type.containsKey(goterm)) { |
| 104 |
return type.get(goterm); |
| 105 |
} else { |
| 106 |
return ""; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public boolean isTypeofInterst(String goterm) { |
| 111 |
if(!type.containsKey(goterm)) { // only happens for GO:0048220 |
| 112 |
return true; // no idea why it is missing in goterm.txt |
| 113 |
} |
| 114 |
String tmp = type.get(goterm); |
| 115 |
if(tmp.equals("biological_process") || tmp.equals("molecular_function") || tmp.equals("cellular_component")) { |
| 116 |
return true; |
| 117 |
} else if (!noCC && tmp.equals("cellular_component")){ |
| 118 |
return true; |
| 119 |
} else { |
| 120 |
return false; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public String[][] getExclusivePaths(String[] goterms) { |
| 127 |
|
| 128 |
String[][] results = new String[goterms.length][]; |
| 129 |
HashSet<String> allreadydone = new HashSet<String>(); |
| 130 |
|
| 131 |
for (int i = 0; i < goterms.length; i++) { |
| 132 |
String goterm = goterms[i]; |
| 133 |
String[] anc = getAncestors(goterm); |
| 134 |
HashSet<String> exc = new HashSet<String>(); |
| 135 |
|
| 136 |
for (String a : anc) { |
| 137 |
if( ! allreadydone.contains(a)) { |
| 138 |
exc.add(a); |
| 139 |
allreadydone.add(a); |
| 140 |
} |
| 141 |
} |
| 142 |
String[] tmp = new String[0]; |
| 143 |
results[i] = (String[]) exc.toArray(tmp); |
| 144 |
} |
| 145 |
return results; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public static void main(String[] args) { |
| 151 |
|
| 152 |
Relations test = new Relations("C:\\Dokumente und Einstellungen\\murgs\\Eigene Dateien\\Studium\\9. Semester\\PP2\\ProteinPrediction2\\data\\deductiveClosureGO.txt" |
| 153 |
,"C:\\Dokumente und Einstellungen\\murgs\\Eigene Dateien\\Studium\\9. Semester\\PP2\\ProteinPrediction2\\data\\goterm.txt",true); |
| 154 |
String[] tmp = {"GO:999","GO:0005737","GO:0019134","GO:0000287","GO:0003977","GO:0000902","GO:0007047","GO:0009103","GO:0009252","GO:0008360","GO:0046872","GO:0008152"}; |
| 155 |
DAG dag = new DAG(test, tmp); |
| 156 |
System.out.println(Arrays.toString(dag.getNodes())); |
| 157 |
System.out.println(Arrays.toString(dag.getLeaves())); |
| 158 |
System.out.println(Arrays.toString(dag.getAncestors("GO:0005737"))); |
| 159 |
System.out.println(Arrays.deepToString(test.getExclusivePaths(tmp))); |
| 160 |
} |
| 161 |
} |