| 1 |
package tax; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.BitSet; |
| 5 |
import java.util.PriorityQueue; |
| 6 |
|
| 7 |
import shared.Tools; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class IDNode implements Comparable<IDNode>{ |
| 16 |
|
| 17 |
public static IDNode makeTree(IDNode[] nodes){ |
| 18 |
|
| 19 |
PriorityQueue<IDNode> heap=new PriorityQueue<IDNode>(nodes.length); |
| 20 |
ArrayList<IDNode> list=new ArrayList<IDNode>(2*nodes.length); |
| 21 |
for(IDNode n : nodes){ |
| 22 |
list.add(n); |
| 23 |
heap.add(n); |
| 24 |
} |
| 25 |
|
| 26 |
while(heap.size()>1){ |
| 27 |
IDNode a=heap.poll(); |
| 28 |
|
| 29 |
if(a.parent==null){ |
| 30 |
IDNode b=nodes[a.maxPos]; |
| 31 |
if(b.parent!=null){ |
| 32 |
|
| 33 |
} |
| 34 |
while(b.parent!=null){ |
| 35 |
b=b.parent; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
IDNode c=new IDNode(a, b, list.size()); |
| 40 |
list.add(c); |
| 41 |
heap.add(c); |
| 42 |
|
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
return heap.poll(); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public int compareTo(IDNode idn) { |
| 54 |
if(max==idn.max){return number-idn.number;} |
| 55 |
return max<idn.max ? 1 : -1; |
| 56 |
} |
| 57 |
|
| 58 |
public IDNode(double[] array_, int number_, String name_){ |
| 59 |
array=array_; |
| 60 |
number=number_; |
| 61 |
name=name_; |
| 62 |
left=right=null; |
| 63 |
maxPos=(array.length>0 ? Tools.maxIndex(array) : 0); |
| 64 |
max=(maxPos>=array.length ? 0 : array[maxPos]); |
| 65 |
bs=new BitSet(number+1); |
| 66 |
bs.set(number); |
| 67 |
} |
| 68 |
|
| 69 |
double[] shorter(double[] a, double[] b){ |
| 70 |
return a.length<b.length ? a : b; |
| 71 |
} |
| 72 |
|
| 73 |
double[] longer(double[] a, double[] b){ |
| 74 |
return a.length<b.length ? b : a; |
| 75 |
} |
| 76 |
|
| 77 |
public IDNode(IDNode a, IDNode b, int number_){ |
| 78 |
assert(a!=b) : a+"; "+a.parent+"; "+a.left+"; "+a.right; |
| 79 |
assert(a.parent==null); |
| 80 |
assert(b.parent==null); |
| 81 |
assert(a.max>=b.max); |
| 82 |
|
| 83 |
number=number_; |
| 84 |
|
| 85 |
double[] array1=longer(a.array, b.array); |
| 86 |
double[] array2=shorter(a.array, b.array); |
| 87 |
assert(array1!=array2) : a.array.length+", "+b.array.length+", "+a.array+", "+b.array; |
| 88 |
|
| 89 |
bs=new BitSet(); |
| 90 |
bs.or(a.bs); |
| 91 |
bs.or(b.bs); |
| 92 |
array=array1.clone(); |
| 93 |
for(int i=0; i<array2.length; i++){ |
| 94 |
array[i]=Tools.max(array[i], array2[i]); |
| 95 |
} |
| 96 |
array[a.maxPos]=0; |
| 97 |
for(int i=0; i<array.length; i++){ |
| 98 |
if(bs.get(i)){array[i]=0;} |
| 99 |
} |
| 100 |
maxPos=Tools.maxIndex(array); |
| 101 |
max=array[maxPos]; |
| 102 |
left=a; |
| 103 |
right=b; |
| 104 |
a.parent=b.parent=this; |
| 105 |
} |
| 106 |
|
| 107 |
public StringBuilder toNewick(){ |
| 108 |
StringBuilder sb=new StringBuilder(); |
| 109 |
toNewick(sb); |
| 110 |
return sb; |
| 111 |
} |
| 112 |
|
| 113 |
private void toNewick(StringBuilder sb){ |
| 114 |
if(left!=null){ |
| 115 |
sb.append('('); |
| 116 |
left.toNewick(sb); |
| 117 |
sb.append(','); |
| 118 |
right.toNewick(sb); |
| 119 |
sb.append(')'); |
| 120 |
} |
| 121 |
if(name!=null){ |
| 122 |
for(int i=0; i<name.length(); i++){ |
| 123 |
char c=name.charAt(i); |
| 124 |
if(c=='(' || c==')' || c==':' || c==',' || c==';' || Character.isWhitespace(c)){c='_';} |
| 125 |
sb.append(c); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if(parent!=null){ |
| 130 |
sb.append(':'); |
| 131 |
|
| 132 |
double len; |
| 133 |
if(left==null){ |
| 134 |
len=1-Tools.max(parent.left.max, parent.right.max); |
| 135 |
}else{ |
| 136 |
len=Tools.max(left.max, right.max)-max; |
| 137 |
} |
| 138 |
|
| 139 |
sb.append(Tools.format("%.4f", len)); |
| 140 |
|
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public String toString(){ |
| 146 |
return "("+number+" "+Tools.format("%.4f", max)+" "+toString(array)+")"; |
| 147 |
} |
| 148 |
|
| 149 |
private static String toString(double[] array){ |
| 150 |
StringBuilder sb=new StringBuilder(); |
| 151 |
sb.append('['); |
| 152 |
sb.append(' '); |
| 153 |
for(double d : array){ |
| 154 |
sb.append(Tools.format("%.4f ", d)); |
| 155 |
} |
| 156 |
sb.append(']'); |
| 157 |
return sb.toString(); |
| 158 |
} |
| 159 |
|
| 160 |
public String name; |
| 161 |
public double[] array; |
| 162 |
public final int number; |
| 163 |
public final int maxPos; |
| 164 |
public final double max; |
| 165 |
public final BitSet bs; |
| 166 |
|
| 167 |
public IDNode parent; |
| 168 |
public final IDNode left; |
| 169 |
public final IDNode right; |
| 170 |
|
| 171 |
} |