| 1 |
package shared; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.io.PrintWriter; |
| 6 |
import java.io.StringWriter; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.BitSet; |
| 10 |
import java.util.Collection; |
| 11 |
import java.util.HashSet; |
| 12 |
import java.util.LinkedHashSet; |
| 13 |
import java.util.List; |
| 14 |
import java.util.Locale; |
| 15 |
import java.util.Random; |
| 16 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 17 |
import java.util.concurrent.atomic.AtomicLongArray; |
| 18 |
import java.util.regex.Pattern; |
| 19 |
|
| 20 |
import dna.AminoAcid; |
| 21 |
import dna.Data; |
| 22 |
import fileIO.ByteFile; |
| 23 |
import fileIO.FileFormat; |
| 24 |
import fileIO.ReadWrite; |
| 25 |
import stream.Read; |
| 26 |
import stream.ReadInputStream; |
| 27 |
import stream.SamLine; |
| 28 |
import stream.SiteScore; |
| 29 |
import structures.CoverageArray; |
| 30 |
import structures.DoubleList; |
| 31 |
import structures.IntHashSet; |
| 32 |
import structures.IntList; |
| 33 |
import structures.Point; |
| 34 |
|
| 35 |
public final class Tools { |
| 36 |
|
| 37 |
public static void main(String[] args){ |
| 38 |
|
| 39 |
long[] array=new long[1000]; |
| 40 |
for(int i=0; i<10; i++){ |
| 41 |
for(int j=0; j<array.length; j++){ |
| 42 |
array[j]=(int)Math.round((Math.random()*100)); |
| 43 |
} |
| 44 |
System.err.println(Tools.format("%.2f",weightedAverage(array))); |
| 45 |
System.err.println(Tools.format("%.2f",mean(array))); |
| 46 |
|
| 47 |
Arrays.sort(array); |
| 48 |
System.err.println(Tools.format("%.2f",weightedAverage(array))); |
| 49 |
System.err.println(Tools.format("%.2f",mean(array))); |
| 50 |
|
| 51 |
System.err.println("\n"); |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
public static final String format(String s, Object...args) { |
| 57 |
return String.format(Locale.ROOT, s, args); |
| 58 |
} |
| 59 |
|
| 60 |
public static String plural(String s, int count){ |
| 61 |
return count+" "+(count==1 ? s : s+"s"); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public static double[] linearRegression(DoubleList xcoords, DoubleList ycoords) { |
| 66 |
int n=xcoords.size; |
| 67 |
double sumX=0; |
| 68 |
double sumY=0; |
| 69 |
double sumXY=0; |
| 70 |
double sumXX=0; |
| 71 |
|
| 72 |
for(int i=0; i<n; i++) { |
| 73 |
final double x=xcoords.get(i), y=ycoords.get(i); |
| 74 |
sumX+=x; |
| 75 |
sumY+=y; |
| 76 |
sumXY+=x*y; |
| 77 |
sumXX+=x*x; |
| 78 |
} |
| 79 |
|
| 80 |
double b=(n*sumXY-sumX*sumY)/(n*sumXX-sumX*sumX); |
| 81 |
double a=(sumY-b*sumX)/n; |
| 82 |
|
| 83 |
return new double[] {a, b}; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public static double[] linearRegression(List<Point> list, double lowFraction, double highFraction) { |
| 89 |
int n0=list.size(); |
| 90 |
double sumX=0; |
| 91 |
double sumY=0; |
| 92 |
double sumXY=0; |
| 93 |
double sumXX=0; |
| 94 |
|
| 95 |
final int minIndex, maxIndex; |
| 96 |
minIndex=(int)Math.round(lowFraction*n0); |
| 97 |
maxIndex=(int)Math.round(highFraction*n0); |
| 98 |
int n=maxIndex-minIndex+1; |
| 99 |
|
| 100 |
for(int i=minIndex; i<maxIndex; i++) { |
| 101 |
final Point p=list.get(i); |
| 102 |
final double x=p.x, y=p.y; |
| 103 |
sumX+=x; |
| 104 |
sumY+=y; |
| 105 |
sumXY+=x*y; |
| 106 |
sumXX+=x*x; |
| 107 |
} |
| 108 |
|
| 109 |
final double b=(n*sumXY-sumX*sumY)/(n*sumXX-sumX*sumX); |
| 110 |
final double a=(sumY-b*sumX)/n; |
| 111 |
|
| 112 |
return new double[] {a, b}; |
| 113 |
} |
| 114 |
|
| 115 |
public static IntHashSet loadIntSet(String numbers){ |
| 116 |
IntList list=loadIntegers(numbers); |
| 117 |
if(list==null || list.isEmpty()){return null;} |
| 118 |
IntHashSet set=new IntHashSet((int)((list.size*5L)/4)); |
| 119 |
set.addAll(list); |
| 120 |
return set; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static IntList loadIntegers(String numbers){ |
| 131 |
return loadIntegers(numbers, null); |
| 132 |
} |
| 133 |
|
| 134 |
private static IntList loadIntegers(String numbers, IntList list){ |
| 135 |
if(numbers==null){return list;} |
| 136 |
if(list==null){list=new IntList();} |
| 137 |
String[] array=commaPattern.split(numbers); |
| 138 |
for(String s : array){ |
| 139 |
if(Tools.isDigit(s.charAt(0)) && Tools.isDigit(s.charAt(s.length()-1))){ |
| 140 |
final int x=Integer.parseInt(s); |
| 141 |
list.add(x); |
| 142 |
}else if(new File(s).exists()){ |
| 143 |
ByteFile tf=ByteFile.makeByteFile(s, false); |
| 144 |
for(byte[] line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 145 |
try { |
| 146 |
list.add(Parse.parseInt(line, 0)); |
| 147 |
} catch (Throwable e) { |
| 148 |
loadIntegers(new String(line), list); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
}else{ |
| 153 |
assert(false) : "Invalid number or file "+s; |
| 154 |
} |
| 155 |
} |
| 156 |
return list; |
| 157 |
} |
| 158 |
|
| 159 |
public static boolean existsInput(String fname) { |
| 160 |
if(fname==null){return false;} |
| 161 |
if(fname.equalsIgnoreCase("stdin") || fname.startsWith("stdin.")){return true;} |
| 162 |
File f=new File(fname); |
| 163 |
return f.exists(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public static boolean testOutputFiles(boolean overwrite, boolean append, boolean allowDuplicates, ArrayList<String>...args){ |
| 168 |
if(args==null || args.length==0){return true;} |
| 169 |
ArrayList<String> list=new ArrayList<String>(); |
| 170 |
for(ArrayList<String> als : args){ |
| 171 |
if(als!=null){ |
| 172 |
list.addAll(als); |
| 173 |
} |
| 174 |
} |
| 175 |
return testOutputFiles(overwrite, append, allowDuplicates, list.toArray(new String[list.size()])); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
public static boolean testOutputFiles(boolean overwrite, boolean append, boolean allowDuplicates, String...args){ |
| 180 |
if(args==null || args.length==0){return true;} |
| 181 |
HashSet<String> set=new HashSet<String>(args.length*2); |
| 182 |
int terms=0; |
| 183 |
for(String s : args){ |
| 184 |
if(s!=null){ |
| 185 |
if(isOutputFileName(s)){ |
| 186 |
terms++; |
| 187 |
|
| 188 |
if(!overwrite && !append && new File(s).exists()){ |
| 189 |
assert(overwrite) : "File "+s+" exists and overwrite=false"; |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
if(!allowDuplicates && set.contains(s)){ |
| 194 |
assert(false) : "Duplicate file "+s+" was specified for multiple output streams."; |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
set.add(s); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
@SafeVarargs |
| 207 |
public static boolean testInputFiles(boolean allowDuplicates, boolean throwException, ArrayList<String>...args){ |
| 208 |
if(args==null || args.length==0){return true;} |
| 209 |
ArrayList<String> list=new ArrayList<String>(); |
| 210 |
for(ArrayList<String> als : args){ |
| 211 |
if(als!=null){ |
| 212 |
list.addAll(als); |
| 213 |
} |
| 214 |
} |
| 215 |
return testInputFiles(allowDuplicates, throwException, list.toArray(new String[list.size()])); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
public static boolean testInputFiles(boolean allowDuplicates, boolean throwException, String[]...args){ |
| 220 |
if(args==null || args.length==0){return true;} |
| 221 |
for(String[] s : args){ |
| 222 |
if(!testInputFiles(allowDuplicates, throwException, s)){return false;} |
| 223 |
} |
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
public static boolean testInputFilesALA(boolean allowDuplicates, boolean throwException, ArrayList<String> list1, ArrayList<String> list2, String...args){ |
| 229 |
ArrayList<String> list3=new ArrayList<String>(); |
| 230 |
if(list1!=null){list3.addAll(list1);} |
| 231 |
if(list2!=null){list3.addAll(list2);} |
| 232 |
if(args!=null){for(String s : args){list3.add(s);}} |
| 233 |
return testInputFiles(allowDuplicates, throwException, list3.toArray(new String[0])); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
public static boolean testInputFiles(boolean allowDuplicates, boolean throwException, String...args){ |
| 238 |
if(args==null || args.length==0){return true;} |
| 239 |
HashSet<String> set=new HashSet<String>(args.length*2); |
| 240 |
int terms=0; |
| 241 |
for(String s : args){ |
| 242 |
if(s!=null){ |
| 243 |
String s2=s.toLowerCase(); |
| 244 |
if(canRead(s)){ |
| 245 |
terms++; |
| 246 |
}else{ |
| 247 |
if(throwException){throw new RuntimeException("Can't read file '"+s+"'");} |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
if(!allowDuplicates && set.contains(s2)){ |
| 252 |
if(throwException){throw new RuntimeException("Duplicate file "+s+" was specified for multiple input streams.");} |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
set.add(s2); |
| 257 |
} |
| 258 |
} |
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
public static boolean testForDuplicateFilesALA(boolean throwException, ArrayList<String> list1, ArrayList<String> list2, String...args){ |
| 264 |
ArrayList<String> list3=new ArrayList<String>(); |
| 265 |
if(list1!=null){list3.addAll(list1);} |
| 266 |
if(list2!=null){list3.addAll(list2);} |
| 267 |
if(args!=null){for(String s : args){list3.add(s);}} |
| 268 |
return testForDuplicateFiles(throwException, list3.toArray(new String[0])); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public static boolean testForDuplicateFiles(boolean throwException, String...args){ |
| 274 |
if(args==null || args.length==0){return true;} |
| 275 |
HashSet<String> set=new HashSet<String>(args.length*2); |
| 276 |
int terms=0; |
| 277 |
for(String s0 : args){ |
| 278 |
if(s0!=null){ |
| 279 |
String s=s0.toLowerCase(); |
| 280 |
terms++; |
| 281 |
if(set.contains(s) && !s.equals("stdout") && !s.startsWith("stdout.") && !s.equals("stderr") && !s.startsWith("stderr.")){ |
| 282 |
if(throwException){throw new RuntimeException("File '"+s0+"' was specified multiple times.");} |
| 283 |
return false; |
| 284 |
} |
| 285 |
set.add(s); |
| 286 |
} |
| 287 |
} |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
public static final boolean canWrite(String s, boolean overwrite){ |
| 292 |
if(isNullFileName(s) || isSpecialOutputName(s)){return true;} |
| 293 |
File f=new File(s); |
| 294 |
if(f.exists()){return overwrite && f.canWrite();} |
| 295 |
return true; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public static final boolean isOutputFileName(String s){ |
| 306 |
return !(isNullFileName(s) || isSpecialOutputName(s)); |
| 307 |
} |
| 308 |
|
| 309 |
public static final boolean isNullFileName(String s){ |
| 310 |
if(s==null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("none")){return true;} |
| 311 |
for(int i=0; i<s.length(); i++){ |
| 312 |
if(!Character.isWhitespace(s.charAt(i))){return false;} |
| 313 |
} |
| 314 |
return true; |
| 315 |
} |
| 316 |
|
| 317 |
public static final boolean isSpecialOutputName(String s){ |
| 318 |
if(s==null){return false;} |
| 319 |
s=s.toLowerCase(); |
| 320 |
return s.equals("stdout") || s.equals("stderr") || s.equals("standardout") || s.equals("standarderr") |
| 321 |
|| s.equals("/dev/null") || s.startsWith("stdout.") || s.startsWith("stderr."); |
| 322 |
} |
| 323 |
|
| 324 |
public static final boolean isSpecialInputName(String s){ |
| 325 |
if(s==null){return false;} |
| 326 |
s=s.toLowerCase(); |
| 327 |
return s.equals("stdin") || s.equals("standardin") || s.startsWith("stdin.") || s.startsWith("jar:"); |
| 328 |
} |
| 329 |
|
| 330 |
public static final boolean canRead(String s){ |
| 331 |
if(s==null){return false;} |
| 332 |
if(isSpecialInputName(s)){return true;} |
| 333 |
File f=new File(s); |
| 334 |
return f.canRead(); |
| 335 |
} |
| 336 |
|
| 337 |
public static boolean addFiles(String b, ArrayList<String> list){ |
| 338 |
if(b==null){ |
| 339 |
list.clear(); |
| 340 |
return true; |
| 341 |
} |
| 342 |
|
| 343 |
boolean added=false; |
| 344 |
boolean failed=false; |
| 345 |
if(b.indexOf(',')>0){ |
| 346 |
for(String s : b.split(",")){ |
| 347 |
boolean x=addFiles(s, list); |
| 348 |
added|=x; |
| 349 |
failed|=!x; |
| 350 |
} |
| 351 |
}else{ |
| 352 |
String s=b; |
| 353 |
if(b.indexOf('@')>0 && !new File(b).exists()){s=b.split("@")[0];} |
| 354 |
if(new File(s).exists() || s.equalsIgnoreCase("stdin") || s.toLowerCase().startsWith("stdin.")){ |
| 355 |
list.add(b); |
| 356 |
return true; |
| 357 |
}else if(s.lastIndexOf('#')>0){ |
| 358 |
list.add(b); |
| 359 |
return true; |
| 360 |
}else{ |
| 361 |
return false; |
| 362 |
} |
| 363 |
} |
| 364 |
return added&!failed; |
| 365 |
} |
| 366 |
|
| 367 |
public static void fill(int[] target, int[] source) { |
| 368 |
for(int i=0; i<target.length; i++){ |
| 369 |
target[i]=source[i]; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
public static boolean isSorted(final int[] array) { |
| 374 |
if(array==null || array.length<2){return true;} |
| 375 |
for(int i=1; i<array.length; i++){ |
| 376 |
if(array[i]<array[i-1]){return false;} |
| 377 |
} |
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
public static String[] fixExtension(String[] fnames){ |
| 382 |
if(!Shared.FIX_EXTENSIONS){return fnames;} |
| 383 |
if(fnames==null){return fnames;} |
| 384 |
for(int i=0; i<fnames.length; i++){ |
| 385 |
fnames[i]=fixExtension(fnames[i]); |
| 386 |
} |
| 387 |
return fnames; |
| 388 |
} |
| 389 |
|
| 390 |
public static ArrayList<String> fixExtension(ArrayList<String> fnames){ |
| 391 |
if(!Shared.FIX_EXTENSIONS){return fnames;} |
| 392 |
if(fnames==null){return fnames;} |
| 393 |
for(int i=0; i<fnames.size(); i++){ |
| 394 |
fnames.set(i,fixExtension(fnames.get(i))); |
| 395 |
} |
| 396 |
return fnames; |
| 397 |
} |
| 398 |
|
| 399 |
public static String fixExtension(String fname){ |
| 400 |
if(!Shared.FIX_EXTENSIONS){return fname;} |
| 401 |
if(fname==null || fname.startsWith("stdin") || new File(fname).exists()){return fname;} |
| 402 |
final String[] suffixes=new String[] {".gz", ".bz2"}; |
| 403 |
for(String suffix : suffixes){ |
| 404 |
if(fname.endsWith(suffix)){ |
| 405 |
String sub=fname.substring(0, fname.length()-suffix.length()); |
| 406 |
if(new File(sub).exists()){return sub;} |
| 407 |
else{return fname;} |
| 408 |
} |
| 409 |
} |
| 410 |
for(String suffix : suffixes){ |
| 411 |
String s=fname+suffix; |
| 412 |
if(new File(s).exists()){return s;} |
| 413 |
} |
| 414 |
return fname; |
| 415 |
} |
| 416 |
|
| 417 |
public static String padRight(long number, int pad) { |
| 418 |
String s=number+""; |
| 419 |
while(s.length()<pad){s=s+" ";} |
| 420 |
return s; |
| 421 |
} |
| 422 |
|
| 423 |
public static String padRight(String s, int pad) { |
| 424 |
while(s.length()<pad){s=s+" ";} |
| 425 |
return s; |
| 426 |
} |
| 427 |
|
| 428 |
public static String padLeft(long number, int pad) { |
| 429 |
String s=number+""; |
| 430 |
while(s.length()<pad){s=" "+s;} |
| 431 |
return s; |
| 432 |
} |
| 433 |
|
| 434 |
public static String padLeft(String s, int pad) { |
| 435 |
while(s.length()<pad){s=" "+s;} |
| 436 |
return s; |
| 437 |
} |
| 438 |
|
| 439 |
public static String padKMB(long number, int pad) { |
| 440 |
String s; |
| 441 |
if(Shared.OUTPUT_KMG){ |
| 442 |
if(number<100000) {s=""+number;} |
| 443 |
else if(number<100000000) {s=""+(number/1000)+"k";} |
| 444 |
else if(number<100000000000L) {s=""+(number/1000000)+"m";} |
| 445 |
else if(number<100000000000000L) {s=""+(number/1000000000)+"b";} |
| 446 |
else if(number<100000000000000000L) {s=""+(number/1000000000000L)+"t";} |
| 447 |
else{s=""+(number/1000000000000000L)+"q";} |
| 448 |
}else{ |
| 449 |
s=""+number; |
| 450 |
} |
| 451 |
while(s.length()<pad){s=" "+s;} |
| 452 |
return s; |
| 453 |
} |
| 454 |
|
| 455 |
public static String timeReadsBasesProcessed(Timer t, long reads, long bases, int pad){ |
| 456 |
return time(t, pad)+"\n"+readsBasesProcessed(t.elapsed, reads, bases, pad); |
| 457 |
} |
| 458 |
|
| 459 |
public static String timeZMWsReadsBasesProcessed(Timer t, long ZMWs, long reads, long bases, int pad){ |
| 460 |
return time(t, pad)+"\n"+ZMWsReadsBasesProcessed(t.elapsed, ZMWs, reads, bases, pad); |
| 461 |
} |
| 462 |
|
| 463 |
public static String timeQueriesComparisonsProcessed(Timer t, long x, long y, int pad){ |
| 464 |
return time(t, pad)+"\n"+queriesComparisonsProcessed(t.elapsed, x, y, pad); |
| 465 |
} |
| 466 |
|
| 467 |
public static String time(Timer t, int pad){ |
| 468 |
return ("Time: \t"+t); |
| 469 |
} |
| 470 |
|
| 471 |
public static String readsBasesProcessed(long elapsed, long reads, long bases, int pad){ |
| 472 |
double rpnano=reads/(double)elapsed; |
| 473 |
double bpnano=bases/(double)elapsed; |
| 474 |
|
| 475 |
String rstring=padKMB(reads, pad); |
| 476 |
String bstring=padKMB(bases, pad); |
| 477 |
StringBuilder sb=new StringBuilder(); |
| 478 |
sb.append("Reads Processed: ").append(rstring).append(Tools.format(" \t%.2fk reads/sec", rpnano*1000000)).append('\n'); |
| 479 |
sb.append("Bases Processed: ").append(bstring).append(Tools.format(" \t%.2fm bases/sec", bpnano*1000)); |
| 480 |
return sb.toString(); |
| 481 |
} |
| 482 |
|
| 483 |
public static String things(String things, long amt, int pad){ |
| 484 |
String rstring=padKMB(amt, pad); |
| 485 |
StringBuilder sb=new StringBuilder(); |
| 486 |
sb.append(things).append(": "); |
| 487 |
while(sb.length()<"Reads Processed: ".length()){sb.append(' ');} |
| 488 |
return sb.append(rstring).toString(); |
| 489 |
} |
| 490 |
|
| 491 |
public static String ZMWsReadsBasesProcessed(long elapsed, long ZMWs, long reads, long bases, int pad){ |
| 492 |
double zpnano=ZMWs/(double)elapsed; |
| 493 |
double rpnano=reads/(double)elapsed; |
| 494 |
double bpnano=bases/(double)elapsed; |
| 495 |
|
| 496 |
String zstring=padKMB(ZMWs, pad); |
| 497 |
String rstring=padKMB(reads, pad); |
| 498 |
String bstring=padKMB(bases, pad); |
| 499 |
StringBuilder sb=new StringBuilder(); |
| 500 |
sb.append("ZMWs Processed: ").append(zstring).append(Tools.format(" \t%.2fk ZMWs/sec", zpnano*1000000)).append('\n'); |
| 501 |
sb.append("Reads Processed: ").append(rstring).append(Tools.format(" \t%.2fk reads/sec", rpnano*1000000)).append('\n'); |
| 502 |
sb.append("Bases Processed: ").append(bstring).append(Tools.format(" \t%.2fm bases/sec", bpnano*1000)); |
| 503 |
return sb.toString(); |
| 504 |
} |
| 505 |
|
| 506 |
public static String queriesComparisonsProcessed(long elapsed, long queries, long comparisons, int pad){ |
| 507 |
double rpnano=queries/(double)elapsed; |
| 508 |
double bpnano=comparisons/(double)elapsed; |
| 509 |
|
| 510 |
String rstring=padKMB(queries, pad); |
| 511 |
String bstring=padKMB(comparisons, pad); |
| 512 |
StringBuilder sb=new StringBuilder(); |
| 513 |
sb.append("Queries: ").append(rstring).append(Tools.format(" \t%.2f queries/sec", rpnano*1000000000)).append('\n'); |
| 514 |
sb.append("Comparisons: ").append(bstring).append(Tools.format(" \t%.2f comparisons/sec", bpnano*1000000000)); |
| 515 |
return sb.toString(); |
| 516 |
} |
| 517 |
|
| 518 |
public static String timeSketchesKeysProcessed(Timer t, long sketchesProcessed, long keysProcessed, int pad){ |
| 519 |
return time(t, pad)+"\n"+sketchesKeysProcessed(t.elapsed, sketchesProcessed, keysProcessed, pad); |
| 520 |
} |
| 521 |
|
| 522 |
public static String sketchesKeysProcessed(long elapsed, long sketches, long keys, int pad){ |
| 523 |
double rpnano=sketches/(double)elapsed; |
| 524 |
double bpnano=keys/(double)elapsed; |
| 525 |
|
| 526 |
String rstring=padKMB(sketches, pad); |
| 527 |
String bstring=padKMB(keys, pad); |
| 528 |
StringBuilder sb=new StringBuilder(); |
| 529 |
sb.append("Sketches Processed: ").append(rstring).append(Tools.format(" \t%.2fk sketches/sec", rpnano*1000000)).append('\n'); |
| 530 |
sb.append("Keys Processed: ").append(bstring).append(Tools.format(" \t%.2fm keys/sec", bpnano*1000)); |
| 531 |
return sb.toString(); |
| 532 |
} |
| 533 |
|
| 534 |
public static String readsBasesOut(long readsIn, long basesIn, long readsOut, long basesOut, int pad, boolean percent){ |
| 535 |
double rpct=readsOut*100.0/readsIn; |
| 536 |
double bpct=basesOut*100.0/basesIn; |
| 537 |
String rstring=padKMB(readsOut, pad); |
| 538 |
String bstring=padKMB(basesOut, pad); |
| 539 |
StringBuilder sb=new StringBuilder(); |
| 540 |
sb.append("Reads Out: ").append(rstring).append(percent ? Tools.format(" \t%.2f%%", rpct) : "").append('\n'); |
| 541 |
sb.append("Bases Out: ").append(bstring).append(percent ? Tools.format(" \t%.2f%%", bpct) : ""); |
| 542 |
return sb.toString(); |
| 543 |
} |
| 544 |
|
| 545 |
public static String readsBasesOut(long elapsed, long reads, long bases, int pad){ |
| 546 |
double rpnano=reads/(double)elapsed; |
| 547 |
double bpnano=bases/(double)elapsed; |
| 548 |
|
| 549 |
String rstring=padKMB(reads, pad); |
| 550 |
String bstring=padKMB(bases, pad); |
| 551 |
StringBuilder sb=new StringBuilder(); |
| 552 |
sb.append("Reads Out: ").append(rstring).append(Tools.format(" \t%.2fk reads/sec", rpnano*1000000)).append('\n'); |
| 553 |
sb.append("Bases Out: ").append(bstring).append(Tools.format(" \t%.2fm bases/sec", bpnano*1000)); |
| 554 |
return sb.toString(); |
| 555 |
} |
| 556 |
|
| 557 |
public static String ZMWsReadsBasesOut(long ZMWsIn, long readsIn, long basesIn, long ZMWsOut, long readsOut, long basesOut, int pad, boolean percent){ |
| 558 |
double zpct=ZMWsOut*100.0/ZMWsIn; |
| 559 |
double rpct=readsOut*100.0/readsIn; |
| 560 |
double bpct=basesOut*100.0/basesIn; |
| 561 |
String zstring=padKMB(ZMWsOut, pad); |
| 562 |
String rstring=padKMB(readsOut, pad); |
| 563 |
String bstring=padKMB(basesOut, pad); |
| 564 |
StringBuilder sb=new StringBuilder(); |
| 565 |
sb.append("ZMWs Out: ").append(zstring).append(percent ? Tools.format(" \t%.2f%%", zpct) : "").append('\n'); |
| 566 |
sb.append("Reads Out: ").append(rstring).append(percent ? Tools.format(" \t%.2f%%", rpct) : "").append('\n'); |
| 567 |
sb.append("Bases Out: ").append(bstring).append(percent ? Tools.format(" \t%.2f%%", bpct) : ""); |
| 568 |
return sb.toString(); |
| 569 |
} |
| 570 |
|
| 571 |
public static String numberPercent(String text, long number, double percent, int decimals, int pad){ |
| 572 |
String rstring=padLeft(number, pad); |
| 573 |
StringBuilder sb=new StringBuilder(); |
| 574 |
while(text.length()<20){text+=" ";} |
| 575 |
sb.append(text).append(rstring).append(Tools.format(" \t%."+decimals+"f%%", percent)); |
| 576 |
return sb.toString(); |
| 577 |
} |
| 578 |
|
| 579 |
public static String number(String text, double number, int decimals, int pad){ |
| 580 |
String rstring=padLeft(Tools.format("%."+decimals+"f", number), pad); |
| 581 |
StringBuilder sb=new StringBuilder(); |
| 582 |
while(text.length()<20){text+=" ";} |
| 583 |
sb.append(text).append(rstring); |
| 584 |
return sb.toString(); |
| 585 |
} |
| 586 |
|
| 587 |
public static String number(String text, long number, int pad){ |
| 588 |
String rstring=padLeft(""+number, pad); |
| 589 |
StringBuilder sb=new StringBuilder(); |
| 590 |
while(text.length()<20){text+=" ";} |
| 591 |
sb.append(text).append(rstring); |
| 592 |
return sb.toString(); |
| 593 |
} |
| 594 |
|
| 595 |
public static String string(String text, String value, int pad){ |
| 596 |
String rstring=padLeft(value, pad); |
| 597 |
StringBuilder sb=new StringBuilder(); |
| 598 |
while(text.length()<20){text+=" ";} |
| 599 |
sb.append(text).append(rstring); |
| 600 |
return sb.toString(); |
| 601 |
} |
| 602 |
|
| 603 |
public static String timeLinesBytesProcessed(Timer t, long linesProcessed, long bytesProcessed, int pad){ |
| 604 |
return ("Time: \t"+t+"\n"+linesBytesProcessed(t.elapsed, linesProcessed, bytesProcessed, pad)); |
| 605 |
} |
| 606 |
|
| 607 |
public static String linesBytesProcessed(long elapsed, long lines, long bytes, int pad){ |
| 608 |
double rpnano=lines/(double)elapsed; |
| 609 |
double bpnano=bytes/(double)elapsed; |
| 610 |
|
| 611 |
String rstring=padKMB(lines, pad); |
| 612 |
String bstring=padKMB(bytes, pad); |
| 613 |
StringBuilder sb=new StringBuilder(); |
| 614 |
sb.append("Lines Processed: ").append(rstring).append(Tools.format(" \t%.2fk lines/sec", rpnano*1000000)).append('\n'); |
| 615 |
sb.append("Bytes Processed: ").append(bstring).append(Tools.format(" \t%.2fm bytes/sec", bpnano*1000)); |
| 616 |
return sb.toString(); |
| 617 |
} |
| 618 |
|
| 619 |
public static String timeLinesProcessed(Timer t, long linesProcessed, int pad){ |
| 620 |
return ("Time: \t"+t+"\n"+linesProcessed(t.elapsed, linesProcessed, pad)); |
| 621 |
} |
| 622 |
|
| 623 |
public static String linesProcessed(long elapsed, long lines, int pad){ |
| 624 |
double rpnano=lines/(double)elapsed; |
| 625 |
|
| 626 |
String rstring=padKMB(lines, pad); |
| 627 |
StringBuilder sb=new StringBuilder(); |
| 628 |
sb.append("Lines Processed: ").append(rstring).append(Tools.format(" \t%.2fk lines/sec", rpnano*1000000)); |
| 629 |
return sb.toString(); |
| 630 |
} |
| 631 |
|
| 632 |
public static String thingsProcessed(long elapsed, long count, int pad, String name){ |
| 633 |
double tpnano=(count/(double)elapsed)*1000; |
| 634 |
String rateString=null; |
| 635 |
if(tpnano>=1) { |
| 636 |
rateString=Tools.format(" \t%.2fm %s/sec", tpnano, name.toLowerCase()); |
| 637 |
}else if(tpnano>=0.001) { |
| 638 |
tpnano*=1000; |
| 639 |
rateString=Tools.format(" \t%.2fk %s/sec", tpnano, name.toLowerCase()); |
| 640 |
}else { |
| 641 |
tpnano*=1000; |
| 642 |
rateString=Tools.format(" \t%.2f %s/sec", tpnano, name.toLowerCase()); |
| 643 |
} |
| 644 |
|
| 645 |
String tstring=padKMB(count, pad); |
| 646 |
StringBuilder sb=new StringBuilder(); |
| 647 |
sb.append(name).append(" Processed: "); |
| 648 |
while(sb.length()<20) {sb.append(' ');} |
| 649 |
sb.append(tstring).append(rateString); |
| 650 |
return sb.toString(); |
| 651 |
} |
| 652 |
|
| 653 |
public static String linesBytesOut(long linesIn, long bytesIn, long linesOut, long bytesOut, int pad, boolean percent){ |
| 654 |
double rpct=linesOut*100.0/linesIn; |
| 655 |
double bpct=bytesOut*100.0/bytesIn; |
| 656 |
String rstring=padKMB(linesOut, pad); |
| 657 |
String bstring=padKMB(bytesOut, pad); |
| 658 |
StringBuilder sb=new StringBuilder(); |
| 659 |
sb.append("Lines Out: ").append(rstring).append(percent ? Tools.format(" \t%.2f%%", rpct) : "").append('\n'); |
| 660 |
sb.append("Bytes Out: ").append(bstring).append(percent ? Tools.format(" \t%.2f%%", bpct) : ""); |
| 661 |
return sb.toString(); |
| 662 |
} |
| 663 |
|
| 664 |
public static ArrayList<byte[]> split(byte[] line, int start, byte delimiter) { |
| 665 |
if(line.length<start){return null;} |
| 666 |
int a=start-1, b=start; |
| 667 |
ArrayList<byte[]> list=new ArrayList<byte[]>(8); |
| 668 |
while(b<line.length){ |
| 669 |
byte c=line[b]; |
| 670 |
if(c==delimiter){ |
| 671 |
list.add(Arrays.copyOfRange(line, a+1, b)); |
| 672 |
a=b; |
| 673 |
} |
| 674 |
b++; |
| 675 |
} |
| 676 |
list.add(Arrays.copyOfRange(line, a+1, b)); |
| 677 |
return list; |
| 678 |
} |
| 679 |
|
| 680 |
public static void breakReads(ArrayList<Read> list, final int max, int min, final PrintStream outstream){ |
| 681 |
if(!containsReadsOutsideSizeRange(list, min, max)){return;} |
| 682 |
assert(max>0 || min>0) : "min or max read length must be positive."; |
| 683 |
assert(max<1 || max>=min) : "max read length must be at least min read length: "+max+"<"+min; |
| 684 |
min=Tools.max(0, min); |
| 685 |
|
| 686 |
ArrayList<Read> temp=new ArrayList<Read>(list.size()*2); |
| 687 |
for(Read r : list){ |
| 688 |
if(r==null || r.bases==null){ |
| 689 |
temp.add(r); |
| 690 |
}else if(r.length()<min){ |
| 691 |
temp.add(null); |
| 692 |
}else if(max<1 || r.length()<=max){ |
| 693 |
temp.add(r); |
| 694 |
}else{ |
| 695 |
final byte[] bases=r.bases; |
| 696 |
final byte[] quals=r.quality; |
| 697 |
final String name=r.id; |
| 698 |
final int limit=bases.length-min; |
| 699 |
for(int num=1, start=0, stop=max; start<limit; num++, start+=max, stop+=max){ |
| 700 |
if(outstream!=null){ |
| 701 |
outstream.println(bases.length+", "+start+", "+stop); |
| 702 |
if(quals!=null){outstream.println(quals.length+", "+start+", "+stop);} |
| 703 |
} |
| 704 |
stop=Tools.min(stop, bases.length); |
| 705 |
byte[] b2=KillSwitch.copyOfRange(bases, start, stop); |
| 706 |
byte[] q2=(quals==null ? null : KillSwitch.copyOfRange(quals, start, stop)); |
| 707 |
String n2=name+"_"+num; |
| 708 |
Read r2=new Read(b2, q2, n2, r.numericID, r.flags); |
| 709 |
r2.setMapped(false); |
| 710 |
temp.add(r2); |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
list.clear(); |
| 715 |
list.ensureCapacity(temp.size()); |
| 716 |
|
| 717 |
for(Read r : temp){ |
| 718 |
if(r!=null){list.add(r);} |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
private static boolean containsReadsAboveSize(ArrayList<Read> list, int size){ |
| 723 |
for(Read r : list){ |
| 724 |
if(r!=null && r.bases!=null){ |
| 725 |
if(r.length()>size){ |
| 726 |
assert(r.mate==null) : "Read of length "+r.length()+">"+size+". Paired input is incompatible with 'breaklength'"; |
| 727 |
return true; |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
return false; |
| 732 |
} |
| 733 |
|
| 734 |
private static boolean containsReadsOutsideSizeRange(ArrayList<Read> list, int min, int max){ |
| 735 |
for(Read r : list){ |
| 736 |
if(r!=null && r.bases!=null){ |
| 737 |
if((max>0 && r.length()>max) || r.length()<min){ |
| 738 |
assert(r.mate==null) : "Read of length "+r.length()+" outside of range "+min+"-"+max+". Paired input is incompatible with 'breaklength'"; |
| 739 |
return true; |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
return false; |
| 744 |
} |
| 745 |
|
| 746 |
public static void shiftRight(final byte[] array, final int amt){ |
| 747 |
for(int i=array.length-1-amt, j=array.length-1; i>=0; i--, j--){ |
| 748 |
array[j]=array[i]; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
public static void shiftLeft(final byte[] array, final int amt){ |
| 753 |
for(int i=amt, j=0; i<array.length; i++, j++){ |
| 754 |
array[j]=array[i]; |
| 755 |
} |
| 756 |
} |
| 757 |
public static int lastChar(String s) { |
| 758 |
return s==null || s.length()<1 ? -1 : s.charAt(s.length()-1); |
| 759 |
} |
| 760 |
|
| 761 |
public static boolean startsWithIgnoreCase(String s, String prefix){ |
| 762 |
if(s==null || s.length()<prefix.length()){return false;} |
| 763 |
for(int i=0; i<prefix.length(); i++){ |
| 764 |
if(Tools.toLowerCase(s.charAt(i))!=Tools.toLowerCase(prefix.charAt(i))){ |
| 765 |
return false; |
| 766 |
} |
| 767 |
} |
| 768 |
return true; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
public static double observedToActualCoverage_iterative(double y, double error){ |
| 773 |
double guess=y-0.95/Math.pow(y, 1.4); |
| 774 |
double y2=actualToObservedCoverage(guess); |
| 775 |
double dif=y-y2; |
| 776 |
for(int i=0; i<20 && Math.abs(dif)>error; i++){ |
| 777 |
guess=guess+dif*0.9; |
| 778 |
y2=actualToObservedCoverage(guess); |
| 779 |
dif=y-y2; |
| 780 |
} |
| 781 |
return guess; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public static double observedToActualCoverage(double y){ |
| 788 |
return Tools.max(0, y-Math.exp(-0.885*(y-1))); |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
private static double actualToObservedCoverage(double x){ |
| 795 |
return x+Math.exp(-0.597*x); |
| 796 |
} |
| 797 |
|
| 798 |
public static double kmerToReadCoverage(double cov, double readlen, int k){ |
| 799 |
return readlen<=k ? 0 : cov*readlen/(readlen-k+1); |
| 800 |
} |
| 801 |
|
| 802 |
public static double readToKmerCoverage(double cov, double readlen, int k){ |
| 803 |
return readlen<=k ? 0 : cov*(readlen-k+1)/readlen; |
| 804 |
} |
| 805 |
|
| 806 |
public static boolean isNumeric(String s) { |
| 807 |
if(s==null || s.length()<1){return false;} |
| 808 |
char first=s.charAt(0); |
| 809 |
int dots=0, signs=0, nums=0; |
| 810 |
if(first=='-'){signs++;} |
| 811 |
else if(first>='0' && first<='9'){nums++;} |
| 812 |
else if(first=='.'){dots++;} |
| 813 |
else{return false;} |
| 814 |
|
| 815 |
for(int i=1; i<s.length(); i++){ |
| 816 |
char c=s.charAt(i); |
| 817 |
if(c>='0' && c<='9'){nums++;} |
| 818 |
else if(c=='.'){dots++;} |
| 819 |
else{return false;} |
| 820 |
} |
| 821 |
return nums>0 && dots<=1; |
| 822 |
} |
| 823 |
|
| 824 |
public static void toUpperCase(byte[] s) { |
| 825 |
for(int i=0; i<s.length; i++) {s[i]=toUpperCase(s[i]);} |
| 826 |
} |
| 827 |
|
| 828 |
public static int countLetters(String a) { |
| 829 |
int count=0; |
| 830 |
for(int i=0; i<a.length(); i++) { |
| 831 |
count+=(isLetter(a.charAt(i)) ? 1 : 0); |
| 832 |
} |
| 833 |
return count; |
| 834 |
} |
| 835 |
|
| 836 |
public static boolean isDigitOrSign(int c) {return c<0 ? false : signOrDigitMap[c];} |
| 837 |
public static boolean isNumeric(int c) {return c<0 ? false : numericMap[c];} |
| 838 |
public static boolean isDigit(int c) {return c>='0' && c<='9';} |
| 839 |
public static boolean isLetter(int c) {return c<0 ? false : letterMap[c];} |
| 840 |
public static boolean isLetterOrDigit(int c) {return c<0 ? false : isDigit(c) || letterMap[c];} |
| 841 |
public static boolean isUpperCase(int c) {return c>='A' && c<='Z';} |
| 842 |
public static boolean isLowerCase(int c) {return c>='a' && c<='z';} |
| 843 |
public static int toUpperCase(int c) {return c<'a' || c>'z' ? c : c-32;} |
| 844 |
public static int toLowerCase(int c) {return c<'A' || c>'Z' ? c : c+32;} |
| 845 |
|
| 846 |
public static boolean isDigitOrSign(byte c) {return c<0 ? false : signOrDigitMap[c];} |
| 847 |
public static boolean isNumeric(byte c) {return c<0 ? false : numericMap[c];} |
| 848 |
public static boolean isDigit(byte c) {return c>='0' && c<='9';} |
| 849 |
public static boolean isLetter(byte c) {return c<0 ? false : letterMap[c];} |
| 850 |
public static boolean isLetterOrDigit(byte c) {return c<0 ? false : isDigit(c) || letterMap[c];} |
| 851 |
public static boolean isUpperCase(byte c) {return c>='A' && c<='Z';} |
| 852 |
public static boolean isLowerCase(byte c) {return c>='a' && c<='z';} |
| 853 |
public static byte toUpperCase(byte c) {return c<'a' || c>'z' ? c : (byte)(c-32);} |
| 854 |
public static byte toLowerCase(byte c) {return c<'A' || c>'Z' ? c : (byte)(c+32);} |
| 855 |
|
| 856 |
public static boolean isDigitOrSign(char c) {return c>127 ? false : signOrDigitMap[c];} |
| 857 |
public static boolean isNumeric(char c) {return c>127 ? false : numericMap[c];} |
| 858 |
public static boolean isDigit(char c) {return c>='0' && c<='9';} |
| 859 |
public static boolean isLetter(char c) {return c>127 ? false : letterMap[c];} |
| 860 |
public static boolean isLetterOrDigit(char c) {return c<0 ? false : isDigit(c) || letterMap[c];} |
| 861 |
public static boolean isUpperCase(char c) {return c>='A' && c<='Z';} |
| 862 |
public static boolean isLowerCase(char c) {return c>='a' && c<='z';} |
| 863 |
public static char toUpperCase(char c) {return c<'a' || c>'z' ? c : (char)(c-32);} |
| 864 |
public static char toLowerCase(char c) {return c<'A' || c>'Z' ? c : (char)(c+32);} |
| 865 |
|
| 866 |
|
| 867 |
public static String toString(Throwable t){ |
| 868 |
StringWriter sw = new StringWriter(); |
| 869 |
PrintWriter pw = new PrintWriter(sw); |
| 870 |
t.printStackTrace(pw); |
| 871 |
String sStackTrace = sw.toString(); |
| 872 |
return sStackTrace; |
| 873 |
} |
| 874 |
public static int countKmers(byte[] bases, int k){ |
| 875 |
if(bases==null || bases.length<k || k<1){return 0;} |
| 876 |
int len=0; |
| 877 |
int kmers=0; |
| 878 |
for(byte b : bases){ |
| 879 |
if(AminoAcid.isFullyDefined(b)){len++;} |
| 880 |
else{ |
| 881 |
if(len>=k){kmers=kmers+len-k+1;} |
| 882 |
len=0; |
| 883 |
} |
| 884 |
} |
| 885 |
if(len>=k){kmers=kmers+len-k+1;} |
| 886 |
return kmers; |
| 887 |
} |
| 888 |
|
| 889 |
public static double countCorrectKmers(byte[] quals, int k){ |
| 890 |
if(quals==null || quals.length<k || k<1){return 0;} |
| 891 |
int len=0; |
| 892 |
double kmers=0; |
| 893 |
double prob=1; |
| 894 |
for(int i=0; i<quals.length; i++){ |
| 895 |
final byte q=quals[i]; |
| 896 |
if(q>0){ |
| 897 |
len++; |
| 898 |
prob=prob*align2.QualityTools.PROB_CORRECT[q]; |
| 899 |
if(len>k){ |
| 900 |
byte oldq=quals[i-k]; |
| 901 |
prob=prob*align2.QualityTools.PROB_CORRECT_INVERSE[oldq]; |
| 902 |
} |
| 903 |
if(len>=k){kmers+=prob;} |
| 904 |
}else{ |
| 905 |
len=0; |
| 906 |
prob=1; |
| 907 |
} |
| 908 |
} |
| 909 |
return kmers; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
public static long estimateFileSize(String fname){ |
| 914 |
FileFormat ff=FileFormat.testInput(fname, FileFormat.FASTQ, null, false, false); |
| 915 |
if(ff==null || ff.stdio()){return -1;} |
| 916 |
|
| 917 |
double mult=1; |
| 918 |
if(ff.compressed()){ |
| 919 |
if(ff.bam()){mult=6;} |
| 920 |
if(ff.fasta()){mult=5;} |
| 921 |
else{mult=4;} |
| 922 |
} |
| 923 |
|
| 924 |
File f=new File(fname); |
| 925 |
long size=f.length(); |
| 926 |
double diskEstimate=size*mult; |
| 927 |
return (long)diskEstimate; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
public static double[] estimateFileMemory(String fname, int readsToFetch, double extraOverheadPerRead, boolean earlyExit, boolean lowComplexity){ |
| 938 |
|
| 939 |
FileFormat ff=FileFormat.testInput(fname, FileFormat.FASTQ, null, false, false); |
| 940 |
if(ff==null || ff.stdio()){return null;} |
| 941 |
|
| 942 |
File f=new File(fname); |
| 943 |
final long size=f.length(); |
| 944 |
|
| 945 |
if(earlyExit){ |
| 946 |
|
| 947 |
long available=Shared.memFree(); |
| 948 |
|
| 949 |
|
| 950 |
double memRatio, diskRatio, readRatio; |
| 951 |
if(ff.compressed()){ |
| 952 |
memRatio=40; |
| 953 |
diskRatio=5; |
| 954 |
}else{ |
| 955 |
memRatio=8; |
| 956 |
diskRatio=1; |
| 957 |
} |
| 958 |
readRatio=diskRatio/100; |
| 959 |
|
| 960 |
long memEstimate=(long)(memRatio*size); |
| 961 |
long diskEstimate=(long)(diskRatio*size); |
| 962 |
long readsEstimate=(long)(readRatio*size); |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
if(memEstimate*2.1<available && readsEstimate*4<Integer.MAX_VALUE){ |
| 967 |
return new double[] {memEstimate, diskEstimate, memRatio, diskRatio, readsEstimate}; |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
readsToFetch=Tools.max(readsToFetch, 200); |
| 972 |
ff=FileFormat.testInput(fname, FileFormat.FASTQ, null, ff.compressed() && !ff.gzip(), true); |
| 973 |
ArrayList<Read> reads=ReadInputStream.toReads(ff, readsToFetch); |
| 974 |
long minBytes=Integer.MAX_VALUE; |
| 975 |
long maxBytes=1; |
| 976 |
long sumBytes=0; |
| 977 |
long minMem=Integer.MAX_VALUE; |
| 978 |
long maxMem=1; |
| 979 |
long sumMem=0; |
| 980 |
long minLen=Integer.MAX_VALUE; |
| 981 |
long maxLen=1; |
| 982 |
long sumLen=0; |
| 983 |
long minQLen=Integer.MAX_VALUE; |
| 984 |
long maxQLen=1; |
| 985 |
long sumQLen=0; |
| 986 |
long minHdr=Integer.MAX_VALUE; |
| 987 |
long maxHdr=1; |
| 988 |
long sumHdr=0; |
| 989 |
long readCount=0; |
| 990 |
|
| 991 |
BitSet qualities=new BitSet(); |
| 992 |
|
| 993 |
if(reads==null || reads.size()<1){ |
| 994 |
minBytes=maxBytes=minLen=maxLen=minMem=maxMem=minHdr=maxHdr=1; |
| 995 |
}else{ |
| 996 |
for(Read r1 : reads){ |
| 997 |
long x; |
| 998 |
readCount++; |
| 999 |
|
| 1000 |
x=r1.length(); |
| 1001 |
minLen=min(minLen, x); |
| 1002 |
maxLen=max(maxLen, x); |
| 1003 |
sumLen+=x; |
| 1004 |
|
| 1005 |
x=r1.qlength(); |
| 1006 |
minQLen=min(minQLen, x); |
| 1007 |
maxQLen=max(maxQLen, x); |
| 1008 |
sumQLen+=x; |
| 1009 |
|
| 1010 |
if(x>0){ |
| 1011 |
for(byte q : r1.quality){qualities.set(q);} |
| 1012 |
} |
| 1013 |
|
| 1014 |
x=r1.countBytes(); |
| 1015 |
minMem=min(minMem, x); |
| 1016 |
maxMem=max(maxMem, x); |
| 1017 |
sumMem+=x; |
| 1018 |
|
| 1019 |
x=r1.id.length(); |
| 1020 |
minHdr=min(minHdr, x); |
| 1021 |
maxHdr=max(maxHdr, x); |
| 1022 |
sumHdr+=x; |
| 1023 |
|
| 1024 |
x=r1.countFastqBytes(); |
| 1025 |
minBytes=min(minBytes, x); |
| 1026 |
maxBytes=max(maxBytes, x); |
| 1027 |
sumBytes+=x; |
| 1028 |
|
| 1029 |
Read r2=r1.mate; |
| 1030 |
if(r2!=null){ |
| 1031 |
readCount++; |
| 1032 |
|
| 1033 |
x=r2.length(); |
| 1034 |
minLen=min(minLen, x); |
| 1035 |
maxLen=max(maxLen, x); |
| 1036 |
sumLen+=x; |
| 1037 |
|
| 1038 |
x=r2.qlength(); |
| 1039 |
minQLen=min(minQLen, x); |
| 1040 |
maxQLen=max(maxQLen, x); |
| 1041 |
sumQLen+=x; |
| 1042 |
|
| 1043 |
if(x>0){ |
| 1044 |
for(byte q : r2.quality){qualities.set(q);} |
| 1045 |
} |
| 1046 |
|
| 1047 |
x=r2.countBytes(); |
| 1048 |
minMem=min(minMem, x); |
| 1049 |
maxMem=max(maxMem, x); |
| 1050 |
sumMem+=x; |
| 1051 |
|
| 1052 |
x=r2.id.length(); |
| 1053 |
minHdr=min(minHdr, x); |
| 1054 |
maxHdr=max(maxHdr, x); |
| 1055 |
sumHdr+=x; |
| 1056 |
|
| 1057 |
x=r2.countFastqBytes(); |
| 1058 |
minBytes=min(minBytes, x); |
| 1059 |
maxBytes=max(maxBytes, x); |
| 1060 |
sumBytes+=x; |
| 1061 |
} |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
int numQualities=Tools.max(2, qualities.cardinality()); |
| 1066 |
double bitsPerQuality=(Math.log(numQualities)/Math.log(2)); |
| 1067 |
boolean binned=numQualities<=8; |
| 1068 |
|
| 1069 |
double compressedSize; |
| 1070 |
if(ff.compressed()){ |
| 1071 |
compressedSize=0.125*( |
| 1072 |
0.2*sumHdr |
| 1073 |
+(lowComplexity ? 0.5 : 1.5)*sumLen |
| 1074 |
+(binned ? 0.5 : 2.5)*sumQLen |
| 1075 |
+(4*6*readCount) |
| 1076 |
+(8*2*readCount) |
| 1077 |
); |
| 1078 |
if(ff.bz2() || ff.fqz()){ |
| 1079 |
compressedSize*=0.83; |
| 1080 |
} |
| 1081 |
}else{ |
| 1082 |
compressedSize=sumBytes; |
| 1083 |
} |
| 1084 |
double memRatio=(sumMem+readCount*extraOverheadPerRead)/compressedSize; |
| 1085 |
double diskRatio=sumBytes/compressedSize; |
| 1086 |
|
| 1087 |
long memEstimate=(long)(memRatio*size); |
| 1088 |
long diskEstimate=(long)(diskRatio*size); |
| 1089 |
double readRatio=readCount/(double)(Tools.max(1, sumBytes)); |
| 1090 |
long readEstimate=(long)(readRatio*diskEstimate); |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
return new double[] {memEstimate, diskEstimate, memRatio, diskRatio, readEstimate}; |
| 1100 |
} |
| 1101 |
|
| 1102 |
public static final boolean nextBoolean(Random randy){ |
| 1103 |
return randy.nextBoolean(); |
| 1104 |
|
| 1105 |
|
| 1106 |
} |
| 1107 |
|
| 1108 |
public static float[] inverse(float[] array) { |
| 1109 |
float[] out=new float[array.length]; |
| 1110 |
for(int i=0; i<array.length; i++){ |
| 1111 |
|
| 1112 |
out[i]=1/array[i]; |
| 1113 |
} |
| 1114 |
return out; |
| 1115 |
} |
| 1116 |
|
| 1117 |
public static double[] inverse(double[] array) { |
| 1118 |
double[] out=new double[array.length]; |
| 1119 |
for(int i=0; i<array.length; i++){ |
| 1120 |
out[i]=1/array[i]; |
| 1121 |
} |
| 1122 |
return out; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
public static boolean checkHeader(String s){ |
| 1127 |
if(s==null || s.length()<1){return false;} |
| 1128 |
boolean ok=true; |
| 1129 |
for(int i=0; i<s.length() && ok; i++){ |
| 1130 |
char c=s.charAt(i); |
| 1131 |
ok=(c>=32 && c<=126); |
| 1132 |
} |
| 1133 |
return ok; |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
public static String fixHeader(String s, boolean allowNull, boolean processAssertions){ |
| 1138 |
|
| 1139 |
if(checkHeader(s)){return s;} |
| 1140 |
if(s==null || s.length()==0){ |
| 1141 |
if(processAssertions && !allowNull){KillSwitch.kill("Sequence found with null header (unfixable). To bypass, set allownullheader=true.");} |
| 1142 |
return ""; |
| 1143 |
} |
| 1144 |
StringBuilder sb=new StringBuilder(s.length()); |
| 1145 |
for(int i=0; i<s.length(); i++){ |
| 1146 |
final char c=s.charAt(i), d; |
| 1147 |
|
| 1148 |
if(c>=0 && c<=255){ |
| 1149 |
d=specialChars[c]; |
| 1150 |
}else{ |
| 1151 |
d='X'; |
| 1152 |
} |
| 1153 |
|
| 1154 |
sb.append(d); |
| 1155 |
} |
| 1156 |
return sb.toString(); |
| 1157 |
} |
| 1158 |
|
| 1159 |
public static int secondHighestPosition(int[] array) { |
| 1160 |
int maxP, maxP2; |
| 1161 |
if(array[0]>=array[1]){ |
| 1162 |
maxP=0; |
| 1163 |
maxP2=1; |
| 1164 |
}else{ |
| 1165 |
maxP=1; |
| 1166 |
maxP2=0; |
| 1167 |
} |
| 1168 |
for(int i=2; i<array.length; i++){ |
| 1169 |
int x=array[i]; |
| 1170 |
if(x>array[maxP2]){ |
| 1171 |
if(x>=array[maxP]){ |
| 1172 |
maxP2=maxP; |
| 1173 |
maxP=i; |
| 1174 |
}else{ |
| 1175 |
maxP2=i; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
} |
| 1179 |
return maxP2; |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
public static ArrayList<String> getFileOrFiles(String b, ArrayList<String> list, boolean fasta, boolean fastq, boolean sam, boolean any){ |
| 1194 |
if(list==null){list=new ArrayList<String>();} |
| 1195 |
{ |
| 1196 |
File f=new File(b); |
| 1197 |
if(f.exists() && f.isFile()) { |
| 1198 |
list.add(b); |
| 1199 |
return list; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
String[] split=b.split(","); |
| 1203 |
for(String s : split){ |
| 1204 |
File f=new File(s); |
| 1205 |
if(f.isDirectory()){ |
| 1206 |
for(File f2 : f.listFiles()){ |
| 1207 |
if(f2.isFile()){ |
| 1208 |
String name=f2.getName().toLowerCase(); |
| 1209 |
String ext=ReadWrite.rawExtension(name); |
| 1210 |
|
| 1211 |
boolean pass=any || (fasta && FileFormat.isFastaExt(ext)) || |
| 1212 |
(fastq && FileFormat.isFastqExt(ext)) || (sam && FileFormat.isSamOrBamExt(ext)); |
| 1213 |
|
| 1214 |
if(pass){ |
| 1215 |
String s2=f2.getAbsolutePath(); |
| 1216 |
list.add(s2); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
} |
| 1220 |
}else{ |
| 1221 |
list.add(s); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
return list; |
| 1225 |
} |
| 1226 |
|
| 1227 |
public static ArrayList<byte[]> toAdapterList(String name, int maxLength){ |
| 1228 |
if(maxLength<1){maxLength=Integer.MAX_VALUE;} |
| 1229 |
if(name==null){return null;} |
| 1230 |
String[] split; |
| 1231 |
|
| 1232 |
LinkedHashSet<String> set=new LinkedHashSet<String>(); |
| 1233 |
if(new File(name).exists()){ |
| 1234 |
split=new String[] {name}; |
| 1235 |
}else{ |
| 1236 |
split=name.split(","); |
| 1237 |
} |
| 1238 |
for(String s : split){ |
| 1239 |
if(new File(s).exists()){ |
| 1240 |
ArrayList<Read> reads=ReadInputStream.toReads(s, FileFormat.FASTA, -1); |
| 1241 |
for(Read r : reads){ |
| 1242 |
if(r!=null && r.length()>0){ |
| 1243 |
byte[] array=checkAdapter(r.bases, maxLength); |
| 1244 |
if(array.length>0){set.add(new String(array));} |
| 1245 |
} |
| 1246 |
} |
| 1247 |
}else{ |
| 1248 |
byte[] array=checkAdapter(s.getBytes(), maxLength); |
| 1249 |
if(array.length>0){set.add(new String(array));} |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
if(set.isEmpty()){return null;} |
| 1254 |
ArrayList<byte[]> list=new ArrayList<byte[]>(set.size()); |
| 1255 |
for(String s : set){ |
| 1256 |
list.add(s.getBytes()); |
| 1257 |
} |
| 1258 |
return list; |
| 1259 |
} |
| 1260 |
|
| 1261 |
private static byte[] checkAdapter(byte[] array, int maxLength){ |
| 1262 |
if(array.length>maxLength){array=Arrays.copyOf(array, maxLength);} |
| 1263 |
|
| 1264 |
for(int i=0; i<array.length; i++){ |
| 1265 |
byte b=array[i]; |
| 1266 |
int x=AminoAcid.baseToNumberExtended[b]; |
| 1267 |
if(x<0 || !Tools.isLetter(b) || !Tools.isUpperCase(b)){ |
| 1268 |
throw new RuntimeException("Invalid nucleotide "+(char)b+" in literal sequence "+new String(array)+"\n" |
| 1269 |
+ "If this was supposed to be a filename, the file was not found."); |
| 1270 |
} |
| 1271 |
if(AminoAcid.baseToNumber[b]<0){array[i]='N';} |
| 1272 |
} |
| 1273 |
|
| 1274 |
int trailingNs=0; |
| 1275 |
for(int i=array.length-1; i>=0; i--){ |
| 1276 |
if(array[i]=='N'){trailingNs++;} |
| 1277 |
} |
| 1278 |
if(trailingNs>0){ |
| 1279 |
array=Arrays.copyOf(array, array.length-trailingNs); |
| 1280 |
} |
| 1281 |
return array; |
| 1282 |
} |
| 1283 |
|
| 1284 |
public static byte[][] toAdapters(String name, final int maxLength){ |
| 1285 |
ArrayList<byte[]> list=toAdapterList(name, maxLength); |
| 1286 |
return list==null ? null : list.toArray(new byte[list.size()][]); |
| 1287 |
} |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
public static final int addNames(String s, Collection<String> names, boolean allowSubprocess){ |
| 1297 |
int added=0; |
| 1298 |
if(new File(s).exists()){ |
| 1299 |
|
| 1300 |
int[] vector=FileFormat.testFormat(s, false, false); |
| 1301 |
final int type=vector[0]; |
| 1302 |
ByteFile bf=ByteFile.makeByteFile(s, allowSubprocess); |
| 1303 |
|
| 1304 |
if(type==FileFormat.FASTQ){ |
| 1305 |
int num=0; |
| 1306 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine(), num++){ |
| 1307 |
if((num&3)==0 && line.length>0){ |
| 1308 |
names.add(new String(line, 1, line.length-1)); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
}else if(type==FileFormat.FASTA){ |
| 1312 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ |
| 1313 |
if(line.length>0 && line[0]=='>'){ |
| 1314 |
names.add(new String(line, 1, line.length-1)); |
| 1315 |
} |
| 1316 |
} |
| 1317 |
}else if(type==FileFormat.SAM){ |
| 1318 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ |
| 1319 |
if(line.length>0 && line[0]!='@'){ |
| 1320 |
String name=SamLine.parseNameOnly(line); |
| 1321 |
if(name!=null && name.length()>0){names.add(name);} |
| 1322 |
} |
| 1323 |
} |
| 1324 |
}else{ |
| 1325 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){ |
| 1326 |
if(line.length>0){ |
| 1327 |
names.add(new String(line)); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
} |
| 1331 |
bf.close(); |
| 1332 |
}else{ |
| 1333 |
added++; |
| 1334 |
names.add(s); |
| 1335 |
} |
| 1336 |
return added; |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
public static ArrayList<Read> replicateAmbiguous(ArrayList<Read> reads, int minlen) { |
| 1346 |
ArrayList<Read> out=new ArrayList<Read>(); |
| 1347 |
for(Read r1 : reads){ |
| 1348 |
final Read r2=r1.mate; |
| 1349 |
r1.mate=null; |
| 1350 |
|
| 1351 |
if(r1.containsUndefined() && r1.length()>=minlen){ |
| 1352 |
ArrayList<Read> temp=makeReplicates(r1); |
| 1353 |
out.addAll(temp); |
| 1354 |
}else{ |
| 1355 |
out.add(r1); |
| 1356 |
} |
| 1357 |
if(r2!=null){ |
| 1358 |
r2.mate=null; |
| 1359 |
if(r2.containsUndefined() && r2.length()>=minlen){ |
| 1360 |
ArrayList<Read> temp=makeReplicates(r2); |
| 1361 |
out.addAll(temp); |
| 1362 |
}else{ |
| 1363 |
out.add(r2); |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
return out; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
public static ArrayList<Read> makeReplicates(final Read r) { |
| 1377 |
|
| 1378 |
ArrayList<Read> temp=null; |
| 1379 |
if(!r.containsUndefined()){ |
| 1380 |
temp=new ArrayList<Read>(); |
| 1381 |
temp.add(r); |
| 1382 |
return temp; |
| 1383 |
} |
| 1384 |
final byte[] bases=r.bases; |
| 1385 |
for(int i=0; i<r.bases.length; i++){ |
| 1386 |
byte b=bases[i]; |
| 1387 |
if(!AminoAcid.isFullyDefined(b)){ |
| 1388 |
temp=replicateAtPosition(r, i); |
| 1389 |
break; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
assert(temp!=null); |
| 1393 |
final ArrayList<Read> out; |
| 1394 |
if(temp.get(0).containsUndefined()){ |
| 1395 |
out=new ArrayList<Read>(); |
| 1396 |
for(Read rr : temp){ |
| 1397 |
out.addAll(makeReplicates(rr)); |
| 1398 |
} |
| 1399 |
}else{ |
| 1400 |
out=temp; |
| 1401 |
} |
| 1402 |
return out; |
| 1403 |
} |
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
private static ArrayList<Read> replicateAtPosition(final Read r, final int pos) { |
| 1411 |
|
| 1412 |
if(r.quality!=null){ |
| 1413 |
r.quality[pos]=Shared.FAKE_QUAL; |
| 1414 |
} |
| 1415 |
final byte[] bases=r.bases; |
| 1416 |
final byte b=bases[pos]; |
| 1417 |
final int num=AminoAcid.baseToNumberExtended[b]&0xF; |
| 1418 |
assert(num>0 && Integer.bitCount(num)>1 && Integer.bitCount(num)<=4) : b+", "+num; |
| 1419 |
ArrayList<Read> out=new ArrayList<Read>(4); |
| 1420 |
for(int i=0; i<4; i++){ |
| 1421 |
int mask=(1<<i); |
| 1422 |
if((num&mask)==mask){ |
| 1423 |
Read rr=r.clone(); |
| 1424 |
rr.bases=rr.bases.clone(); |
| 1425 |
rr.bases[pos]=AminoAcid.numberToBase[i]; |
| 1426 |
|
| 1427 |
out.add(rr); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
return out; |
| 1431 |
} |
| 1432 |
|
| 1433 |
|
| 1434 |
public static final int locationOf(final byte[] big, final byte[] small, final int maxMismatches){ |
| 1435 |
int x=containsForward(big, small, maxMismatches); |
| 1436 |
return x>=0 ? x : containsReverse(big, small, maxMismatches); |
| 1437 |
} |
| 1438 |
|
| 1439 |
|
| 1440 |
public static final int containsForward(final byte[] big, final byte[] small, final int maxMismatches){ |
| 1441 |
final int ilimit=big.length-small.length; |
| 1442 |
|
| 1443 |
for(int i=0; i<=ilimit; i++){ |
| 1444 |
int mismatches=0; |
| 1445 |
for(int j=0; j<small.length && mismatches<=maxMismatches; j++){ |
| 1446 |
final byte b=big[i+j]; |
| 1447 |
final byte s=small[j]; |
| 1448 |
if(b!=s){mismatches++;} |
| 1449 |
} |
| 1450 |
if(mismatches<=maxMismatches){ |
| 1451 |
|
| 1452 |
return i; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
return -1; |
| 1456 |
} |
| 1457 |
|
| 1458 |
|
| 1459 |
public static final int containsReverse(final byte[] big, final byte[] small, final int maxMismatches){ |
| 1460 |
final int ilimit=big.length-small.length; |
| 1461 |
for(int i=0; i<=ilimit; i++){ |
| 1462 |
int mismatches=0; |
| 1463 |
for(int j=0, k=small.length-1; j<small.length && mismatches<=maxMismatches; j++, k--){ |
| 1464 |
final byte b=big[i+j]; |
| 1465 |
final byte s=AminoAcid.baseToComplementExtended[small[k]]; |
| 1466 |
if(b!=s){mismatches++;} |
| 1467 |
} |
| 1468 |
if(mismatches<=maxMismatches){return i;} |
| 1469 |
} |
| 1470 |
return -1; |
| 1471 |
} |
| 1472 |
|
| 1473 |
|
| 1474 |
public static final <X> int condense(ArrayList<X> list){ |
| 1475 |
if(list==null || list.size()==0){return 0;} |
| 1476 |
int removed=0; |
| 1477 |
|
| 1478 |
for(int i=list.size()-1; i>0; i--){ |
| 1479 |
if(list.get(i)==null){ |
| 1480 |
removed++; |
| 1481 |
X last=list.get(list.size()-1); |
| 1482 |
list.set(i, last); |
| 1483 |
list.remove(list.size()-1); |
| 1484 |
} |
| 1485 |
} |
| 1486 |
return removed; |
| 1487 |
} |
| 1488 |
|
| 1489 |
|
| 1490 |
public static final <X> int condenseStrict(ArrayList<X> list){ |
| 1491 |
if(list==null || list.size()==0){return 0;} |
| 1492 |
int removed=0; |
| 1493 |
|
| 1494 |
int insertPos=0; |
| 1495 |
for(int i=0; i<list.size(); i++){ |
| 1496 |
X x=list.get(i); |
| 1497 |
if(x!=null){ |
| 1498 |
if(insertPos!=i){ |
| 1499 |
assert(insertPos<i); |
| 1500 |
while(list.get(insertPos)!=null){insertPos++;} |
| 1501 |
assert(insertPos<i && list.get(insertPos)==null) : insertPos+", "+i; |
| 1502 |
list.set(i, null); |
| 1503 |
list.set(insertPos, x); |
| 1504 |
} |
| 1505 |
insertPos++; |
| 1506 |
}else{ |
| 1507 |
removed++; |
| 1508 |
} |
| 1509 |
} |
| 1510 |
for(int i=0; i<removed; i++){ |
| 1511 |
X x=list.remove(list.size()-1); |
| 1512 |
assert(x==null); |
| 1513 |
} |
| 1514 |
return removed; |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
public static final <X> X[] condenseStrict(X[] array){ |
| 1519 |
if(array==null){return array;} |
| 1520 |
int nulls=0; |
| 1521 |
for(X x : array){if(x==null){nulls++;}} |
| 1522 |
if(nulls==0){return array;} |
| 1523 |
X[] array2=Arrays.copyOf(array, array.length-nulls); |
| 1524 |
|
| 1525 |
int j=0; |
| 1526 |
for(X x : array){ |
| 1527 |
if(x!=null){ |
| 1528 |
array2[j]=x; |
| 1529 |
j++; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
return array2; |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
public static final <X> ArrayList<X> condenseNew(ArrayList<X> list){ |
| 1537 |
ArrayList<X> temp=new ArrayList<X>(list.size()); |
| 1538 |
for(X x : list){ |
| 1539 |
if(x!=null){temp.add(x);} |
| 1540 |
} |
| 1541 |
return temp; |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
public static final int trimSiteList(ArrayList<SiteScore> ssl, float fractionOfMax, boolean retainPaired, boolean retainSemiperfect, |
| 1590 |
int minSitesToRetain, int maxSitesToRetain){ |
| 1591 |
|
| 1592 |
if(ssl==null || ssl.size()==0){return -999999;} |
| 1593 |
if(ssl.size()==1){return ssl.get(0).score;} |
| 1594 |
int maxScore=-999999; |
| 1595 |
|
| 1596 |
if(minSitesToRetain>1 && minSitesToRetain<ssl.size()){ |
| 1597 |
assert(inOrder(ssl)); |
| 1598 |
maxScore=ssl.get(0).score; |
| 1599 |
}else{ |
| 1600 |
for(SiteScore ss : ssl){ |
| 1601 |
maxScore=Tools.max(maxScore, ss.score); |
| 1602 |
} |
| 1603 |
} |
| 1604 |
|
| 1605 |
int cutoff=(int) (maxScore*fractionOfMax); |
| 1606 |
trimSitesBelowCutoff(ssl, cutoff, retainPaired, retainSemiperfect, minSitesToRetain, maxSitesToRetain); |
| 1607 |
return maxScore; |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
public static final void trimSiteListByMax(ArrayList<SiteScore> ssl, int cutoff, boolean retainPaired, boolean retainSemiperfect, |
| 1612 |
int minSitesToRetain, int maxSitesToRetain){ |
| 1613 |
|
| 1614 |
if(ssl==null || ssl.size()==0){return;} |
| 1615 |
if(ssl.size()==1){return;} |
| 1616 |
|
| 1617 |
trimSitesBelowCutoff(ssl, cutoff, retainPaired, retainSemiperfect, minSitesToRetain, maxSitesToRetain); |
| 1618 |
} |
| 1619 |
|
| 1620 |
public static final <X extends Comparable<? super X>> boolean inOrder(ArrayList<X> list){ |
| 1621 |
if(list==null || list.size()<2){return true;} |
| 1622 |
for(int i=1; i<list.size(); i++){ |
| 1623 |
X xa=list.get(i-1); |
| 1624 |
X xb=list.get(i); |
| 1625 |
if(xa.compareTo(xb)>0){return false;} |
| 1626 |
} |
| 1627 |
return true; |
| 1628 |
} |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
public static final int mergeDuplicateSites(ArrayList<SiteScore> list, boolean doAssertions, boolean mergeDifferentGaps){ |
| 1633 |
if(list==null || list.size()<2){return 0;} |
| 1634 |
Shared.sort(list, SiteScore.PCOMP); |
| 1635 |
|
| 1636 |
int removed=0; |
| 1637 |
|
| 1638 |
SiteScore a=list.get(0); |
| 1639 |
for(int i=1; i<list.size(); i++){ |
| 1640 |
SiteScore b=list.get(i); |
| 1641 |
if(a.positionalMatch(b, true)){ |
| 1642 |
|
| 1643 |
if(doAssertions){ |
| 1644 |
if(!(a.perfect==b.perfect || |
| 1645 |
(a.perfect && (a.score>b.score || a.slowScore>b.slowScore)))){ |
| 1646 |
throw new RuntimeException("\n"+SiteScore.header()+"\n"+a.toText()+"\n"+b.toText()+"\n"); |
| 1647 |
} |
| 1648 |
|
| 1649 |
assert(a.perfect==b.perfect || |
| 1650 |
(a.perfect && (a.score>b.score || a.slowScore>b.slowScore))) : |
| 1651 |
"\n"+SiteScore.header()+"\n"+a.toText()+"\n"+b.toText()+"\n"; |
| 1652 |
} |
| 1653 |
|
| 1654 |
a.setSlowScore(max(a.slowScore, b.slowScore)); |
| 1655 |
|
| 1656 |
a.setPairedScore((a.pairedScore<=a.slowScore && b.pairedScore<=a.slowScore) ? 0 : max(0, a.pairedScore, b.pairedScore)); |
| 1657 |
a.setScore(max(a.score, b.score)); |
| 1658 |
a.perfect=(a.perfect || b.perfect); |
| 1659 |
a.semiperfect=(a.semiperfect || b.semiperfect); |
| 1660 |
|
| 1661 |
removed++; |
| 1662 |
list.set(i, null); |
| 1663 |
}else if(mergeDifferentGaps && a.positionalMatch(b, false)){ |
| 1664 |
|
| 1665 |
SiteScore better=null; |
| 1666 |
if(a.score!=b.score){ |
| 1667 |
better=(a.score>b.score ? a : b); |
| 1668 |
}else if(a.slowScore!=b.slowScore){ |
| 1669 |
better=(a.slowScore>b.slowScore ? a : b); |
| 1670 |
}else if(a.pairedScore!=b.pairedScore){ |
| 1671 |
better=(a.pairedScore>b.pairedScore ? a : b); |
| 1672 |
}else{ |
| 1673 |
better=a; |
| 1674 |
} |
| 1675 |
|
| 1676 |
a.setSlowScore(max(a.slowScore, b.slowScore)); |
| 1677 |
a.setPairedScore((a.pairedScore<=a.slowScore && b.pairedScore<=a.slowScore) ? 0 : max(0, a.pairedScore, b.pairedScore)); |
| 1678 |
a.setScore(max(a.score, b.score)); |
| 1679 |
a.perfect=(a.perfect || b.perfect); |
| 1680 |
a.semiperfect=(a.semiperfect || b.semiperfect); |
| 1681 |
a.gaps=better.gaps; |
| 1682 |
|
| 1683 |
removed++; |
| 1684 |
list.set(i, null); |
| 1685 |
} |
| 1686 |
else{ |
| 1687 |
a=b; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
|
| 1692 |
if(removed>0){condenseStrict(list);} |
| 1693 |
return removed; |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
public static final int subsumeOverlappingSites(ArrayList<SiteScore> list, boolean subsumeIfOnlyStartMatches, boolean subsumeInexact){ |
| 1699 |
if(list==null || list.size()<2){return 0;} |
| 1700 |
Shared.sort(list, SiteScore.PCOMP); |
| 1701 |
|
| 1702 |
int removed=0; |
| 1703 |
|
| 1704 |
|
| 1705 |
for(int i=0; i<list.size(); i++){ |
| 1706 |
SiteScore a=list.get(i); |
| 1707 |
|
| 1708 |
assert(a==null || !a.perfect || a.semiperfect); |
| 1709 |
|
| 1710 |
boolean overlappingA=true; |
| 1711 |
if(a!=null){ |
| 1712 |
for(int j=i+1; overlappingA && j<list.size(); j++){ |
| 1713 |
SiteScore b=list.get(j); |
| 1714 |
assert(b==null || !b.perfect || b.semiperfect); |
| 1715 |
if(b!=null){ |
| 1716 |
overlappingA=(a.chrom==b.chrom && b.start<a.stop && b.stop>a.start); |
| 1717 |
if(overlappingA && a.strand==b.strand){ |
| 1718 |
|
| 1719 |
SiteScore better=null; |
| 1720 |
if(a.perfect!=b.perfect){ |
| 1721 |
better=a.perfect ? a : b; |
| 1722 |
}else if(a.semiperfect!=b.semiperfect){ |
| 1723 |
better=a.semiperfect ? a : b; |
| 1724 |
}else if(a.score!=b.score){ |
| 1725 |
better=(a.score>b.score ? a : b); |
| 1726 |
}else if(a.slowScore!=b.slowScore){ |
| 1727 |
better=(a.slowScore>b.slowScore ? a : b); |
| 1728 |
}else if(a.pairedScore!=b.pairedScore){ |
| 1729 |
better=(a.pairedScore>b.pairedScore ? a : b); |
| 1730 |
}else if(a.pairedScore!=b.pairedScore){ |
| 1731 |
better=(a.quickScore>b.quickScore ? a : b); |
| 1732 |
}else{ |
| 1733 |
better=a; |
| 1734 |
} |
| 1735 |
|
| 1736 |
|
| 1737 |
if(a.semiperfect && b.semiperfect){ |
| 1738 |
if(a.start==b.start || a.stop==b.stop){ |
| 1739 |
list.set(i, better); |
| 1740 |
list.set(j, null); |
| 1741 |
removed++; |
| 1742 |
a=better; |
| 1743 |
}else{ |
| 1744 |
|
| 1745 |
} |
| 1746 |
}else if(a.perfect || b.perfect){ |
| 1747 |
list.set(i, better); |
| 1748 |
list.set(j, null); |
| 1749 |
removed++; |
| 1750 |
a=better; |
| 1751 |
}else if(a.semiperfect || b.semiperfect){ |
| 1752 |
if(a.start==b.start && a.stop==b.stop){ |
| 1753 |
list.set(i, better); |
| 1754 |
list.set(j, null); |
| 1755 |
removed++; |
| 1756 |
a=better; |
| 1757 |
}else{ |
| 1758 |
|
| 1759 |
} |
| 1760 |
}else if(subsumeInexact || (a.start==b.start && (subsumeIfOnlyStartMatches || a.stop==b.stop))){ |
| 1761 |
assert(!a.semiperfect && !a.perfect && !b.semiperfect && !b.perfect); |
| 1762 |
a.setLimits(min(a.start, b.start), max(a.stop, b.stop)); |
| 1763 |
a.setSlowScore(max(a.slowScore, b.slowScore)); |
| 1764 |
a.setPairedScore(a.pairedScore<=0 && b.pairedScore<=0 ? 0 : max(a.slowScore+1, a.pairedScore, b.pairedScore)); |
| 1765 |
a.quickScore=max(a.quickScore, b.quickScore); |
| 1766 |
a.setScore(max(a.score, b.score, a.pairedScore)); |
| 1767 |
a.gaps=better.gaps; |
| 1768 |
|
| 1769 |
list.set(j, null); |
| 1770 |
removed++; |
| 1771 |
} |
| 1772 |
} |
| 1773 |
} |
| 1774 |
} |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
if(removed>0){condenseStrict(list);} |
| 1780 |
return removed; |
| 1781 |
} |
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
public static final int removeOverlappingSites(ArrayList<SiteScore> list, boolean requireAMatchingEnd){ |
| 1786 |
if(list==null || list.size()<2){return 0;} |
| 1787 |
Shared.sort(list, SiteScore.PCOMP); |
| 1788 |
|
| 1789 |
int removed=0; |
| 1790 |
|
| 1791 |
|
| 1792 |
for(int i=0; i<list.size(); i++){ |
| 1793 |
SiteScore a=list.get(i); |
| 1794 |
boolean overlappingA=true; |
| 1795 |
if(a!=null){ |
| 1796 |
for(int j=i+1; overlappingA && j<list.size(); j++){ |
| 1797 |
SiteScore b=list.get(j); |
| 1798 |
if(b!=null){ |
| 1799 |
overlappingA=(a.chrom==b.chrom && b.start<a.stop && b.stop>a.start); |
| 1800 |
if(overlappingA && a.strand==b.strand){ |
| 1801 |
|
| 1802 |
SiteScore better=null; |
| 1803 |
if(a.perfect!=b.perfect){ |
| 1804 |
better=a.perfect ? a : b; |
| 1805 |
}else if(a.score!=b.score){ |
| 1806 |
better=(a.score>b.score ? a : b); |
| 1807 |
}else if(a.slowScore!=b.slowScore){ |
| 1808 |
better=(a.slowScore>b.slowScore ? a : b); |
| 1809 |
}else if(a.pairedScore!=b.pairedScore){ |
| 1810 |
better=(a.pairedScore>b.pairedScore ? a : b); |
| 1811 |
}else if(a.pairedScore!=b.pairedScore){ |
| 1812 |
better=(a.quickScore>b.quickScore ? a : b); |
| 1813 |
}else{ |
| 1814 |
better=a; |
| 1815 |
} |
| 1816 |
|
| 1817 |
if(a.start==b.start && a.stop==b.stop){ |
| 1818 |
list.set(i, better); |
| 1819 |
list.set(j, null); |
| 1820 |
a=better; |
| 1821 |
removed++; |
| 1822 |
}else if(a.start==b.start || a.stop==b.stop){ |
| 1823 |
list.set(i, better); |
| 1824 |
list.set(j, null); |
| 1825 |
a=better; |
| 1826 |
removed++; |
| 1827 |
}else if(!requireAMatchingEnd && a.score!=b.score){ |
| 1828 |
list.set(i, better); |
| 1829 |
list.set(j, null); |
| 1830 |
a=better; |
| 1831 |
removed++; |
| 1832 |
} |
| 1833 |
} |
| 1834 |
} |
| 1835 |
} |
| 1836 |
} |
| 1837 |
} |
| 1838 |
|
| 1839 |
|
| 1840 |
if(removed>0){condenseStrict(list);} |
| 1841 |
return removed; |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
public static final int countTopScores(ArrayList<SiteScore> list, int thresh){ |
| 1849 |
assert(thresh>=0) : thresh; |
| 1850 |
if(list==null || list.isEmpty()){return 0;} |
| 1851 |
int count=1; |
| 1852 |
final SiteScore ss=list.get(0); |
| 1853 |
final int limit=ss.score-thresh; |
| 1854 |
|
| 1855 |
for(int i=1; i<list.size(); i++){ |
| 1856 |
SiteScore ss2=list.get(i); |
| 1857 |
if(ss2.score<limit){break;} |
| 1858 |
if(ss.start!=ss2.start && ss.stop!=ss2.stop){ |
| 1859 |
count++; |
| 1860 |
} |
| 1861 |
} |
| 1862 |
return count; |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
public static final int removeLowQualitySitesPaired(ArrayList<SiteScore> list, int maxSwScore, float multSingle, float multPaired){ |
| 1870 |
if(list==null || list.size()==0){return 0;} |
| 1871 |
|
| 1872 |
assert(multSingle>=multPaired); |
| 1873 |
|
| 1874 |
int initialSize=list.size(); |
| 1875 |
final int swScoreThresh=(int)(maxSwScore*multSingle); |
| 1876 |
final int swScoreThreshPaired=(int)(maxSwScore*multPaired); |
| 1877 |
if(list.get(0).score<swScoreThreshPaired){list.clear(); return initialSize;} |
| 1878 |
|
| 1879 |
for(int i=list.size()-1; i>=0; i--){ |
| 1880 |
SiteScore ss=list.get(i); |
| 1881 |
assert(ss.score==ss.slowScore) : ss.quickScore+", "+ss.slowScore+", "+ss.pairedScore+", "+ss.score+"\n"+ss; |
| 1882 |
assert(i==0 || ss.slowScore<=list.get(i-1).slowScore) : "List is not sorted by singleton score!"; |
| 1883 |
if(ss.pairedScore>0){ |
| 1884 |
assert(ss.pairedScore>ss.quickScore || ss.pairedScore>ss.slowScore) : ss; |
| 1885 |
if(ss.slowScore<swScoreThreshPaired){list.remove(i);} |
| 1886 |
}else{ |
| 1887 |
assert(ss.pairedScore<=0) : ss.toText(); |
| 1888 |
if(ss.slowScore<swScoreThresh){list.remove(i);} |
| 1889 |
} |
| 1890 |
} |
| 1891 |
|
| 1892 |
return initialSize-list.size(); |
| 1893 |
} |
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
public static final int removeLowQualitySitesUnpaired(ArrayList<SiteScore> list, int thresh){ |
| 1922 |
if(list==null || list.size()==0){return 0;} |
| 1923 |
|
| 1924 |
int initialSize=list.size(); |
| 1925 |
if(list.get(0).score<thresh){list.clear(); return initialSize;} |
| 1926 |
|
| 1927 |
|
| 1928 |
for(int i=list.size()-1; i>1; i--){ |
| 1929 |
SiteScore ss=list.get(i); |
| 1930 |
assert(ss.score==ss.slowScore || (ss.score<=0 && ss.slowScore<=0)) : ss; |
| 1931 |
assert(i==0 || ss.slowScore<=list.get(i-1).slowScore) : "List is not sorted by singleton score!"; |
| 1932 |
assert(ss.pairedScore<=0) : ss.toText(); |
| 1933 |
if(ss.slowScore<thresh){list.remove(i);} |
| 1934 |
} |
| 1935 |
|
| 1936 |
return initialSize-list.size(); |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
public static final int removeLowQualitySitesPaired2(ArrayList<SiteScore> list, int maxSwScore, float multSingle, float multPaired, int expectedSites){ |
| 1944 |
if(list==null || list.size()==0){return 0;} |
| 1945 |
|
| 1946 |
assert(multSingle>=multPaired); |
| 1947 |
|
| 1948 |
int initialSize=list.size(); |
| 1949 |
final int swScoreThresh=(int)(maxSwScore*multSingle); |
| 1950 |
final int swScoreThreshPaired=(int)(maxSwScore*multPaired); |
| 1951 |
final int swScoreThresh2=(int)(maxSwScore*multSingle*1.2f); |
| 1952 |
final int swScoreThreshPaired2=(int)(maxSwScore*multPaired*1.1f); |
| 1953 |
if(list.get(0).score<swScoreThreshPaired){list.clear(); return initialSize;} |
| 1954 |
final int nthBest=list.get(Tools.min(list.size(), expectedSites)-1).score-maxSwScore/64; |
| 1955 |
|
| 1956 |
for(int i=list.size()-1, min=expectedSites*2; i>min; i--){ |
| 1957 |
if(list.get(i).slowScore>=nthBest){break;} |
| 1958 |
list.remove(i); |
| 1959 |
} |
| 1960 |
|
| 1961 |
for(int i=list.size()-1; i>=0; i--){ |
| 1962 |
SiteScore ss=list.get(i); |
| 1963 |
assert(ss.score==ss.slowScore); |
| 1964 |
assert(i==0 || ss.slowScore<=list.get(i-1).slowScore) : "List is not sorted by singleton score!"; |
| 1965 |
if(ss.pairedScore>0){ |
| 1966 |
int thresh=(i>=expectedSites ? swScoreThreshPaired2 : swScoreThreshPaired); |
| 1967 |
assert(ss.pairedScore>ss.quickScore || ss.pairedScore>ss.slowScore) : ss; |
| 1968 |
if(ss.slowScore<thresh){list.remove(i);} |
| 1969 |
}else{ |
| 1970 |
int thresh=(i>=expectedSites ? swScoreThresh2 : swScoreThresh); |
| 1971 |
|
| 1972 |
if(ss.slowScore<thresh){list.remove(i);} |
| 1973 |
} |
| 1974 |
} |
| 1975 |
|
| 1976 |
return initialSize-list.size(); |
| 1977 |
} |
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
public static final int removeLowQualitySitesUnpaired2(ArrayList<SiteScore> list, int maxSwScore, float multSingle, int expectedSites){ |
| 1986 |
if(list==null || list.size()==0){return 0;} |
| 1987 |
|
| 1988 |
for(int i=expectedSites/2; i<list.size(); i++){ |
| 1989 |
if(list.get(i).perfect){expectedSites++;} |
| 1990 |
} |
| 1991 |
|
| 1992 |
int initialSize=list.size(); |
| 1993 |
final int swScoreThresh=(int)(maxSwScore*multSingle); |
| 1994 |
final int swScoreThresh2=(int)(maxSwScore*multSingle*1.2f); |
| 1995 |
if(list.get(0).score<swScoreThresh){list.clear(); return initialSize;} |
| 1996 |
final int nthBest=list.get(Tools.min(list.size(), expectedSites)-1).score-maxSwScore/64; |
| 1997 |
|
| 1998 |
for(int i=list.size()-1, min=expectedSites*2; i>min; i--){ |
| 1999 |
if(list.get(i).slowScore>=nthBest){break;} |
| 2000 |
list.remove(i); |
| 2001 |
} |
| 2002 |
|
| 2003 |
|
| 2004 |
for(int i=list.size()-1; i>=1; i--){ |
| 2005 |
SiteScore ss=list.get(i); |
| 2006 |
assert(ss.score==ss.slowScore); |
| 2007 |
assert(i==0 || ss.slowScore<=list.get(i-1).slowScore) : "List is not sorted by singleton score!"; |
| 2008 |
assert(ss.pairedScore<=0) : ss.toText(); |
| 2009 |
int thresh=(i>=expectedSites ? swScoreThresh2 : swScoreThresh); |
| 2010 |
if(ss.slowScore<thresh){list.remove(i);} |
| 2011 |
} |
| 2012 |
|
| 2013 |
return initialSize-list.size(); |
| 2014 |
} |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
public static final void trimSitesBelowCutoff(ArrayList<SiteScore> ssl, int cutoff, boolean retainPaired, boolean retainSemiperfect, |
| 2049 |
int minSitesToRetain, int maxSitesToRetain){ |
| 2050 |
|
| 2051 |
assert(minSitesToRetain>=1); |
| 2052 |
assert(maxSitesToRetain>minSitesToRetain) : maxSitesToRetain+", "+minSitesToRetain+"\nError - maxsites2 must be greater than "+minSitesToRetain+"!"; |
| 2053 |
if(ssl==null || ssl.size()<=minSitesToRetain){return;} |
| 2054 |
while(ssl.size()>maxSitesToRetain){ssl.remove(ssl.size()-1);} |
| 2055 |
|
| 2056 |
int removed=0; |
| 2057 |
final int maxToRemove=ssl.size()-minSitesToRetain; |
| 2058 |
|
| 2059 |
assert(minSitesToRetain==1 || inOrder(ssl)); |
| 2060 |
|
| 2061 |
if(retainPaired){ |
| 2062 |
for(int i=ssl.size()-1; i>=0; i--){ |
| 2063 |
SiteScore ss=ssl.get(i); |
| 2064 |
if(!retainSemiperfect || !ss.semiperfect){ |
| 2065 |
if(ss.score<cutoff && ss.pairedScore<=0){ |
| 2066 |
ssl.set(i, null); |
| 2067 |
removed++; |
| 2068 |
if(removed>=maxToRemove){ |
| 2069 |
assert(removed==maxToRemove); |
| 2070 |
break; |
| 2071 |
} |
| 2072 |
} |
| 2073 |
} |
| 2074 |
} |
| 2075 |
}else{ |
| 2076 |
for(int i=ssl.size()-1; i>=0; i--){ |
| 2077 |
SiteScore ss=ssl.get(i); |
| 2078 |
if(!retainSemiperfect || !ss.semiperfect){ |
| 2079 |
if(ss.score<cutoff){ |
| 2080 |
ssl.set(i, null); |
| 2081 |
removed++; |
| 2082 |
if(removed>=maxToRemove){ |
| 2083 |
assert(removed==maxToRemove); |
| 2084 |
break; |
| 2085 |
} |
| 2086 |
} |
| 2087 |
} |
| 2088 |
} |
| 2089 |
} |
| 2090 |
|
| 2091 |
if(removed>0){ |
| 2092 |
condenseStrict(ssl); |
| 2093 |
} |
| 2094 |
assert(ssl.size()>=minSitesToRetain); |
| 2095 |
} |
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
public static CharSequence toStringSafe(byte[] array){ |
| 2115 |
if(array==null){return "null";} |
| 2116 |
StringBuilder sb=new StringBuilder(); |
| 2117 |
sb.append(Arrays.toString(array)); |
| 2118 |
if(array.length<1 || array[0]<32 || array[0]>126){return sb;} |
| 2119 |
sb.append('\n'); |
| 2120 |
for(int i=0; i<array.length; i++){ |
| 2121 |
byte b=array[i]; |
| 2122 |
if(b<32 || b>126){break;} |
| 2123 |
sb.append((char)b); |
| 2124 |
} |
| 2125 |
return sb; |
| 2126 |
} |
| 2127 |
|
| 2128 |
public static boolean equals(long[] a, long[] b){ |
| 2129 |
if(a==b){return true;} |
| 2130 |
if(a==null || b==null){return false;} |
| 2131 |
if(a.length!=b.length){return false;} |
| 2132 |
for(int i=0; i<a.length; i++){ |
| 2133 |
if(a[i]!=b[i]){return false;} |
| 2134 |
} |
| 2135 |
return true; |
| 2136 |
} |
| 2137 |
|
| 2138 |
public static boolean equals(int[] a, int[] b){ |
| 2139 |
if(a==b){return true;} |
| 2140 |
if(a==null || b==null){return false;} |
| 2141 |
if(a.length!=b.length){return false;} |
| 2142 |
for(int i=0; i<a.length; i++){ |
| 2143 |
if(a[i]!=b[i]){return false;} |
| 2144 |
} |
| 2145 |
return true; |
| 2146 |
} |
| 2147 |
|
| 2148 |
public static boolean equals(float[] a, float[] b){ |
| 2149 |
if(a==b){return true;} |
| 2150 |
if(a==null || b==null){return false;} |
| 2151 |
if(a.length!=b.length){return false;} |
| 2152 |
for(int i=0; i<a.length; i++){ |
| 2153 |
if(a[i]!=b[i]){return false;} |
| 2154 |
} |
| 2155 |
return true; |
| 2156 |
} |
| 2157 |
|
| 2158 |
public static boolean equals(byte[] a, byte[] b){ |
| 2159 |
if(a==b){return true;} |
| 2160 |
if(a==null || b==null){return false;} |
| 2161 |
if(a.length!=b.length){return false;} |
| 2162 |
for(int i=0; i<a.length; i++){ |
| 2163 |
if(a[i]!=b[i]){return false;} |
| 2164 |
} |
| 2165 |
return true; |
| 2166 |
} |
| 2167 |
|
| 2168 |
public static boolean equals(String a, byte[] b){ |
| 2169 |
if(a==null || b==null){ |
| 2170 |
return (a==null && b==null); |
| 2171 |
} |
| 2172 |
if(a.length()!=b.length){return false;} |
| 2173 |
for(int i=0; i<b.length; i++){ |
| 2174 |
if(a.charAt(i)!=b[i]){return false;} |
| 2175 |
} |
| 2176 |
return true; |
| 2177 |
} |
| 2178 |
|
| 2179 |
public static boolean equalsSubstring(String a, String b, int from, int toExclusive) { |
| 2180 |
if(a==null || b==null || a.length()<toExclusive || b.length()<toExclusive) {return false;} |
| 2181 |
boolean equal=true; |
| 2182 |
for(int i=from; i<toExclusive && equal; i++) { |
| 2183 |
equal=(a.charAt(i)==b.charAt(i)); |
| 2184 |
} |
| 2185 |
return equal; |
| 2186 |
} |
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
public static boolean contains(byte[] a, byte[] b, int start){ |
| 2195 |
if(a==null || b==null){ |
| 2196 |
return (a==null && b==null); |
| 2197 |
} |
| 2198 |
if(a.length<b.length+start){return false;} |
| 2199 |
for(int i=start, j=0; j<b.length; i++, j++){ |
| 2200 |
if(a[i]!=b[j]){return false;} |
| 2201 |
} |
| 2202 |
return true; |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
public static boolean contains(String a, String b, int start){ |
| 2212 |
if(a==null || b==null){ |
| 2213 |
return (a==null && b==null); |
| 2214 |
} |
| 2215 |
if(a.length()<b.length()+start){return false;} |
| 2216 |
for(int i=start, j=0; j<b.length(); i++, j++){ |
| 2217 |
if(a.charAt(i)!=b.charAt(j)){return false;} |
| 2218 |
} |
| 2219 |
return true; |
| 2220 |
} |
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
public static boolean startsWith(byte[] array, String s) { |
| 2228 |
return startsWith(array, s, 0); |
| 2229 |
} |
| 2230 |
|
| 2231 |
public static boolean equals(byte[] array, String s) { |
| 2232 |
return array.length==s.length() && startsWith(array, s, 0); |
| 2233 |
} |
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
public static boolean startsWith(byte[] array, byte[] s) { |
| 2241 |
return startsWith(array, s, 0); |
| 2242 |
} |
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
public static boolean endsWith(byte[] array, String s) { |
| 2250 |
if(s==null || array==null){return false;} |
| 2251 |
if(s.length()>array.length){return false;} |
| 2252 |
for(int i=s.length()-1, j=array.length-1; i>=0 && j>=0; i--, j--){ |
| 2253 |
if(s.charAt(i)!=array[j]){return false;} |
| 2254 |
} |
| 2255 |
return true; |
| 2256 |
} |
| 2257 |
|
| 2258 |
public static boolean endsWithLetter(String s) { |
| 2259 |
if(s==null || s.length()==0) {return false;} |
| 2260 |
return Character.isLetter(s.charAt(s.length()-1)); |
| 2261 |
} |
| 2262 |
|
| 2263 |
public static boolean endsWithLetter(byte[] s) { |
| 2264 |
if(s==null || s.length==0) {return false;} |
| 2265 |
return Character.isLetter(s[s.length-1]); |
| 2266 |
} |
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
public static boolean startsWith(String s, char b) { |
| 2274 |
return s!=null && s.length()>0 && s.charAt(0)==b; |
| 2275 |
} |
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
public static boolean startsWith(String s, byte b) { |
| 2283 |
return s!=null && s.length()>0 && s.charAt(0)==b; |
| 2284 |
} |
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
public static boolean startsWith(byte[] array, char b) { |
| 2292 |
return startsWith(array, (byte)b, 0); |
| 2293 |
} |
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
public static boolean startsWith(byte[] array, byte b) { |
| 2301 |
return startsWith(array, b, 0); |
| 2302 |
} |
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
public static boolean startsWith(byte[] array, byte b, int initialPos) { |
| 2310 |
if(array==null || array.length+initialPos<1){return false;} |
| 2311 |
return array[initialPos]==b; |
| 2312 |
} |
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
public static boolean startsWith(byte[] array, String s, int initialPos) { |
| 2320 |
if(array==null || s==null || array.length+initialPos<s.length()){return false;} |
| 2321 |
for(int i=initialPos; i<s.length(); i++){ |
| 2322 |
if(array[i]!=s.charAt(i)){return false;} |
| 2323 |
} |
| 2324 |
return true; |
| 2325 |
} |
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
public static boolean startsWith(byte[] array, byte[] s, int initialPos) { |
| 2333 |
if(array==null || s==null || array.length+initialPos<s.length){return false;} |
| 2334 |
for(int i=initialPos; i<s.length; i++){ |
| 2335 |
if(array[i]!=s[i]){return false;} |
| 2336 |
} |
| 2337 |
return true; |
| 2338 |
} |
| 2339 |
|
| 2340 |
public static int compare(byte[] a, byte[] b){ |
| 2341 |
if(a==b){return 0;} |
| 2342 |
if(a==null){return -1;} |
| 2343 |
if(b==null){return 1;} |
| 2344 |
int lim=min(a.length, b.length); |
| 2345 |
for(int i=0; i<lim; i++){ |
| 2346 |
if(a[i]!=b[i]){return a[i]-b[i];} |
| 2347 |
} |
| 2348 |
return a.length-b.length; |
| 2349 |
} |
| 2350 |
|
| 2351 |
public static void fill(long[][][] matrix, int x) { |
| 2352 |
for(long[][] sub : matrix){ |
| 2353 |
fill(sub, x); |
| 2354 |
} |
| 2355 |
} |
| 2356 |
|
| 2357 |
public static void fill(long[][] matrix, int x) { |
| 2358 |
for(long[] sub : matrix){ |
| 2359 |
Arrays.fill(sub, x); |
| 2360 |
} |
| 2361 |
} |
| 2362 |
|
| 2363 |
|
| 2364 |
public static int sumInt(byte[] array){ |
| 2365 |
long x=0; |
| 2366 |
for(byte y : array){x+=y;} |
| 2367 |
assert(x<=Integer.MAX_VALUE && x>=Integer.MIN_VALUE) : x; |
| 2368 |
return (int)x; |
| 2369 |
} |
| 2370 |
|
| 2371 |
public static void multiplyBy(int[] array, double mult) { |
| 2372 |
for(int i=0; i<array.length; i++){ |
| 2373 |
array[i]=(int)Math.round(array[i]*mult); |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
public static void multiplyBy(long[] array, double mult) { |
| 2378 |
for(int i=0; i<array.length; i++){ |
| 2379 |
array[i]=Math.round(array[i]*mult); |
| 2380 |
} |
| 2381 |
} |
| 2382 |
|
| 2383 |
public static void multiplyBy(long[][] matrix, double mult) { |
| 2384 |
for(long[] array : matrix){ |
| 2385 |
multiplyBy(array, mult); |
| 2386 |
} |
| 2387 |
} |
| 2388 |
|
| 2389 |
public static void multiplyBy(long[][][] matrix, double mult) { |
| 2390 |
for(long[][] array : matrix){ |
| 2391 |
multiplyBy(array, mult); |
| 2392 |
} |
| 2393 |
} |
| 2394 |
|
| 2395 |
public static void add(int[] array, int[] incr) { |
| 2396 |
for(int i=0; i<array.length; i++){ |
| 2397 |
array[i]+=incr[i]; |
| 2398 |
} |
| 2399 |
} |
| 2400 |
|
| 2401 |
public static void add(AtomicLongArray array, AtomicLongArray incr) { |
| 2402 |
for(int i=0; i<array.length(); i++){ |
| 2403 |
array.addAndGet(i, incr.get(i)); |
| 2404 |
} |
| 2405 |
} |
| 2406 |
|
| 2407 |
public static void add(long[] array, long[] incr) { |
| 2408 |
for(int i=0; i<array.length; i++){ |
| 2409 |
array[i]+=incr[i]; |
| 2410 |
} |
| 2411 |
} |
| 2412 |
|
| 2413 |
public static void add(long[][] array, long[][] incr) { |
| 2414 |
for(int i=0; i<array.length; i++){ |
| 2415 |
add(array[i], incr[i]); |
| 2416 |
} |
| 2417 |
} |
| 2418 |
|
| 2419 |
public static void add(long[][][] array, long[][][] incr) { |
| 2420 |
for(int i=0; i<array.length; i++){ |
| 2421 |
add(array[i], incr[i]); |
| 2422 |
} |
| 2423 |
} |
| 2424 |
|
| 2425 |
public static void add(double[] array, double[] incr) { |
| 2426 |
for(int i=0; i<array.length; i++){ |
| 2427 |
array[i]+=incr[i]; |
| 2428 |
} |
| 2429 |
} |
| 2430 |
|
| 2431 |
public static double sum(float[] array){ |
| 2432 |
return Vector.sum(array); |
| 2433 |
} |
| 2434 |
|
| 2435 |
public static long sum(byte[] array){ |
| 2436 |
return Vector.sum(array); |
| 2437 |
} |
| 2438 |
|
| 2439 |
public static long sum(char[] array){ |
| 2440 |
return Vector.sum(array); |
| 2441 |
} |
| 2442 |
|
| 2443 |
public static long sum(short[] array){ |
| 2444 |
return Vector.sum(array); |
| 2445 |
} |
| 2446 |
|
| 2447 |
public static long sum(int[] array){ |
| 2448 |
return Vector.sum(array); |
| 2449 |
} |
| 2450 |
|
| 2451 |
public static double sum(double[] array){ |
| 2452 |
return Vector.sum(array); |
| 2453 |
} |
| 2454 |
|
| 2455 |
public static long sum(long[] array){ |
| 2456 |
return Vector.sum(array); |
| 2457 |
} |
| 2458 |
|
| 2459 |
public static long sum(int[] array, int from, int to){ |
| 2460 |
return Vector.sum(array, from, to); |
| 2461 |
} |
| 2462 |
|
| 2463 |
public static long sum(long[] array, int from, int to){ |
| 2464 |
return Vector.sum(array, from, to); |
| 2465 |
} |
| 2466 |
|
| 2467 |
public static long sum(AtomicIntegerArray array){ |
| 2468 |
long x=0; |
| 2469 |
for(int i=0; i<array.length(); i++){x+=array.get(i);} |
| 2470 |
return x; |
| 2471 |
} |
| 2472 |
|
| 2473 |
public static long sum(AtomicLongArray array){ |
| 2474 |
long x=0; |
| 2475 |
for(int i=0; i<array.length(); i++){x+=array.get(i);} |
| 2476 |
return x; |
| 2477 |
} |
| 2478 |
|
| 2479 |
public static double mean(int[] array){ |
| 2480 |
return Vector.sum(array)/(double)array.length; |
| 2481 |
} |
| 2482 |
|
| 2483 |
public static double mean(long[] array){ |
| 2484 |
return Vector.sum(array)/(double)array.length; |
| 2485 |
} |
| 2486 |
|
| 2487 |
public static int cardinality(short[] array){ |
| 2488 |
int x=0; |
| 2489 |
for(int y : array){if(y!=0){x++;}} |
| 2490 |
return x; |
| 2491 |
} |
| 2492 |
|
| 2493 |
public static double harmonicMean(int[] array){ |
| 2494 |
double sum=0; |
| 2495 |
for(int x : array){ |
| 2496 |
if(x>0){sum+=1.0/x;} |
| 2497 |
} |
| 2498 |
return array.length/sum; |
| 2499 |
} |
| 2500 |
|
| 2501 |
public static int cardinality(int[] array){ |
| 2502 |
int x=0; |
| 2503 |
for(int y : array){if(y!=0){x++;}} |
| 2504 |
return x; |
| 2505 |
} |
| 2506 |
|
| 2507 |
public static double weightedAverage(long[] array){ |
| 2508 |
if(array.length<2){ |
| 2509 |
return array.length==1 ? array[0] : 0; |
| 2510 |
} |
| 2511 |
double wsum=0; |
| 2512 |
long div=0; |
| 2513 |
final int mid=array.length/2; |
| 2514 |
for(int i=0; i<mid; i++){ |
| 2515 |
wsum+=(i+1)*(array[i]+array[array.length-i-1]); |
| 2516 |
div+=(i+1)*2; |
| 2517 |
} |
| 2518 |
if((array.length&1)==1){ |
| 2519 |
wsum+=(mid+1)*array[mid]; |
| 2520 |
div+=(mid+1); |
| 2521 |
} |
| 2522 |
return wsum/div; |
| 2523 |
} |
| 2524 |
|
| 2525 |
public static long sumHistogram(long[] array){ |
| 2526 |
long x=0; |
| 2527 |
for(int i=1; i<array.length; i++){ |
| 2528 |
x+=(i*array[i]); |
| 2529 |
} |
| 2530 |
return x; |
| 2531 |
} |
| 2532 |
|
| 2533 |
public static long minHistogram(long[] array){ |
| 2534 |
for(int i=0; i<array.length; i++){ |
| 2535 |
if(array[i]>0){return i;} |
| 2536 |
} |
| 2537 |
return 0; |
| 2538 |
} |
| 2539 |
|
| 2540 |
public static long maxHistogram(long[] array){ |
| 2541 |
for(int i=array.length-1; i>=0; i--){ |
| 2542 |
if(array[i]>0){return i;} |
| 2543 |
} |
| 2544 |
return 0; |
| 2545 |
} |
| 2546 |
|
| 2547 |
public static long[] toArray(AtomicLongArray array){ |
| 2548 |
long[] x=new long[array.length()]; |
| 2549 |
for(int i=0; i<array.length(); i++){x[i]=array.get(i);} |
| 2550 |
return x; |
| 2551 |
} |
| 2552 |
|
| 2553 |
public static long[] toArray(CoverageArray array){ |
| 2554 |
long[] x=new long[array.maxIndex+1]; |
| 2555 |
for(int i=0; i<=array.maxIndex; i++){x[i]=array.get(i);} |
| 2556 |
return x; |
| 2557 |
} |
| 2558 |
|
| 2559 |
public static int min(int[] array){ |
| 2560 |
int min=Integer.MAX_VALUE; |
| 2561 |
for(int y : array){if(y<min){min=y;}} |
| 2562 |
return min; |
| 2563 |
} |
| 2564 |
|
| 2565 |
public static byte min(byte[] array){ |
| 2566 |
byte min=Byte.MAX_VALUE; |
| 2567 |
for(byte y : array){if(y<min){min=y;}} |
| 2568 |
return min; |
| 2569 |
} |
| 2570 |
|
| 2571 |
public static int intSum(int[] array){ |
| 2572 |
int x=0; |
| 2573 |
for(int y : array){x+=y;} |
| 2574 |
return x; |
| 2575 |
} |
| 2576 |
|
| 2577 |
public static void reverseInPlace(final byte[] array){ |
| 2578 |
if(array==null){return;} |
| 2579 |
final int max=array.length/2, last=array.length-1; |
| 2580 |
for(int i=0; i<max; i++){ |
| 2581 |
byte temp=array[i]; |
| 2582 |
array[i]=array[last-i]; |
| 2583 |
array[last-i]=temp; |
| 2584 |
} |
| 2585 |
} |
| 2586 |
|
| 2587 |
public static void reverseInPlace(final char[] array){ |
| 2588 |
if(array==null){return;} |
| 2589 |
final int max=array.length/2, last=array.length-1; |
| 2590 |
for(int i=0; i<max; i++){ |
| 2591 |
char temp=array[i]; |
| 2592 |
array[i]=array[last-i]; |
| 2593 |
array[last-i]=temp; |
| 2594 |
} |
| 2595 |
} |
| 2596 |
|
| 2597 |
public static void reverseInPlace(final int[] array){ |
| 2598 |
if(array==null){return;} |
| 2599 |
reverseInPlace(array, 0, array.length); |
| 2600 |
} |
| 2601 |
|
| 2602 |
public static void reverseInPlace(final long[] array){ |
| 2603 |
if(array==null){return;} |
| 2604 |
reverseInPlace(array, 0, array.length); |
| 2605 |
} |
| 2606 |
|
| 2607 |
public static void reverseInPlace(final float[] array){ |
| 2608 |
if(array==null){return;} |
| 2609 |
reverseInPlace(array, 0, array.length); |
| 2610 |
} |
| 2611 |
|
| 2612 |
public static void reverseInPlace(final AtomicIntegerArray array){ |
| 2613 |
if(array==null){return;} |
| 2614 |
reverseInPlace(array, 0, array.length()); |
| 2615 |
} |
| 2616 |
|
| 2617 |
public static <X> void reverseInPlace(final X[] array){ |
| 2618 |
if(array==null){return;} |
| 2619 |
reverseInPlace(array, 0, array.length); |
| 2620 |
} |
| 2621 |
|
| 2622 |
public static <X> void reverseInPlace(final X[] array, final int from, final int to){ |
| 2623 |
if(array==null){return;} |
| 2624 |
for(int i=from, j=to-1; i<j; i++, j--){ |
| 2625 |
X temp=array[i]; |
| 2626 |
array[i]=array[j]; |
| 2627 |
array[j]=temp; |
| 2628 |
} |
| 2629 |
} |
| 2630 |
|
| 2631 |
public static void reverseInPlace(final byte[] array, final int from, final int to){ |
| 2632 |
if(array==null){return;} |
| 2633 |
for(int i=from, j=to-1; i<j; i++, j--){ |
| 2634 |
byte temp=array[i]; |
| 2635 |
array[i]=array[j]; |
| 2636 |
array[j]=temp; |
| 2637 |
} |
| 2638 |
} |
| 2639 |
|
| 2640 |
public static void reverseInPlace(final int[] array, final int from, final int to){ |
| 2641 |
if(array==null){return;} |
| 2642 |
for(int i=from, j=to-1; i<j; i++, j--){ |
| 2643 |
int temp=array[i]; |
| 2644 |
array[i]=array[j]; |
| 2645 |
array[j]=temp; |
| 2646 |
} |
| 2647 |
} |
| 2648 |
|
| 2649 |
public static void reverseInPlace(final long[] array, final int from, final int to){ |
| 2650 |
if(array==null){return;} |
| 2651 |
for(int i=from, j=to-1; i<j; i++, j--){ |
| 2652 |
long temp=array[i]; |
| 2653 |
array[i]=array[j]; |
| 2654 |
array[j]=temp; |
| 2655 |
} |
| 2656 |
} |
| 2657 |
|
| 2658 |
public static void reverseInPlace(final float[] array, final int from, final int to){ |
| 2659 |
if(array==null){return;} |
| 2660 |
for(int i=from, j=to-1; i<j; i++, j--){ |
| 2661 |
float temp=array[i]; |
| 2662 |
array[i]=array[j]; |
| 2663 |
array[j]=temp; |
| 2664 |
} |
| 2665 |
} |
| 2666 |
|
| 2667 |
public static void reverseInPlace(final double[] array, final int from, final int to){ |
| 2668 |
if(array==null){return;} |
| 2669 |
for(int i=from, j=to-1; i<j; i++, j--){ |
| 2670 |
double temp=array[i]; |
| 2671 |
array[i]=array[j]; |
| 2672 |
array[j]=temp; |
| 2673 |
} |
| 2674 |
} |
| 2675 |
|
| 2676 |
public static void reverseInPlace(final AtomicIntegerArray array, final int from, final int to){ |
| 2677 |
if(array==null){return;} |
| 2678 |
for(int i=from, j=to-1; i<j; i++, j--){ |
| 2679 |
int temp=array.get(i); |
| 2680 |
array.set(i, array.get(j)); |
| 2681 |
array.set(j, temp); |
| 2682 |
} |
| 2683 |
} |
| 2684 |
|
| 2685 |
public static byte[] reverseAndCopy(final byte[] array){ |
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
return reverseAndCopy(array, null); |
| 2691 |
} |
| 2692 |
|
| 2693 |
public static char[] reverseAndCopy(final char[] array){ |
| 2694 |
return reverseAndCopy(array, null); |
| 2695 |
} |
| 2696 |
|
| 2697 |
public static int[] reverseAndCopy(final int[] array){ |
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
return reverseAndCopy(array, null); |
| 2703 |
} |
| 2704 |
|
| 2705 |
public static void copy(String s, byte[] bs) { |
| 2706 |
for(int i=0; i<s.length(); i++) { |
| 2707 |
bs[i]=(byte)s.charAt(i); |
| 2708 |
} |
| 2709 |
} |
| 2710 |
|
| 2711 |
public static byte[] reverseAndCopy(final byte[] array, byte[] out){ |
| 2712 |
if(array==null){ |
| 2713 |
assert(out==null); |
| 2714 |
return null; |
| 2715 |
} |
| 2716 |
if(out==null){out=new byte[array.length];} |
| 2717 |
assert(array.length==out.length && array!=out); |
| 2718 |
for(int i=0, last=array.length-1; i<array.length; i++){out[i]=array[last-i];} |
| 2719 |
return out; |
| 2720 |
} |
| 2721 |
|
| 2722 |
public static char[] reverseAndCopy(final char[] array, char[] out){ |
| 2723 |
if(array==null){ |
| 2724 |
assert(out==null); |
| 2725 |
return null; |
| 2726 |
} |
| 2727 |
if(out==null){out=new char[array.length];} |
| 2728 |
assert(array.length==out.length && array!=out); |
| 2729 |
for(int i=0, last=array.length-1; i<array.length; i++){out[i]=array[last-i];} |
| 2730 |
return out; |
| 2731 |
} |
| 2732 |
|
| 2733 |
public static int[] reverseAndCopy(final int[] array, int[] out){ |
| 2734 |
if(array==null){ |
| 2735 |
assert(out==null); |
| 2736 |
return null; |
| 2737 |
} |
| 2738 |
if(out==null){out=new int[array.length];} |
| 2739 |
assert(array.length==out.length && array!=out); |
| 2740 |
for(int i=0, last=array.length-1; i<array.length; i++){out[i]=array[last-i];} |
| 2741 |
return out; |
| 2742 |
} |
| 2743 |
|
| 2744 |
public static void cullHighFreqEntries(int[][] data, float fractionToExclude){ |
| 2745 |
if(fractionToExclude<=0){return;} |
| 2746 |
int[] count=new int[data.length]; |
| 2747 |
|
| 2748 |
long numBases=0; |
| 2749 |
|
| 2750 |
for(int i=0; i<data.length; i++){ |
| 2751 |
count[i]=(data[i]==null ? 0 : data[i].length); |
| 2752 |
numBases+=count[i]; |
| 2753 |
} |
| 2754 |
|
| 2755 |
int numIndicesToRemove=((int)(numBases*fractionToExclude)); |
| 2756 |
|
| 2757 |
Arrays.sort(count); |
| 2758 |
|
| 2759 |
for(int i=1; i<count.length; i++){ |
| 2760 |
assert(count[i]>=count[i-1]) : "\n\ncount["+i+"]="+count[i]+"\ncount["+(i-1)+"]="+count[i-1]+"\n"; |
| 2761 |
} |
| 2762 |
|
| 2763 |
int pos=count.length-1; |
| 2764 |
for(int sum=0; pos>1 && sum<numIndicesToRemove; pos--){ |
| 2765 |
sum+=count[pos]; |
| 2766 |
} |
| 2767 |
int maxLengthToKeep2=count[pos]; |
| 2768 |
|
| 2769 |
for(int i=0; i<data.length; i++){ |
| 2770 |
if(data[i]!=null && data[i].length>maxLengthToKeep2){data[i]=null;} |
| 2771 |
} |
| 2772 |
} |
| 2773 |
|
| 2774 |
public static int findLimitForHighFreqEntries(int[][] data, float fractionToExclude){ |
| 2775 |
if(fractionToExclude<=0){return Integer.MAX_VALUE;} |
| 2776 |
int[] count=new int[data.length]; |
| 2777 |
|
| 2778 |
long numBases=0; |
| 2779 |
|
| 2780 |
for(int i=0; i<data.length; i++){ |
| 2781 |
count[i]=(data[i]==null ? 0 : data[i].length); |
| 2782 |
numBases+=count[i]; |
| 2783 |
} |
| 2784 |
|
| 2785 |
int numIndicesToRemove=((int)(numBases*fractionToExclude)); |
| 2786 |
|
| 2787 |
Arrays.sort(count); |
| 2788 |
|
| 2789 |
for(int i=1; i<count.length; i++){ |
| 2790 |
assert(count[i]>=count[i-1]) : "\n\ncount["+i+"]="+count[i]+"\ncount["+(i-1)+"]="+count[i-1]+"\n"; |
| 2791 |
} |
| 2792 |
|
| 2793 |
int pos=count.length-1; |
| 2794 |
for(int sum=0; pos>1 && sum<numIndicesToRemove; pos--){ |
| 2795 |
sum+=count[pos]; |
| 2796 |
} |
| 2797 |
int maxLengthToKeep2=count[pos]; |
| 2798 |
|
| 2799 |
return maxLengthToKeep2; |
| 2800 |
} |
| 2801 |
|
| 2802 |
public static void cullClumpyEntries(final int[][] data, final int maxDist, final int minLength, final float fraction){ |
| 2803 |
|
| 2804 |
long total=0; |
| 2805 |
long removedSites=0; |
| 2806 |
long removedKeys=0; |
| 2807 |
|
| 2808 |
if(maxDist<=0){return;} |
| 2809 |
for(int i=0; i<data.length; i++){ |
| 2810 |
int[] array=data[i]; |
| 2811 |
total+=(array==null ? 0 : array.length); |
| 2812 |
if(array!=null && array.length>=minLength){ |
| 2813 |
if(isClumpy(array, maxDist, fraction)){ |
| 2814 |
removedSites+=array.length; |
| 2815 |
removedKeys++; |
| 2816 |
data[i]=null; |
| 2817 |
} |
| 2818 |
} |
| 2819 |
} |
| 2820 |
|
| 2821 |
|
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
|
| 2826 |
} |
| 2827 |
|
| 2828 |
public static HashSet<Integer> banClumpyEntries(final int[][] data, final int maxDist, final int minLength, final float fraction){ |
| 2829 |
|
| 2830 |
HashSet<Integer> set=new HashSet<Integer>(128); |
| 2831 |
|
| 2832 |
long total=0; |
| 2833 |
long removedSites=0; |
| 2834 |
long removedKeys=0; |
| 2835 |
|
| 2836 |
if(maxDist<=0){return set;} |
| 2837 |
|
| 2838 |
for(int i=0; i<data.length; i++){ |
| 2839 |
int[] array=data[i]; |
| 2840 |
total+=(array==null ? 0 : array.length); |
| 2841 |
if(array!=null && array.length>=minLength){ |
| 2842 |
if(isClumpy(array, maxDist, fraction)){ |
| 2843 |
removedSites+=array.length; |
| 2844 |
removedKeys++; |
| 2845 |
set.add(i); |
| 2846 |
} |
| 2847 |
} |
| 2848 |
} |
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
return set; |
| 2856 |
|
| 2857 |
} |
| 2858 |
|
| 2859 |
public static final boolean isClumpy(final int[] array, final int maxDist, final float fraction){ |
| 2860 |
if(array==null){return false;} |
| 2861 |
int count=0; |
| 2862 |
for(int i=1; i<array.length; i++){ |
| 2863 |
int dif=array[i]-array[i-1]; |
| 2864 |
if(dif<=maxDist){count++;} |
| 2865 |
} |
| 2866 |
return count>=(array.length*fraction); |
| 2867 |
} |
| 2868 |
|
| 2869 |
public static int[] makeLengthHistogram(int[][] x, int buckets) { |
| 2870 |
int[] lengths=new int[x.length]; |
| 2871 |
long total=0; |
| 2872 |
for(int i=0; i<x.length; i++){ |
| 2873 |
int[] list=x[i]; |
| 2874 |
if(list!=null){ |
| 2875 |
lengths[i]=list.length; |
| 2876 |
total+=list.length; |
| 2877 |
} |
| 2878 |
} |
| 2879 |
Arrays.sort(lengths); |
| 2880 |
|
| 2881 |
int[] hist=new int[buckets+1]; |
| 2882 |
|
| 2883 |
long sum=0; |
| 2884 |
int ptr=0; |
| 2885 |
for(int i=0; i<buckets; i++){ |
| 2886 |
long nextLimit=((total*i)+buckets/2)/buckets; |
| 2887 |
while(ptr<lengths.length && sum<nextLimit){ |
| 2888 |
sum+=lengths[ptr]; |
| 2889 |
ptr++; |
| 2890 |
} |
| 2891 |
|
| 2892 |
hist[i]=lengths[Tools.max(0, ptr-1)]; |
| 2893 |
} |
| 2894 |
hist[hist.length-1]=lengths[lengths.length-1]; |
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
return hist; |
| 2899 |
} |
| 2900 |
|
| 2901 |
public static String toKMG(long x){ |
| 2902 |
double div=1; |
| 2903 |
String ext=""; |
| 2904 |
if(x>10000000000000L){ |
| 2905 |
div=1000000000000L; |
| 2906 |
ext="T"; |
| 2907 |
}else if(x>10000000000L){ |
| 2908 |
div=1000000000L; |
| 2909 |
ext="B"; |
| 2910 |
}else if(x>10000000){ |
| 2911 |
div=1000000; |
| 2912 |
ext="M"; |
| 2913 |
}else if(x>100000){ |
| 2914 |
div=1000; |
| 2915 |
ext="K"; |
| 2916 |
} |
| 2917 |
return Tools.format("%.2f", x/div)+ext; |
| 2918 |
} |
| 2919 |
|
| 2920 |
|
| 2921 |
public static int remapAndCount(byte[] remap, byte[] array) { |
| 2922 |
if(array==null){return 0;} |
| 2923 |
assert(remap!=null); |
| 2924 |
int swaps=0; |
| 2925 |
for(int i=0; i<array.length; i++){ |
| 2926 |
byte a=array[i]; |
| 2927 |
byte b=remap[a]; |
| 2928 |
if(a!=b){ |
| 2929 |
array[i]=b; |
| 2930 |
swaps++; |
| 2931 |
} |
| 2932 |
} |
| 2933 |
return swaps; |
| 2934 |
} |
| 2935 |
|
| 2936 |
|
| 2937 |
public static String remap(byte[] remap, String in) { |
| 2938 |
if(in==null){return in;} |
| 2939 |
byte[] array=in.getBytes(); |
| 2940 |
int x=remapAndCount(remap, array); |
| 2941 |
return (x>0 ? new String(array) : in); |
| 2942 |
} |
| 2943 |
|
| 2944 |
public static int[] makeHistogram(AtomicLongArray data, int buckets) { |
| 2945 |
long total=sum(data); |
| 2946 |
long increment=total/(buckets+1); |
| 2947 |
|
| 2948 |
int[] hist=new int[buckets+1]; |
| 2949 |
|
| 2950 |
long sum=0; |
| 2951 |
long target=increment/2; |
| 2952 |
int ptr=0; |
| 2953 |
for(int i=0; i<hist.length; i++){ |
| 2954 |
while(ptr<data.length() && sum<target){ |
| 2955 |
sum+=data.get(ptr); |
| 2956 |
ptr++; |
| 2957 |
} |
| 2958 |
hist[i]=ptr; |
| 2959 |
target+=increment; |
| 2960 |
} |
| 2961 |
return hist; |
| 2962 |
} |
| 2963 |
|
| 2964 |
|
| 2965 |
public static int[] makeLengthHistogram2(int[] x, int buckets, boolean verbose) { |
| 2966 |
int[] lengths=KillSwitch.copyOf(x, x.length); |
| 2967 |
long total=sum(x); |
| 2968 |
Shared.sort(lengths); |
| 2969 |
|
| 2970 |
if(verbose){ |
| 2971 |
System.out.println("Length array size:\t"+x.length); |
| 2972 |
System.out.println("Min value: \t"+lengths[0]); |
| 2973 |
System.out.println("Med value: \t"+lengths[lengths.length/2]); |
| 2974 |
System.out.println("Max value: \t"+lengths[lengths.length-1]); |
| 2975 |
System.out.println("Total: \t"+total); |
| 2976 |
} |
| 2977 |
|
| 2978 |
int[] hist=new int[buckets+1]; |
| 2979 |
|
| 2980 |
long sum=0; |
| 2981 |
int ptr=0; |
| 2982 |
for(int i=0; i<buckets; i++){ |
| 2983 |
long nextLimit=((total*i)+buckets/2)/buckets; |
| 2984 |
while(ptr<lengths.length && sum<nextLimit){ |
| 2985 |
sum+=lengths[ptr]; |
| 2986 |
ptr++; |
| 2987 |
} |
| 2988 |
|
| 2989 |
hist[i]=lengths[Tools.max(0, ptr-1)]; |
| 2990 |
} |
| 2991 |
hist[hist.length-1]=lengths[lengths.length-1]; |
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
return hist; |
| 2996 |
} |
| 2997 |
|
| 2998 |
public static int[] makeLengthHistogram3(int[] x, int buckets, boolean verbose) { |
| 2999 |
int max=max(x); |
| 3000 |
if(max>x.length){ |
| 3001 |
Data.sysout.println("Reverted to old histogram mode."); |
| 3002 |
return makeLengthHistogram2(x, buckets, verbose); |
| 3003 |
} |
| 3004 |
|
| 3005 |
int[] counts=new int[max+1]; |
| 3006 |
long total=0; |
| 3007 |
for(int i=0; i<x.length; i++){ |
| 3008 |
int a=x[i]; |
| 3009 |
if(a>=0){ |
| 3010 |
counts[a]++; |
| 3011 |
total+=a; |
| 3012 |
} |
| 3013 |
} |
| 3014 |
|
| 3015 |
return makeLengthHistogram4(counts, buckets, total, verbose); |
| 3016 |
} |
| 3017 |
|
| 3018 |
|
| 3019 |
public static int[] makeLengthHistogram4(int[] counts, int buckets, long total, boolean verbose) { |
| 3020 |
if(total<=0){ |
| 3021 |
total=0; |
| 3022 |
for(int i=1; i<counts.length; i++){ |
| 3023 |
total+=(i*counts[i]); |
| 3024 |
} |
| 3025 |
} |
| 3026 |
|
| 3027 |
if(verbose){ |
| 3028 |
|
| 3029 |
|
| 3030 |
|
| 3031 |
|
| 3032 |
System.err.println("Total: \t"+total); |
| 3033 |
} |
| 3034 |
|
| 3035 |
int[] hist=new int[buckets+1]; |
| 3036 |
|
| 3037 |
long sum=0; |
| 3038 |
int ptr=0; |
| 3039 |
for(int i=0; i<buckets; i++){ |
| 3040 |
long nextLimit=((total*i)+buckets/2)/buckets; |
| 3041 |
while(ptr<counts.length && sum<nextLimit){ |
| 3042 |
sum+=counts[ptr]*ptr; |
| 3043 |
ptr++; |
| 3044 |
} |
| 3045 |
|
| 3046 |
hist[i]=Tools.max(0, ptr-1); |
| 3047 |
} |
| 3048 |
hist[hist.length-1]=counts.length-1; |
| 3049 |
|
| 3050 |
|
| 3051 |
|
| 3052 |
return hist; |
| 3053 |
} |
| 3054 |
|
| 3055 |
|
| 3056 |
|
| 3057 |
|
| 3058 |
|
| 3059 |
public static int averageInt(short[] array) { |
| 3060 |
return (int)(array==null || array.length==0 ? 0 : sum(array)/array.length); |
| 3061 |
} |
| 3062 |
|
| 3063 |
|
| 3064 |
|
| 3065 |
|
| 3066 |
|
| 3067 |
public static int averageInt(int[] array) { |
| 3068 |
return (int)(array==null || array.length==0 ? 0 : sum(array)/array.length); |
| 3069 |
} |
| 3070 |
|
| 3071 |
public static double averageDouble(int[] array) { |
| 3072 |
return (array==null || array.length==0 ? 0 : sum(array)/(double)array.length); |
| 3073 |
} |
| 3074 |
|
| 3075 |
public static double averageDouble(float[] array) { |
| 3076 |
return (array==null || array.length==0 ? 0 : sum(array)/(double)array.length); |
| 3077 |
} |
| 3078 |
|
| 3079 |
|
| 3080 |
public static int medianHistogram(int[] array){return percentileHistogram(array, .5);} |
| 3081 |
|
| 3082 |
|
| 3083 |
public static int medianHistogram(long[] array){return percentileHistogram(array, .5);} |
| 3084 |
|
| 3085 |
|
| 3086 |
public static int percentileHistogram(int[] array, double fraction){ |
| 3087 |
if(array==null || array.length<1){return 0;} |
| 3088 |
long target=(long)(sum(array)*fraction); |
| 3089 |
long sum=0; |
| 3090 |
for(int i=0; i<array.length; i++){ |
| 3091 |
sum+=array[i]; |
| 3092 |
if(sum>=target){ |
| 3093 |
return i; |
| 3094 |
} |
| 3095 |
} |
| 3096 |
return array.length-1; |
| 3097 |
} |
| 3098 |
|
| 3099 |
|
| 3100 |
public static int percentileHistogram(long[] array, double fraction){ |
| 3101 |
if(array==null || array.length<1){return 0;} |
| 3102 |
long target=(long)(sum(array)*fraction); |
| 3103 |
long sum=0; |
| 3104 |
for(int i=0; i<array.length; i++){ |
| 3105 |
sum+=array[i]; |
| 3106 |
if(sum>=target){ |
| 3107 |
return i; |
| 3108 |
} |
| 3109 |
} |
| 3110 |
return array.length-1; |
| 3111 |
} |
| 3112 |
|
| 3113 |
public static int calcModeHistogram(long array[]){ |
| 3114 |
if(array==null || array.length<1){return 0;} |
| 3115 |
int median=percentileHistogram(array, 0.5); |
| 3116 |
int mode=0; |
| 3117 |
long modeCount=array[mode]; |
| 3118 |
for(int i=1; i<array.length; i++){ |
| 3119 |
long count=array[i]; |
| 3120 |
if(count>modeCount || (count==modeCount && absdif(i, median)<absdif(mode, median))){ |
| 3121 |
mode=i; |
| 3122 |
modeCount=count; |
| 3123 |
} |
| 3124 |
} |
| 3125 |
return mode; |
| 3126 |
} |
| 3127 |
|
| 3128 |
public static final int absdif(int a, int b) { |
| 3129 |
|
| 3130 |
return Math.abs(a-b); |
| 3131 |
} |
| 3132 |
|
| 3133 |
public static final float absdif(float a, float b) { |
| 3134 |
|
| 3135 |
return Math.abs(a-b); |
| 3136 |
} |
| 3137 |
|
| 3138 |
public static final double absdif(double a, double b) { |
| 3139 |
|
| 3140 |
return Math.abs(a-b); |
| 3141 |
} |
| 3142 |
|
| 3143 |
|
| 3144 |
public static final int absdifUnsigned(int a, int b){ |
| 3145 |
return (a<0 == b<0) ? a>b ? a-b : b-a : Integer.MAX_VALUE; |
| 3146 |
} |
| 3147 |
|
| 3148 |
|
| 3149 |
public static final boolean overlap(int a1, int b1, int a2, int b2){ |
| 3150 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 3151 |
return a2<=b1 && b2>=a1; |
| 3152 |
} |
| 3153 |
|
| 3154 |
public static final int overlapLength(int a1, int b1, int a2, int b2){ |
| 3155 |
if(!overlap(a1,b1,a2,b2)){return 0;} |
| 3156 |
if(a1<=a2){ |
| 3157 |
return b1>=b2 ? b2-a2+1 : b1-a2+1; |
| 3158 |
}else{ |
| 3159 |
return b2>=b1 ? b1-a1+1 : b2-a1+1; |
| 3160 |
} |
| 3161 |
} |
| 3162 |
|
| 3163 |
|
| 3164 |
public static final boolean isWithin(int a1, int b1, int a2, int b2){ |
| 3165 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 3166 |
return a1>=a2 && b1<=b2; |
| 3167 |
} |
| 3168 |
|
| 3169 |
public static final int constrict(int point, int a, int b){ |
| 3170 |
assert(a<=b); |
| 3171 |
return(point<a ? a : point>b ? b : point); |
| 3172 |
} |
| 3173 |
|
| 3174 |
public static int trailingDigits(String line) { |
| 3175 |
for(int x=line.length()-1; x>=0; x--) { |
| 3176 |
if(!isDigit(line.charAt(x))) { |
| 3177 |
return line.length()-x-1; |
| 3178 |
} |
| 3179 |
} |
| 3180 |
return line.length(); |
| 3181 |
} |
| 3182 |
|
| 3183 |
public static int trailingDigits(byte[] line) { |
| 3184 |
for(int x=line.length-1; x>=0; x--) { |
| 3185 |
if(!isDigit(line[x])) { |
| 3186 |
return line.length-x-1; |
| 3187 |
} |
| 3188 |
} |
| 3189 |
return line.length; |
| 3190 |
} |
| 3191 |
|
| 3192 |
public static final int indexOf(byte[] array, char b){ |
| 3193 |
return indexOf(array, (byte)b, 0); |
| 3194 |
} |
| 3195 |
|
| 3196 |
public static final int indexOf(byte[] array, byte b){ |
| 3197 |
return indexOf(array, b, 0); |
| 3198 |
} |
| 3199 |
|
| 3200 |
public static final int indexOfNth(byte[] array, byte b, int n){ |
| 3201 |
return indexOfNth(array, b, n, 0); |
| 3202 |
} |
| 3203 |
|
| 3204 |
public static final int indexOfNth(byte[] array, char b, int n){ |
| 3205 |
return indexOfNth(array, (byte)b, n, 0); |
| 3206 |
} |
| 3207 |
|
| 3208 |
public static final int indexOf(final String array, final char b, final int start){ |
| 3209 |
int i=start; |
| 3210 |
|
| 3211 |
while(i<array.length() && array.charAt(i)!=b){ |
| 3212 |
|
| 3213 |
|
| 3214 |
i++; |
| 3215 |
} |
| 3216 |
|
| 3217 |
|
| 3218 |
|
| 3219 |
|
| 3220 |
return (i==array.length() ? -1 : i); |
| 3221 |
} |
| 3222 |
|
| 3223 |
public static final int indexOf(final byte[] array, final char b, final int start){return indexOf(array, (byte)b, start);} |
| 3224 |
public static final int indexOf(final byte[] array, final byte b, final int start){ |
| 3225 |
int i=start; |
| 3226 |
while(i<array.length && array[i]!=b){i++;} |
| 3227 |
return (i==array.length ? -1 : i); |
| 3228 |
} |
| 3229 |
|
| 3230 |
public static final int indexOfNth(final byte[] array, final char b, final int n, final int start){ |
| 3231 |
return indexOfNth(array, (byte)b, n, start); |
| 3232 |
} |
| 3233 |
|
| 3234 |
public static final int indexOfNth(final byte[] array, final byte b, final int n, final int start){ |
| 3235 |
int i=start, seen=0; |
| 3236 |
while(i<array.length && seen<n){ |
| 3237 |
if(array[i]==b){seen++;} |
| 3238 |
i++; |
| 3239 |
} |
| 3240 |
return (i==array.length ? -1 : i-1); |
| 3241 |
} |
| 3242 |
|
| 3243 |
public static final int indexOf(final byte[] ref, final String query, final int start){ |
| 3244 |
int i=start; |
| 3245 |
final int lim=ref.length-query.length(); |
| 3246 |
final byte first=(byte)query.charAt(0); |
| 3247 |
for(; i<=lim; i++){ |
| 3248 |
if(ref[i]==first && matches(ref, query, i)){return i;} |
| 3249 |
} |
| 3250 |
return -1; |
| 3251 |
} |
| 3252 |
|
| 3253 |
public static final int indexOfDelimited(final byte[] ref, final String query, final int start, final byte delimiter){ |
| 3254 |
|
| 3255 |
final int lim=ref.length-query.length(); |
| 3256 |
if(matches(ref, query, start)){return start;} |
| 3257 |
for(int i=start+1; i<=lim; i++){ |
| 3258 |
if(ref[i]==delimiter && matches(ref, query, i+1)){ |
| 3259 |
|
| 3260 |
return i+1; |
| 3261 |
} |
| 3262 |
} |
| 3263 |
return -1; |
| 3264 |
} |
| 3265 |
|
| 3266 |
private static boolean matches(byte[] ref, String query, int loc){ |
| 3267 |
if(ref.length-query.length()<loc){return false;} |
| 3268 |
final int max=loc+query.length(); |
| 3269 |
|
| 3270 |
for(int i=0; loc<max; i++, loc++){ |
| 3271 |
if(ref[loc]!=query.charAt(i)){return false;} |
| 3272 |
} |
| 3273 |
return true; |
| 3274 |
} |
| 3275 |
|
| 3276 |
public static final byte[] trimToWhitespace(byte[] array){ |
| 3277 |
if(array!=null){ |
| 3278 |
int index=indexOfWhitespace(array); |
| 3279 |
if(index>=0){return Arrays.copyOf(array, index);} |
| 3280 |
} |
| 3281 |
return array; |
| 3282 |
} |
| 3283 |
|
| 3284 |
public static final int indexOfWhitespace(byte[] array){ |
| 3285 |
int i=0; |
| 3286 |
while(i<array.length && !Character.isWhitespace(array[i])){i++;} |
| 3287 |
return (i==array.length ? -1 : i); |
| 3288 |
} |
| 3289 |
|
| 3290 |
public static final String trimToWhitespace(String array){ |
| 3291 |
if(array!=null){ |
| 3292 |
int index=indexOfWhitespace(array); |
| 3293 |
if(index>=0){return array.substring(0, index);} |
| 3294 |
} |
| 3295 |
return array; |
| 3296 |
} |
| 3297 |
|
| 3298 |
public static final int indexOfWhitespace(String array){ |
| 3299 |
int i=0; |
| 3300 |
while(i<array.length() && !Character.isWhitespace(array.charAt(i))){i++;} |
| 3301 |
return (i==array.length() ? -1 : i); |
| 3302 |
} |
| 3303 |
|
| 3304 |
public static final int indexOf(char[] array, char b){ |
| 3305 |
int i=0; |
| 3306 |
while(i<array.length && array[i]!=b){i++;} |
| 3307 |
return (i==array.length ? -1 : i); |
| 3308 |
} |
| 3309 |
|
| 3310 |
public static final int lastIndexOf(byte[] array, byte b){ |
| 3311 |
int i=array.length-1; |
| 3312 |
while(i>=0 && array[i]!=b){i--;} |
| 3313 |
return i; |
| 3314 |
} |
| 3315 |
|
| 3316 |
public static final int stringLength(long x){ |
| 3317 |
if(x<0){ |
| 3318 |
if(x==Integer.MIN_VALUE){return 11;} |
| 3319 |
return lengthOf(-x)+1; |
| 3320 |
} |
| 3321 |
return lengthOf(x); |
| 3322 |
} |
| 3323 |
|
| 3324 |
public static final int stringLength(int x){ |
| 3325 |
if(x<0){ |
| 3326 |
if(x==Long.MIN_VALUE){return 20;} |
| 3327 |
return lengthOf(-x)+1; |
| 3328 |
} |
| 3329 |
return lengthOf(x); |
| 3330 |
} |
| 3331 |
|
| 3332 |
public static final int lengthOf(int x){ |
| 3333 |
assert(x>=0); |
| 3334 |
int i=1; |
| 3335 |
while(x>ilens[i]){i++;} |
| 3336 |
return i; |
| 3337 |
} |
| 3338 |
|
| 3339 |
public static final int lengthOf(long x){ |
| 3340 |
assert(x>=0); |
| 3341 |
int i=1; |
| 3342 |
while(x>llens[i]){i++;} |
| 3343 |
return i; |
| 3344 |
} |
| 3345 |
|
| 3346 |
public static final byte max(byte[] array){return array[maxIndex(array)];} |
| 3347 |
public static final float max(float[] array){return array[maxIndex(array)];} |
| 3348 |
|
| 3349 |
public static final int maxIndex(byte[] array){ |
| 3350 |
byte max=array[0]; |
| 3351 |
int maxIndex=0; |
| 3352 |
for(int i=1; i<array.length; i++){ |
| 3353 |
if(array[i]>max){max=array[i];maxIndex=i;} |
| 3354 |
} |
| 3355 |
return maxIndex; |
| 3356 |
} |
| 3357 |
|
| 3358 |
public static final int maxIndex(float[] array){ |
| 3359 |
float max=array[0]; |
| 3360 |
int maxIndex=0; |
| 3361 |
for(int i=1; i<array.length; i++){ |
| 3362 |
if(array[i]>max){max=array[i];maxIndex=i;} |
| 3363 |
} |
| 3364 |
return maxIndex; |
| 3365 |
} |
| 3366 |
|
| 3367 |
public static final int max(short[] array){return array[maxIndex(array)];} |
| 3368 |
|
| 3369 |
public static final int maxIndex(short[] array){ |
| 3370 |
short max=array[0]; |
| 3371 |
int maxIndex=0; |
| 3372 |
for(int i=1; i<array.length; i++){ |
| 3373 |
if(array[i]>max){max=array[i];maxIndex=i;} |
| 3374 |
} |
| 3375 |
return maxIndex; |
| 3376 |
} |
| 3377 |
|
| 3378 |
public static final int max(int[] array){return array[maxIndex(array)];} |
| 3379 |
|
| 3380 |
public static final int maxIndex(int[] array){ |
| 3381 |
int max=array[0], maxIndex=0; |
| 3382 |
for(int i=1; i<array.length; i++){ |
| 3383 |
if(array[i]>max){max=array[i];maxIndex=i;} |
| 3384 |
} |
| 3385 |
return maxIndex; |
| 3386 |
} |
| 3387 |
|
| 3388 |
public static final long max(long[] array){return array[maxIndex(array)];} |
| 3389 |
|
| 3390 |
public static final int maxIndex(long[] array){ |
| 3391 |
long max=array[0]; |
| 3392 |
int maxIndex=0; |
| 3393 |
for(int i=1; i<array.length; i++){ |
| 3394 |
if(array[i]>max){max=array[i];maxIndex=i;} |
| 3395 |
} |
| 3396 |
return maxIndex; |
| 3397 |
} |
| 3398 |
|
| 3399 |
public static final double max(double[] array){return array[maxIndex(array)];} |
| 3400 |
|
| 3401 |
public static final int maxIndex(double[] array){ |
| 3402 |
double max=array[0]; |
| 3403 |
int maxIndex=0; |
| 3404 |
for(int i=1; i<array.length; i++){ |
| 3405 |
if(array[i]>max){max=array[i];maxIndex=i;} |
| 3406 |
} |
| 3407 |
return maxIndex; |
| 3408 |
} |
| 3409 |
|
| 3410 |
public static final double standardDeviation(long[] numbers){ |
| 3411 |
if(numbers==null || numbers.length<2){return 0;} |
| 3412 |
long sum=sum(numbers); |
| 3413 |
double avg=sum/(double)numbers.length; |
| 3414 |
double sumdev2=0; |
| 3415 |
for(int i=0; i<numbers.length; i++){ |
| 3416 |
long x=numbers[i]; |
| 3417 |
double dev=avg-x; |
| 3418 |
sumdev2+=(dev*dev); |
| 3419 |
} |
| 3420 |
return Math.sqrt(sumdev2/numbers.length); |
| 3421 |
} |
| 3422 |
|
| 3423 |
public static final double standardDeviation(double[] numbers){ |
| 3424 |
if(numbers==null || numbers.length<2){return 0;} |
| 3425 |
double sum=sum(numbers); |
| 3426 |
double avg=sum/(double)numbers.length; |
| 3427 |
double sumdev2=0; |
| 3428 |
for(int i=0; i<numbers.length; i++){ |
| 3429 |
double x=numbers[i]; |
| 3430 |
double dev=avg-x; |
| 3431 |
sumdev2+=(dev*dev); |
| 3432 |
} |
| 3433 |
return Math.sqrt(sumdev2/numbers.length); |
| 3434 |
} |
| 3435 |
|
| 3436 |
public static final double standardDeviation(float[] numbers){ |
| 3437 |
if(numbers==null || numbers.length<2){return 0;} |
| 3438 |
double sum=sum(numbers); |
| 3439 |
double avg=sum/(double)numbers.length; |
| 3440 |
double sumdev2=0; |
| 3441 |
for(int i=0; i<numbers.length; i++){ |
| 3442 |
double x=numbers[i]; |
| 3443 |
double dev=avg-x; |
| 3444 |
sumdev2+=(dev*dev); |
| 3445 |
} |
| 3446 |
return Math.sqrt(sumdev2/numbers.length); |
| 3447 |
} |
| 3448 |
|
| 3449 |
|
| 3450 |
|
| 3451 |
|
| 3452 |
|
| 3453 |
|
| 3454 |
public static final double standardDeviation(int[] numbers){ |
| 3455 |
if(numbers==null || numbers.length<2){return 0;} |
| 3456 |
long sum=sum(numbers); |
| 3457 |
double avg=sum/(double)numbers.length; |
| 3458 |
double sumdev2=0; |
| 3459 |
for(int i=0; i<numbers.length; i++){ |
| 3460 |
long x=numbers[i]; |
| 3461 |
double dev=avg-x; |
| 3462 |
sumdev2+=(dev*dev); |
| 3463 |
} |
| 3464 |
return Math.sqrt(sumdev2/numbers.length); |
| 3465 |
} |
| 3466 |
|
| 3467 |
public static final double standardDeviation(AtomicIntegerArray numbers){ |
| 3468 |
if(numbers==null || numbers.length()<2){return 0;} |
| 3469 |
long sum=sum(numbers); |
| 3470 |
double avg=sum/(double)numbers.length(); |
| 3471 |
double sumdev2=0; |
| 3472 |
for(int i=0; i<numbers.length(); i++){ |
| 3473 |
long x=numbers.get(i); |
| 3474 |
double dev=avg-x; |
| 3475 |
sumdev2+=(dev*dev); |
| 3476 |
} |
| 3477 |
return Math.sqrt(sumdev2/numbers.length()); |
| 3478 |
} |
| 3479 |
|
| 3480 |
public static final double standardDeviation(char[] numbers){ |
| 3481 |
if(numbers==null || numbers.length<2){return 0;} |
| 3482 |
long sum=sum(numbers); |
| 3483 |
double avg=sum/(double)numbers.length; |
| 3484 |
double sumdev2=0; |
| 3485 |
for(int i=0; i<numbers.length; i++){ |
| 3486 |
long x=numbers[i]; |
| 3487 |
double dev=avg-x; |
| 3488 |
sumdev2+=(dev*dev); |
| 3489 |
} |
| 3490 |
return Math.sqrt(sumdev2/numbers.length); |
| 3491 |
} |
| 3492 |
|
| 3493 |
public static final double standardDeviation(short[] numbers){ |
| 3494 |
if(numbers==null || numbers.length<2){return 0;} |
| 3495 |
long sum=sum(numbers); |
| 3496 |
double avg=sum/(double)numbers.length; |
| 3497 |
double sumdev2=0; |
| 3498 |
for(int i=0; i<numbers.length; i++){ |
| 3499 |
long x=numbers[i]; |
| 3500 |
double dev=avg-x; |
| 3501 |
sumdev2+=(dev*dev); |
| 3502 |
} |
| 3503 |
return Math.sqrt(sumdev2/numbers.length); |
| 3504 |
} |
| 3505 |
|
| 3506 |
public static final double averageHistogram(long[] histogram){ |
| 3507 |
long sum=max(1, sum(histogram)); |
| 3508 |
long sum2=0; |
| 3509 |
for(int i=0; i<histogram.length; i++){ |
| 3510 |
sum2+=(histogram[i]*i); |
| 3511 |
} |
| 3512 |
double avg=sum2/(double)sum; |
| 3513 |
return avg; |
| 3514 |
} |
| 3515 |
|
| 3516 |
public static final double standardDeviationHistogram(char[] histogram){ |
| 3517 |
long sum=max(1, sum(histogram)); |
| 3518 |
long sum2=0; |
| 3519 |
for(int i=0; i<histogram.length; i++){ |
| 3520 |
sum2+=(histogram[i]*i); |
| 3521 |
} |
| 3522 |
double avg=sum2/(double)sum; |
| 3523 |
double sumdev2=0; |
| 3524 |
for(int i=0; i<histogram.length; i++){ |
| 3525 |
double dev=avg-i; |
| 3526 |
double dev2=dev*dev; |
| 3527 |
sumdev2+=(histogram[i]*dev2); |
| 3528 |
} |
| 3529 |
return Math.sqrt(sumdev2/sum); |
| 3530 |
} |
| 3531 |
|
| 3532 |
public static final double standardDeviationHistogram(int[] histogram){ |
| 3533 |
long sum=max(1, sum(histogram)); |
| 3534 |
long sum2=0; |
| 3535 |
for(int i=0; i<histogram.length; i++){ |
| 3536 |
sum2+=(histogram[i]*i); |
| 3537 |
} |
| 3538 |
double avg=sum2/(double)sum; |
| 3539 |
double sumdev2=0; |
| 3540 |
for(int i=0; i<histogram.length; i++){ |
| 3541 |
double dev=avg-i; |
| 3542 |
double dev2=dev*dev; |
| 3543 |
sumdev2+=(histogram[i]*dev2); |
| 3544 |
} |
| 3545 |
return Math.sqrt(sumdev2/sum); |
| 3546 |
} |
| 3547 |
|
| 3548 |
public static final double standardDeviationHistogram(long[] histogram){ |
| 3549 |
long sum=max(1, sum(histogram)); |
| 3550 |
long sum2=0; |
| 3551 |
for(int i=0; i<histogram.length; i++){ |
| 3552 |
sum2+=(histogram[i]*i); |
| 3553 |
} |
| 3554 |
double avg=sum2/(double)sum; |
| 3555 |
double sumdev2=0; |
| 3556 |
for(int i=0; i<histogram.length; i++){ |
| 3557 |
double dev=avg-i; |
| 3558 |
double dev2=dev*dev; |
| 3559 |
sumdev2+=(histogram[i]*dev2); |
| 3560 |
} |
| 3561 |
return Math.sqrt(sumdev2/sum); |
| 3562 |
} |
| 3563 |
|
| 3564 |
|
| 3565 |
public static final double standardDeviationHistogramKmer(long[] histogram){ |
| 3566 |
final long sum=sum(histogram); |
| 3567 |
double sumU=0; |
| 3568 |
for(int i=0; i<histogram.length; i++){ |
| 3569 |
long x=histogram[i]; |
| 3570 |
sumU+=(x/(double)max(i, 1)); |
| 3571 |
} |
| 3572 |
double avg=sum/max(sumU, 1); |
| 3573 |
double sumdev2=0; |
| 3574 |
for(int i=1; i<histogram.length; i++){ |
| 3575 |
double dev=avg-i; |
| 3576 |
double dev2=dev*dev; |
| 3577 |
long x=histogram[i]; |
| 3578 |
sumdev2+=((x/(double)max(i, 1))*dev2); |
| 3579 |
} |
| 3580 |
return Math.sqrt(sumdev2/sumU); |
| 3581 |
} |
| 3582 |
|
| 3583 |
public static final double standardDeviationHistogram(AtomicLongArray histogram){ |
| 3584 |
long sum=max(1, sum(histogram)); |
| 3585 |
long sum2=0; |
| 3586 |
for(int i=0; i<histogram.length(); i++){ |
| 3587 |
sum2+=(histogram.get(i)*i); |
| 3588 |
} |
| 3589 |
double avg=sum2/(double)sum; |
| 3590 |
double sumdev2=0; |
| 3591 |
for(int i=0; i<histogram.length(); i++){ |
| 3592 |
double dev=avg-i; |
| 3593 |
double dev2=dev*dev; |
| 3594 |
sumdev2+=(histogram.get(i)*dev2); |
| 3595 |
} |
| 3596 |
return Math.sqrt(sumdev2/sum); |
| 3597 |
} |
| 3598 |
|
| 3599 |
|
| 3600 |
public static final double standardDeviationHistogramKmer(AtomicLongArray histogram){ |
| 3601 |
final long sum=sum(histogram); |
| 3602 |
double sumU=0; |
| 3603 |
for(int i=0; i<histogram.length(); i++){ |
| 3604 |
long x=histogram.get(i); |
| 3605 |
sumU+=(x/(double)max(i, 1)); |
| 3606 |
} |
| 3607 |
double avg=sum/max(sumU, 1); |
| 3608 |
double sumdev2=0; |
| 3609 |
for(int i=1; i<histogram.length(); i++){ |
| 3610 |
double dev=avg-i; |
| 3611 |
double dev2=dev*dev; |
| 3612 |
long x=histogram.get(i); |
| 3613 |
sumdev2+=((x/(double)max(i, 1))*dev2); |
| 3614 |
} |
| 3615 |
return Math.sqrt(sumdev2/sumU); |
| 3616 |
} |
| 3617 |
|
| 3618 |
public static final long[] downsample(long[] array, int bins){ |
| 3619 |
if(array==null || array.length==bins){return array;} |
| 3620 |
assert(bins<=array.length); |
| 3621 |
assert(bins>=0); |
| 3622 |
long[] r=new long[bins]; |
| 3623 |
if(bins==0){return r;} |
| 3624 |
double mult=bins/(double)array.length; |
| 3625 |
for(int i=0; i<array.length; i++){ |
| 3626 |
int j=(int)(mult*i); |
| 3627 |
r[j]+=array[i]; |
| 3628 |
|
| 3629 |
} |
| 3630 |
return r; |
| 3631 |
} |
| 3632 |
|
| 3633 |
|
| 3634 |
public static final void pause(int millis){ |
| 3635 |
try { |
| 3636 |
Thread.sleep(millis); |
| 3637 |
} catch (InterruptedException e) { |
| 3638 |
|
| 3639 |
e.printStackTrace(); |
| 3640 |
} |
| 3641 |
} |
| 3642 |
|
| 3643 |
public static final String getTempExt(FileFormat ffin, FileFormat ffout, String extout) { |
| 3644 |
String tempExt=".fq.gz"; |
| 3645 |
if(extout==null){ |
| 3646 |
if(ffout!=null){ |
| 3647 |
tempExt=ffout.fasta() ? ".fa" : ffout.samOrBam() ? ".sam" : ".fq"; |
| 3648 |
if(ffout.compressed()){tempExt+=".gz";} |
| 3649 |
}else{ |
| 3650 |
tempExt=ffin.fasta() ? ".fa" : ffin.samOrBam() ? ".sam" : ".fq"; |
| 3651 |
if(ffin.compressed()){tempExt+=".gz";} |
| 3652 |
} |
| 3653 |
}else{ |
| 3654 |
tempExt=extout; |
| 3655 |
} |
| 3656 |
return tempExt; |
| 3657 |
} |
| 3658 |
|
| 3659 |
public static final int min(int x, int y){return x<y ? x : y;} |
| 3660 |
public static final int max(int x, int y){return x>y ? x : y;} |
| 3661 |
public static final int min(int x, int y, int z){return x<y ? (x<z ? x : z) : (y<z ? y : z);} |
| 3662 |
public static final int max(int x, int y, int z){return x>y ? (x>z ? x : z) : (y>z ? y : z);} |
| 3663 |
public static final int min(int x, int y, int z, int z2){return min(min(x,y), min(z,z2));} |
| 3664 |
public static final int max(int x, int y, int z, int z2){return max(max(x,y), max(z,z2));} |
| 3665 |
|
| 3666 |
|
| 3667 |
public static final int mid(int x, int y, int z){return x<y ? (x<z ? min(y, z) : x) : (y<z ? min(x, z) : y);} |
| 3668 |
|
| 3669 |
public static final char min(char x, char y){return x<y ? x : y;} |
| 3670 |
public static final char max(char x, char y){return x>y ? x : y;} |
| 3671 |
|
| 3672 |
public static final byte min(byte x, byte y){return x<y ? x : y;} |
| 3673 |
public static final byte max(byte x, byte y){return x>y ? x : y;} |
| 3674 |
public static final byte min(byte x, byte y, byte z){return x<y ? min(x, z) : min(y, z);} |
| 3675 |
public static final byte max(byte x, byte y, byte z){return x>y ? max(x, z) : max(y, z);} |
| 3676 |
public static final byte min(byte x, byte y, byte z, byte a){return min(min(x, y), min(z, a));} |
| 3677 |
public static final byte max(byte x, byte y, byte z, byte a){return max(max(x, y), max(z, a));} |
| 3678 |
|
| 3679 |
public static final byte mid(byte x, byte y, byte z){return x<y ? (x<z ? min(y, z) : x) : (y<z ? min(x, z) : y);} |
| 3680 |
|
| 3681 |
public static final long min(long x, long y){return x<y ? x : y;} |
| 3682 |
public static final long max(long x, long y){return x>y ? x : y;} |
| 3683 |
public static final long min(long x, long y, long z){return x<y ? (x<z ? x : z) : (y<z ? y : z);} |
| 3684 |
public static final long max(long x, long y, long z){return x>y ? (x>z ? x : z) : (y>z ? y : z);} |
| 3685 |
public static final long min(long x, long y, long z, long z2){return min(min(x,y), min(z,z2));} |
| 3686 |
public static final long max(long x, long y, long z, long z2){return max(max(x,y), max(z,z2));} |
| 3687 |
public static final long mid(long x, long y, long z){return x<y ? (x<z ? min(y, z) : x) : (y<z ? min(x, z) : y);} |
| 3688 |
public static final int longToInt(long x){return x<Integer.MIN_VALUE ? Integer.MIN_VALUE : x>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)x;} |
| 3689 |
|
| 3690 |
public static final double min(double x, double y){return x<y ? x : y;} |
| 3691 |
public static final double max(double x, double y){return x>y ? x : y;} |
| 3692 |
public static final double min(double x, double y, double z){return x<y ? (x<z ? x : z) : (y<z ? y : z);} |
| 3693 |
public static final double max(double x, double y, double z){return x>y ? (x>z ? x : z) : (y>z ? y : z);} |
| 3694 |
public static final double max(double w, double x, double y, double z){return max(max(w, x), max(y, z));} |
| 3695 |
public static final double mid(double x, double y, double z){return x<y ? (x<z ? min(y, z) : x) : (y<z ? min(x, z) : y);} |
| 3696 |
|
| 3697 |
public static final float min(float x, float y){return x<y ? x : y;} |
| 3698 |
public static final float max(float x, float y){return x>y ? x : y;} |
| 3699 |
public static final float min(float x, float y, float z){return x<y ? (x<z ? x : z) : (y<z ? y : z);} |
| 3700 |
public static final float max(float x, float y, float z){return x>y ? (x>z ? x : z) : (y>z ? y : z);} |
| 3701 |
public static final float min(float x, float y, float z, float z2){return min(min(x, y), min(z, z2));} |
| 3702 |
public static final float max(float x, float y, float z, float z2){return max(max(x, y), max(z, z2));} |
| 3703 |
public static final float mid(float x, float y, float z){return x<y ? (x<z ? min(y, z) : x) : (y<z ? min(x, z) : y);} |
| 3704 |
|
| 3705 |
public static final int min(int[] array, int fromIndex, int toIndex){ |
| 3706 |
int min=array[fromIndex]; |
| 3707 |
for(int i=fromIndex+1; i<=toIndex; i++){ |
| 3708 |
min=min(min, array[i]); |
| 3709 |
} |
| 3710 |
return min; |
| 3711 |
} |
| 3712 |
|
| 3713 |
public static final int max(int[] array, int fromIndex, int toIndex){ |
| 3714 |
int max=array[fromIndex]; |
| 3715 |
for(int i=fromIndex+1; i<=toIndex; i++){ |
| 3716 |
max=max(max, array[i]); |
| 3717 |
} |
| 3718 |
return max; |
| 3719 |
} |
| 3720 |
|
| 3721 |
public static int minIndex(int[] array) { |
| 3722 |
if(array==null || array.length<1){return -1;} |
| 3723 |
float min=array[0]; |
| 3724 |
int index=0; |
| 3725 |
for(int i=1; i<array.length; i++){ |
| 3726 |
if(array[i]<min){ |
| 3727 |
min=array[i]; |
| 3728 |
index=i; |
| 3729 |
} |
| 3730 |
} |
| 3731 |
return index; |
| 3732 |
} |
| 3733 |
|
| 3734 |
public static String trimWhitespace(String s){ |
| 3735 |
for(int i=0; i<s.length(); i++){ |
| 3736 |
if(Character.isWhitespace(s.charAt(i))){ |
| 3737 |
String s2=s.substring(0, i); |
| 3738 |
return s2; |
| 3739 |
} |
| 3740 |
} |
| 3741 |
return s; |
| 3742 |
} |
| 3743 |
|
| 3744 |
public static float calcGC(byte[] s) { |
| 3745 |
if(s==null) {return 0;} |
| 3746 |
return calcGC(s, 0, s.length-1); |
| 3747 |
} |
| 3748 |
|
| 3749 |
public static float calcGC(byte[] s, int from, int to) { |
| 3750 |
if(s==null) {return 0;} |
| 3751 |
int[] acgtn=localACGTN.get(); |
| 3752 |
Arrays.fill(acgtn, 0); |
| 3753 |
for(int i=from; i<=to; i++) { |
| 3754 |
byte b=s[i]; |
| 3755 |
int x=AminoAcid.baseToNumber4[b]; |
| 3756 |
acgtn[x]++; |
| 3757 |
} |
| 3758 |
int gc=acgtn[1]+acgtn[2]; |
| 3759 |
int at=acgtn[0]+acgtn[3]; |
| 3760 |
return gc/(float)(at+gc); |
| 3761 |
} |
| 3762 |
|
| 3763 |
|
| 3764 |
public static int hash(final byte[] s, final int affixLen) { |
| 3765 |
if(s==null) {return 0;} |
| 3766 |
int code=s.length; |
| 3767 |
final int len=min(affixLen, s.length, (s.length+1)/2); |
| 3768 |
|
| 3769 |
for(int i=0; i<len; i++) { |
| 3770 |
int x=baseToHashcode[s[i]]; |
| 3771 |
code=Integer.rotateLeft(code, 5)^x; |
| 3772 |
} |
| 3773 |
for(int i=s.length-1, min=s.length-len; i>=min; i--) { |
| 3774 |
int x=baseToHashcode[s[i]]; |
| 3775 |
code=Integer.rotateLeft(code, 5)^x; |
| 3776 |
} |
| 3777 |
return code; |
| 3778 |
} |
| 3779 |
|
| 3780 |
|
| 3781 |
public static boolean canonical(byte[] s) { |
| 3782 |
for(int i=0, j=s.length-1; i<=j; i++, j--) { |
| 3783 |
byte a=baseToNumberExtended[s[i]], b=baseToComplementNumberExtended[s[j]]; |
| 3784 |
if(a<b) {return true;} |
| 3785 |
else if(a>b) {return false;} |
| 3786 |
} |
| 3787 |
return true; |
| 3788 |
} |
| 3789 |
|
| 3790 |
public static boolean canonize(byte[] s) { |
| 3791 |
if(canonical(s)) {return false;} |
| 3792 |
AminoAcid.reverseComplementBasesInPlace(s); |
| 3793 |
return true; |
| 3794 |
} |
| 3795 |
|
| 3796 |
|
| 3797 |
|
| 3798 |
|
| 3799 |
|
| 3800 |
|
| 3801 |
public static long hash64shift(long key){ |
| 3802 |
key = (~key) + (key << 21); |
| 3803 |
key = key ^ (key >>> 24); |
| 3804 |
key = (key + (key << 3)) + (key << 8); |
| 3805 |
key = key ^ (key >>> 14); |
| 3806 |
key = (key + (key << 2)) + (key << 4); |
| 3807 |
key = key ^ (key >>> 28); |
| 3808 |
key = key + (key << 31); |
| 3809 |
return key; |
| 3810 |
} |
| 3811 |
|
| 3812 |
public static double exponential(Random randy, double lamda){ |
| 3813 |
|
| 3814 |
|
| 3815 |
|
| 3816 |
|
| 3817 |
|
| 3818 |
|
| 3819 |
double p=randy.nextDouble(); |
| 3820 |
return -Math.log(1-p)/lamda; |
| 3821 |
} |
| 3822 |
|
| 3823 |
public static double log2(double d){ |
| 3824 |
return Math.log(d)*invlog2; |
| 3825 |
} |
| 3826 |
|
| 3827 |
public static double logRoot2(double d){ |
| 3828 |
return Math.log(d)*invlogRoot2; |
| 3829 |
} |
| 3830 |
|
| 3831 |
public static double log1point2(double d){ |
| 3832 |
return Math.log(d)*invlog1point2; |
| 3833 |
} |
| 3834 |
|
| 3835 |
private static final double log2=Math.log(2); |
| 3836 |
private static final double invlog2=1/log2; |
| 3837 |
private static final double logRoot2=Math.log(Math.sqrt(2)); |
| 3838 |
private static final double invlogRoot2=1/logRoot2; |
| 3839 |
private static final double log1point2=Math.log(1.2); |
| 3840 |
private static final double invlog1point2=1/log1point2; |
| 3841 |
|
| 3842 |
public static final boolean[] digitMap; |
| 3843 |
public static final boolean[] signOrDigitMap; |
| 3844 |
public static final boolean[] numericMap; |
| 3845 |
public static final boolean[] letterMap; |
| 3846 |
|
| 3847 |
|
| 3848 |
public static final char[] specialChars; |
| 3849 |
|
| 3850 |
public static final int[] ilens; |
| 3851 |
public static final long[] llens; |
| 3852 |
|
| 3853 |
|
| 3854 |
|
| 3855 |
|
| 3856 |
public static final Pattern whitespace = Pattern.compile("\\s"); |
| 3857 |
|
| 3858 |
public static final Pattern whitespacePlus = Pattern.compile("\\s+"); |
| 3859 |
|
| 3860 |
public static final Pattern commaPattern = Pattern.compile(","); |
| 3861 |
|
| 3862 |
public static final Pattern dotPattern = Pattern.compile("\\."); |
| 3863 |
|
| 3864 |
public static final Pattern tabPattern = Pattern.compile("\t"); |
| 3865 |
|
| 3866 |
public static final Pattern colonPattern = Pattern.compile(":"); |
| 3867 |
|
| 3868 |
public static final Pattern semiPattern = Pattern.compile(";"); |
| 3869 |
|
| 3870 |
public static final Pattern pipePattern = Pattern.compile("\\|"); |
| 3871 |
|
| 3872 |
public static final Pattern spacePattern = Pattern.compile(" "); |
| 3873 |
|
| 3874 |
public static final Pattern equalsPattern = Pattern.compile("="); |
| 3875 |
|
| 3876 |
public static final Pattern underscorePattern = Pattern.compile("_"); |
| 3877 |
|
| 3878 |
public static boolean FORCE_JAVA_PARSE_DOUBLE=false; |
| 3879 |
|
| 3880 |
static{ |
| 3881 |
digitMap=new boolean[128]; |
| 3882 |
signOrDigitMap=new boolean[128]; |
| 3883 |
numericMap=new boolean[128]; |
| 3884 |
letterMap=new boolean[128]; |
| 3885 |
for(int i='a'; i<='z'; i++){letterMap[i]=true;} |
| 3886 |
for(int i='A'; i<='Z'; i++){letterMap[i]=true;} |
| 3887 |
for(int i='0'; i<='9'; i++){digitMap[i]=numericMap[i]=signOrDigitMap[i]=true;} |
| 3888 |
numericMap['-']=signOrDigitMap['-']=true; |
| 3889 |
numericMap['.']=true; |
| 3890 |
|
| 3891 |
ilens=new int[Integer.toString(Integer.MAX_VALUE).length()+1]; |
| 3892 |
llens=new long[Long.toString(Long.MAX_VALUE).length()+1]; |
| 3893 |
for(int i=1, x=9; i<ilens.length; i++){ |
| 3894 |
ilens[i]=x; |
| 3895 |
x=(x*10)+9; |
| 3896 |
} |
| 3897 |
ilens[ilens.length-1]=Integer.MAX_VALUE; |
| 3898 |
for(long i=1, x=9; i<llens.length; i++){ |
| 3899 |
llens[(int)i]=x; |
| 3900 |
x=(x*10)+9; |
| 3901 |
} |
| 3902 |
llens[llens.length-1]=Long.MAX_VALUE; |
| 3903 |
|
| 3904 |
specialChars=new char[256]; |
| 3905 |
Arrays.fill(specialChars, 'X'); |
| 3906 |
for(int i=0; i<32; i++){ |
| 3907 |
specialChars[i]=' '; |
| 3908 |
} |
| 3909 |
for(int i=32; i<127; i++){ |
| 3910 |
specialChars[i]=(char)i; |
| 3911 |
} |
| 3912 |
specialChars[127]=' '; |
| 3913 |
specialChars[128]='C'; |
| 3914 |
specialChars[129]='u'; |
| 3915 |
specialChars[130]='e'; |
| 3916 |
specialChars[131]='a'; |
| 3917 |
specialChars[132]='a'; |
| 3918 |
specialChars[133]='a'; |
| 3919 |
specialChars[134]='a'; |
| 3920 |
specialChars[135]='c'; |
| 3921 |
specialChars[136]='e'; |
| 3922 |
specialChars[137]='e'; |
| 3923 |
specialChars[138]='e'; |
| 3924 |
specialChars[139]='i'; |
| 3925 |
specialChars[140]='i'; |
| 3926 |
specialChars[141]='i'; |
| 3927 |
specialChars[142]='S'; |
| 3928 |
specialChars[143]='S'; |
| 3929 |
specialChars[144]='E'; |
| 3930 |
specialChars[145]='a'; |
| 3931 |
specialChars[146]='a'; |
| 3932 |
specialChars[147]='o'; |
| 3933 |
specialChars[148]='o'; |
| 3934 |
specialChars[149]='o'; |
| 3935 |
specialChars[150]='u'; |
| 3936 |
specialChars[151]='u'; |
| 3937 |
specialChars[152]='y'; |
| 3938 |
specialChars[153]='O'; |
| 3939 |
specialChars[154]='U'; |
| 3940 |
specialChars[155]='c'; |
| 3941 |
specialChars[156]='L'; |
| 3942 |
specialChars[157]='Y'; |
| 3943 |
specialChars[158]='P'; |
| 3944 |
specialChars[159]='f'; |
| 3945 |
specialChars[160]='a'; |
| 3946 |
specialChars[161]='i'; |
| 3947 |
specialChars[162]='o'; |
| 3948 |
specialChars[163]='u'; |
| 3949 |
specialChars[164]='n'; |
| 3950 |
specialChars[165]='N'; |
| 3951 |
specialChars[166]='a'; |
| 3952 |
specialChars[167]='o'; |
| 3953 |
specialChars[168]='?'; |
| 3954 |
specialChars[224]='a'; |
| 3955 |
specialChars[224]='B'; |
| 3956 |
specialChars[230]='u'; |
| 3957 |
specialChars[252]='n'; |
| 3958 |
specialChars[253]='2'; |
| 3959 |
} |
| 3960 |
|
| 3961 |
public static final boolean startsWithLetter(String a) { |
| 3962 |
return a!=null && a.length()>0 && Character.isLetter(a.charAt(0)); |
| 3963 |
} |
| 3964 |
|
| 3965 |
public static final boolean startsWithLetter(byte[] a) { |
| 3966 |
return a!=null && a.length>0 && Character.isLetter(a[0]); |
| 3967 |
} |
| 3968 |
|
| 3969 |
public static final boolean startsWithDigit(String a) { |
| 3970 |
return a!=null && a.length()>0 && Character.isDigit(a.charAt(0)); |
| 3971 |
} |
| 3972 |
|
| 3973 |
public static final boolean startsWithNumeric(String a) { |
| 3974 |
if(a==null || a.length()<1) {return false;} |
| 3975 |
char c=a.charAt(0); |
| 3976 |
return c=='.' || Character.isDigit(c); |
| 3977 |
} |
| 3978 |
|
| 3979 |
|
| 3980 |
|
| 3981 |
|
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
public static final int find(String a, String[] array){ |
| 3986 |
for(int i=0; i<array.length; i++){ |
| 3987 |
if(a.equals(array[i])){return i;} |
| 3988 |
} |
| 3989 |
return -1; |
| 3990 |
} |
| 3991 |
|
| 3992 |
|
| 3993 |
|
| 3994 |
|
| 3995 |
|
| 3996 |
|
| 3997 |
|
| 3998 |
public static final int findIC(String a, String[] array){ |
| 3999 |
for(int i=0; i<array.length; i++){ |
| 4000 |
if(a.equalsIgnoreCase(array[i])){return i;} |
| 4001 |
} |
| 4002 |
return -1; |
| 4003 |
} |
| 4004 |
|
| 4005 |
|
| 4006 |
|
| 4007 |
|
| 4008 |
|
| 4009 |
|
| 4010 |
|
| 4011 |
public static final int find2(String a, String[] array){ |
| 4012 |
for(int i=0; i<array.length; i++){ |
| 4013 |
if(a.equals(array[i])){return i;} |
| 4014 |
} |
| 4015 |
return array.length-1; |
| 4016 |
} |
| 4017 |
|
| 4018 |
|
| 4019 |
public static int binarySearch(float[] array, float key) { |
| 4020 |
if(array.length<2) {return 0;} |
| 4021 |
else if(array.length==2) { |
| 4022 |
return Math.abs(key-array[0])<=Math.abs(key-array[1]) ? 0 : 1; |
| 4023 |
} |
| 4024 |
int a=0, b=array.length-1; |
| 4025 |
while(b>a){ |
| 4026 |
final int mid=(a+b)/2; |
| 4027 |
final float f=array[mid]; |
| 4028 |
if(key<f){b=mid;} |
| 4029 |
else if(key>f){a=mid+1;} |
| 4030 |
else{return mid;} |
| 4031 |
} |
| 4032 |
assert(a==b) : a+", "+b; |
| 4033 |
if(a==0 || a==array.length-1) {return a;} |
| 4034 |
float dif1=Math.abs(key-array[a-1]); |
| 4035 |
float dif2=Math.abs(key-array[a]); |
| 4036 |
float dif3=Math.abs(key-array[a+1]); |
| 4037 |
if(dif1<dif2) {return a-1;} |
| 4038 |
if(dif3<dif2) {return a+1;} |
| 4039 |
return a; |
| 4040 |
} |
| 4041 |
|
| 4042 |
public static final <X> X getLast(ArrayList<X> list) { |
| 4043 |
return (list.size()>0 ? list.get(list.size()-1) : null); |
| 4044 |
} |
| 4045 |
|
| 4046 |
public static boolean isReadableFile(String s) { |
| 4047 |
if(s==null) {return false;} |
| 4048 |
File f=new File(s); |
| 4049 |
return f.canRead() && f.isFile(); |
| 4050 |
} |
| 4051 |
|
| 4052 |
public static boolean looksLikeInputStream(String arg) { |
| 4053 |
if(arg==null || arg.indexOf('=')>=0) {return false;} |
| 4054 |
return arg.toLowerCase().startsWith("stdin") || isReadableFile(arg); |
| 4055 |
} |
| 4056 |
|
| 4057 |
public static void sleep(int millis){ |
| 4058 |
if(millis<1){return;} |
| 4059 |
|
| 4060 |
long until=System.currentTimeMillis()+millis; |
| 4061 |
while(System.currentTimeMillis()<until){ |
| 4062 |
try { |
| 4063 |
Thread.sleep(millis); |
| 4064 |
} catch (InterruptedException e) {} |
| 4065 |
} |
| 4066 |
} |
| 4067 |
|
| 4068 |
private static final byte[] baseToNumber=AminoAcid.baseToNumber; |
| 4069 |
private static final byte[] baseToNumber0=AminoAcid.baseToNumber0; |
| 4070 |
private static final byte[] baseToComplementNumber=AminoAcid.baseToComplementNumber; |
| 4071 |
private static final byte[] baseToHashcode=AminoAcid.baseToHashcode; |
| 4072 |
private static final byte[] baseToNumberExtended=AminoAcid.baseToNumberExtended; |
| 4073 |
private static final byte[] baseToComplementNumberExtended=AminoAcid.baseToComplementNumberExtended; |
| 4074 |
|
| 4075 |
private static final ThreadLocal<int[]> localACGTN=new ThreadLocal<int[]>(){ |
| 4076 |
@Override protected int[] initialValue() {return new int[5];} |
| 4077 |
}; |
| 4078 |
|
| 4079 |
} |