use of com.github.lindenb.jvarkit.lang.OpenJdkCompiler in project jvarkit by lindenb.
the class SamCustomSortJdk method doWork.
@Override
public int doWork(final List<String> args) {
SAMRecordIterator iter = null;
SamReader samFileReader = null;
SAMFileWriter sw = null;
SortingCollection<SAMRecord> sorter = null;
CloseableIterator<SAMRecord> iter2 = null;
try {
final String code;
if (this.scriptFile != null) {
code = IOUtil.slurp(this.scriptFile);
} else if (!StringUtil.isBlank(this.scriptExpr)) {
code = this.scriptExpr;
} else {
LOG.error("Option -e or -f are required. The content of those empty mut be not empty");
return -1;
}
final Random rand = new Random(System.currentTimeMillis());
final String javaClassName = SamCustomSortJdk.class.getSimpleName() + "Custom" + Math.abs(rand.nextInt());
final StringWriter codeWriter = new StringWriter();
final PrintWriter pw = new PrintWriter(codeWriter);
pw.println("import java.util.*;");
pw.println("import java.util.stream.*;");
pw.println("import java.util.function.*;");
pw.println("import htsjdk.samtools.*;");
pw.println("import htsjdk.samtools.util.*;");
pw.println("import com.github.lindenb.jvarkit.tools.misc.IlluminaReadName;");
pw.println("import javax.annotation.processing.Generated;");
pw.println("@Generated(value=\"" + SamCustomSortJdk.class.getSimpleName() + "\",date=\"" + new Iso8601Date(new Date()) + "\")");
pw.println("public class " + javaClassName + " extends " + AbstractSamComparator.class.getName().replace('$', '.') + " {");
pw.println(" public " + javaClassName + "(final SAMFileHeader header) {");
pw.println(" super(header);");
pw.println(" }");
if (user_code_is_body) {
pw.println(" //user's code starts here");
pw.println(code);
pw.println(" // user's code ends here");
} else {
pw.println(" @Override");
pw.println(" public int compare(final SAMRecord R1,final SAMRecord R2) {");
pw.println(" /** user's code starts here */");
pw.println(code);
pw.println("/** user's code ends here */");
pw.println(" }");
}
pw.println("}");
pw.flush();
if (!hideGeneratedCode) {
LOG.debug(" Compiling :\n" + OpenJdkCompiler.beautifyCode(codeWriter.toString()));
}
if (this.saveCodeInDir != null) {
PrintWriter cw = null;
try {
IOUtil.assertDirectoryIsWritable(this.saveCodeInDir);
cw = new PrintWriter(new File(this.saveCodeInDir, javaClassName + ".java"));
cw.write(codeWriter.toString());
cw.flush();
cw.close();
cw = null;
LOG.info("saved " + javaClassName + ".java in " + this.saveCodeInDir);
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(cw);
}
}
final OpenJdkCompiler inMemoryCompiler = OpenJdkCompiler.getInstance();
final Class<?> compiledClass = inMemoryCompiler.compileClass(javaClassName, codeWriter.toString());
final Constructor<?> ctor = compiledClass.getDeclaredConstructor(SAMFileHeader.class);
samFileReader = openSamReader(oneFileOrNull(args));
final SAMFileHeader headerIn = samFileReader.getFileHeader();
@SuppressWarnings("unchecked") final StableSort customComparator = new StableSort((Comparator<SAMRecord>) ctor.newInstance(headerIn));
final BAMRecordCodec bamRecordCodec = new BAMRecordCodec(headerIn);
sorter = SortingCollection.newInstance(SAMRecord.class, bamRecordCodec, customComparator, this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
sorter.setDestructiveIteration(true);
SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(headerIn).logger(LOG);
iter = samFileReader.iterator();
while (iter.hasNext()) {
sorter.add(progress.watch(iter.next()));
}
samFileReader.close();
samFileReader = null;
sorter.doneAdding();
final SAMFileHeader headerOut = headerIn.clone();
headerOut.setSortOrder(SAMFileHeader.SortOrder.unsorted);
headerOut.addComment(getProgramName() + " " + getVersion() + " " + getProgramCommandLine());
sw = this.writingBamArgs.openSAMFileWriter(this.outputFile, headerOut, false);
progress = new SAMSequenceDictionaryProgress(headerIn).logger(LOG);
iter2 = sorter.iterator();
while (iter2.hasNext()) {
sw.addAlignment(progress.watch(iter2.next()));
}
iter2.close();
iter2 = null;
sw.close();
sw = null;
progress.finish();
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
try {
if (sorter != null)
sorter.cleanup();
} catch (Exception e) {
}
CloserUtil.close(iter);
CloserUtil.close(iter2);
CloserUtil.close(samFileReader);
CloserUtil.close(sw);
}
}
use of com.github.lindenb.jvarkit.lang.OpenJdkCompiler in project jvarkit by lindenb.
the class VcfFilterJdk method run.
private int run(final VCFIterator iter, final VariantContextWriter out) {
ProgressFactory.Watcher<VariantContext> progress = null;
String code = null;
try {
if (this.scriptPath != null) {
code = IOUtils.slurpPath(this.scriptPath);
} else {
code = this.scriptExpr;
}
final Random rand = new Random(System.currentTimeMillis());
final String javaClassName = VcfFilterJdk.class.getSimpleName() + "Custom" + Math.abs(rand.nextInt());
final String generatedClassName = OpenJdkCompiler.getGeneratedAnnotationClassName();
final StringWriter codeWriter = new StringWriter();
final PrintWriter pw = new PrintWriter(codeWriter);
pw.println("import java.util.*;");
pw.println("import java.util.stream.*;");
pw.println("import java.util.function.*;");
pw.println("import htsjdk.samtools.util.*;");
pw.println("import htsjdk.variant.variantcontext.*;");
pw.println("import htsjdk.variant.vcf.*;");
if (!StringUtil.isBlank(generatedClassName)) {
pw.println("@" + generatedClassName + "(value=\"" + VcfFilterJdk.class.getSimpleName() + "\",date=\"" + new Iso8601Date(new Date()) + "\")");
}
pw.println("public class " + javaClassName + " extends " + AbstractFilter.class.getName().replace('$', '.') + " {");
pw.println(" public " + javaClassName + "(final VCFHeader header) {");
pw.println(" super(header);");
pw.println(" }");
if (this.user_code_is_body) {
pw.println(" /** user's code starts here */");
pw.println(code);
pw.println("/** user's code ends here */");
} else {
pw.println(" @Override");
pw.println(" public Object apply(final VariantContext " + getVariantVariableName() + ") {");
pw.println(" /** user's code starts here */");
pw.println(code);
pw.println("/** user's code ends here */");
pw.println(" }");
}
pw.println("}");
pw.flush();
if (!this.hideGeneratedCode) {
LOG.debug(" Compiling :\n" + OpenJdkCompiler.beautifyCode(codeWriter.toString()));
}
if (this.saveCodeInDir != null) {
BufferedWriter cw = null;
try {
IOUtil.assertDirectoryIsWritable(this.saveCodeInDir);
cw = Files.newBufferedWriter(this.saveCodeInDir.resolve(javaClassName + ".java"));
cw.write(codeWriter.toString());
cw.flush();
cw.close();
cw = null;
LOG.info("saved " + javaClassName + ".java in " + this.saveCodeInDir);
} catch (final Exception err) {
throw new RuntimeIOException(err);
} finally {
CloserUtil.close(cw);
}
}
final OpenJdkCompiler compiler = OpenJdkCompiler.getInstance();
final Class<?> compiledClass = compiler.compileClass(javaClassName, codeWriter.toString());
final Constructor<?> constructor = compiledClass.getDeclaredConstructor(VCFHeader.class);
final VCFHeader header = iter.getHeader();
final AbstractFilter filter_instance;
;
/* change header */
final VCFHeader h2 = new VCFHeader(header);
/**
* recalculate INFO/AF, INFO/AN ... variables and 'add'
*/
final Consumer<VariantContext> recalcAndAdd;
if (this.recalcAttributes && header.hasGenotypingData()) {
final VariantAttributesRecalculator recalculator = new VariantAttributesRecalculator();
recalculator.setHeader(h2);
recalcAndAdd = V -> out.add(recalculator.apply(V));
} else {
recalcAndAdd = V -> out.add(V);
}
final VCFFilterHeaderLine filterHeaderLine = StringUtils.isBlank(filteredTag) ? null : new VCFFilterHeaderLine(this.filteredTag.trim(), "Filtered with " + VcfFilterJdk.class.getSimpleName());
if (filterHeaderLine != null) {
h2.addMetaDataLine(filterHeaderLine);
}
// add genotype filter key if missing
if (h2.getFormatHeaderLine(VCFConstants.GENOTYPE_FILTER_KEY) == null) {
h2.addMetaDataLine(VCFStandardHeaderLines.getFormatLine(VCFConstants.GENOTYPE_FILTER_KEY, true));
}
for (final String xf : Arrays.stream(this.extraFilters.split("[ ,;]+")).filter(S -> !StringUtil.isBlank(S)).collect(Collectors.toSet())) {
h2.addMetaDataLine(new VCFFilterHeaderLine(xf, "Custom FILTER inserted with " + VcfFilterJdk.class.getSimpleName()));
}
try {
filter_instance = (AbstractFilter) constructor.newInstance(header);
} catch (final Throwable err) {
LOG.error(err);
return -1;
}
/* end change header */
JVarkitVersion.getInstance().addMetaData(this, h2);
out.writeHeader(h2);
if (this.pedigreePath != null) {
filter_instance.pedigree = new PedigreeParser().parse(this.pedigreePath);
}
filter_instance.userData.put("first.variant", Boolean.TRUE);
filter_instance.userData.put("last.variant", Boolean.FALSE);
progress = ProgressFactory.newInstance().dictionary(header).logger(LOG).build();
while (iter.hasNext() && !out.checkError()) {
final VariantContext variation = progress.apply(iter.next());
/* handle variant */
final Object result = filter_instance.apply(variation);
// result is an array of a collection of variants
if (result != null && (result.getClass().isArray() || (result instanceof Collection))) {
final Collection<?> col;
if (result.getClass().isArray()) {
final Object[] array = (Object[]) result;
col = Arrays.asList(array);
} else {
col = (Collection<?>) result;
}
// write all of variants
for (final Object item : col) {
if (item == null)
throw new JvarkitException.UserError("item in array is null");
if (!(item instanceof VariantContext))
throw new JvarkitException.UserError("item in array is not a VariantContext " + item.getClass());
recalcAndAdd.accept(VariantContext.class.cast(item));
}
} else // result is a VariantContext
if (result != null && (result instanceof VariantContext)) {
recalcAndAdd.accept(VariantContext.class.cast(result));
} else {
boolean accept = true;
if (result == null) {
accept = false;
} else if (result instanceof Boolean) {
if (Boolean.FALSE.equals(result))
accept = false;
} else if (result instanceof Number) {
if (((Number) result).intValue() != 1)
accept = false;
} else {
LOG.warn("Script returned something that is not a boolean or a number:" + result.getClass());
accept = false;
}
if (!accept) {
if (filterHeaderLine != null) {
final VariantContextBuilder vcb = new VariantContextBuilder(variation);
vcb.filter(filterHeaderLine.getID());
recalcAndAdd.accept(vcb.make());
}
continue;
}
// set PASS filter if needed
if (filterHeaderLine != null && !variation.isFiltered()) {
recalcAndAdd.accept(new VariantContextBuilder(variation).passFilters().make());
continue;
}
recalcAndAdd.accept(variation);
}
/* end handle variant */
filter_instance.userData.put("first.variant", Boolean.FALSE);
filter_instance.userData.put("last.variant", !iter.hasNext());
final Object stop = filter_instance.userData.get("STOP");
if (Boolean.TRUE.equals(stop))
break;
}
progress.close();
progress = null;
return 0;
} catch (final Throwable err) {
LOG.error(err);
return -1;
} finally {
code = null;
CloserUtil.close(progress);
}
}
use of com.github.lindenb.jvarkit.lang.OpenJdkCompiler in project jvarkit by lindenb.
the class SamJdk method doWork.
@Override
public int doWork(final List<String> args) {
SAMRecordIterator iter = null;
SamReader samFileReader = null;
SAMFileWriter sw = null;
try {
final String code;
if (this.scriptPath != null && !StringUtil.isBlank(this.scriptExpr)) {
LOG.error("Option -e or -f are both defined.");
return -1;
} else if (this.scriptPath != null) {
code = IOUtils.slurpPath(this.scriptPath);
} else if (!StringUtil.isBlank(this.scriptExpr)) {
code = this.scriptExpr;
} else {
LOG.error("Option -e or -f are required. The content of those empty mut be not empty");
return -1;
}
final Random rand = new Random(System.currentTimeMillis());
final String javaClassName = SamJdk.class.getSimpleName() + "Custom" + Math.abs(rand.nextInt());
final String generatedClassName = OpenJdkCompiler.getGeneratedAnnotationClassName();
final StringWriter codeWriter = new StringWriter();
final PrintWriter pw = new PrintWriter(codeWriter);
pw.println("import java.util.*;");
pw.println("import java.util.stream.*;");
pw.println("import java.util.function.*;");
pw.println("import htsjdk.samtools.*;");
pw.println("import htsjdk.samtools.util.*;");
if (!StringUtils.isBlank(generatedClassName)) {
pw.println("@" + generatedClassName + "(value=\"" + SamJdk.class.getSimpleName() + "\",date=\"" + new Iso8601Date(new Date()) + "\")");
}
pw.println("public class " + javaClassName + " extends " + (this.pair_mode ? AbstractListFilter.class : AbstractFilter.class).getName().replace('$', '.') + " {");
pw.println(" public " + javaClassName + "(final SAMFileHeader header) {");
pw.println(" super(header);");
pw.println(" }");
if (user_code_is_body) {
pw.println(" //user's code starts here");
pw.println(code);
pw.println(" // user's code ends here");
} else {
pw.println(" @Override");
pw.println(" public Object apply(final " + (this.pair_mode ? "List<SAMRecord> records" : "SAMRecord record") + ") {");
pw.println(" /** user's code starts here */");
pw.println(code);
pw.println("/** user's code ends here */");
pw.println(" }");
}
pw.println("}");
pw.flush();
if (!hideGeneratedCode) {
LOG.debug(" Compiling :\n" + OpenJdkCompiler.beautifyCode(codeWriter.toString()));
}
if (this.saveCodeInDir != null) {
BufferedWriter cw = null;
try {
IOUtil.assertDirectoryIsWritable(this.saveCodeInDir);
cw = Files.newBufferedWriter(this.saveCodeInDir.resolve(javaClassName + ".java"));
cw.write(codeWriter.toString());
cw.flush();
cw.close();
cw = null;
LOG.info("saved " + javaClassName + ".java in " + this.saveCodeInDir);
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(cw);
}
}
final OpenJdkCompiler compiler = OpenJdkCompiler.getInstance();
final Class<?> compiledClass = compiler.compileClass(javaClassName, codeWriter.toString());
final Constructor<?> ctor = compiledClass.getDeclaredConstructor(SAMFileHeader.class);
final String input = oneFileOrNull(args);
final SamReaderFactory srf = this.createSamReaderFactory();
if (this.refFaidx != null)
srf.referenceSequence(this.refFaidx);
if (input == null) {
samFileReader = srf.open(SamInputResource.of(stdin()));
} else {
samFileReader = srf.open(SamInputResource.of(input));
}
final SAMFileHeader header = samFileReader.getFileHeader();
if (this.pair_mode) {
final SAMFileHeader.SortOrder order = header.getSortOrder();
if (order == null || order.equals(SAMFileHeader.SortOrder.unsorted)) {
LOG.warning("In `--pair` mode , the input BAM is expected to be sorted on queryname but current sort-order is " + order + " ... Continue...");
} else if (!order.equals(SAMFileHeader.SortOrder.queryname)) {
LOG.error("In `--pair` mode , the input BAM is expected to be sorted on queryname but I've got \"" + order + "\". " + "Use picard SortSam (not `samtools sort` https://github.com/samtools/hts-specs/issues/5 )");
return -1;
}
}
long count = 0L;
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header).logger(LOG);
sw = this.writingBamArgs.setReferencePath(this.refFaidx).openSamWriter(this.outputFile, header, true);
iter = samFileReader.iterator();
if (this.pair_mode) {
SAMRecord prev = null;
final AbstractListFilter filter = (AbstractListFilter) ctor.newInstance(header);
final List<SAMRecord> buffer = new ArrayList<>();
for (; ; ) {
int numWarnings = 100;
final Comparator<SAMRecord> nameComparator = ReadNameSortMethod.picard.get();
final SAMRecord record = (iter.hasNext() ? progress.watch(iter.next()) : null);
if (prev != null && record != null && numWarnings > 0 && nameComparator.compare(prev, record) > 0) {
LOG.warn("SamRecord doesn't look sorted on query name using a picard/htsjdk method. Got " + record + " affter " + prev + ". " + "In '--pair' mode, reads should be sorted on query name using **picard/htsjdk**. (samtools != picard) see https://github.com/samtools/hts-specs/issues/5");
--numWarnings;
}
prev = record;
if (record == null || (!buffer.isEmpty() && !buffer.get(0).getReadName().equals(record.getReadName()))) {
if (!buffer.isEmpty()) {
final Object result = filter.apply(buffer);
// result is an array of a collection of reads
if (result != null && (result.getClass().isArray() || (result instanceof Collection))) {
final Collection<?> col;
if (result.getClass().isArray()) {
final Object[] array = (Object[]) result;
col = Arrays.asList(array);
} else {
col = (Collection<?>) result;
}
// write all of reads
for (final Object item : col) {
if (item == null)
throw new JvarkitException.UserError("item in array is null");
if (!(item instanceof SAMRecord))
throw new JvarkitException.UserError("item in array is not a SAMRecord " + item.getClass());
++count;
sw.addAlignment(SAMRecord.class.cast(item));
if (this.LIMIT > 0L && count >= this.LIMIT)
break;
}
} else // result is a SAMRecord
if (result != null && (result instanceof SAMRecord)) {
++count;
sw.addAlignment(SAMRecord.class.cast(result));
} else {
boolean accept = true;
if (result == null) {
accept = false;
} else if (result instanceof Boolean) {
if (Boolean.FALSE.equals(result))
accept = false;
} else if (result instanceof Number) {
if (((Number) result).intValue() != 1)
accept = false;
} else {
LOG.warn("Script returned something that is not a boolean or a number:" + result.getClass());
accept = false;
}
if (!accept) {
for (final SAMRecord item : buffer) {
failing(item, header);
}
} else {
for (final SAMRecord item : buffer) {
++count;
sw.addAlignment(item);
}
}
}
}
// end of if !buffer.isEmpty()
if (record == null)
break;
buffer.clear();
}
// end flush flush
if (this.LIMIT > 0L && count >= this.LIMIT)
break;
buffer.add(record);
}
// infinite loop
} else {
final AbstractFilter filter = (AbstractFilter) ctor.newInstance(header);
while (iter.hasNext()) {
final SAMRecord record = progress.watch(iter.next());
final Object result = filter.apply(record);
// result is an array of a collection of reads
if (result != null && (result.getClass().isArray() || (result instanceof Collection))) {
final Collection<?> col;
if (result.getClass().isArray()) {
final Object[] array = (Object[]) result;
col = Arrays.asList(array);
} else {
col = (Collection<?>) result;
}
// write all of reads
for (final Object item : col) {
if (item == null)
throw new JvarkitException.UserError("item in array is null");
if (!(item instanceof SAMRecord))
throw new JvarkitException.UserError("item in array is not a SAMRecord " + item.getClass());
++count;
sw.addAlignment(SAMRecord.class.cast(item));
}
} else // result is a SAMRecord
if (result != null && (result instanceof SAMRecord)) {
++count;
sw.addAlignment(SAMRecord.class.cast(result));
} else {
boolean accept = true;
if (result == null) {
accept = false;
} else if (result instanceof Boolean) {
if (Boolean.FALSE.equals(result))
accept = false;
} else if (result instanceof Number) {
if (((Number) result).intValue() != 1)
accept = false;
} else {
LOG.warn("Script returned something that is not a boolean or a number:" + result.getClass());
accept = false;
}
if (!accept) {
failing(record, header);
} else {
++count;
sw.addAlignment(record);
}
}
if (this.LIMIT > 0L && count >= this.LIMIT)
break;
}
}
sw.close();
/* create empty if never called */
openFailing(header);
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(iter);
CloserUtil.close(samFileReader);
CloserUtil.close(sw);
CloserUtil.close(this.failingReadsWriter);
}
}
use of com.github.lindenb.jvarkit.lang.OpenJdkCompiler in project jvarkit by lindenb.
the class Optimizer method doWork.
@Override
public int doWork(final List<String> args) {
if (this.useSourceCode == null) {
LOG.error("use source code is missing");
return -1;
}
this.random = new Random(randomSeed == -1 ? System.currentTimeMillis() : randomSeed);
try {
if (this.readParams(new File(super.oneAndOnlyOneFile(args))) != 0) {
LOG.error("Cannot read params");
return -1;
}
if (this.variableParams.isEmpty()) {
LOG.error("no params found");
return -1;
}
final String className = "CustomOptimizer" + Math.abs(this.random.nextInt());
final StringWriter codestr = new StringWriter();
final PrintWriter w = new PrintWriter(codestr);
w.println("import java.util.*;");
w.println("import java.io.*;");
w.println("import java.util.stream.*;");
w.println("import java.util.function.*;");
w.println("import htsjdk.samtools.util.*;");
w.println("import htsjdk.variant.variantcontext.*;");
w.println("import htsjdk.variant.vcf.*;");
w.println("import javax.annotation.processing.Generated;");
w.println("@Generated(value=\"" + Optimizer.class.getSimpleName() + "\",date=\"" + new Iso8601Date(new Date()) + "\")");
w.println("public class " + className + " extends " + Solution.class.getName().replace("$", ".") + "{");
w.println("public " + className + "(final Map<String,Object> params) {");
w.println("super(params);");
w.println("}");
w.println(" /** user's code starts here */");
w.println(IOUtil.slurp(this.useSourceCode));
w.println(" /** user's code ends here */");
w.println("}");
w.flush();
final String code = codestr.toString().replaceAll("__BASE__", Solution.class.getName().replace("$", ".")).replaceAll("__CLASS__", className);
LOG.debug(" Compiling :\n" + OpenJdkCompiler.beautifyCode(code));
final OpenJdkCompiler inMemoryCompiler = OpenJdkCompiler.getInstance();
final Class<?> clazz = inMemoryCompiler.compileClass(className, code);
this.solutionConstructor = clazz.getConstructor(Map.class);
if (this.run_all_combinations) {
this.runAll();
} else {
this.runRandom();
}
if (this.bestSolutions.isEmpty()) {
LOG.error("no solution was found");
return -1;
}
for (final Solution sol : this.bestSolutions) {
sol.delete();
}
return 0;
} catch (final Exception e) {
LOG.error(e);
return -1;
} finally {
}
}
Aggregations