Debian Codegraph @

current/pacbio/MakePacBioScript.java

raw symbols SCIP
1 package pacbio;
2
3 import java.io.File;
4
5 import dna.Data;
6 import fileIO.ReadWrite;
7 import fileIO.TextFile;
8 import shared.Parse;
9 import shared.Shared;
10
11 /**
12 * @author Brian Bushnell
13 * @date Oct 2, 2012
14 *
15 */
16 public class MakePacBioScript {
17
18 /**
19 Be sure to replace:
20 @BUILDNUM with a number
21 @DIRTY_INPUT with the PacBio file
22 @CLEAN_INPUT_1 with the Illumina file
23 @ORGANISM with the name of the organism (or whatever)
24 @NUMSLOTS with the number of slots requested
25 @TARGET_SIZE with an estimate of the genome size, in bases. Examples: 160000000 or 160m or 0.16g are equivalent.
26 @RAM with e.g. Xmx31g
27 @MAXRAM with e.g. Xmx220g
28 @SCRIPT with the output file, e.g. run.sh
29 @MERGEREF with a list of reference files, e.g. chrom1.fa,chrom2.fa,chrom3.fa
30 @MERGEDIRTY with a list of dirty files, e.g. subreads1.fa,subreads2.fa,subreads3.fa
31 @MERGECLEAN with a list of clean files, e.g. illumina1.fq,illumina2.fq,illumina3.fq
32 @EXTRA with extra files for Illumina error correction. e.g. extra=a.fq,b.fq,c.fq
33
34 Optional:
35 @MAXREADS with the max number of clean reads to use in phase 1 (the slowest phase)
36 @REFERENCE with a reference file (optional)
37 @REFBUILD with a number
38 */
39 public static void main(String[] args){
40
41 if(args==null || args.length<1){
42 System.out.println("\nThis program generates a script for error-correcting PacBio reads using Illumina reads.\nSample command line:\n");
43 // System.out.println("java -ea -Xmx64m"+(Shared.WINDOWS ? "" : " -cp "+Data.ROOT)+" jgi.MakePacBioScript " +
44 // "dirty=subreads.fa clean=illumina.fq ref=ecoliRef.fa name=ecoli " +
45 // "out=run.sh template="+(Shared.WINDOWS ? "" : "/house/homedirs/b/bushnell/template/")+"cleanPacbioTemplate.sh " +
46 // "targetsize=5.4m threads=24 ram=31 maxram=100 noderam=256 build=-1 refbuild=-1 maxreads=-1");
47 // System.out.println("java -ea -Xmx64m"+(Shared.WINDOWS ? "" : " -cp "+Data.ROOT)+" jgi.MakePacBioScript " +
48 // "dirty=subreads.fa clean=illumina.fq ref=ecoliRef.fa name=ecoli " +
49 // "out=run.sh template="+(Shared.WINDOWS ? "" : "/house/homedirs/b/bushnell/template/")+"cleanPacbioTemplate.sh " +
50 // "targetsize=5.4m threads=24 noderam=256");
51 // System.out.println("\n\nOr to be concise:");
52 System.out.println("java -ea -Xmx64m"+(Shared.WINDOWS ? "" : " -cp "+Data.ROOT())+" jgi.MakePacBioScript " +
53 "d=subreads.fa c=illumina.fq tpl=template.sh ts=5.4m t=24 nm=256");
54 System.out.println("\n\nInput files can optionally be comma-separated lists of files, and absolute pathing can be used.");
55 System.out.println("All input files may be raw, gzipped, or bzipped as long as they have the correct file extension.");
56 System.out.println();
57 System.out.println("\n***** Required Parameters *****\n");
58 System.out.println("d=, dirty= \tPath to dirty (PacBio) reads. May be comma-delimited for multiple files.");
59 System.out.println("c=, clean= \tPath to clean (Illumina) reads. May be comma-delimited for multiple files.");
60 System.out.println("t=, threads= \tNumber of threads. Should equal the number of qsub slots or cores on the target machine.");
61 System.out.println("nm=, nodemem= \tPhysical memory (RAM) of target machine, in gigabytes.");
62 System.out.println("ts=, targetsize= \tEstimated size of target genome, in bases (k, m, or g may be used). Optional ONLY if a reference is supplied.");
63 System.out.println("\n***** Optional Parameters *****\n");
64 System.out.println("tpl=, template= \tPath to template for this script. Default is /house/homedirs/b/bushnell/template/cleanPacbioTemplate_ecc.sh");
65 System.out.println("mode= \tCan be specified instead of 'template='. Values are 'pacbio', 'assembly', or 'ccs'");
66 System.out.println("sort= \tTrue or false. Determines whether clean reads are sorted (alphabetically) and duplicates are removed.");
67 System.out.println("r=, ref= \tPath to reference fasta. May be comma-delimited for multiple files.");
68 System.out.println("o=, out= \tName of output script. Default is 'run.sh'.");
69 System.out.println("name= \tName of organism. Default is 'organism'.");
70 System.out.println("h=, hours= \tTime limit (in hours) for autogenerated qsub command line.");
71 System.out.println("m=,mem= \tAmount of heap memory for Java to use. Default is 31g; must be at least 10x number input PacBio bases." +
72 "\n \tNote! Two steps, Illumina error correction and site stacking, will ignore this and use all physical memory.");
73 System.out.println("b=,build= \tPrefix for index build number. Default is 2, yielding successively improved builds 2, 200, 201, 202, ... 208");
74 System.out.println("rb=,refbuild= \tReference build number. Default is 1.");
75 System.out.println("cp=,classpath= \tClasspath to the program. If unspecified, will be autodetected as "+
76 (Shared.WINDOWS ? "/house/homedirs/b/bushnell/beta19/" : Data.ROOT()));
77 // r=ref.fa o=run.sh
78 System.exit(0);
79 }
80
81 String dirty=null;
82 String clean=null;
83 String name="organism";
84 String targetsize=null;
85 String ref=null;
86 String template=null;
87 String output="run.sh";
88 String extra="";
89 String classpath=(Shared.WINDOWS ? "/house/homedirs/b/bushnell/beta19/" : Data.ROOT());
90 String sort_in="";
91 String sorted="sorted_topo#.txt.gz";
92 String sorted_out="sorted_topo1.txt.gz";
93
94 String mergeref=null;
95 String mergedirty=null;
96 String mergeclean=null;
97
98 String qsub=null;
99
100 String cleanecc="@ORGANISM_ecc_1.txt.gz";
101 String cleanbadecc="@ORGANISM_ecc_1_BAD.txt.gz";
102 String cleanallecc="@ORGANISM_ecc_1_ALL.txt.gz";
103
104 String mode="pacbio";
105
106 int build=2;
107 int threads=24;
108 int ram=31;
109 int maxram=-1;
110 int refbuild=-1;
111 int noderam=-1;
112 long maxReads=-1;
113 int runtime=499;
114
115 boolean ecc=false;
116 boolean sort=true;
117
118 for(int i=0; i<args.length; i++){
119 final String arg=args[i];
120 final String[] split=arg.split("=");
121 if(split.length!=2){
122 System.out.println("Wrong number of arguments for variable "+split[0]);
123 System.exit(0);
124 }
125 String a=split[0].toLowerCase();
126 String b=split[1];
127 if(b.equalsIgnoreCase("null")){b=null;}
128
129 if(a.equals("threads") || a.startsWith("slots") || a.equals("t")){
130 threads=Integer.parseInt(b);
131 }else if(a.equals("mode")){
132 mode=b;
133 }else if(a.startsWith("reads") || a.startsWith("maxreads") || a.equals("rd")){
134 maxReads=Parse.parseKMG(b);
135 }else if(a.startsWith("build") || a.startsWith("genome") || a.equals("b")){
136 build=Integer.parseInt(b);
137 String s=Data.chromFname(1, build);
138 if((new File(s)).exists()){System.out.println("Warning! Genome build "+b+" already exists at "+s);}
139 }else if(a.startsWith("refbuild") || a.startsWith("refgenome") || a.equals("rb")){
140 refbuild=Integer.parseInt(b);
141 String s=Data.chromFname(1, refbuild);
142 if((new File(s)).exists()){System.out.println("Warning! Genome build "+b+" already exists at "+s);}
143 }else if(a.startsWith("ram") || a.startsWith("mem") || a.equals("m")){
144 ram=Integer.parseInt(b.toLowerCase().replaceAll("g", ""));
145 }else if(a.startsWith("maxram") || a.startsWith("maxmem") || a.equals("mm")){
146 maxram=Integer.parseInt(b.toLowerCase().replaceAll("g", ""));
147 }else if(a.startsWith("noderam") || a.startsWith("nodemem") || a.equals("nm")){
148 if(b!=null){noderam=Integer.parseInt(b.toLowerCase().replaceAll("g", ""));}
149 }else if(a.equals("runtime") || a.equals("hours") || a.equals("time") || a.equals("h")){
150 runtime=Integer.parseInt(b.toLowerCase().replaceAll("h", ""));
151 }else if(a.startsWith("dirty") || a.startsWith("pacbio") || a.equals("d")){
152 dirty=b;
153 if(dirty.contains(",")){
154 mergedirty=dirty;
155 dirty="concatenatedDirtyFiles.fa.gz";
156 if((new File(dirty)).exists()){System.out.println("Warning! file already exists: "+dirty);}
157 }else{
158 if(!(new File(b)).exists()){System.out.println("Warning! No such file: "+b);}
159 }
160 }else if(a.startsWith("clean") || a.startsWith("illumina") || a.equals("c")){
161 clean=b;
162
163 if(clean.contains(",")){
164 String ext="fq";
165 if(clean.contains(".fasta,") || clean.contains(".fa,") || clean.contains(".fasta.gz,") || clean.contains(".fa.gz,")){ext="fa";}
166 else if(clean.contains(".txt,") || clean.contains(".txt.gz,")){ext="txt";}
167
168 mergeclean=clean;
169 clean="concatenatedCleanFiles."+ext+".gz";
170 if((new File(clean)).exists()){System.out.println("Warning! file already exists: "+clean);}
171 }else{
172 if(!(new File(b)).exists()){System.out.println("Warning! No such file: "+b);}
173 }
174 }else if(a.startsWith("name") || a.startsWith("organism")){
175 name=b;
176 if(name==null){name="organism";}
177 }else if(a.startsWith("size") || a.startsWith("targetsize") || a.equals("ts")){
178 targetsize=b;
179 }else if(a.equals("path") || a.equals("classpath") || a.equals("cp")){
180 classpath=b;
181 }else if(a.startsWith("template") || a.equals("tpl")){
182 if(b!=null){
183 template=b;
184 if(!(new File(b)).exists()){System.out.println("Warning! No such file: "+b);}
185 }
186 }else if(a.startsWith("extra") || a.equals("ex")){
187 extra=("extra="+b);
188 if(!b.contains(",") && !(new File(b)).exists()){System.out.println("Warning! No such file: "+b);}
189 }else if(a.startsWith("ref") || a.equals("r")){
190 ref=b;
191 if(ref.contains(",")){
192 mergeref=ref;
193 ref="concatenatedReferenceFiles.fa.gz";
194 if((new File(ref)).exists()){System.out.println("Warning! file already exists: "+ref);}
195 }else{
196 if(!(new File(b)).exists()){System.out.println("Warning! No such file: "+b);}
197 }
198 }else if(a.startsWith("out") || a.equals("o")){
199 output=b;
200 if((new File(b)).exists()){System.out.println("Warning! Outfile already exists: "+b);}
201 }else if(a.startsWith("ecc")){
202 ecc=Parse.parseBoolean(b);
203 }else if(a.equals("sort")){
204 sort=Parse.parseBoolean(b);
205 }else{
206 throw new RuntimeException("Unknown parameter "+args[i]);
207 }
208 }
209
210 if(template==null){
211 if(mode==null){mode="pacbio";}
212 mode=mode.toLowerCase();
213 if(mode.equals("pacbio") || mode.equals("pacbio_illumina")){
214 template=(Shared.WINDOWS ? "C:/workspace/prune/cleanPacbioTemplate_ecc.sh" : "/house/homedirs/b/bushnell/template/cleanPacbioTemplate_ecc_maxram.sh");
215 }else if(mode.equals("assembly") || mode.equals("assembly_illumina")
216 || mode.equals("reference") || mode.equals("reference_illumina")){
217 template=(Shared.WINDOWS ? "C:/workspace/prune/correctReference.sh" : "/house/homedirs/b/bushnell/template/correctReference_maxram.sh");
218 }else if(mode.equals("ccs") || mode.startsWith("ccs_")){
219 throw new RuntimeException("TODO: Mode "+mode+" is unfinished.");
220 }else if(mode.equals("pacbio_ccs") || mode.endsWith("_ccs")){
221 throw new RuntimeException("TODO: Mode "+mode+" is unfinished.");
222 }
223 }
224 // assert(false) : mode+", "+template;
225
226 if(ecc){
227 if(sort){
228 sort_in=cleanecc;
229 sorted_out=sorted.replaceFirst("#", "1");
230 }else{
231 sorted_out=sorted=clean;
232 }
233 }else{
234 if(sort){
235 sort_in=clean;
236 sorted_out=sorted.replaceFirst("#", "1");
237 cleanecc=sorted_out;
238 cleanbadecc=sorted_out;
239 cleanallecc=sorted_out;
240 }else{
241 sorted_out=sorted=clean;
242 cleanecc=clean;
243 cleanbadecc=clean;
244 cleanallecc=clean;
245 }
246 }
247
248 assert(threads>0);
249
250 if(noderam<1){
251 if(threads<9){noderam=144;}
252 else if(threads<25){noderam=252;}//Changed due to crash at 217 GB on 24-core nodes.
253 else if(threads<33){noderam=512;}
254 else if(threads<41){noderam=1024;}
255 else{noderam=2048;}
256 System.out.println("Set noderam at "+noderam+"g");
257 }
258
259 String slotram;
260 if(noderam%threads==0){slotram=(noderam/threads)+"G";}
261 else{slotram=((noderam*990)/threads)+"M";}
262
263 if(noderam>0){
264 if(maxram<1){
265 maxram=(int)(noderam*(noderam>256 ? 0.83 : 0.85f));
266 System.out.println("Set maxram at "+maxram+"g");
267 }
268 }
269
270 if(ram>maxram){
271 ram=maxram;
272 System.out.println("Set ram at "+maxram+"g");
273 }
274
275 if("auto".equalsIgnoreCase(targetsize) || (targetsize==null && ref!=null)){
276 if(ref==null){throw new RuntimeException("Ref file must be specified for auto targetsize.");}
277 File f=new File(ref);
278 if(!f.exists()){throw new RuntimeException("Ref file must exist for auto targetsize.");}
279 if(f.exists()){
280 targetsize=""+new File(ref).length();
281 if(ref.endsWith(".gz") || ref.endsWith(".gzip") || ref.endsWith(".zip") || ref.endsWith(".bz2")){
282 TextFile tf=new TextFile(ref, false);
283 long x=1;
284 for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){x+=s.length();}
285 tf.close();
286 targetsize=""+x;
287 }
288 }
289 }
290
291 if(ref!=null && refbuild<1){
292 if(build==1){refbuild=2;}
293 else{refbuild=1;}
294 }
295
296 if(dirty==null){throw new RuntimeException("No dirty file specified.");}
297 if(clean==null){throw new RuntimeException("No clean file specified.");}
298 if(targetsize==null){throw new RuntimeException("No targetsize specified.");}
299 if(template==null){throw new RuntimeException("No template file specified.");}
300 if(!new File(template).exists()){throw new RuntimeException("Template file "+template+" does not exist; please specify a different template.");}
301 if(build==refbuild){throw new RuntimeException("Build id and ref build id must differ.");}
302 if(build<1){throw new RuntimeException("No build id.");}
303 if(ref!=null && refbuild<1){throw new RuntimeException("No ref build id.");}
304 if(ref==null && refbuild>0 && !(new File(Data.chromFname(1, refbuild))).exists()){throw new RuntimeException("Ref build id specified, but no reference file.");}
305
306 String[] lines;
307 {
308 TextFile tf=new TextFile(template, false);
309 lines=tf.toStringLines();
310 }
311
312
313 StringBuilder sb=new StringBuilder();
314 for(int i=0; i<lines.length; i++){
315 String s=lines[i];
316
317 boolean eccline=s.contains("?ecc?");
318 boolean sortline=s.contains("?sort?");
319 boolean refline=s.contains("?ref?");
320 boolean mergeline=s.contains("?ref?");
321 boolean optional=(!eccline && !sortline && !refline && !mergeline && s.startsWith("#?")); //Optional for some other reason
322
323 if(eccline){
324 s=s.replaceAll("\\?ecc\\?", "");
325 while(ecc && s.startsWith("#")){s=s.substring(1);}
326 }
327 if(sortline){
328 s=s.replaceAll("\\?sort\\?", "");
329 while(sort && s.startsWith("#")){s=s.substring(1);}
330 }
331 if(refline){
332 s=s.replaceAll("\\?ref\\?", "");
333 while(refbuild>0 && s.startsWith("#")){s=s.substring(1);}
334 }
335
336 if(!s.startsWith("#")){
337 if((eccline && !ecc) || (sortline && !sort) || (refline && refbuild<1)){s="#"+s;}
338 }
339
340
341 if(optional){
342 optional=true;
343 s=s.substring(2);
344 }
345
346 if((s.contains("@MAXRAM") && maxram>31) || (s.contains("@RAM") && ram>31)){
347 s=s.replace("-XX:+UseCompressedOops ", "");
348 }
349
350 s=s.replace("@CLEAN_ECC_1", cleanecc);
351 s=s.replace("@CLEAN_BAD_ECC_1", cleanbadecc);
352 s=s.replace("@CLEAN_ALL_ECC_1", cleanallecc);
353 s=s.replace("@SORT_IN", sort_in);
354 s=s.replace("@SORTED_OUT", sorted_out);
355 s=s.replace("@SORTED", sorted);
356
357 s=s.replace("@SLOTRAM", slotram);
358 s=s.replace("@BUILDNUM", ""+build);
359 s=s.replace("@DIRTY_INPUT", dirty);
360 s=s.replace("@CLEAN_INPUT_1", clean);
361 s=s.replace("@ORGANISM", name);
362 s=s.replace("@NUMSLOTS", ""+threads);
363 s=s.replace("@TARGET_SIZE", targetsize);
364 s=s.replace("@RAM", "-Xmx"+ram+"g");
365 s=s.replace("@MAXRAM", "-Xmx"+maxram+"g");
366 s=s.replace("@MAXREADS", ""+maxReads);
367 s=s.replace("@SCRIPT", (output==null ? "run.sh" : output));
368 s=s.replace("@EXTRA", extra);
369 s=s.replace("@RUNTIME", ""+runtime);
370 s=s.replace("@CLASSPATH", classpath);
371
372 if(s.contains("@REFBUILD")){
373 if(refbuild<1){
374 s="#"+s;
375 }else{
376 s=s.replace("@REFBUILD", ""+refbuild);
377 }
378 }
379
380
381 if(s.contains("@REFERENCE")){
382 if(ref==null){
383 s="#"+s;
384 }else{
385 s=s.replace("@REFERENCE", ref);
386 }
387 }
388
389 if(s.contains("@MERGECLEAN")){
390 if(mergeclean==null){
391 s="#"+s;
392 }else{
393 s=s.replace("@MERGECLEAN", mergeclean);
394 }
395 }
396
397 if(s.contains("@MERGEDIRTY")){
398 if(mergedirty==null){
399 s="#"+s;
400 }else{
401 s=s.replace("@MERGEDIRTY", mergedirty);
402 }
403 }
404
405 if(s.contains("@MERGEREF")){
406 if(mergeref==null){
407 s="#"+s;
408 }else{
409 s=s.replace("@MERGEREF", mergeref);
410 }
411 }
412
413 while(s.startsWith("##")){s=s.substring(1);}
414
415 assert(s==null || s.length()<1 || s.startsWith("#") || !s.contains("@")) : s;
416
417 if(s!=null && !s.startsWith("#//")){sb.append(s).append('\n');}
418
419 if(qsub==null && s.contains("export task") && s.contains("qsub")){
420 qsub=s;
421 }
422 }
423
424 if(output==null){
425 System.out.println(sb);
426 }else{
427 ReadWrite.writeString(sb, output, false);
428 System.out.println("Wrote "+output);
429 if(qsub!=null){
430 while(qsub.startsWith("#")){qsub=qsub.substring(1);}
431 System.out.println("The script can be executed on Genepool with the following command:\n\n"+qsub.trim());
432 }
433 }
434
435
436 }
437
438
439 }