Search in sources :

Example 1 with OpenJdkCompiler

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);
    }
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) SAMFileWriter(htsjdk.samtools.SAMFileWriter) Date(java.util.Date) Iso8601Date(htsjdk.samtools.util.Iso8601Date) OpenJdkCompiler(com.github.lindenb.jvarkit.lang.OpenJdkCompiler) SamReader(htsjdk.samtools.SamReader) BAMRecordCodec(htsjdk.samtools.BAMRecordCodec) Random(java.util.Random) StringWriter(java.io.StringWriter) SAMRecord(htsjdk.samtools.SAMRecord) Iso8601Date(htsjdk.samtools.util.Iso8601Date) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with OpenJdkCompiler

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);
    }
}
Also used : WritingVariantsDelegate(com.github.lindenb.jvarkit.variant.variantcontext.writer.WritingVariantsDelegate) Allele(htsjdk.variant.variantcontext.Allele) Arrays(java.util.Arrays) Program(com.github.lindenb.jvarkit.util.jcommander.Program) IOUtil(htsjdk.samtools.util.IOUtil) VCFStandardHeaderLines(htsjdk.variant.vcf.VCFStandardHeaderLines) Date(java.util.Date) VCFHeader(htsjdk.variant.vcf.VCFHeader) Random(java.util.Random) StringUtil(htsjdk.samtools.util.StringUtil) Map(java.util.Map) Path(java.nio.file.Path) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) OpenJdkCompiler(com.github.lindenb.jvarkit.lang.OpenJdkCompiler) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Logger(com.github.lindenb.jvarkit.util.log.Logger) Collectors(java.util.stream.Collectors) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) List(java.util.List) StringUtils(com.github.lindenb.jvarkit.lang.StringUtils) VariantAttributesRecalculator(com.github.lindenb.jvarkit.util.vcf.VariantAttributesRecalculator) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VcfTools(com.github.lindenb.jvarkit.util.vcf.VcfTools) VariantContext(htsjdk.variant.variantcontext.VariantContext) Iso8601Date(htsjdk.samtools.util.Iso8601Date) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) Genotype(htsjdk.variant.variantcontext.Genotype) VCFIterator(htsjdk.variant.vcf.VCFIterator) Parameter(com.beust.jcommander.Parameter) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) ParametersDelegate(com.beust.jcommander.ParametersDelegate) PedigreeParser(com.github.lindenb.jvarkit.pedigree.PedigreeParser) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) VCFConstants(htsjdk.variant.vcf.VCFConstants) Counter(com.github.lindenb.jvarkit.util.Counter) VCFFilterHeaderLine(htsjdk.variant.vcf.VCFFilterHeaderLine) Pedigree(com.github.lindenb.jvarkit.pedigree.Pedigree) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) ProgressFactory(com.github.lindenb.jvarkit.util.log.ProgressFactory) JVarkitVersion(com.github.lindenb.jvarkit.util.JVarkitVersion) Consumer(java.util.function.Consumer) ProgressFactory(com.github.lindenb.jvarkit.util.log.ProgressFactory) VariantContext(htsjdk.variant.variantcontext.VariantContext) BufferedWriter(java.io.BufferedWriter) PedigreeParser(com.github.lindenb.jvarkit.pedigree.PedigreeParser) Random(java.util.Random) StringWriter(java.io.StringWriter) VCFFilterHeaderLine(htsjdk.variant.vcf.VCFFilterHeaderLine) Iso8601Date(htsjdk.samtools.util.Iso8601Date) VCFHeader(htsjdk.variant.vcf.VCFHeader) PrintWriter(java.io.PrintWriter) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) Date(java.util.Date) Iso8601Date(htsjdk.samtools.util.Iso8601Date) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) OpenJdkCompiler(com.github.lindenb.jvarkit.lang.OpenJdkCompiler) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantAttributesRecalculator(com.github.lindenb.jvarkit.util.vcf.VariantAttributesRecalculator) Collection(java.util.Collection)

Example 3 with OpenJdkCompiler

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);
    }
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) SamReader(htsjdk.samtools.SamReader) Random(java.util.Random) StringWriter(java.io.StringWriter) Iso8601Date(htsjdk.samtools.util.Iso8601Date) PrintWriter(java.io.PrintWriter) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) SAMFileWriter(htsjdk.samtools.SAMFileWriter) Date(java.util.Date) Iso8601Date(htsjdk.samtools.util.Iso8601Date) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) OpenJdkCompiler(com.github.lindenb.jvarkit.lang.OpenJdkCompiler) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) SAMRecord(htsjdk.samtools.SAMRecord) Collection(java.util.Collection) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Example 4 with OpenJdkCompiler

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 {
    }
}
Also used : Date(java.util.Date) Iso8601Date(htsjdk.samtools.util.Iso8601Date) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OpenJdkCompiler(com.github.lindenb.jvarkit.lang.OpenJdkCompiler) Random(java.util.Random) StringWriter(java.io.StringWriter) Iso8601Date(htsjdk.samtools.util.Iso8601Date) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Aggregations

OpenJdkCompiler (com.github.lindenb.jvarkit.lang.OpenJdkCompiler)4 Iso8601Date (htsjdk.samtools.util.Iso8601Date)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 Date (java.util.Date)4 Random (java.util.Random)4 JvarkitException (com.github.lindenb.jvarkit.lang.JvarkitException)2 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)2 SAMFileHeader (htsjdk.samtools.SAMFileHeader)2 SAMFileWriter (htsjdk.samtools.SAMFileWriter)2 SAMRecord (htsjdk.samtools.SAMRecord)2 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)2 SamReader (htsjdk.samtools.SamReader)2 BufferedWriter (java.io.BufferedWriter)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Parameter (com.beust.jcommander.Parameter)1 ParametersDelegate (com.beust.jcommander.ParametersDelegate)1 IOUtils (com.github.lindenb.jvarkit.io.IOUtils)1