Debian Codegraph @

current/bin/KmerProb.java

raw symbols SCIP
1 package bin;
2
3 import java.util.ArrayList;
4
5 import dna.Data;
6 import fileIO.ByteFile;
7 import shared.LineParser1;
8 import shared.Tools;
9 import structures.FloatList;
10
11 public class KmerProb {
12
13 public static synchronized void load() {
14 if(matrix!=null) {return;}
15 String fname=Data.findPath("?shred4merFractions.tsv");
16 matrix=load(fname);
17 }
18
19 private static float[][] load(String fname){
20 ArrayList<byte[]> lines=ByteFile.toLines(fname);
21 ArrayList<FloatList> vectors=new ArrayList<FloatList>();
22 LineParser1 lp=new LineParser1('\t');
23 for(byte[] line : lines) {
24 lp.set(line);
25 if(!lp.startsWith('#')) {
26 // System.err.println("Making a vector: "+lp.terms());
27 FloatList list=new FloatList(lp.terms()-2);
28 for(int i=2; i<lp.terms(); i++) {
29 float f=lp.parseFloat(i);
30 assert(f>=0 && f<=1);
31 list.add(f);
32 }
33 vectors.add(list);
34 }
35 }
36 float[][] data=new float[vectors.size()][];
37 for(int i=0; i<vectors.size(); i++) {
38 FloatList list=vectors.get(i);
39 assert(list.size()==list.array.length);
40 list.shrink();
41 data[i]=list.array;
42 }
43 // System.err.println("Read "+data.length+" vectors.");
44 return data;
45 }
46
47 /** Probability that two sequences with this
48 * kmer frequency cosine difference come from the same genome.
49 * The length is the length of the shorter sequence.
50 * Genomes used for this were 5000 bacteria, <95% ANI,
51 * around 700m pairs.
52 * @param length
53 * @param dif
54 * @return
55 */
56 public static float prob(long length, float dif) {
57 int idx=quantizeLength(length);
58 float[] array=matrix[idx];
59 int idx2=Math.min(array.length-1, (int)(dif*1024));
60 return array[idx2];
61 }
62
63 //Bins contain everything UP TO that size.
64 //For example, bin 0 is 256, which contains 1-256.
65 //Bin 1 is 256-362, then 363-512, etc.
66 static int quantizeLength(long size) {
67 size=Tools.mid(size, 200, 200000);
68 int idx=(int)Math.ceil(2*Tools.log2(size));
69 return idx-idxOffset;
70 }
71
72 static int dequantizeLength(int idx) {
73 idx+=idxOffset;
74 int size=(int)Math.pow(2, idx/2f);
75 return size;
76 }
77
78 private static final int idxOffset=(int)Math.ceil(2*Tools.log2(200));
79
80 public static float[][] matrix;
81
82 }