| 1 |
package align2; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.Collections; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.HashSet; |
| 10 |
import java.util.LinkedHashMap; |
| 11 |
import java.util.LinkedHashSet; |
| 12 |
|
| 13 |
import dna.Data; |
| 14 |
import fileIO.FileFormat; |
| 15 |
import fileIO.ReadWrite; |
| 16 |
import fileIO.TextFile; |
| 17 |
import fileIO.TextStreamWriter; |
| 18 |
import shared.Parse; |
| 19 |
import shared.PreParser; |
| 20 |
import shared.Shared; |
| 21 |
import shared.Timer; |
| 22 |
import shared.Tools; |
| 23 |
import stream.ConcurrentReadOutputStream; |
| 24 |
import stream.Read; |
| 25 |
import stream.SiteScore; |
| 26 |
import tracker.ReadStats; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class BBSplitter { |
| 34 |
|
| 35 |
public static void main(String[] args){ |
| 36 |
if(Shared.COMMAND_LINE==null){ |
| 37 |
Shared.COMMAND_LINE=(args==null ? null : args.clone()); |
| 38 |
Shared.BBMAP_CLASS="BBSplitter"; |
| 39 |
} |
| 40 |
Timer t=new Timer(); |
| 41 |
String[] margs=processArgs(args); |
| 42 |
ReadWrite.waitForWritingToFinish(); |
| 43 |
t.stop(); |
| 44 |
Data.sysout.println("Ref merge time: \t"+t); |
| 45 |
Data.scaffoldPrefixes=true; |
| 46 |
if(MAP_MODE==MAP_NORMAL){ |
| 47 |
BBMap.main(margs); |
| 48 |
}else if(MAP_MODE==MAP_ACC){ |
| 49 |
BBMapAcc.main(margs); |
| 50 |
}else if(MAP_MODE==MAP_PACBIO){ |
| 51 |
BBMapPacBio.main(margs); |
| 52 |
}else if(MAP_MODE==MAP_PACBIOSKIMMER){ |
| 53 |
BBMapPacBioSkimmer.main(margs); |
| 54 |
}else{ |
| 55 |
throw new RuntimeException(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
clearStatics(); |
| 60 |
} |
| 61 |
|
| 62 |
public static String[] processArgs(String[] args){ |
| 63 |
{ |
| 64 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 65 |
args=pp.args; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
String[] oldargs=args; |
| 71 |
args=remakeArgs(args); |
| 72 |
if(args!=oldargs){ |
| 73 |
Data.sysout.println("Converted arguments to "+Arrays.toString(args)); |
| 74 |
} |
| 75 |
|
| 76 |
AbstractMapper.DEFAULT_OUTPUT_FORMAT=FileFormat.FASTQ; |
| 77 |
ReadWrite.ZIPLEVEL=2; |
| 78 |
|
| 79 |
Timer t=new Timer(); |
| 80 |
|
| 81 |
|
| 82 |
int ziplevel=-1; |
| 83 |
int build=1; |
| 84 |
|
| 85 |
LinkedHashSet<String> nameSet=new LinkedHashSet<String>(); |
| 86 |
HashMap<String, LinkedHashSet<String>> table=new HashMap<String, LinkedHashSet<String>>(); |
| 87 |
|
| 88 |
ArrayList<String> unparsed=new ArrayList<String>(); |
| 89 |
|
| 90 |
String basename=null; |
| 91 |
|
| 92 |
for(int i=0; i<args.length; i++){ |
| 93 |
final String arg=args[i]; |
| 94 |
final String[] split=arg.split("="); |
| 95 |
String a0=split[0]; |
| 96 |
String a=a0.toLowerCase(); |
| 97 |
String b=split.length>1 ? split[1] : null; |
| 98 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 99 |
|
| 100 |
if(a.equals("blacklist") || a.equals("ref_blacklist")){a="ref_blacklist";} |
| 101 |
if(a.equals("whitelist") || a.equals("ref_whitelist")){a="ref_whitelist";} |
| 102 |
if(a.equals("ref") || a.equals("reference")){a="ref_ref";} |
| 103 |
|
| 104 |
if(b!=null && (a.startsWith("ref_"))){ |
| 105 |
String setName=a0.substring(4); |
| 106 |
if(setName.indexOf(',')>=0){setName=setName.replace(',', '_');} |
| 107 |
if(setName.indexOf('$')>=0){setName=setName.replace('$', '_');} |
| 108 |
nameSet.add(setName); |
| 109 |
if(!table.containsKey(setName)){table.put(setName, new LinkedHashSet<String>());} |
| 110 |
LinkedHashSet<String> set=table.get(setName); |
| 111 |
|
| 112 |
File f; |
| 113 |
if((f=new File(b)).exists()){ |
| 114 |
try { |
| 115 |
String s=f.getCanonicalPath(); |
| 116 |
set.add(s); |
| 117 |
} catch (IOException e) { |
| 118 |
|
| 119 |
e.printStackTrace(); |
| 120 |
} |
| 121 |
}else{ |
| 122 |
for(String x : b.split(",")){ |
| 123 |
f=new File(x); |
| 124 |
if(f.exists()){ |
| 125 |
try { |
| 126 |
set.add(f.getCanonicalPath()); |
| 127 |
} catch (IOException e) { |
| 128 |
|
| 129 |
e.printStackTrace(); |
| 130 |
} |
| 131 |
}else{ |
| 132 |
assert(x.startsWith("stdin")) : "Can't find file "+x; |
| 133 |
set.add(x); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
}else{ |
| 139 |
if(a.startsWith("-xmx") || a.startsWith("-xms")){ |
| 140 |
|
| 141 |
}else if(a.equals("path") || a.equals("root")){ |
| 142 |
Data.setPath(b); |
| 143 |
}else if(a.equals("ziplevel") || a.equals("zl")){ |
| 144 |
ReadWrite.ZIPLEVEL=Integer.parseInt(b); |
| 145 |
unparsed.add(args[i]); |
| 146 |
}else if(a.equals("build")){ |
| 147 |
build=Integer.parseInt(b); |
| 148 |
unparsed.add(args[i]); |
| 149 |
}else if(a.equals("basename") || a.equals("pattern")){ |
| 150 |
basename=b; |
| 151 |
assert(b==null || (b.indexOf('%')>=0 && (b.indexOf('%')<b.lastIndexOf('.')))) : |
| 152 |
"basename must contain a '%' symbol prior to file extension."; |
| 153 |
}else if(a.equals("append") || a.equals("app")){ |
| 154 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 155 |
|
| 156 |
unparsed.add(args[i]); |
| 157 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 158 |
overwrite=Parse.parseBoolean(b); |
| 159 |
|
| 160 |
unparsed.add(args[i]); |
| 161 |
}else if(a.equals("verbose")){ |
| 162 |
verbose=Parse.parseBoolean(b); |
| 163 |
unparsed.add(args[i]); |
| 164 |
}else if(a.equals("rebuild")){ |
| 165 |
forceRebuild=Parse.parseBoolean(b); |
| 166 |
unparsed.add(args[i]); |
| 167 |
}else if(a.equals("fastawrap") || a.equals("wrap")){ |
| 168 |
Shared.FASTA_WRAP=Integer.parseInt(b); |
| 169 |
}else{ |
| 170 |
unparsed.add(args[i]); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
String refname=mergeReferences(nameSet, table, build); |
| 176 |
ArrayList<String> outnames=gatherLists(nameSet, basename); |
| 177 |
|
| 178 |
unparsed.add("ref="+refname); |
| 179 |
|
| 180 |
String[] margs=new String[unparsed.size()+(outnames==null ? 0 : outnames.size())]; |
| 181 |
int idx=0; |
| 182 |
for(int i=0; i<unparsed.size(); i++){ |
| 183 |
margs[idx]=unparsed.get(i); |
| 184 |
idx++; |
| 185 |
} |
| 186 |
if(outnames!=null){ |
| 187 |
for(int i=0; i<outnames.size(); i++){ |
| 188 |
margs[idx]=outnames.get(i); |
| 189 |
idx++; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return margs; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
public static String[] remakeArgs(String[] args){ |
| 198 |
|
| 199 |
LinkedHashSet<String> set=new LinkedHashSet<String>(); |
| 200 |
HashMap<String,LinkedHashSet<String>> map=new HashMap<String,LinkedHashSet<String>>(); |
| 201 |
int removed=0; |
| 202 |
|
| 203 |
for(int i=0; i<args.length; i++){ |
| 204 |
final String arg=args[i]; |
| 205 |
final String[] split=arg.split("="); |
| 206 |
String a=split[0].toLowerCase(); |
| 207 |
String b=split.length>1 ? split[1] : null; |
| 208 |
|
| 209 |
if(a.equals("mapmode") && b!=null){ |
| 210 |
args[i]=null; |
| 211 |
removed++; |
| 212 |
if(b.equalsIgnoreCase("normal")){MAP_MODE=MAP_NORMAL;} |
| 213 |
else if(b.equalsIgnoreCase("accurate") || b.equalsIgnoreCase("acc")){MAP_MODE=MAP_ACC;} |
| 214 |
else if(b.equalsIgnoreCase("pacbio") || b.equalsIgnoreCase("pb") || b.equalsIgnoreCase("bp")){MAP_MODE=MAP_PACBIO;} |
| 215 |
else if(b.equalsIgnoreCase("pacbioskimmer") || b.equalsIgnoreCase("pbs") || b.equalsIgnoreCase("bps")){MAP_MODE=MAP_PACBIOSKIMMER;} |
| 216 |
else{throw new RuntimeException("Unknown mode: "+b);} |
| 217 |
}else if(a.equals("ref") && b!=null){ |
| 218 |
args[i]=null; |
| 219 |
removed++; |
| 220 |
processRef(b, set, map); |
| 221 |
} |
| 222 |
} |
| 223 |
if(set.isEmpty() && removed==0){return args;} |
| 224 |
if(MAP_MODE==MAP_ACC){removed--;} |
| 225 |
String[] args2=new String[args.length+set.size()-removed]; |
| 226 |
|
| 227 |
int i=0, j=0; |
| 228 |
if(MAP_MODE==MAP_ACC){ |
| 229 |
args2[j]="minratio=0.4"; |
| 230 |
j++; |
| 231 |
} |
| 232 |
for(; i<args.length; i++){ |
| 233 |
if(args[i]!=null){ |
| 234 |
args2[j]=args[i]; |
| 235 |
j++; |
| 236 |
} |
| 237 |
} |
| 238 |
for(String key : set){ |
| 239 |
LinkedHashSet<String> list=map.get(key); |
| 240 |
StringBuilder sb=new StringBuilder(200); |
| 241 |
sb.append("ref_"+key+"="); |
| 242 |
String comma=""; |
| 243 |
for(String s : list){ |
| 244 |
sb.append(comma); |
| 245 |
sb.append(s); |
| 246 |
comma=","; |
| 247 |
} |
| 248 |
args2[j]=sb.toString(); |
| 249 |
j++; |
| 250 |
} |
| 251 |
return args2; |
| 252 |
} |
| 253 |
|
| 254 |
private static void processRef(String b, LinkedHashSet<String> set, HashMap<String,LinkedHashSet<String>> map){ |
| 255 |
|
| 256 |
ArrayList<String> files=Tools.getFileOrFiles(b, null, true, false, false, false); |
| 257 |
for(String file : files){ |
| 258 |
String name=file.replace('\\', '/'); |
| 259 |
int x=name.lastIndexOf('/'); |
| 260 |
if(x>=0){name=name.substring(x+1);} |
| 261 |
while(name.endsWith(".zip") || name.endsWith(".bz2") || name.endsWith(".gz") || name.endsWith(".gzip")){ |
| 262 |
name=name.substring(0, name.lastIndexOf('.')); |
| 263 |
} |
| 264 |
if(name.lastIndexOf('.')>=0){ |
| 265 |
name=name.substring(0, name.lastIndexOf('.')); |
| 266 |
} |
| 267 |
set.add(name); |
| 268 |
LinkedHashSet<String> list=map.get(name); |
| 269 |
if(list==null){ |
| 270 |
list=new LinkedHashSet<String>(); |
| 271 |
map.put(name, list); |
| 272 |
} |
| 273 |
list.add(file); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
public static ArrayList<String> gatherLists(LinkedHashSet<String> nameSet, String basename){ |
| 278 |
if(basename==null){return null;} |
| 279 |
ArrayList<String> args=new ArrayList<String>(); |
| 280 |
for(String name : nameSet){ |
| 281 |
if(basename!=null){ |
| 282 |
args.add("out_"+name+"="+(basename.replaceFirst("%", name))); |
| 283 |
} |
| 284 |
} |
| 285 |
return args; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
public static String mergeReferences(LinkedHashSet<String> nameSet, HashMap<String, LinkedHashSet<String>> nameToFileTable, int build){ |
| 290 |
LinkedHashSet<String> fnames=new LinkedHashSet<String>(); |
| 291 |
|
| 292 |
|
| 293 |
addNames(fnames, nameToFileTable, "whitelist"); |
| 294 |
for(String s : nameSet){ |
| 295 |
if(!s.equalsIgnoreCase("blacklist") && !s.equalsIgnoreCase("whitelist")){ |
| 296 |
addNames(fnames, nameToFileTable, s); |
| 297 |
} |
| 298 |
} |
| 299 |
addNames(fnames, nameToFileTable, "blacklist"); |
| 300 |
|
| 301 |
final HashMap<String, LinkedHashSet<String>> fileToNameTable=new HashMap<String, LinkedHashSet<String>>(); |
| 302 |
for(String name : nameSet){ |
| 303 |
LinkedHashSet<String> files=nameToFileTable.get(name); |
| 304 |
if(files!=null){ |
| 305 |
for(String f : files){ |
| 306 |
LinkedHashSet<String> names=fileToNameTable.get(f); |
| 307 |
if(names==null){ |
| 308 |
names=new LinkedHashSet<String>(); |
| 309 |
fileToNameTable.put(f, names); |
| 310 |
} |
| 311 |
names.add(name); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
final String root=Data.ROOT_GENOME+build; |
| 317 |
{ |
| 318 |
File f=new File(root); |
| 319 |
if(!f.exists()){f.mkdirs();} |
| 320 |
} |
| 321 |
|
| 322 |
{ |
| 323 |
final String reflist=root+"/reflist.txt"; |
| 324 |
final String namelist=root+"/namelist.txt"; |
| 325 |
final boolean reflistExists=new File(reflist).exists(); |
| 326 |
boolean writeReflist=false; |
| 327 |
String[] oldrefs=null; |
| 328 |
String[] oldnames=null; |
| 329 |
if(reflistExists){ |
| 330 |
TextFile tf=new TextFile(reflist, false); |
| 331 |
oldrefs=tf.toStringLines(); |
| 332 |
tf.close(); |
| 333 |
|
| 334 |
tf=new TextFile(namelist, false); |
| 335 |
oldnames=tf.toStringLines(); |
| 336 |
tf.close(); |
| 337 |
} |
| 338 |
if(fnames.size()>0){ |
| 339 |
writeReflist=true; |
| 340 |
ArrayList<String> fl=new ArrayList<String>(fnames.size()); |
| 341 |
fl.addAll(fnames); |
| 342 |
ArrayList<String> nl=new ArrayList<String>(nameSet.size()); |
| 343 |
nl.addAll(nameSet); |
| 344 |
|
| 345 |
}else{ |
| 346 |
assert(oldrefs!=null) : "No reference specified, and none exists. Please regenerate the index."; |
| 347 |
for(String s : oldrefs){fnames.add(s);} |
| 348 |
|
| 349 |
assert(oldnames!=null) : "No reference specified, and none exists. Please regenerate the index."; |
| 350 |
for(String s : oldnames){nameSet.add(s);} |
| 351 |
|
| 352 |
writeReflist=false; |
| 353 |
} |
| 354 |
if(writeReflist){ |
| 355 |
{ |
| 356 |
|
| 357 |
|
| 358 |
TextStreamWriter tsw=new TextStreamWriter(reflist, overwrite, append, false); |
| 359 |
tsw.start(); |
| 360 |
for(String s : fnames){tsw.println(s);} |
| 361 |
tsw.poisonAndWait(); |
| 362 |
assert(new File(reflist).exists()) : reflist+".exists? "+new File(reflist).exists(); |
| 363 |
} |
| 364 |
{ |
| 365 |
|
| 366 |
TextStreamWriter tsw=new TextStreamWriter(namelist, overwrite, append, false); |
| 367 |
tsw.start(); |
| 368 |
for(String s : nameSet){tsw.println(s);} |
| 369 |
tsw.poisonAndWait(); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if(fnames.size()<1){ |
| 375 |
assert(false) : "No references specified." + |
| 376 |
"\nTODO: This is really annoying; I need to include reference names in some auxillary file."; |
| 377 |
return null; |
| 378 |
}else if(fnames.size()==1){ |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
long key=0; |
| 385 |
for(String s : nameSet){ |
| 386 |
key=Long.rotateLeft(key, 21); |
| 387 |
key=key^s.hashCode(); |
| 388 |
|
| 389 |
} |
| 390 |
key=(key&Long.MAX_VALUE); |
| 391 |
String refname0="merged_ref_"+key+".fa.gz"; |
| 392 |
String refname=root+"/"+refname0; |
| 393 |
|
| 394 |
if(!forceRebuild){ |
| 395 |
File f=new File(refname); |
| 396 |
if(f.exists()){ |
| 397 |
|
| 398 |
Data.sysout.println("Merged reference file "+refname+" already exists; skipping merge."); |
| 399 |
return refname; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
Data.sysout.println("Creating merged reference file "+refname); |
| 408 |
|
| 409 |
TextStreamWriter tsw=new TextStreamWriter(refname, overwrite || forceRebuild, false, true); |
| 410 |
tsw.start(); |
| 411 |
for(String fname : fnames){ |
| 412 |
TextFile tf=new TextFile(fname, false); |
| 413 |
LinkedHashSet<String> listnames=fileToNameTable.get(fname); |
| 414 |
|
| 415 |
String prefix=null; |
| 416 |
{ |
| 417 |
StringBuilder sb=new StringBuilder(100); |
| 418 |
sb.append('>'); |
| 419 |
if(listnames!=null){ |
| 420 |
String sep=""; |
| 421 |
for(String s : listnames){ |
| 422 |
sb.append(sep); |
| 423 |
sb.append(s); |
| 424 |
sep=","; |
| 425 |
} |
| 426 |
} |
| 427 |
sb.append('$'); |
| 428 |
prefix=sb.toString(); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 433 |
if(prefix!=null && line.charAt(0)=='>'){ |
| 434 |
|
| 435 |
tsw.print(prefix); |
| 436 |
tsw.println(line.substring(1)); |
| 437 |
}else{ |
| 438 |
tsw.println(line); |
| 439 |
} |
| 440 |
} |
| 441 |
tf.close(); |
| 442 |
} |
| 443 |
tsw.poisonAndWait(); |
| 444 |
|
| 445 |
return refname; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public static HashSet<String> getScaffoldAffixes(boolean getPrefixes){ |
| 454 |
final byte[][][] b3=Data.scaffoldNames; |
| 455 |
|
| 456 |
int size=(int)Tools.min((10+Data.numScaffolds*3)/2, Integer.MAX_VALUE); |
| 457 |
HashSet<String> set=new HashSet<String>(size); |
| 458 |
|
| 459 |
assert(b3!=null); |
| 460 |
for(byte[][] b2 : b3){ |
| 461 |
if(b2!=null){ |
| 462 |
for(byte[] bname : b2){ |
| 463 |
if(bname!=null){ |
| 464 |
int idx=Tools.indexOf(bname, (byte)'$'); |
| 465 |
String prefix=null, suffix=null; |
| 466 |
if(idx>=0){ |
| 467 |
if(getPrefixes){prefix=new String(bname, 0, idx);} |
| 468 |
else{suffix=new String(bname, idx+1, bname.length-idx-1);} |
| 469 |
}else{ |
| 470 |
if(!getPrefixes){suffix=new String(bname);} |
| 471 |
} |
| 472 |
|
| 473 |
if(getPrefixes){ |
| 474 |
if(prefix!=null){ |
| 475 |
if(prefix.indexOf(',')>=0){ |
| 476 |
for(String s : prefix.split(",")){ |
| 477 |
set.add(s); |
| 478 |
} |
| 479 |
}else{ |
| 480 |
set.add(prefix); |
| 481 |
} |
| 482 |
} |
| 483 |
}else{ |
| 484 |
if(suffix!=null){ |
| 485 |
set.add(suffix); |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
return set; |
| 493 |
} |
| 494 |
|
| 495 |
public static synchronized HashMap<String, ConcurrentReadOutputStream> makeOutputStreams(String[] args, boolean OUTPUT_READS, boolean OUTPUT_ORDERED_READS, |
| 496 |
int buff, boolean paired, boolean overwrite_, boolean append_, boolean ambiguous){ |
| 497 |
|
| 498 |
HashMap<String, ConcurrentReadOutputStream> table=new HashMap<String, ConcurrentReadOutputStream>(); |
| 499 |
for(String arg : args){ |
| 500 |
String[] split=arg.split("="); |
| 501 |
String a=split[0]; |
| 502 |
String b=split.length>1 ? split[1] : null; |
| 503 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 504 |
|
| 505 |
|
| 506 |
if(arg.indexOf('=')>0 && a.toLowerCase().startsWith("out_")){ |
| 507 |
assert(b!=null) : "Bad parameter: "+arg+"\n"+Arrays.toString(args); |
| 508 |
String name=a.substring(4).replace('\\', '/'); |
| 509 |
|
| 510 |
final String fname1, fname2; |
| 511 |
|
| 512 |
if(ambiguous){ |
| 513 |
if(b.indexOf('/')>=0){ |
| 514 |
int x=b.lastIndexOf('/'); |
| 515 |
b=b.substring(0, x+1)+"AMBIGUOUS_"+b.substring(x+1); |
| 516 |
}else{ |
| 517 |
b="AMBIGUOUS_"+b; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if(!FileFormat.hasSamOrBamExtension(b) && ReadWrite.stripExtension(b).contains("#")){ |
| 522 |
fname1=b.replace('#', '1'); |
| 523 |
fname2=b.replace('#', '2'); |
| 524 |
}else{ |
| 525 |
fname1=b; |
| 526 |
fname2=null; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
FileFormat ff1=FileFormat.testOutput(fname1, FileFormat.SAM, null, true, overwrite_, append_, OUTPUT_ORDERED_READS); |
| 532 |
FileFormat ff2=paired ? FileFormat.testOutput(fname2, FileFormat.SAM, null, true, overwrite_, append_, OUTPUT_ORDERED_READS) : null; |
| 533 |
ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ff1, ff2, null, null, buff, null, false); |
| 534 |
ros.start(); |
| 535 |
|
| 536 |
table.put(name, ros); |
| 537 |
AbstractMapThread.OUTPUT_SAM|=ff1.samOrBam(); |
| 538 |
} |
| 539 |
} |
| 540 |
return table.isEmpty() ? null : table; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
public static synchronized LinkedHashMap<String, SetCount> makeSetCountTable(){ |
| 545 |
assert(setCountTable==null); |
| 546 |
HashSet<String> names=getScaffoldAffixes(true); |
| 547 |
setCountTable=new LinkedHashMap<String, SetCount>(); |
| 548 |
for(String s : names){setCountTable.put(s, new SetCount(s));} |
| 549 |
return setCountTable; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
public static synchronized LinkedHashMap<String, SetCount> makeScafCountTable(){ |
| 554 |
assert(scafCountTable==null); |
| 555 |
HashSet<String> names=getScaffoldAffixes(false); |
| 556 |
scafCountTable=new LinkedHashMap<String, SetCount>(); |
| 557 |
for(String s : names){scafCountTable.put(s, new SetCount(s));} |
| 558 |
|
| 559 |
return scafCountTable; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
public static void printReads(ArrayList<Read> readlist, long listID, HashMap<String, ArrayList<Read>> splitTable, int clearzone){ |
| 570 |
if(clearzone>=0 || TRACK_SET_STATS || TRACK_SCAF_STATS){ |
| 571 |
printReadsAndProcessAmbiguous(readlist, listID, splitTable, null, clearzone); |
| 572 |
return; |
| 573 |
} |
| 574 |
assert((streamTable!=null && streamTable.size()>0) || (setCountTable!=null && setCountTable.size()>0) || (scafCountTable!=null && scafCountTable.size()>0)); |
| 575 |
boolean clear=true; |
| 576 |
if(splitTable==null){ |
| 577 |
splitTable=new HashMap<String, ArrayList<Read>>(); |
| 578 |
clear=false; |
| 579 |
} |
| 580 |
|
| 581 |
if(!readlist.isEmpty()){ |
| 582 |
HashSet<String> set=new HashSet<String>(8); |
| 583 |
for(Read r : readlist){ |
| 584 |
if(r!=null){ |
| 585 |
set=toListNames(r, set); |
| 586 |
for(String s : set){ |
| 587 |
ArrayList<Read> alr=splitTable.get(s); |
| 588 |
if(alr==null){ |
| 589 |
alr=new ArrayList<Read>(); |
| 590 |
splitTable.put(s, alr); |
| 591 |
} |
| 592 |
|
| 593 |
alr.add(r); |
| 594 |
} |
| 595 |
set.clear(); |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
for(String s : streamTable.keySet()){ |
| 601 |
ArrayList<Read> alr=splitTable.get(s); |
| 602 |
if(alr==null){alr=blank;} |
| 603 |
ConcurrentReadOutputStream tros=streamTable.get(s); |
| 604 |
tros.add(alr, listID); |
| 605 |
} |
| 606 |
if(clear){splitTable.clear();} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
public static void printReadsAndProcessAmbiguous(ArrayList<Read> readlist, long listID, HashMap<String, ArrayList<Read>> splitTable, |
| 617 |
HashMap<String, ArrayList<Read>> splitTableA, int clearzone){ |
| 618 |
assert(clearzone>=0 || TRACK_SET_STATS || TRACK_SCAF_STATS); |
| 619 |
assert((streamTable!=null && streamTable.size()>0) || (setCountTable!=null && setCountTable.size()>0) || (scafCountTable!=null && scafCountTable.size()>0)); |
| 620 |
boolean clear=streamTable!=null, clearA=streamTableAmbiguous!=null; |
| 621 |
if(splitTable==null && streamTable!=null){ |
| 622 |
splitTable=new HashMap<String, ArrayList<Read>>(); |
| 623 |
clear=false; |
| 624 |
} |
| 625 |
if(splitTableA==null && streamTableAmbiguous!=null){ |
| 626 |
splitTableA=new HashMap<String, ArrayList<Read>>(); |
| 627 |
clearA=false; |
| 628 |
} |
| 629 |
|
| 630 |
final HashSet<String> hss0, hss1, hss2, hss3, hsspr, hssam; |
| 631 |
final HashSet<String>[] hssa; |
| 632 |
if(TRACK_SET_STATS || streamTable!=null){ |
| 633 |
hss0=new HashSet<String>(16); |
| 634 |
hss1=new HashSet<String>(16); |
| 635 |
hss2=new HashSet<String>(16); |
| 636 |
hss3=new HashSet<String>(16); |
| 637 |
hsspr=new HashSet<String>(16); |
| 638 |
hssam=new HashSet<String>(16); |
| 639 |
hssa=(HashSet<String>[])new HashSet[] {hss0, hss1, hss2, hss3}; |
| 640 |
}else if(TRACK_SCAF_STATS){ |
| 641 |
hss0=new HashSet<String>(16); |
| 642 |
hss1=null; hss2=null; hss3=null; hsspr=null; hssam=null; hssa=null; |
| 643 |
}else{ |
| 644 |
hss0=null; hss1=null; hss2=null; hss3=null; hsspr=null; hssam=null; hssa=null; |
| 645 |
} |
| 646 |
|
| 647 |
for(final Read r1 : readlist){ |
| 648 |
|
| 649 |
final Read r2=r1==null ? null : r1.mate; |
| 650 |
|
| 651 |
if(r1!=null){addToScafCounts(r1, clearzone, hss0);} |
| 652 |
if(r2!=null){addToScafCounts(r2, clearzone, hss0);} |
| 653 |
|
| 654 |
if(r1!=null){ |
| 655 |
|
| 656 |
final HashSet<String>[] sets=(TRACK_SET_STATS || streamTable!=null) ? getSets(r1, clearzone, hssa) : null; |
| 657 |
boolean ambiguous=false; |
| 658 |
if(sets!=null){ |
| 659 |
final HashSet<String> p1=(sets[0].isEmpty() ? null : sets[0]), s1=(sets[1].isEmpty() ? null : sets[1]), |
| 660 |
p2=(sets[2].isEmpty() ? null : sets[2]), s2=(sets[3].isEmpty() ? null : sets[3]); |
| 661 |
assert(sets==hssa); |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
if(p1!=null && p2!=null && !p1.equals(p2)){ambiguous=true;} |
| 668 |
else if(p1!=null && s1!=null && !p1.containsAll(s1)){ambiguous=true;} |
| 669 |
else if(p2!=null && s2!=null && !p2.containsAll(s2)){ambiguous=true;} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
HashSet<String> primarySet=hsspr, ambigSet=hssam; |
| 676 |
primarySet.clear(); |
| 677 |
ambigSet.clear(); |
| 678 |
if(AMBIGUOUS2_MODE==AMBIGUOUS2_FIRST || AMBIGUOUS2_MODE==AMBIGUOUS2_UNSET){ |
| 679 |
if(r2==null || r1.mapScore>=r2.mapScore){ |
| 680 |
if(p1!=null){primarySet.addAll(p1);} |
| 681 |
}else{ |
| 682 |
if(p2!=null){primarySet.addAll(p2);} |
| 683 |
} |
| 684 |
}else{ |
| 685 |
if(p1!=null){primarySet.addAll(p1);} |
| 686 |
if(p2!=null){primarySet.addAll(p2);} |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
if(ambiguous){ |
| 691 |
if(AMBIGUOUS2_MODE==AMBIGUOUS2_SPLIT){ |
| 692 |
if(primarySet!=null && s1!=null){primarySet.addAll(s1);} |
| 693 |
if(primarySet!=null && s2!=null){primarySet.addAll(s2);} |
| 694 |
ambigSet=primarySet; |
| 695 |
primarySet=null; |
| 696 |
}else if(AMBIGUOUS2_MODE==AMBIGUOUS2_ALL){ |
| 697 |
if(primarySet!=null && s1!=null){primarySet.addAll(s1);} |
| 698 |
if(primarySet!=null && s2!=null){primarySet.addAll(s2);} |
| 699 |
ambigSet=null; |
| 700 |
}else if(AMBIGUOUS2_MODE==AMBIGUOUS2_RANDOM){ |
| 701 |
throw new RuntimeException("AMBIGUOUS2_RANDOM: Not yet implemented."); |
| 702 |
}else if(AMBIGUOUS2_MODE==AMBIGUOUS2_TOSS){ |
| 703 |
primarySet=null; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
if(primarySet!=null && splitTable!=null){ |
| 708 |
for(String s : primarySet){ |
| 709 |
ArrayList<Read> alr=splitTable.get(s); |
| 710 |
if(alr==null){ |
| 711 |
alr=new ArrayList<Read>(); |
| 712 |
splitTable.put(s, alr); |
| 713 |
} |
| 714 |
alr.add(r1); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
if(ambigSet!=null && splitTableA!=null){ |
| 719 |
for(String s : ambigSet){ |
| 720 |
ArrayList<Read> alr=splitTableA.get(s); |
| 721 |
if(alr==null){ |
| 722 |
alr=new ArrayList<Read>(); |
| 723 |
splitTableA.put(s, alr); |
| 724 |
} |
| 725 |
alr.add(r1); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
if(setCountTable!=null){ |
| 730 |
|
| 731 |
primarySet=hsspr; |
| 732 |
primarySet.clear(); |
| 733 |
if(p1!=null){primarySet.addAll(p1);} |
| 734 |
if(p2!=null){primarySet.addAll(p2);} |
| 735 |
if(ambiguous){ |
| 736 |
if(s1!=null){primarySet.addAll(s1);} |
| 737 |
if(s2!=null){primarySet.addAll(s2);} |
| 738 |
} |
| 739 |
|
| 740 |
final int incrR=r1.pairCount(); |
| 741 |
final int incrB=r1.pairLength(); |
| 742 |
|
| 743 |
int num=0; |
| 744 |
for(String s : primarySet){ |
| 745 |
SetCount sc=setCountTable.get(s); |
| 746 |
assert(sc!=null) : s; |
| 747 |
if(ambiguous){ |
| 748 |
synchronized(sc){ |
| 749 |
|
| 750 |
sc.ambiguousReads+=incrR; |
| 751 |
sc.ambiguousBases+=incrB; |
| 752 |
if(num==0){ |
| 753 |
sc.assignedReads+=incrR; |
| 754 |
sc.assignedBases+=incrB; |
| 755 |
} |
| 756 |
} |
| 757 |
}else{ |
| 758 |
synchronized(sc){ |
| 759 |
|
| 760 |
sc.mappedReads+=incrR; |
| 761 |
sc.mappedBases+=incrB; |
| 762 |
if(num==0){ |
| 763 |
sc.assignedReads+=incrR; |
| 764 |
sc.assignedBases+=incrB; |
| 765 |
} |
| 766 |
} |
| 767 |
} |
| 768 |
num++; |
| 769 |
} |
| 770 |
} |
| 771 |
for(HashSet<String> set : sets){set.clear();} |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
if(streamTable!=null){ |
| 776 |
for(String s : streamTable.keySet()){ |
| 777 |
|
| 778 |
|
| 779 |
ArrayList<Read> alr=splitTable.get(s); |
| 780 |
|
| 781 |
if(alr==null){alr=blank;} |
| 782 |
ConcurrentReadOutputStream tros=streamTable.get(s); |
| 783 |
tros.add(alr, listID); |
| 784 |
} |
| 785 |
} |
| 786 |
if(streamTableAmbiguous!=null){ |
| 787 |
for(String s : streamTableAmbiguous.keySet()){ |
| 788 |
ArrayList<Read> alr=splitTableA.get(s); |
| 789 |
if(alr==null){alr=blank;} |
| 790 |
ConcurrentReadOutputStream tros=streamTableAmbiguous.get(s); |
| 791 |
tros.add(alr, listID); |
| 792 |
} |
| 793 |
} |
| 794 |
if(clear){splitTable.clear();} |
| 795 |
if(clearA){splitTableA.clear();} |
| 796 |
} |
| 797 |
|
| 798 |
private static void addToScafCounts(Read r, int clearzone, HashSet<String> hss0){ |
| 799 |
if(r==null || !r.mapped()){return;} |
| 800 |
assert((scafCountTable!=null)==TRACK_SCAF_STATS) : TRACK_SCAF_STATS; |
| 801 |
if(scafCountTable!=null){ |
| 802 |
HashSet<String> set=getScaffolds(r, clearzone, hss0, false); |
| 803 |
if(set!=null && !set.isEmpty()){ |
| 804 |
int incrRM=0; |
| 805 |
int incrRA=0; |
| 806 |
int incrBM=0; |
| 807 |
int incrBA=0; |
| 808 |
|
| 809 |
int incrRS=1+(r.mate!=null && !r.mateMapped() ? 1 : 0); |
| 810 |
int incrBS=r.length()+(r.mate!=null && !r.mateMapped() ? r.mateLength() : 0); |
| 811 |
|
| 812 |
|
| 813 |
if(r.ambiguous()){ |
| 814 |
incrRA+=1; |
| 815 |
incrBA+=r.length(); |
| 816 |
if(r.mate!=null && !r.mateMapped()){ |
| 817 |
incrRA++; |
| 818 |
incrBA+=r.mateLength(); |
| 819 |
} |
| 820 |
}else{ |
| 821 |
incrRM+=1; |
| 822 |
incrBM+=r.length(); |
| 823 |
} |
| 824 |
int num=0; |
| 825 |
for(String s : set){ |
| 826 |
SetCount sc=scafCountTable.get(s); |
| 827 |
assert(sc!=null) : "Can't find "+s+"\nin\n"+scafCountTable.keySet()+"\n"; |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
synchronized(sc){ |
| 832 |
|
| 833 |
sc.mappedReads+=incrRM; |
| 834 |
sc.mappedBases+=incrBM; |
| 835 |
sc.ambiguousReads+=incrRA; |
| 836 |
sc.ambiguousBases+=incrBA; |
| 837 |
if(num==0){ |
| 838 |
sc.assignedReads+=incrRS; |
| 839 |
sc.assignedBases+=incrBS; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
num++; |
| 846 |
} |
| 847 |
set.clear(); |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
public static HashSet<String>[] getSets(Read r1, int clearzone, HashSet<String>[] sets){ |
| 855 |
Read r2=r1.mate; |
| 856 |
if(!r1.mapped() && (r2==null || !r2.mapped())){return null;} |
| 857 |
|
| 858 |
if(sets==null){ |
| 859 |
assert(false); |
| 860 |
sets=new HashSet[4]; |
| 861 |
}else{ |
| 862 |
for(HashSet<String> set : sets){ |
| 863 |
assert(set==null || set.isEmpty()); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
HashSet<String> primary1=sets[0], other1=sets[1], primary2=sets[2], other2=sets[3]; |
| 868 |
if(r1.mapped()){ |
| 869 |
|
| 870 |
SiteScore s0=r1.topSite(); |
| 871 |
primary1=toListNames(s0, primary1); |
| 872 |
for(int i=1; i<r1.sites.size(); i++){ |
| 873 |
SiteScore ss=r1.sites.get(i); |
| 874 |
if(ss.score+clearzone<s0.score){break;} |
| 875 |
other1=toListNames(ss, other1); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
} |
| 880 |
if(r2!=null && r2.mapped()){ |
| 881 |
SiteScore s0=r2.topSite(); |
| 882 |
primary2=toListNames(s0, primary2); |
| 883 |
for(int i=1; i<r2.sites.size(); i++){ |
| 884 |
SiteScore ss=r2.sites.get(i); |
| 885 |
if(ss.score+clearzone<s0.score){break;} |
| 886 |
other2=toListNames(ss, other2); |
| 887 |
} |
| 888 |
} |
| 889 |
sets[0]=primary1; |
| 890 |
sets[1]=other1; |
| 891 |
sets[2]=primary2; |
| 892 |
sets[3]=other2; |
| 893 |
return sets; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
public static HashSet<String> getScaffolds(Read r1, int clearzone, HashSet<String> set, boolean includeMate){ |
| 898 |
Read r2=(includeMate ? r1.mate : null); |
| 899 |
if(!r1.mapped() && (r2==null || !r2.mapped())){return null;} |
| 900 |
assert(set==null || set.isEmpty()); |
| 901 |
|
| 902 |
if(!r1.ambiguous() && (r2==null || !r2.ambiguous())){ |
| 903 |
byte[] scafb1=r1.getScaffoldName(false); |
| 904 |
byte[] scafb2=(r2==null ? null : r2.getScaffoldName(false)); |
| 905 |
if(scafb1==null){scafb1=scafb2;} |
| 906 |
if(scafb1==null){ |
| 907 |
assert(false) : r1; |
| 908 |
return null; |
| 909 |
} |
| 910 |
if(scafb2==null || scafb1==scafb2){ |
| 911 |
final String s; |
| 912 |
if(Data.scaffoldPrefixes){ |
| 913 |
int idx=Tools.indexOf(scafb1, (byte)'$'); |
| 914 |
assert(idx>=0) : idx+", "+new String(scafb1); |
| 915 |
s=(idx>=0 ? new String(scafb1, idx+1, scafb1.length-idx-1) : new String(scafb1)); |
| 916 |
}else{ |
| 917 |
s=new String(scafb1); |
| 918 |
} |
| 919 |
if(set==null){set=new HashSet<String>(1);} |
| 920 |
|
| 921 |
set.add(s); |
| 922 |
return set; |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
if(set==null){set=new HashSet<String>(4);} |
| 927 |
if(r1.mapped()){ |
| 928 |
SiteScore s0=r1.topSite(); |
| 929 |
for(SiteScore ss : r1.sites){ |
| 930 |
if(ss.score+clearzone<s0.score){break;} |
| 931 |
byte[] b=ss.getScaffoldName(false); |
| 932 |
assert(b!=null); |
| 933 |
final String s; |
| 934 |
if(Data.scaffoldPrefixes){ |
| 935 |
int idx=Tools.indexOf(b, (byte)'$'); |
| 936 |
assert(idx>=0) : idx+", "+new String(b); |
| 937 |
s=(idx>=0 ? new String(b, idx+1, b.length-idx-1) : new String(b)); |
| 938 |
}else{ |
| 939 |
s=new String(b); |
| 940 |
} |
| 941 |
set.add(s); |
| 942 |
} |
| 943 |
} |
| 944 |
if(r2!=null && r2.mapped()){ |
| 945 |
SiteScore s0=r2.topSite(); |
| 946 |
for(SiteScore ss : r2.sites){ |
| 947 |
if(ss.score+clearzone<s0.score){break;} |
| 948 |
byte[] b=ss.getScaffoldName(false); |
| 949 |
assert(b!=null); |
| 950 |
final String s; |
| 951 |
if(Data.scaffoldPrefixes){ |
| 952 |
int idx=Tools.indexOf(b, (byte)'$'); |
| 953 |
assert(idx>=0) : idx+", "+new String(b); |
| 954 |
s=(idx>=0 ? new String(b, idx+1, b.length-idx-1) : new String(b)); |
| 955 |
}else{ |
| 956 |
s=new String(b); |
| 957 |
} |
| 958 |
set.add(s); |
| 959 |
} |
| 960 |
} |
| 961 |
assert(set.size()>0); |
| 962 |
return set; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
public static HashSet<String> toListNames(Read r, HashSet<String> set) { |
| 971 |
if(r==null){return set;} |
| 972 |
byte[] scaf1=r.getScaffoldName(false); |
| 973 |
byte[] scaf2=(r.mate==null ? null : r.mate.getScaffoldName(false)); |
| 974 |
if(scaf1==null && scaf2==null){return set;} |
| 975 |
|
| 976 |
if(set==null){set=new HashSet<String>(8);} |
| 977 |
else{assert(set.isEmpty());} |
| 978 |
|
| 979 |
int x=scaf1==null ? -1 : Tools.indexOf(scaf1, (byte)'$'); |
| 980 |
if(x>=0){ |
| 981 |
String s=new String(scaf1, 0, x); |
| 982 |
if(s.indexOf(',')<0){ |
| 983 |
set.add(s); |
| 984 |
}else{ |
| 985 |
for(String s2 : s.split(",")){set.add(s2);} |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
x=(scaf2==null || scaf2==scaf1) ? -1 : Tools.indexOf(scaf2, (byte)'$'); |
| 990 |
if(x>=0){ |
| 991 |
String s=new String(scaf2, 0, x); |
| 992 |
if(s.indexOf(',')<0){ |
| 993 |
set.add(s); |
| 994 |
}else{ |
| 995 |
for(String s2 : s.split(",")){set.add(s2);} |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
return set; |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
public static HashMap<String, Read> toNameMap(Read r, HashMap<String, Read> map) { |
| 1008 |
|
| 1009 |
if(true){throw new RuntimeException("TODO");} |
| 1010 |
|
| 1011 |
if(r==null){return map;} |
| 1012 |
byte[] scaf1=r.getScaffoldName(false); |
| 1013 |
byte[] scaf2=(r.mate==null ? null : r.mate.getScaffoldName(false)); |
| 1014 |
if(scaf1==null && scaf2==null){return map;} |
| 1015 |
|
| 1016 |
if(map==null){map=new HashMap<String, Read>(8);} |
| 1017 |
else{assert(map.isEmpty());} |
| 1018 |
|
| 1019 |
int x=scaf1==null ? -1 : Tools.indexOf(scaf1, (byte)'$'); |
| 1020 |
if(x>=0){ |
| 1021 |
String s=new String(scaf1, 0, x); |
| 1022 |
if(s.indexOf(',')<0){ |
| 1023 |
if(!map.containsKey(s)){ |
| 1024 |
map.put(s, cloneRead(r, s)); |
| 1025 |
} |
| 1026 |
}else{ |
| 1027 |
for(String s2 : s.split(",")){ |
| 1028 |
if(!map.containsKey(s2)){ |
| 1029 |
map.put(s2, cloneRead(r, s2)); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
x=(scaf2==null || scaf2==scaf1) ? -1 : Tools.indexOf(scaf2, (byte)'$'); |
| 1036 |
if(x>=0){ |
| 1037 |
String s=new String(scaf2, 0, x); |
| 1038 |
if(s.indexOf(',')<0){ |
| 1039 |
if(!map.containsKey(s)){ |
| 1040 |
map.put(s, cloneRead(r, s)); |
| 1041 |
} |
| 1042 |
}else{ |
| 1043 |
for(String s2 : s.split(",")){ |
| 1044 |
if(!map.containsKey(s2)){ |
| 1045 |
map.put(s2, cloneRead(r, s2)); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
return map; |
| 1052 |
} |
| 1053 |
|
| 1054 |
private static Read cloneRead(Read r, String ref){ |
| 1055 |
throw new RuntimeException("TODO"); |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
public static HashSet<String> toListNames(SiteScore r, HashSet<String> set) { |
| 1064 |
if(r==null){return set;} |
| 1065 |
byte[] scaf1=r.getScaffoldName(false); |
| 1066 |
if(scaf1==null){return set;} |
| 1067 |
if(set==null){set=new HashSet<String>(8);} |
| 1068 |
int x=scaf1==null ? -1 : Tools.indexOf(scaf1, (byte)'$'); |
| 1069 |
if(x>=0){ |
| 1070 |
String s=new String(scaf1, 0, x); |
| 1071 |
if(s.indexOf(',')<0){ |
| 1072 |
set.add(s); |
| 1073 |
}else{ |
| 1074 |
for(String s2 : s.split(",")){set.add(s2);} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
return set; |
| 1078 |
} |
| 1079 |
|
| 1080 |
private static void addNames(LinkedHashSet<String> fnames, HashMap<String, LinkedHashSet<String>> table, String setName){ |
| 1081 |
LinkedHashSet<String> set=table.get(setName); |
| 1082 |
if(set==null){return;} |
| 1083 |
for(String s : set){fnames.add(s);} |
| 1084 |
} |
| 1085 |
|
| 1086 |
public static void makeBamScript(String outname, ArrayList<String> list, String...sams){ |
| 1087 |
LinkedHashSet<String> set=new LinkedHashSet<String>(); |
| 1088 |
if(sams!=null){ |
| 1089 |
for(String s : sams){ |
| 1090 |
if(s!=null && (s.endsWith(".sam") || s.endsWith(".sam.gz") || s.endsWith(".bam"))){ |
| 1091 |
set.add(s); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
if(list!=null){ |
| 1096 |
for(String s : list){ |
| 1097 |
if(s!=null && (s.endsWith(".sam") || s.endsWith(".sam.gz") || s.endsWith(".bam"))){ |
| 1098 |
set.add(s); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
} |
| 1102 |
if(streamTable!=null){ |
| 1103 |
for(ConcurrentReadOutputStream ros : streamTable.values()){ |
| 1104 |
String s=ros.fname(); |
| 1105 |
if(s.endsWith(".sam") || s.endsWith(".sam.gz") || s.endsWith(".bam")){ |
| 1106 |
set.add(s); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
TextStreamWriter tsw=new TextStreamWriter(outname, overwrite, append, false); |
| 1111 |
tsw.start(); |
| 1112 |
|
| 1113 |
String memstring=null; |
| 1114 |
if(set.size()>0){ |
| 1115 |
tsw.println("#!/bin/bash"); |
| 1116 |
|
| 1117 |
long mem=Runtime.getRuntime().maxMemory()/3400000; |
| 1118 |
mem=Tools.min(100000, mem); |
| 1119 |
if(mem<2048){memstring=mem+"M";} |
| 1120 |
else{memstring=(mem/1024)+"G";} |
| 1121 |
|
| 1122 |
tsw.println("echo \"Note: This script is designed to run with the amount of memory detected by BBMap.\""); |
| 1123 |
tsw.println("echo \" If Samtools crashes, please ensure you are running on the same platform as BBMap,\""); |
| 1124 |
tsw.println("echo \" or reduce Samtools' memory setting (the -m flag).\""); |
| 1125 |
} |
| 1126 |
|
| 1127 |
for(String sam : set){ |
| 1128 |
String bam; |
| 1129 |
if(sam.endsWith(".sam.gz")){bam=sam.substring(0, sam.length()-6)+"bam";} |
| 1130 |
else if(sam.endsWith(".sam")){bam=sam.substring(0, sam.length()-3)+"bam";} |
| 1131 |
else{bam=sam;} |
| 1132 |
String bam2=bam.substring(0, bam.length()-4)+"_sorted"; |
| 1133 |
String bam3=bam2+".bam"; |
| 1134 |
|
| 1135 |
if(Data.SAMTOOLS() && !Data.SAMTOOLS_VERSION_1x){ |
| 1136 |
|
| 1137 |
}else{ |
| 1138 |
bam2="-o "+bam2+".bam"; |
| 1139 |
} |
| 1140 |
|
| 1141 |
boolean pipe=true; |
| 1142 |
if(pipe && sam!=bam){ |
| 1143 |
|
| 1144 |
tsw.println("echo \"Note: Please ignore any warnings about 'EOF marker is absent'; " + |
| 1145 |
"this is a bug in samtools that occurs when using piped input.\""); |
| 1146 |
|
| 1147 |
tsw.println("samtools view -bShu "+sam+" | samtools sort -m "+memstring+" -@ 3 - "+bam2); |
| 1148 |
}else{ |
| 1149 |
if(sam!=bam){tsw.println("samtools view -bSh1 -o "+bam+" "+sam);} |
| 1150 |
tsw.println("samtools sort -m "+memstring+" -@ 3 "+bam+" "+bam2); |
| 1151 |
} |
| 1152 |
|
| 1153 |
tsw.println("samtools index "+bam3); |
| 1154 |
} |
| 1155 |
tsw.poisonAndWait(); |
| 1156 |
|
| 1157 |
try { |
| 1158 |
File f=new File(outname); |
| 1159 |
f.setExecutable(true, false); |
| 1160 |
} catch (Exception e) { |
| 1161 |
|
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
public static class SetCount implements Comparable<SetCount>{ |
| 1166 |
|
| 1167 |
public SetCount(String s){ |
| 1168 |
name=s; |
| 1169 |
} |
| 1170 |
|
| 1171 |
@Override |
| 1172 |
public boolean equals(Object other){return equals((SetCount)other);} |
| 1173 |
public boolean equals(SetCount other){return compareTo(other)==0;} |
| 1174 |
|
| 1175 |
@Override |
| 1176 |
public int hashCode() { |
| 1177 |
assert(false) : "This class should not be hashed."; |
| 1178 |
return super.hashCode(); |
| 1179 |
} |
| 1180 |
|
| 1181 |
@Override |
| 1182 |
public int compareTo(SetCount o) { |
| 1183 |
if(mappedReads!=o.mappedReads){return mappedReads>o.mappedReads ? 1 : -1;} |
| 1184 |
if(ambiguousReads!=o.ambiguousReads){return ambiguousReads>o.ambiguousReads ? 1 : -1;} |
| 1185 |
return name.compareTo(o.name); |
| 1186 |
} |
| 1187 |
|
| 1188 |
@Override |
| 1189 |
public String toString(){ |
| 1190 |
return name+", "+mappedReads+", "+ambiguousReads+", "+mappedBases+", "+ambiguousBases; |
| 1191 |
} |
| 1192 |
|
| 1193 |
public final String name; |
| 1194 |
public long mappedReads; |
| 1195 |
public long ambiguousReads; |
| 1196 |
public long assignedReads; |
| 1197 |
public long mappedBases; |
| 1198 |
public long ambiguousBases; |
| 1199 |
public long assignedBases; |
| 1200 |
|
| 1201 |
} |
| 1202 |
|
| 1203 |
public static void printCounts(String fname, LinkedHashMap<String, SetCount> map, boolean header, long totalReads, boolean nzo, boolean sort){ |
| 1204 |
final ArrayList<SetCount> list=new ArrayList<SetCount>(map.size()); |
| 1205 |
for(String name : map.keySet()){ |
| 1206 |
list.add(map.get(name)); |
| 1207 |
} |
| 1208 |
final TextStreamWriter tsw=new TextStreamWriter(fname, overwrite, append, false); |
| 1209 |
tsw.start(); |
| 1210 |
if(sort){ |
| 1211 |
Shared.sort(list); |
| 1212 |
Collections.reverse(list); |
| 1213 |
} |
| 1214 |
|
| 1215 |
if(header){ |
| 1216 |
tsw.print("#name\t%unambiguousReads\tunambiguousMB\t%ambiguousReads\tambiguousMB\tunambiguousReads\tambiguousReads\tassignedReads\tassignedBases\n"); |
| 1217 |
} |
| 1218 |
final StringBuilder sb=new StringBuilder(1024); |
| 1219 |
final double divR=100.0/(totalReads); |
| 1220 |
final double divB=1.0/1000000; |
| 1221 |
for(SetCount sc : list){ |
| 1222 |
if(!nzo || sc.mappedReads>0 || sc.ambiguousReads>0 || sc.assignedReads>0){ |
| 1223 |
sb.append(sc.name).append('\t'); |
| 1224 |
sb.append(Tools.format("%.5f\t", sc.mappedReads*divR)); |
| 1225 |
sb.append(Tools.format("%.6f\t", sc.mappedBases*divB)); |
| 1226 |
sb.append(Tools.format("%.5f\t", sc.ambiguousReads*divR)); |
| 1227 |
sb.append(Tools.format("%.6f\t", sc.ambiguousBases*divB)); |
| 1228 |
sb.append(sc.mappedReads).append('\t'); |
| 1229 |
sb.append(sc.ambiguousReads).append('\t'); |
| 1230 |
sb.append(sc.assignedReads).append('\t'); |
| 1231 |
sb.append(sc.assignedBases); |
| 1232 |
sb.append('\n'); |
| 1233 |
tsw.print(sb.toString()); |
| 1234 |
sb.setLength(0); |
| 1235 |
} |
| 1236 |
} |
| 1237 |
tsw.poisonAndWait(); |
| 1238 |
} |
| 1239 |
|
| 1240 |
static final void clearStatics(){ |
| 1241 |
|
| 1242 |
|
| 1243 |
setCountTable=null; |
| 1244 |
scafCountTable=null; |
| 1245 |
streamTable=null; |
| 1246 |
streamTableAmbiguous=null; |
| 1247 |
|
| 1248 |
AMBIGUOUS2_MODE=AMBIGUOUS2_UNSET; |
| 1249 |
TRACK_SET_STATS=false; |
| 1250 |
TRACK_SCAF_STATS=false; |
| 1251 |
SCAF_STATS_FILE=null; |
| 1252 |
SET_STATS_FILE=null; |
| 1253 |
overwrite=true; |
| 1254 |
append=false; |
| 1255 |
verbose=false; |
| 1256 |
forceRebuild=false; |
| 1257 |
|
| 1258 |
MAP_MODE=MAP_NORMAL; |
| 1259 |
} |
| 1260 |
|
| 1261 |
public static LinkedHashMap<String, SetCount> setCountTable=null; |
| 1262 |
public static LinkedHashMap<String, SetCount> scafCountTable=null; |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
public static HashMap<String, ConcurrentReadOutputStream> streamTable=null; |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
public static HashMap<String, ConcurrentReadOutputStream> streamTableAmbiguous=null; |
| 1273 |
public static final int AMBIGUOUS2_UNSET=0; |
| 1274 |
public static final int AMBIGUOUS2_FIRST=1; |
| 1275 |
public static final int AMBIGUOUS2_SPLIT=2; |
| 1276 |
public static final int AMBIGUOUS2_TOSS=3; |
| 1277 |
public static final int AMBIGUOUS2_RANDOM=4; |
| 1278 |
public static final int AMBIGUOUS2_ALL=5; |
| 1279 |
public static int AMBIGUOUS2_MODE=AMBIGUOUS2_UNSET; |
| 1280 |
public static boolean TRACK_SET_STATS=false; |
| 1281 |
public static boolean TRACK_SCAF_STATS=false; |
| 1282 |
public static String SCAF_STATS_FILE=null; |
| 1283 |
public static String SET_STATS_FILE=null; |
| 1284 |
public static boolean overwrite=true; |
| 1285 |
public static boolean append=false; |
| 1286 |
public static boolean verbose=false; |
| 1287 |
public static boolean forceRebuild=false; |
| 1288 |
private static final ArrayList<Read> blank=new ArrayList<Read>(0); |
| 1289 |
|
| 1290 |
public static final int MAP_NORMAL=1; |
| 1291 |
public static final int MAP_ACC=2; |
| 1292 |
public static final int MAP_PACBIO=3; |
| 1293 |
public static final int MAP_PACBIOSKIMMER=4; |
| 1294 |
public static int MAP_MODE=MAP_NORMAL; |
| 1295 |
|
| 1296 |
} |