annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/fun/ProbShared3.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 package fun;
jpayne@68 2
jpayne@68 3 import java.util.HashSet;
jpayne@68 4 import java.util.Random;
jpayne@68 5
jpayne@68 6 public class ProbShared3 {
jpayne@68 7
jpayne@68 8 public static void main(String args[]){
jpayne@68 9 int k=Integer.parseInt(args[0]);
jpayne@68 10 int len1=Integer.parseInt(args[1]);
jpayne@68 11 int len2=Integer.parseInt(args[2]);
jpayne@68 12 int rounds=Integer.parseInt(args[3]);
jpayne@68 13
jpayne@68 14 System.out.println("Probability: "+simulate(k, len1, len2, rounds));
jpayne@68 15 }
jpayne@68 16
jpayne@68 17 static double simulate(int k, int len1, int len2, int rounds){
jpayne@68 18 int successes=0;
jpayne@68 19 final HashSet<Long> set=new HashSet<Long>();
jpayne@68 20 for(int i=0; i<rounds; i++){
jpayne@68 21 successes+=simulateOnePair(k, len1, len2, set);
jpayne@68 22 }
jpayne@68 23 return successes/(double)rounds;
jpayne@68 24 }
jpayne@68 25
jpayne@68 26 static int simulateOnePair(int k, int len1, int len2, HashSet<Long> set){
jpayne@68 27 fillRandomSet(k, len2, set);
jpayne@68 28 final long space=(long)Math.pow(4, k);
jpayne@68 29 final int kmers=len1-k+1;
jpayne@68 30 for(int i=0; i<kmers; i++){
jpayne@68 31 long kmer=(randy.nextLong()&Long.MAX_VALUE)%space;
jpayne@68 32 if(set.contains(kmer)){return 1;}
jpayne@68 33 }
jpayne@68 34 return 0;
jpayne@68 35 }
jpayne@68 36
jpayne@68 37 static void fillRandomSet(int k, int len, HashSet<Long> set){
jpayne@68 38 set.clear();
jpayne@68 39 final long space=(long)Math.pow(4, k);
jpayne@68 40 final int kmers=len-k+1;
jpayne@68 41 for(int i=0; i<kmers; i++){
jpayne@68 42 set.add((randy.nextLong()&Long.MAX_VALUE)%space);
jpayne@68 43 }
jpayne@68 44 }
jpayne@68 45
jpayne@68 46 static final Random randy=new Random();
jpayne@68 47
jpayne@68 48 }