| 1 |
package structures; |
| 2 |
import java.io.Serializable; |
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.HashMap; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.FileFormat; |
| 9 |
import fileIO.ReadWrite; |
| 10 |
import shared.LineParser; |
| 11 |
import shared.LineParser1; |
| 12 |
import shared.Shared; |
| 13 |
import shared.Tools; |
| 14 |
|
| 15 |
|
| 16 |
public abstract class CoverageArray implements Serializable { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private static final long serialVersionUID = -7175422489330746676L; |
| 22 |
|
| 23 |
|
| 24 |
public static final CoverageArray read(String fname){ |
| 25 |
|
| 26 |
if(!fname.contains(".ca")){ |
| 27 |
throw new RuntimeException(); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
fname=ReadWrite.findFileExtension(fname); |
| 34 |
|
| 35 |
|
| 36 |
return ReadWrite.read(CoverageArray.class, fname, true); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
public static final Class<? extends CoverageArray> getType(boolean atomic, boolean bits32){ |
| 44 |
if(atomic && bits32) {return CoverageArray3A.class;} |
| 45 |
else if(!atomic && bits32) {return CoverageArray3.class;} |
| 46 |
else if(atomic && !bits32) {return CoverageArray2A.class;} |
| 47 |
else if(!atomic && !bits32) {return CoverageArray2.class;} |
| 48 |
throw new RuntimeException("No type."); |
| 49 |
} |
| 50 |
|
| 51 |
public CoverageArray(int chrom, int len){ |
| 52 |
chromosome=chrom; |
| 53 |
length=len; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public abstract void increment(int loc, int amt); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public abstract void increment(int loc); |
| 66 |
|
| 67 |
public final void incrementRange(int min, int max){incrementRange(min, max, 1);} |
| 68 |
public abstract void incrementRange(int min, int max, int amt); |
| 69 |
public abstract void incrementRangeSynchronized(int min, int max, int amt); |
| 70 |
|
| 71 |
public void incrementRanges(IntList ranges, int amt){ |
| 72 |
for(int i=0; i<ranges.size; i+=2){ |
| 73 |
int a=ranges.get(i), b=ranges.get(i+1); |
| 74 |
incrementRange(a, b-1, 1); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public abstract void set(int loc, int val); |
| 79 |
|
| 80 |
public abstract int get(int loc); |
| 81 |
|
| 82 |
public abstract void resize(int newlen); |
| 83 |
|
| 84 |
|
| 85 |
public final double[][] toGraph(int blocksize, int min, int max){ |
| 86 |
|
| 87 |
min=max(min, minIndex); |
| 88 |
max=min(max, maxIndex); |
| 89 |
int length=max-min; |
| 90 |
|
| 91 |
ArrayList<double[]> list=new ArrayList<double[]>(); |
| 92 |
|
| 93 |
int block; |
| 94 |
|
| 95 |
if(blocksize<=0){ |
| 96 |
|
| 97 |
|
| 98 |
block=((length+31499)/31500); |
| 99 |
}else{ |
| 100 |
block=blocksize; |
| 101 |
} |
| 102 |
block=max(block, 1); |
| 103 |
|
| 104 |
int current=0; |
| 105 |
double[] sum=new double[2]; |
| 106 |
for(int loc=min; loc<=max; loc++){ |
| 107 |
if(current==block){ |
| 108 |
for(int i=0; i<sum.length; i++){ |
| 109 |
sum[i]=sum[i]/current; |
| 110 |
} |
| 111 |
sum[0]=Math.round(sum[0]); |
| 112 |
list.add(sum); |
| 113 |
sum=new double[2]; |
| 114 |
current=0; |
| 115 |
} |
| 116 |
|
| 117 |
sum[0]+=loc; |
| 118 |
sum[1]+=get(loc); |
| 119 |
|
| 120 |
current++; |
| 121 |
} |
| 122 |
|
| 123 |
return list.toArray(new double[0][]); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
public static final void print(double[][] data){ |
| 129 |
|
| 130 |
|
| 131 |
assert(false) : "Smoother disabled in this code purely to reduce dependancies."; |
| 132 |
StringBuilder sb=new StringBuilder(data.length*20); |
| 133 |
for(double[] d : data){ |
| 134 |
sb.append(Tools.format("%d\t%.2f\n",(int)d[0],d[1])); |
| 135 |
} |
| 136 |
System.out.print(sb); |
| 137 |
} |
| 138 |
|
| 139 |
public static CoverageArray makeArray(int num, int size, Class<? extends CoverageArray> c){ |
| 140 |
if(c==CoverageArray2.class){ |
| 141 |
return new CoverageArray2(num, size); |
| 142 |
}else if(c==CoverageArray3.class){ |
| 143 |
return new CoverageArray3(num, size); |
| 144 |
}else if(c==CoverageArray2A.class){ |
| 145 |
return new CoverageArray2A(num, size); |
| 146 |
}else if(c==CoverageArray3A.class){ |
| 147 |
return new CoverageArray3A(num, size); |
| 148 |
} |
| 149 |
throw new RuntimeException("Unhandled class: "+c); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public static HashMap<String, CoverageArray> loadDepth(FileFormat ffdepth, Class<? extends CoverageArray> c) { |
| 155 |
ByteFile bf=ByteFile.makeByteFile(ffdepth); |
| 156 |
HashMap<String, CoverageArray> map=new HashMap<String, CoverageArray>(); |
| 157 |
|
| 158 |
String prevName=null; |
| 159 |
int prevPos=-1; |
| 160 |
int prevDepth=0; |
| 161 |
|
| 162 |
CoverageArray prevArray=null; |
| 163 |
LineParser lp=new LineParser1('\t'); |
| 164 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ |
| 165 |
if(line[0]!='#'){ |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
lp.set(line); |
| 171 |
final String name=(Tools.startsWith(line, prevName) || lp.length(0)<1 ? prevName : lp.parseString(0)); |
| 172 |
final int pos=(lp.length(1)<1 ? prevPos+1 : lp.parseInt(1)); |
| 173 |
final int depth=(lp.length(2)<1 ? prevDepth : lp.parseInt(2)); |
| 174 |
CoverageArray current; |
| 175 |
if(name.equals(prevName)){ |
| 176 |
current=prevArray; |
| 177 |
}else{ |
| 178 |
assert(!map.containsKey(name)) : name; |
| 179 |
current=makeArray(map.size()+1, 64, c); |
| 180 |
map.put(name, current); |
| 181 |
prevName=name; |
| 182 |
prevArray=current; |
| 183 |
} |
| 184 |
if(depth>0){current.set(pos, depth);} |
| 185 |
prevPos=pos; |
| 186 |
prevDepth=depth; |
| 187 |
} |
| 188 |
} |
| 189 |
return map; |
| 190 |
} |
| 191 |
|
| 192 |
public final int basesUnderAverageCoverage(final double avg, final int window){ |
| 193 |
if(underWindowAverage>-1) {return underWindowAverage;} |
| 194 |
|
| 195 |
|
| 196 |
if(length<window){return underWindowAverage=length;} |
| 197 |
final int usedDif=maxIndex-minIndex; |
| 198 |
if(usedDif<0) {return underWindowAverage=length;} |
| 199 |
|
| 200 |
final long limit=(long)Math.ceil(window*avg); |
| 201 |
long covSum=0; |
| 202 |
int baseCount=0; |
| 203 |
for(int i=0; i<window; i++){ |
| 204 |
covSum+=get(i); |
| 205 |
} |
| 206 |
|
| 207 |
boolean below=false; |
| 208 |
int lastStop=-1, lastStart=0; |
| 209 |
for(int a=0, b=window; b<length; a++, b++){ |
| 210 |
if(covSum>=limit){ |
| 211 |
if(below){ |
| 212 |
baseCount=b-Tools.max(lastStop+1, lastStart); |
| 213 |
lastStop=b-1; |
| 214 |
below=false; |
| 215 |
} |
| 216 |
}else{ |
| 217 |
if(!below){ |
| 218 |
lastStart=a; |
| 219 |
below=true; |
| 220 |
} |
| 221 |
} |
| 222 |
covSum-=get(a); |
| 223 |
assert(covSum>=0); |
| 224 |
covSum+=get(b); |
| 225 |
} |
| 226 |
|
| 227 |
if(below){ |
| 228 |
baseCount=length()-Tools.max(lastStop, lastStart); |
| 229 |
} |
| 230 |
|
| 231 |
assert(baseCount>=0) : baseCount+", "+avg+", "+window; |
| 232 |
return underWindowAverage=baseCount; |
| 233 |
} |
| 234 |
|
| 235 |
public final long sum() { |
| 236 |
if(sum>=0) {return sum;} |
| 237 |
sum=0; |
| 238 |
final int usedDif=maxIndex-minIndex; |
| 239 |
if(usedDif<0) {return sum;} |
| 240 |
for(int i=minIndex; i<=maxIndex; i++) {sum+=get(i);} |
| 241 |
return sum; |
| 242 |
} |
| 243 |
|
| 244 |
public final int covered(int minDepth) { |
| 245 |
if(covered>=0) {return covered;} |
| 246 |
calcSumAndCovered(minDepth); |
| 247 |
return covered; |
| 248 |
} |
| 249 |
|
| 250 |
public final long calcSumAndCovered(int minDepth) { |
| 251 |
if(sum>=0 && covered>=0) {return sum;} |
| 252 |
sum=0; |
| 253 |
covered=0; |
| 254 |
final int usedDif=maxIndex-minIndex; |
| 255 |
if(usedDif<0) {return sum;} |
| 256 |
for(int i=minIndex; i<=maxIndex; i++) { |
| 257 |
int d=get(i); |
| 258 |
sum+=d; |
| 259 |
covered+=(d>=minDepth ? 1 : 0); |
| 260 |
} |
| 261 |
return sum; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
public final double standardDeviation(){ |
| 266 |
if(stdev>=0) {return stdev;} |
| 267 |
int length=length(); |
| 268 |
if(length<2){return 0;} |
| 269 |
final int usedDif=maxIndex-minIndex; |
| 270 |
if(usedDif<0) {return stdev=0;} |
| 271 |
long sum=sum(); |
| 272 |
double avg=sum/(double)length; |
| 273 |
double sumdev2=0; |
| 274 |
for(int i=minIndex; i<length; i++){ |
| 275 |
int x=get(i); |
| 276 |
double dev=avg-x; |
| 277 |
sumdev2+=(dev*dev); |
| 278 |
} |
| 279 |
return stdev=Math.sqrt(sumdev2/length); |
| 280 |
} |
| 281 |
|
| 282 |
public final double devSum(double globalMean){ |
| 283 |
if(devSum>=0) {return devSum;} |
| 284 |
final int length=length(); |
| 285 |
int usedDif=maxIndex-minIndex; |
| 286 |
if(usedDif<0) {return devSum=globalMean*globalMean*length;} |
| 287 |
devSum=0; |
| 288 |
if(length<1){return 0;} |
| 289 |
for(int i=minIndex; i<length; i++){ |
| 290 |
int x=get(i); |
| 291 |
double dev=globalMean-x; |
| 292 |
devSum+=(dev*dev); |
| 293 |
} |
| 294 |
return devSum; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public final int median(){ |
| 300 |
if(median>=0) {return median;} |
| 301 |
final int usedDif=maxIndex-minIndex; |
| 302 |
if(usedDif<0) {return 0;} |
| 303 |
Object o=toArray(); |
| 304 |
if(o.getClass()==int[].class) { |
| 305 |
int[] array=(int[])o; |
| 306 |
Shared.sort(array); |
| 307 |
Tools.reverseInPlace(array); |
| 308 |
median=array[array.length/2]; |
| 309 |
}else { |
| 310 |
char[] array=(char[])o; |
| 311 |
Arrays.sort(array); |
| 312 |
Tools.reverseInPlace(array); |
| 313 |
median=array[array.length/2]; |
| 314 |
} |
| 315 |
|
| 316 |
minIndex=0; |
| 317 |
maxIndex=minIndex+usedDif; |
| 318 |
return median; |
| 319 |
} |
| 320 |
|
| 321 |
public abstract Object toArray(); |
| 322 |
|
| 323 |
@Override |
| 324 |
public abstract String toString(); |
| 325 |
|
| 326 |
static final long min(long x, long y){return x<y ? x : y;} |
| 327 |
static final long max(long x, long y){return x>y ? x : y;} |
| 328 |
static final int min(int x, int y){return x<y ? x : y;} |
| 329 |
static final int max(int x, int y){return x>y ? x : y;} |
| 330 |
|
| 331 |
public int maxIndex=-1; |
| 332 |
public int minIndex=Integer.MAX_VALUE; |
| 333 |
private final int length; |
| 334 |
public int length(){return length;} |
| 335 |
public abstract int arrayLength(); |
| 336 |
|
| 337 |
public void setUnderWindowAverage(int x) {underWindowAverage=x;} |
| 338 |
public void setMedian(int x) {median=x;} |
| 339 |
public void setCovered(int x) {covered=x;} |
| 340 |
public void setSum(long x) {sum=x;} |
| 341 |
public void setStdev(double x) {stdev=x;} |
| 342 |
public void setDevSum(double x) {devSum=x;} |
| 343 |
|
| 344 |
private int underWindowAverage=-1; |
| 345 |
private int median=-1; |
| 346 |
private int covered=-1; |
| 347 |
private long sum=-1; |
| 348 |
private double stdev=-1; |
| 349 |
private double devSum=-1; |
| 350 |
|
| 351 |
|
| 352 |
public int chromosome; |
| 353 |
|
| 354 |
private static boolean OVERFLOWED=false; |
| 355 |
|
| 356 |
} |