Search in sources :

Example 1 with InMemoryCompiler

use of com.github.lindenb.jvarkit.lang.InMemoryCompiler 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.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" + InMemoryCompiler.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 InMemoryCompiler inMemoryCompiler = new InMemoryCompiler();
        final Class<?> compiledClass = inMemoryCompiler.compileClass(javaClassName, codeWriter.toString());
        final Constructor<?> ctor = compiledClass.getDeclaredConstructor(SAMFileHeader.class);
        samFileReader = openSamReader(oneFileOrNull(args));
        final SAMFileHeader headerIn = samFileReader.getFileHeader();
        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) SamReader(htsjdk.samtools.SamReader) BAMRecordCodec(htsjdk.samtools.BAMRecordCodec) Random(java.util.Random) StringWriter(java.io.StringWriter) SAMRecord(htsjdk.samtools.SAMRecord) InMemoryCompiler(com.github.lindenb.jvarkit.lang.InMemoryCompiler) Iso8601Date(htsjdk.samtools.util.Iso8601Date) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with InMemoryCompiler

use of com.github.lindenb.jvarkit.lang.InMemoryCompiler 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.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" + InMemoryCompiler.beautifyCode(code));
        final InMemoryCompiler inMemoryCompiler = new InMemoryCompiler();
        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) Random(java.util.Random) StringWriter(java.io.StringWriter) InMemoryCompiler(com.github.lindenb.jvarkit.lang.InMemoryCompiler) Iso8601Date(htsjdk.samtools.util.Iso8601Date) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 3 with InMemoryCompiler

use of com.github.lindenb.jvarkit.lang.InMemoryCompiler in project jvarkit by lindenb.

the class JdkFilterItemProcessor method beforeStep.

@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
    try {
        InMemoryCompiler javac = new InMemoryCompiler();
        String javaCode = InMemoryCompiler.getTheSourceCode(getExpression(), getScriptFile());
        final Class<?> clazz;
        if (CODE2CLASS.containsKey(javaCode)) {
            if (LOG.isWarnEnabled())
                LOG.warn("Code already compiled");
            clazz = CODE2CLASS.get(javaCode);
        } else {
            final String className = "FunctionImpl" + rand.nextInt() + System.currentTimeMillis();
            if (LOG.isInfoEnabled())
                LOG.info("Compiling\n" + InMemoryCompiler.beautifyCode(javaCode));
            clazz = javac.compileClass(className, javaCode.replace("__CLASS__", className));
            CODE2CLASS.put(javaCode, clazz);
        }
        final Constructor<?> ctor = clazz.getConstructor();
        final Object instance = ctor.newInstance();
        if (!(instance instanceof Function)) {
            throw new JvarkitException.ScriptingError("Not an instance of Function");
        }
        this.transformer = (Function) instance;
    } catch (final Exception err) {
        throw new RuntimeException(err);
    }
}
Also used : Function(java.util.function.Function) InMemoryCompiler(com.github.lindenb.jvarkit.lang.InMemoryCompiler) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) BeforeStep(org.springframework.batch.core.annotation.BeforeStep)

Example 4 with InMemoryCompiler

use of com.github.lindenb.jvarkit.lang.InMemoryCompiler 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.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 = SamJdk.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 javax.annotation.Generated;");
        pw.println("@Generated(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" + InMemoryCompiler.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 InMemoryCompiler inMemoryCompiler = new InMemoryCompiler();
        final Class<?> compiledClass = inMemoryCompiler.compileClass(javaClassName, codeWriter.toString());
        final Constructor<?> ctor = compiledClass.getDeclaredConstructor(SAMFileHeader.class);
        samFileReader = openSamReader(oneFileOrNull(args));
        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.openSAMFileWriter(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) SamReader(htsjdk.samtools.SamReader) Random(java.util.Random) StringWriter(java.io.StringWriter) InMemoryCompiler(com.github.lindenb.jvarkit.lang.InMemoryCompiler) Iso8601Date(htsjdk.samtools.util.Iso8601Date) PrintWriter(java.io.PrintWriter) 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) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) SAMRecord(htsjdk.samtools.SAMRecord) Collection(java.util.Collection) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Aggregations

InMemoryCompiler (com.github.lindenb.jvarkit.lang.InMemoryCompiler)4 Iso8601Date (htsjdk.samtools.util.Iso8601Date)3 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 Date (java.util.Date)3 Random (java.util.Random)3 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 BAMRecordCodec (htsjdk.samtools.BAMRecordCodec)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1