| 1 |
package bin; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Random; |
| 5 |
|
| 6 |
import dna.Data; |
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.FileFormat; |
| 9 |
import shared.LineParser1; |
| 10 |
import shared.Shared; |
| 11 |
import shared.Tools; |
| 12 |
import stream.Read; |
| 13 |
import stream.ReadInputStream; |
| 14 |
import structures.FloatList; |
| 15 |
import tracker.EntropyTracker; |
| 16 |
|
| 17 |
public class AdjustEntropy { |
| 18 |
|
| 19 |
public static void main(String[] args) { |
| 20 |
|
| 21 |
int k=Integer.parseInt(args[0]); |
| 22 |
int window=Integer.parseInt(args[1]); |
| 23 |
float step=Float.parseFloat(args[2]); |
| 24 |
int length=(args.length<4 ? 100000 : Integer.parseInt(args[3])); |
| 25 |
int trials=(args.length<5 ? 9 : Integer.parseInt(args[4])); |
| 26 |
assert(step>0 && step<1); |
| 27 |
assert(length>window); |
| 28 |
assert(window>k); |
| 29 |
assert(k>0 && k<=10); |
| 30 |
|
| 31 |
AdjustEntropy ea=new AdjustEntropy(); |
| 32 |
EntropyTracker et=new EntropyTracker(k, window, false); |
| 33 |
FloatList fl=new FloatList(); |
| 34 |
System.out.println("#K\t"+k); |
| 35 |
System.out.println("#Window\t"+window); |
| 36 |
System.out.println("#Length\t"+length); |
| 37 |
System.out.println("#Step\t"+step); |
| 38 |
System.out.println("#Trials\t"+trials); |
| 39 |
System.out.println("#GC\tAvgEnt\tMaxEnt"); |
| 40 |
for(double gc=0; gc<1 || Tools.absdif(gc, 1)<0.5*step; gc+=step) { |
| 41 |
float entropy=ea.randomSequenceEntropy(length, (float)gc, et, trials); |
| 42 |
fl.add(entropy); |
| 43 |
System.out.println(String.format("%.4f\t%.6f\t%.6f", gc, entropy, ea.max)); |
| 44 |
} |
| 45 |
|
| 46 |
String fname=args.length<6 ? null : args[5]; |
| 47 |
entropyArray=fl.toArray(); |
| 48 |
if(fname!=null) { |
| 49 |
processFile(fname, et); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static void processFile(String fname, EntropyTracker et) { |
| 54 |
ArrayList<Read> reads=ReadInputStream.toReads(fname, FileFormat.FASTA, 2000); |
| 55 |
System.out.println("#GC\tgcCompEntropy\tstrandedness"); |
| 56 |
int[] counts=new int[1<<(2*et.k())]; |
| 57 |
for(Read r : reads) { |
| 58 |
float gc=r.gc(); |
| 59 |
float entropy=et.averageEntropy(r.bases, false); |
| 60 |
float comp=compensate(gc, entropy); |
| 61 |
float strandedness=EntropyTracker.strandedness(r.bases, counts, et.k()); |
| 62 |
System.out.println(String.format("%.4f\t%.6f\t%6f", gc, comp, strandedness)); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static float maxEntropy(float gc, float[] array) { |
| 67 |
int steps=array.length-1; |
| 68 |
float stepSize=1f/steps; |
| 69 |
int lowBin=(int)Math.floor(gc*steps); |
| 70 |
int highBin=(int)Math.ceil(gc*steps); |
| 71 |
float lowE=array[lowBin], highE=array[highBin]; |
| 72 |
float lowGC=stepSize*lowBin, highGC=stepSize*highBin; |
| 73 |
float lowFraction=highGC-gc, highFraction=gc-lowGC; |
| 74 |
float max=(lowFraction*lowE+highFraction*highE)*steps; |
| 75 |
|
| 76 |
return max; |
| 77 |
} |
| 78 |
|
| 79 |
public static synchronized void load() { |
| 80 |
load(4, 150); |
| 81 |
} |
| 82 |
|
| 83 |
public static synchronized void load(int k, int window) { |
| 84 |
String fname="?entropy_k"+k+"_w"+window+".tsv"; |
| 85 |
fname=Data.findPath(fname); |
| 86 |
setEntropyFile(fname); |
| 87 |
} |
| 88 |
|
| 89 |
private static synchronized void setEntropyFile(String fname) { |
| 90 |
assert(fname!=null); |
| 91 |
if(fnameLoaded==null || !fnameLoaded.equals(fname)) { |
| 92 |
entropyArray=loadEntropyFile(fname); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private static float[] loadEntropyFile(String fname) { |
| 97 |
ArrayList<byte[]> lines=ByteFile.toLines(fname); |
| 98 |
FloatList floats=new FloatList(lines.size()); |
| 99 |
LineParser1 lp=new LineParser1('\t'); |
| 100 |
for(byte[] line : lines) { |
| 101 |
lp.set(line); |
| 102 |
if(lp.startsWith('#')){ |
| 103 |
if(lp.startsWith("#K\t")) { |
| 104 |
kLoaded=lp.parseInt(1); |
| 105 |
}else if(lp.startsWith("#Window\t")) { |
| 106 |
wLoaded=lp.parseInt(1); |
| 107 |
} |
| 108 |
}else { |
| 109 |
floats.add(lp.parseFloat(1)); |
| 110 |
} |
| 111 |
} |
| 112 |
return floats.toArray(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
static float compensate(float gc, float entropy) { |
| 117 |
float max=maxEntropy(gc, entropyArray); |
| 118 |
return Tools.min(1, 1-(max-entropy)); |
| 119 |
} |
| 120 |
|
| 121 |
float randomSequenceEntropy(int len, float gc, EntropyTracker et, int trials) { |
| 122 |
FloatList fl=new FloatList(trials); |
| 123 |
for(int i=0; i<trials; i++) {fl.add(randomSequenceEntropy(len, gc, et));} |
| 124 |
fl.sort(); |
| 125 |
min=fl.get(0); |
| 126 |
max=fl.lastElement(); |
| 127 |
assert(min<=max) : fl; |
| 128 |
|
| 129 |
double sum=0; |
| 130 |
int count=0; |
| 131 |
int samples=Tools.max((int)Math.sqrt(trials), trials/4); |
| 132 |
for(int i=0; i<samples; i++) { |
| 133 |
sum+=fl.get(fl.size()-i-1); |
| 134 |
count++; |
| 135 |
} |
| 136 |
assert(count==samples); |
| 137 |
mid=(float)(sum/samples); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
assert(min<=max) : fl; |
| 161 |
assert(min<=mid) : fl; |
| 162 |
assert(mid<=max) : fl; |
| 163 |
return mid; |
| 164 |
} |
| 165 |
|
| 166 |
static float randomSequenceEntropy(int len, float gc, EntropyTracker et) { |
| 167 |
byte[] bases=randomSequence(len, gc); |
| 168 |
return et.averageEntropy(bases, false); |
| 169 |
} |
| 170 |
|
| 171 |
static byte[] randomSequence(int len, float gc) { |
| 172 |
byte[] bases=new byte[len]; |
| 173 |
Random randy=Shared.threadLocalRandom(); |
| 174 |
byte[] atcg={'A','T','C','G'}; |
| 175 |
for(int i=0; i<len; i++) { |
| 176 |
int high=(randy.nextFloat()>=gc ? 0 : 2); |
| 177 |
int low=(randy.nextInt()&1); |
| 178 |
byte b=atcg[high+low]; |
| 179 |
bases[i]=b; |
| 180 |
} |
| 181 |
return bases; |
| 182 |
} |
| 183 |
|
| 184 |
float min, mid, max; |
| 185 |
private static String fnameLoaded=null; |
| 186 |
public static int kLoaded=0; |
| 187 |
public static int wLoaded=0; |
| 188 |
private static float[] entropyArray=null; |
| 189 |
|
| 190 |
} |