| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import shared.Tools; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public final class Info { |
| 13 |
|
| 14 |
public static void main(String[] args){ |
| 15 |
|
| 16 |
if(args.length>0){ |
| 17 |
if(args.length==2 && Tools.isDigit(args[1].charAt(0))){ |
| 18 |
byte[] s=args[0].getBytes(); |
| 19 |
int b=Integer.parseInt(args[1]); |
| 20 |
int len=prefixForInfoBits(s, b); |
| 21 |
if(len<0){ |
| 22 |
System.out.println("Input string only contains "+Tools.format("%.2f",infoInBitsDouble(s, 0, s.length))+" bits."); |
| 23 |
}else{ |
| 24 |
System.out.println("Prefix needed for "+b+" bits is length "+len+": "+args[0].substring(0, len)); |
| 25 |
|
| 26 |
} |
| 27 |
}else{ |
| 28 |
for(String s : args){ |
| 29 |
printInfo(s); |
| 30 |
System.out.println(); |
| 31 |
} |
| 32 |
} |
| 33 |
System.exit(0); |
| 34 |
} |
| 35 |
|
| 36 |
System.out.println(); |
| 37 |
printInfo(""); |
| 38 |
System.out.println(); |
| 39 |
printInfo("A"); |
| 40 |
System.out.println(); |
| 41 |
printInfo("AG"); |
| 42 |
System.out.println(); |
| 43 |
printInfo("AGT"); |
| 44 |
System.out.println(); |
| 45 |
printInfo("AANAA"); |
| 46 |
System.out.println(); |
| 47 |
printInfo("GGGGGGGCGGG"); |
| 48 |
System.out.println(); |
| 49 |
printInfo("CGGGGGGGGGG"); |
| 50 |
System.out.println(); |
| 51 |
printInfo("AGTCAGTCCTAGNGTACGT"); |
| 52 |
System.out.println(); |
| 53 |
printInfo("AGTCAGTCAGTCAGTC"); |
| 54 |
System.out.println(); |
| 55 |
printInfo("GCGCGCGCGCGCGCGC"); |
| 56 |
System.out.println(); |
| 57 |
|
| 58 |
String[] s=new String[] {"A", "G", "C", "T", ""}; |
| 59 |
for(int i=0; i<40; i++){ |
| 60 |
System.out.println(); |
| 61 |
s[4]=s[4]+s[i%4]; |
| 62 |
printInfo(s[4]); |
| 63 |
} |
| 64 |
|
| 65 |
System.out.println("PrefixForBits for AAAATATATGAAATGCATGCAATATGTTATGAAA"); |
| 66 |
for(int i=0; i<60; i+=2){ |
| 67 |
System.out.println(i+"\t"+prefixForInfoBits("AAAATATATGAAATGCATGCAATATGTTATGAAA".getBytes(), i)); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
System.out.println("PrefixForBits for GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC"); |
| 72 |
for(int i=0; i<60; i+=2){ |
| 73 |
System.out.println(i+"\t"+prefixForInfoBits("GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC".getBytes(), i)); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
System.out.println("PrefixForBits for ACGTACGTACGTACGTACGTACGTACGTACGTAC"); |
| 78 |
for(int i=0; i<63; i+=2){ |
| 79 |
System.out.println(i+"\t"+prefixForInfoBits("ACGTACGTACGTACGTACGTACGTACGTACGTAC".getBytes(), i)); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public static void printInfo(String s){ |
| 84 |
long r=info(s); |
| 85 |
double bits=Math.log(r)/Math.log(2); |
| 86 |
System.out.println(s+"\nlen="+s.length()+" \tinfo = "+Tools.format("%.2f", bits)+" bits. \t("+r+")"); |
| 87 |
} |
| 88 |
|
| 89 |
public static long info(String s){ |
| 90 |
return info(s.getBytes(), 0, s.length()); |
| 91 |
} |
| 92 |
|
| 93 |
public static int infoInBits(final byte[] array, final int from, final int len){return 63-Long.numberOfLeadingZeros(info(array, from, len));} |
| 94 |
public static double infoInBitsDouble(final byte[] array, final int from, final int len){return Math.log(info(array, from, len))*invlog2;} |
| 95 |
public static long info(final byte[] array){return info(array, 0, array.length);} |
| 96 |
public static long info(final byte[] array, final int from, final int len){ |
| 97 |
short[] counts=new short[4]; |
| 98 |
long r=1; |
| 99 |
int used=0; |
| 100 |
for(int i=from, lim=min(from+len, array.length); i<lim; i++){ |
| 101 |
|
| 102 |
byte num=baseToNumber[array[i]]; |
| 103 |
|
| 104 |
if(num>=0){ |
| 105 |
counts[num]++; |
| 106 |
used++; |
| 107 |
|
| 108 |
if(used>32 && used>MAX/r){ |
| 109 |
|
| 110 |
return MAX; |
| 111 |
} |
| 112 |
r=r*used; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
r=r/counts[num]; |
| 122 |
} |
| 123 |
} |
| 124 |
return r; |
| 125 |
} |
| 126 |
|
| 127 |
public static int prefixForInfoBits(final byte[] array, final int bits){assert(bits>=0 && bits<63);return prefixForInfo(array, 1L<<bits, 0);} |
| 128 |
public static int prefixForInfoBits(final byte[] array, final int bits, final int from){assert(bits>=0 && bits<63);return prefixForInfo(array, 1L<<bits, from);} |
| 129 |
public static int prefixForInfo(final byte[] array, final long info){return prefixForInfo(array, info, 0);} |
| 130 |
|
| 131 |
public static int prefixForInfo(final byte[] array, final long info, final int from){ |
| 132 |
assert(info>=0); |
| 133 |
short[] counts=new short[4]; |
| 134 |
long r=1; |
| 135 |
int used=0; |
| 136 |
int i=from; |
| 137 |
for(; i<array.length && r<info; i++){ |
| 138 |
|
| 139 |
byte num=baseToNumber[array[i]]; |
| 140 |
|
| 141 |
if(num>=0){ |
| 142 |
counts[num]++; |
| 143 |
used++; |
| 144 |
|
| 145 |
if(used>32 && used>MAX/r){ |
| 146 |
|
| 147 |
return i; |
| 148 |
} |
| 149 |
r=r*used; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
r=r/counts[num]; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
} |
| 168 |
} |
| 169 |
return r<info ? -1 : i; |
| 170 |
} |
| 171 |
|
| 172 |
private static final byte[] numberToBase={ |
| 173 |
'A','C','G','T','N' |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
public static final byte[] baseToNumber=new byte[128]; |
| 178 |
|
| 179 |
static{ |
| 180 |
Arrays.fill(baseToNumber, (byte)-1); |
| 181 |
for(int i=0; i<numberToBase.length; i++){ |
| 182 |
char x=(char)numberToBase[i]; |
| 183 |
if(x=='A' || x=='C' || x=='G' || x=='T'){ |
| 184 |
baseToNumber[x]=(byte)i; |
| 185 |
baseToNumber[Tools.toLowerCase(x)]=(byte)i; |
| 186 |
} |
| 187 |
} |
| 188 |
baseToNumber['U']=3; |
| 189 |
baseToNumber['u']=3; |
| 190 |
} |
| 191 |
|
| 192 |
private static final int min(int x, int y){return x<y ? x : y;} |
| 193 |
private static final int max(int x, int y){return x>y ? x : y;} |
| 194 |
|
| 195 |
private static final long MAX=Long.MAX_VALUE; |
| 196 |
private static final double invlog2=1.0/Math.log(2); |
| 197 |
} |