Debian Codegraph @

metastudentPkg/lib/groupB/pp2/go/DAG.java

raw
1 package pp2.go;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5 import java.util.HashSet;
6
7 import pp2.prediction.knn.DAGNode;
8
9 public class DAG {
10
11 String[] nodes = new String[0];
12 HashMap<String,String[]> ancestors;
13 String[] leaves = new String[0];
14 HashMap<String,DAGNode> DAGNodes;
15
16 /**
17 *
18 * @author Mark Heron
19 *
20 */
21 public DAG(Relations rel, String[] goterms) {
22
23 //filter goterms
24 String[] goterms_f = new String[0];
25 HashSet<String> tmp = new HashSet<String>();
26 tmp.addAll(Arrays.asList(goterms));
27 goterms_f = tmp.toArray(goterms_f);
28 tmp = new HashSet<String>();
29 for (String goterm : goterms_f) {
30 if(rel.getType(goterm).length() > 0 && rel.isTypeofInterst(goterm)) {
31 tmp.add(goterm);
32 }
33 }
34 goterms_f = new String[0];
35 goterms_f = tmp.toArray(goterms_f);
36
37
38 // make nodes
39 HashSet<String> nodes_tmp = new HashSet<String>();
40 nodes_tmp.addAll(Arrays.asList(goterms_f));
41 for (String goterm : goterms_f) {
42 nodes_tmp.addAll(Arrays.asList(rel.getAncestors(goterm)));
43 }
44 nodes = nodes_tmp.toArray(nodes);
45
46 // make ancestors
47 ancestors = new HashMap<String, String[]>();
48 for (String goterm : nodes) {
49 ancestors.put(goterm, rel.getAncestors(goterm));
50 }
51
52 // make leaves
53 HashSet<String> leaves_tmp = new HashSet<String>();
54 for (String goterm : goterms_f) {
55 boolean leaf = true;
56 for (String goterm2 : goterms_f) {
57 if(goterm != goterm2) {
58 for (String ancestor : rel.getAncestors(goterm2)) {
59 if(goterm.equals(ancestor)) {
60 leaf = false;
61 }
62 }
63 }
64 }
65 if(leaf) {
66 leaves_tmp.add(goterm);
67 }
68 }
69 leaves = leaves_tmp.toArray(leaves);
70
71 // make initial scores
72 DAGNodes = new HashMap<String, DAGNode>();
73 for (String node : nodes) {
74 DAGNodes.put(node, new DAGNode());
75
76 // also add scores for the ancestors
77 for (String anc : ancestors.get(node)) {
78 DAGNodes.put(anc, new DAGNode());
79 }
80 }
81
82 }
83
84
85 public String[] getNodes() {
86 return nodes;
87 }
88
89 public String[] getAncestors(String goterm) {
90 return ancestors.get(goterm);
91 }
92
93 public String[] getLeaves() {
94 return leaves;
95 }
96
97 public DAGNode getDAGNode(String goterm) {
98 return DAGNodes.get(goterm);
99 }
100
101 public HashMap<String, DAGNode> getDAGNodes()
102 {
103 return DAGNodes;
104 }
105 }