| 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 |
package dr.evolution.tree; |
| 27 |
|
| 28 |
import java.util.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public interface RankedForest { |
| 34 |
|
| 35 |
public List<RankedNode> getNodes(); |
| 36 |
|
| 37 |
public int getSize(); |
| 38 |
|
| 39 |
public int rank(); |
| 40 |
|
| 41 |
public boolean compatibleRank(List<BitSet> constraints); |
| 42 |
|
| 43 |
public Set<BitSet> clades(); |
| 44 |
|
| 45 |
public int constraintsSatisfied(); |
| 46 |
|
| 47 |
public boolean isClear(); |
| 48 |
|
| 49 |
|
| 50 |
public class Default implements RankedForest { |
| 51 |
|
| 52 |
List<RankedNode> nodes; |
| 53 |
boolean clear; |
| 54 |
|
| 55 |
public Default(int size, boolean clear) { |
| 56 |
nodes = new ArrayList<RankedNode>(size); |
| 57 |
for (int i = size - 1; i >= 0; i--) { |
| 58 |
nodes.add(new RankedNode(i, size)); |
| 59 |
} |
| 60 |
this.clear = clear; |
| 61 |
} |
| 62 |
|
| 63 |
public int getSize() { |
| 64 |
return nodes.get(0).n; |
| 65 |
} |
| 66 |
|
| 67 |
public int rank() { |
| 68 |
return nodes.get(0).rank; |
| 69 |
} |
| 70 |
|
| 71 |
public boolean compatibleRank(List<BitSet> constraints) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
public Set<BitSet> clades() { |
| 76 |
return Collections.emptySet(); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
public int constraintsSatisfied() { |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
public boolean isClear() { |
| 85 |
return clear; |
| 86 |
} |
| 87 |
|
| 88 |
public List<RankedNode> getNodes() { |
| 89 |
return nodes; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public class Parent implements RankedForest { |
| 94 |
|
| 95 |
RankedForest child; |
| 96 |
RankedNode parent; |
| 97 |
List<RankedNode> nodes; |
| 98 |
HashSet<BitSet> clades; |
| 99 |
int constraintsSatisfied = 0; |
| 100 |
boolean clear; |
| 101 |
|
| 102 |
public Parent(RankedForest child, RankedNode parent, List<BitSet> constraints) { |
| 103 |
this.child = child; |
| 104 |
this.parent = parent; |
| 105 |
nodes = new ArrayList<RankedNode>(); |
| 106 |
nodes.add(parent); |
| 107 |
nodes.addAll(child.getNodes()); |
| 108 |
nodes.remove(parent.child1); |
| 109 |
nodes.remove(parent.child2); |
| 110 |
|
| 111 |
clades = new HashSet<BitSet>(); |
| 112 |
clades.addAll(child.clades()); |
| 113 |
clades.add(parent.cladeBits); |
| 114 |
|
| 115 |
if (!clear) { |
| 116 |
constraintsSatisfied = child.constraintsSatisfied(); |
| 117 |
|
| 118 |
if (constraints.contains(parent.cladeBits)) { |
| 119 |
|
| 120 |
constraintsSatisfied += 1; |
| 121 |
} |
| 122 |
clear = (constraintsSatisfied == constraints.size()); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
public List<RankedNode> getNodes() { |
| 129 |
return nodes; |
| 130 |
} |
| 131 |
|
| 132 |
public int getSize() { |
| 133 |
return nodes.get(0).n; |
| 134 |
} |
| 135 |
|
| 136 |
public int rank() { |
| 137 |
return nodes.get(0).rank; |
| 138 |
} |
| 139 |
|
| 140 |
public boolean compatibleRank(List<BitSet> constraints) { |
| 141 |
|
| 142 |
if (constraints.size() == 1) return true; |
| 143 |
|
| 144 |
int rank = 0; |
| 145 |
for (BitSet constraint : constraints) { |
| 146 |
int newRank = getRank(constraint); |
| 147 |
if (newRank < rank) return false; |
| 148 |
rank = newRank; |
| 149 |
} |
| 150 |
if (rank != Integer.MAX_VALUE) clear = true; |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
public Set<BitSet> clades() { |
| 155 |
return clades; |
| 156 |
} |
| 157 |
|
| 158 |
public int constraintsSatisfied() { |
| 159 |
return constraintsSatisfied; |
| 160 |
} |
| 161 |
|
| 162 |
public boolean isClear() { |
| 163 |
return clear; |
| 164 |
} |
| 165 |
|
| 166 |
private int getRank(BitSet constraint) { |
| 167 |
|
| 168 |
|
| 169 |
if (parent.cladeBits.equals(constraint)) { |
| 170 |
return parent.rank; |
| 171 |
} else { |
| 172 |
if (child instanceof Parent) { |
| 173 |
return ((Parent) child).getRank(constraint); |
| 174 |
} |
| 175 |
return Integer.MAX_VALUE; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
} |
| 181 |
} |