Debian Codegraph @

current/bin/SimilarityMeasures.java

raw symbols SCIP
1 package bin;
2
3 import java.util.Arrays;
4
5 import shared.Parse;
6 import shared.Tools;
7
8 /** Mostly written by ChatGPT and modified by me */
9 public class SimilarityMeasures {
10
11 public static void main(String[] args) {
12 float[] sample1={0.1f, 0.2f, 0.3f, 0.4f};
13 float[] sample2={0.1f, 0.2f, 0.4f, 0.3f};
14 int[] sample1i={1, 2, 3, 4};
15 int[] sample2i={1, 2, 4, 3};
16 int[] sample3i={2, 4, 6, 8};
17 int[] sample4i={8, 6, 4, 2};
18
19 // Print the similarity vector
20 System.out.println("Difference Vector Float12: "+Arrays.toString(calculateDifferenceVector(sample1, sample2)));
21 System.out.println("Difference Vector Int12: "+Arrays.toString(calculateDifferenceVector(sample1i, sample2i)));
22 System.out.println("Difference Vector Int13: "+Arrays.toString(calculateDifferenceVector(sample1i, sample3i)));
23 System.out.println("Difference Vector Int14: "+Arrays.toString(calculateDifferenceVector(sample1i, sample4i)));
24 }
25
26 public static boolean parse(String arg, String a, String b){
27 if(a.equals("null")){
28 //Do nothing
29 }else if(a.equals("cosine") || a.equals("cos")){
30 COSINE=Parse.parseBoolean(b);
31 }else if(a.equals("gccompensated")){
32 GC_COMPENSATED=Parse.parseBoolean(b);
33 }else if(a.equals("euclid") || a.equals("euc")){
34 EUCLID=Parse.parseBoolean(b);
35 }else if(a.equals("absolute") || a.equals("abs")){
36 ABSOLUTE=Parse.parseBoolean(b);
37 }else if(a.equals("jsd")){
38 JSD=Parse.parseBoolean(b);
39 }else if(a.equals("hellinger") || a.equals("hell") || a.equals("hel")){
40 HELLINGER=Parse.parseBoolean(b);
41 }else if(a.equals("ks") || a.equals("kst")){
42 KST=Parse.parseBoolean(b);
43 }else {
44 return false;
45 }
46
47 return true;
48 }
49
50 public static float[] calculateDifferenceVector(float[] a, float[] b) {
51 // float cosineSimilarity=cosineSimilarity(a, b);
52 float cosineDifference=cosineDifference(a, b);
53 float euclideanDistance=euclideanDistance(a, b);
54 float absoluteDifference=absDif(a, b);
55 float jensenShannonDivergence=jensenShannonDivergence(a, b);
56 float hellingerDistance=hellingerDistance(a, b);
57 float ksDifference=ksTest(a, b);
58
59 return new float[] {
60 cosineDifference,
61 euclideanDistance,
62 absoluteDifference,
63 jensenShannonDivergence,
64 hellingerDistance,
65 ksDifference
66 };
67 }
68
69 //For setting thresholds before neural net is implemented
70 public static float calculateDifferenceAverage(int[] a, int[] b) {
71 float inva=1f/Tools.max(1, Tools.sum(a));
72 float invb=1f/Tools.max(1, Tools.sum(b));
73 float cosineDifference=(COSINE ? cosineDifference(a, b, inva, invb) : 0);
74 float euclideanDistance=(EUCLID ? euclideanDistance(a, b, inva, invb) : 0);
75 float absoluteDifference=(ABSOLUTE ? absDif(a, b, inva, invb) : 0);
76 float jensenShannonDivergence=(JSD ? jensenShannonDivergence(a, b, inva, invb) : 0);
77 float hellingerDistance=(HELLINGER? hellingerDistance(a, b, inva, invb) : 0);
78 float ksDifference=(KST ? ksTest(a, b, inva, invb) : 0);
79 int div=(COSINE ? 1 : 0)+(EUCLID ? 1 : 0)+(ABSOLUTE ? 1 : 0)+(JSD ? 1 : 0)+(HELLINGER ? 1 : 0)+(KST ? 1 : 0);
80 float ret=(cosineDifference+euclideanDistance+absoluteDifference+
81 jensenShannonDivergence+hellingerDistance+ksDifference)/div;
82 return (Float.isFinite(ret) && ret>0 ? ret : 0);
83 }
84
85 public static float[] calculateDifferenceVector(int[] a, int[] b) {
86 float inva=1f/Tools.max(1, Tools.sum(a));
87 float invb=1f/Tools.max(1, Tools.sum(b));
88 // float cosineSimilarity=cosineSimilarity(a, b, inva, invb);
89 float cosineDifference=cosineDifference(a, b, inva, invb);
90 float euclideanDistance=euclideanDistance(a, b, inva, invb);
91 float absoluteDifference=absDif(a, b, inva, invb);
92 float jensenShannonDivergence=jensenShannonDivergence(a, b, inva, invb);
93 float hellingerDistance=hellingerDistance(a, b, inva, invb);
94 float ksDifference=ksTest(a, b, inva, invb);
95
96 return new float[] {
97 cosineDifference,
98 euclideanDistance,
99 absoluteDifference,
100 jensenShannonDivergence,
101 hellingerDistance,
102 ksDifference
103 };
104 }
105
106 public static float cosineDifference(float[] a, float[] b) {
107 return 1-cosineSimilarity(a, b);
108 }
109
110 public static float cosineSimilarity(float[] a, float[] b) {
111 float dotProduct=0f;
112 float normVec1=0f;
113 float normVec2=0f;
114
115 for (int i=0; i<a.length; i++) {
116 float ai=a[i], bi=b[i];
117 dotProduct+=ai*bi;
118 normVec1+=ai*ai;
119 normVec2+=bi*bi;
120 }
121
122 float ret=(float)(dotProduct/(Math.sqrt(normVec1)*Math.sqrt(normVec2)));
123 return (Float.isFinite(ret) && ret>0 ? ret : 0);
124 }
125
126 public static float cosineDifference(int[] a, int[] b) {
127 float inva=1f/Tools.max(1, Tools.sum(a));
128 float invb=1f/Tools.max(1, Tools.sum(b));
129 float ret=1-cosineSimilarity(a, b, inva, invb);
130 return (Float.isFinite(ret) && ret>0 ? ret : 0);
131 }
132
133 public static float cosineDifference(int[] a, int[] b, float inva, float invb) {
134 return 1-cosineSimilarity(a, b, inva, invb);
135 }
136
137 public static float cosineDifferenceCompensated(int[] a, int[] b, int k) {
138 return 1-cosineSimilarityCompensated(a, b, k, BinObject.gcmapMatrix[k]);
139 }
140
141 public static float cosineSimilarity(int[] a, int[] b, float inva, float invb) {
142 if(GC_COMPENSATED) {return cosineSimilarityCompensated(a, b, 4, BinObject.gcmapMatrix[4]);}
143 float dotProduct=0f;
144 float normVec1=0f;
145 float normVec2=0f;
146
147 for (int i=0; i<a.length; i++) {
148 float ai=a[i]*inva, bi=b[i]*invb;
149 dotProduct+=ai*bi;
150 normVec1+=ai*ai;
151 normVec2+=bi*bi;
152 }
153
154 return (float)(dotProduct/(Math.sqrt(normVec1)*Math.sqrt(normVec2)));
155 }
156
157 public static float cosineSimilarityCompensated(int[] a, int[] b, int k, int[] gcmap) {
158
159 float[] aSum=new float[k+1];
160 float[] bSum=new float[k+1];
161
162 for(int i=0; i<a.length; i++) {
163 int gc=gcmap[i];
164 aSum[gc]+=a[i];
165 bSum[gc]+=b[i];
166 }
167
168 for(int i=0; i<aSum.length; i++) {
169 aSum[i]=1f/Math.max(aSum[i], 1);
170 bSum[i]=1f/Math.max(bSum[i], 1);
171 }
172
173 float dotProduct=0f;
174 float normVec1=0f;
175 float normVec2=0f;
176
177 for (int i=0; i<a.length; i++) {
178 int gc=gcmap[i];
179 float ai=a[i]*aSum[gc], bi=b[i]*bSum[gc];
180 dotProduct+=ai*bi;
181 normVec1+=ai*ai;
182 normVec2+=bi*bi;
183 }
184
185 return (float)(dotProduct/(Math.sqrt(normVec1)*Math.sqrt(normVec2)));
186 }
187
188 public static float euclideanDistance(float[] a, float[] b) {
189 float sumSquaredDifferences=0f;
190
191 for (int i=0; i<a.length; i++) {
192 float ai=a[i], bi=b[i];
193 float d=ai-bi;
194 sumSquaredDifferences+=d*d;
195 }
196
197 return (float)Math.sqrt(sumSquaredDifferences);
198 }
199
200
201 public static float euclideanDistance(int[] a, int[] b) {
202 float inva=1f/Tools.max(1, Tools.sum(a));
203 float invb=1f/Tools.max(1, Tools.sum(b));
204 float ret=euclideanDistance(a, b, inva, invb);
205 return (Float.isFinite(ret) && ret>0 ? ret : 0);
206 }
207
208 public static float euclideanDistance(int[] a, int[] b, float inva, float invb) {
209 float sumSquaredDifferences=0f;
210
211 for (int i=0; i<a.length; i++) {
212 float ai=a[i]*inva, bi=b[i]*invb;
213 float d=ai-bi;
214 sumSquaredDifferences+=d*d;
215 }
216
217 return (float)Math.sqrt(sumSquaredDifferences);
218 }
219
220 /**
221 * @param a Contig kmer frequencies
222 * @param b Cluster kmer frequencies
223 * @return Score
224 */
225 static final float absDif(float[] a, float[] b){
226 assert(a.length==b.length);
227 double sum=0;
228 for(int i=0; i<a.length; i++){
229 sum+=Math.abs(a[i]-b[i]);
230 }
231
232 return (float)sum;
233 }
234
235 /**
236 * @param a Contig kmer counts
237 * @param b Cluster kmer counts
238 * @return Score
239 */
240 static final float absDif(int[] a, int[] b, float inva, float invb){
241 assert(a.length==b.length);
242 float sum=0;
243 for(int i=0; i<a.length; i++){
244 float ai=a[i]*inva, bi=b[i]*invb;
245 sum+=Math.abs(ai-bi);
246 }
247 return sum;
248 }
249
250 public static float jensenShannonDivergence(float[] a, float[] b) {
251 float kldSumA=0, kldSumB=0;
252 for (int i=0; i<a.length; i++) {
253 float ai=a[i]+0.0005f, bi=b[i]+0.0005f;//Prevents zero values
254 float avgi=(ai+bi)*0.5f;
255 kldSumA+=ai*Math.log(ai/avgi);
256 kldSumA+=bi*Math.log(bi/avgi);
257 }
258 return (kldSumA+kldSumB)*invLog2*0.5f;
259 }
260
261 public static float jensenShannonDivergence(int[] a, int[] b, float inva, float invb) {
262 float kldSumA=0, kldSumB=0;
263 for (int i=0; i<a.length; i++) {
264 float ai=a[i]*inva+0.0005f, bi=b[i]*invb+0.0005f;//Prevents zero values
265 float avgi=(ai+bi)*0.5f;
266 kldSumA+=ai*Math.log(ai/avgi);
267 kldSumA+=bi*Math.log(bi/avgi);
268 }
269 return (kldSumA+kldSumB)*invLog2*0.5f;
270 }
271
272 // public static float jensenShannonDivergenceSlow(float[] a, float[] b) {
273 // float[] avg=new float[a.length];
274 // for (int i=0; i<a.length; i++) {
275 // float ai=a[i], bi=b[i];
276 // avg[i]=(ai+bi)*0.5f;
277 // }
278 //
279 // return (kullbackLeiblerDivergence(a, avg)+kullbackLeiblerDivergence(b, avg))*0.5f;
280 // }
281 //
282 // public static float kullbackLeiblerDivergence(float[] p, float[] q) {
283 // float sum=0f;
284 // for (int i=0; i<p.length; i++) {
285 // float pi=p[i], qi=q[i];
286 // if (p[i]!=0) {
287 // sum+=p[i]*Math.log(pi/qi);
288 // }
289 // }
290 // return sum*invLog2;
291 // }
292
293 public static float hellingerDistance(float[] a, float[] b) {
294 float sum=0f;
295 for (int i=0; i<a.length; i++) {
296 float ai=a[i], bi=b[i];
297 float d=(float)(Math.sqrt(ai)-Math.sqrt(bi));
298 sum+=d*d;
299 }
300 return (float)Math.sqrt(sum)*invRoot2;
301 }
302
303 public static float hellingerDistance(int[] a, int[] b, float inva, float invb) {
304 float sum=0f;
305 for (int i=0; i<a.length; i++) {
306 float ai=a[i]*inva, bi=b[i]*invb;
307 float d=(float)(Math.sqrt(ai)-Math.sqrt(bi));
308 sum+=d*d;
309 }
310 return (float)Math.sqrt(sum)*invRoot2;
311 }
312
313 /** This is a KS test for binned histograms, not raw values */
314 public static float ksTest(float[] histogram1, float[] histogram2) {
315 // Ensure both histograms have the same length
316 if (histogram1.length!=histogram2.length) {
317 throw new IllegalArgumentException("Histograms must have the same number of bins");
318 }
319
320 float cd1=0, cd2=0, dMax=0;
321
322 // Compute the KS statistic (maximum absolute difference between the two CDFs)
323 for (int i=0; i<histogram1.length; i++) {
324 cd1+=histogram1[i];
325 cd2+=histogram2[i];
326 dMax=(float)Math.max(dMax, Math.abs(cd1-cd2));
327 }
328
329 return dMax;
330 }
331
332 /** This is a KS test for binned histograms, not raw values */
333 public static float ksTest(int[] a, int[] b, float inva, float invb) {
334 // Ensure both histograms have the same length
335 if (a.length!=b.length) {
336 throw new IllegalArgumentException("Histograms must have the same number of bins");
337 }
338
339 float cda=0, cdb=0, dMax=0;
340
341 // Compute the KS statistic (maximum absolute difference between the two CDFs)
342 for (int i=0; i<a.length; i++) {
343 float ai=a[i]*inva, bi=b[i]*invb;
344 cda+=ai;
345 cdb+=bi;
346 dMax=(float)Math.max(dMax, Math.abs(cda-cdb));
347 }
348
349 return dMax;
350 }
351
352 private static final float root2=(float)Math.sqrt(2);
353 private static final float log2=(float)Math.log(2);
354 private static final float invRoot2=1/root2;
355 private static final float invLog2=1/log2;
356
357
358 public static boolean GC_COMPENSATED=false;
359
360 //2531 kcps (times include contig loading)
361 //26 clusters
362 // Completeness Score: 60.278
363 // Contamination Score: 2.1925
364 public static boolean COSINE=true;
365 //2796 kcps
366 //21 clusters
367 //Completeness Score: 60.909
368 //Contamination Score: 2.3108
369 public static boolean EUCLID=false;//0.008
370 //2636 kcps
371 //23 clusters at 4x threshold of cosine
372 // Completeness Score: 60.947
373 // Contamination Score: 1.7679
374 public static boolean ABSOLUTE=false; //Best at 0.089
375 //183 kcps
376 //22 clusters
377 // Completeness Score: 59.169
378 // Contamination Score: 2.1959
379 public static boolean JSD=false;
380 //953 kcps
381 //~22 at 2x threshold of cosine
382 // Completeness Score: 61.072
383 // Contamination Score: 1.9358
384 public static boolean HELLINGER=false;//0.0425
385 //1859 kcps
386 //20 clusters
387 // Completeness Score: 26.380
388 // Contamination Score: 3.1930
389 public static boolean KST=false;
390
391 }