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