Search in sources :

Example 21 with BiConsumer

use of java.util.function.BiConsumer in project hmftools by hartwigmedical.

the class BreakPointInspectorApplication method main.

public static void main(final String... args) throws IOException {
    final AnalysisBuilder analysisBuilder = new AnalysisBuilder();
    final Options options = createOptions();
    try {
        final CommandLine cmd = createCommandLine(options, args);
        final String refPath = cmd.getOptionValue(REF_PATH);
        final String refSlicePath = cmd.getOptionValue(REF_SLICE);
        final String tumorPath = cmd.getOptionValue(TUMOR_PATH);
        final String tumorSlicePath = cmd.getOptionValue(TUMOR_SLICE);
        final String vcfPath = cmd.getOptionValue(VCF);
        if (cmd.hasOption(PROXIMITY)) {
            analysisBuilder.setRange(Integer.parseInt(cmd.getOptionValue(PROXIMITY, "500")));
        }
        if (cmd.hasOption(CONTAMINATION)) {
            analysisBuilder.setContaminationFraction(Float.parseFloat(cmd.getOptionValue(CONTAMINATION, "0")));
        }
        if (refPath == null || tumorPath == null || vcfPath == null) {
            printHelpAndExit(options);
            return;
        }
        final File tumorBAM = new File(tumorPath);
        final SamReader tumorReader = SamReaderFactory.makeDefault().open(tumorBAM);
        final File refBAM = new File(refPath);
        final SamReader refReader = SamReaderFactory.makeDefault().open(refBAM);
        final File vcfFile = new File(vcfPath);
        final VCFFileReader vcfReader = new VCFFileReader(vcfFile, false);
        final List<String> samples = vcfReader.getFileHeader().getGenotypeSamples();
        if (samples.size() != 2) {
            System.err.println("could not determine tumor and sample from VCF");
            System.exit(1);
            return;
        }
        TSVOutput.PrintHeaders();
        final Analysis analysis = analysisBuilder.setRefReader(refReader).setTumorReader(tumorReader).createAnalysis();
        final List<QueryInterval> combinedQueryIntervals = Lists.newArrayList();
        final Map<String, VariantContext> variantMap = new HashMap<>();
        final List<VariantContext> variants = Lists.newArrayList();
        for (VariantContext variant : vcfReader) {
            variantMap.put(variant.getID(), variant);
            final VariantContext mateVariant = variant;
            if (variant.hasAttribute("MATEID")) {
                variant = variantMap.get(variant.getAttributeAsString("MATEID", ""));
                if (variant == null) {
                    continue;
                }
            }
            final String location = variant.getContig() + ":" + Integer.toString(variant.getStart());
            final Location location1 = Location.parseLocationString(location, tumorReader.getFileHeader().getSequenceDictionary());
            final Range uncertainty1 = extractCIPOS(variant);
            final List<Integer> CIEND = variant.getAttributeAsIntList("CIEND", 0);
            Range uncertainty2 = CIEND.size() == 2 ? new Range(CIEND.get(0), CIEND.get(1)) : null;
            final boolean IMPRECISE = variant.hasAttribute("IMPRECISE");
            HMFVariantType svType;
            final Location location2;
            switch(variant.getStructuralVariantType()) {
                case INS:
                    svType = HMFVariantType.INS;
                    location2 = location1.set(variant.getAttributeAsInt("END", 0));
                    break;
                case INV:
                    if (variant.hasAttribute("INV3")) {
                        svType = HMFVariantType.INV3;
                    } else if (variant.hasAttribute("INV5")) {
                        svType = HMFVariantType.INV5;
                    } else {
                        System.err.println(variant.getID() + " : expected either INV3 or INV5 flag");
                        continue;
                    }
                    location2 = location1.add(Math.abs(variant.getAttributeAsInt("SVLEN", 0)));
                    break;
                case DEL:
                    svType = HMFVariantType.DEL;
                    location2 = location1.add(Math.abs(variant.getAttributeAsInt("SVLEN", 0)));
                    break;
                case DUP:
                    svType = HMFVariantType.DUP;
                    location2 = location1.add(Math.abs(variant.getAttributeAsInt("SVLEN", 0)));
                    break;
                case BND:
                    // process the breakend string
                    final String call = variant.getAlternateAllele(0).getDisplayString();
                    final String[] leftSplit = call.split("\\]");
                    final String[] rightSplit = call.split("\\[");
                    if (leftSplit.length >= 2) {
                        location2 = Location.parseLocationString(leftSplit[1], tumorReader.getFileHeader().getSequenceDictionary());
                        if (leftSplit[0].length() > 0) {
                            svType = HMFVariantType.INV3;
                            uncertainty2 = Range.invert(uncertainty1);
                        } else {
                            svType = HMFVariantType.DUP;
                            uncertainty2 = uncertainty1;
                        }
                    } else if (rightSplit.length >= 2) {
                        location2 = Location.parseLocationString(rightSplit[1], tumorReader.getFileHeader().getSequenceDictionary());
                        if (rightSplit[0].length() > 0) {
                            svType = HMFVariantType.DEL;
                            uncertainty2 = uncertainty1;
                        } else {
                            svType = HMFVariantType.INV5;
                            uncertainty2 = Range.invert(uncertainty1);
                        }
                    } else {
                        System.err.println(variant.getID() + " : could not parse breakpoint");
                        continue;
                    }
                    if (IMPRECISE) {
                        uncertainty2 = extractCIPOS(mateVariant);
                    }
                    break;
                default:
                    System.err.println(variant.getID() + " : UNEXPECTED SVTYPE=" + variant.getStructuralVariantType());
                    continue;
            }
            final HMFVariantContext ctx = new HMFVariantContext(variant.getID(), location1, location2, svType, IMPRECISE);
            ctx.Filter.addAll(variant.getFilters().stream().filter(s -> !s.startsWith("BPI")).collect(Collectors.toSet()));
            ctx.Uncertainty1 = uncertainty1;
            ctx.Uncertainty2 = ObjectUtils.firstNonNull(uncertainty2, fixup(uncertainty1, IMPRECISE, svType == HMFVariantType.INV3 || svType == HMFVariantType.INV5));
            ctx.HomologySequence = variant.getAttributeAsString("HOMSEQ", "");
            if (variant.hasAttribute("LEFT_SVINSSEQ") && variant.hasAttribute("RIGHT_SVINSSEQ")) {
                ctx.InsertSequence = variant.getAttributeAsString("LEFT_SVINSSEQ", "") + "..." + variant.getAttributeAsString("RIGHT_SVINSSEQ", "");
            } else {
                ctx.InsertSequence = variant.getAttributeAsString("SVINSSEQ", "");
            }
            ctx.BND = variant.getStructuralVariantType() == StructuralVariantType.BND;
            switch(ctx.Type) {
                case INS:
                case DEL:
                    ctx.OrientationBP1 = 1;
                    ctx.OrientationBP2 = -1;
                    break;
                case INV3:
                    ctx.OrientationBP1 = 1;
                    ctx.OrientationBP2 = 1;
                    break;
                case INV5:
                    ctx.OrientationBP1 = -1;
                    ctx.OrientationBP2 = -1;
                    break;
                case DUP:
                    ctx.OrientationBP1 = -1;
                    ctx.OrientationBP2 = 1;
                    break;
            }
            final StructuralVariantResult result = analysis.processStructuralVariant(ctx);
            combinedQueryIntervals.addAll(asList(result.QueryIntervals));
            TSVOutput.print(variant, ctx, result);
            final BiConsumer<VariantContext, Boolean> vcfUpdater = (v, swap) -> {
                final Set<String> filters = v.getCommonInfo().getFiltersMaybeNull();
                if (filters != null) {
                    filters.clear();
                }
                // we will map BreakpointError to a flag
                if (result.Filters.contains(Filter.Filters.BreakpointError.toString())) {
                    v.getCommonInfo().putAttribute("BPI_AMBIGUOUS", true, true);
                } else {
                    v.getCommonInfo().addFilters(result.Filters);
                }
                if (result.Filters.isEmpty()) {
                    final List<Double> af = asList(result.AlleleFrequency.getLeft(), result.AlleleFrequency.getRight());
                    v.getCommonInfo().putAttribute(AlleleFrequency.VCF_INFO_TAG, swap ? Lists.reverse(af) : af, true);
                }
                if (result.Breakpoints.getLeft() != null) {
                    v.getCommonInfo().putAttribute(swap ? "BPI_END" : "BPI_START", result.Breakpoints.getLeft().Position, true);
                }
                if (result.Breakpoints.getRight() != null) {
                    v.getCommonInfo().putAttribute(swap ? "BPI_START" : "BPI_END", result.Breakpoints.getRight().Position, true);
                }
                // remove CIPOS / CIEND when we have an insert sequence
                if (!v.hasAttribute("IMPRECISE") && v.hasAttribute("SVINSSEQ")) {
                    v.getCommonInfo().removeAttribute("CIPOS");
                    v.getCommonInfo().removeAttribute("CIEND");
                }
                variants.add(v);
            };
            vcfUpdater.accept(variant, false);
            if (mateVariant != variant) {
                vcfUpdater.accept(mateVariant, true);
            }
        }
        // TODO: update START, END with BPI values and save Manta values in new attributes
        final String vcfOutputPath = cmd.getOptionValue(VCF_OUT);
        if (vcfOutputPath != null) {
            final VCFHeader header = vcfReader.getFileHeader();
            header.addMetaDataLine(new VCFInfoHeaderLine("BPI_START", 1, VCFHeaderLineType.Integer, "BPI adjusted breakend location"));
            header.addMetaDataLine(new VCFInfoHeaderLine("BPI_END", 1, VCFHeaderLineType.Integer, "BPI adjusted breakend location"));
            header.addMetaDataLine(new VCFInfoHeaderLine("BPI_AMBIGUOUS", 0, VCFHeaderLineType.Flag, "BPI could not determine the breakpoints, inspect manually"));
            header.addMetaDataLine(new VCFHeaderLine("bpiVersion", BreakPointInspectorApplication.class.getPackage().getImplementationVersion()));
            Filter.UpdateVCFHeader(header);
            AlleleFrequency.UpdateVCFHeader(header);
            // setup VCF
            final VariantContextWriter writer = new VariantContextWriterBuilder().setReferenceDictionary(header.getSequenceDictionary()).setOutputFile(vcfOutputPath).build();
            writer.writeHeader(header);
            // write variants
            variants.sort(new VariantContextComparator(header.getSequenceDictionary()));
            variants.forEach(writer::add);
            writer.close();
        }
        final QueryInterval[] optimizedIntervals = QueryInterval.optimizeIntervals(combinedQueryIntervals.toArray(new QueryInterval[combinedQueryIntervals.size()]));
        if (tumorSlicePath != null) {
            writeToSlice(tumorSlicePath, tumorReader, optimizedIntervals);
        }
        if (refSlicePath != null) {
            writeToSlice(refSlicePath, refReader, optimizedIntervals);
        }
        refReader.close();
        tumorReader.close();
    } catch (ParseException e) {
        printHelpAndExit(options);
        System.exit(1);
    }
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) Options(org.apache.commons.cli.Options) HashMap(java.util.HashMap) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultParser(org.apache.commons.cli.DefaultParser) Lists(com.google.common.collect.Lists) Arrays.asList(java.util.Arrays.asList) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) VCFHeaderLineType(htsjdk.variant.vcf.VCFHeaderLineType) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) CommandLineParser(org.apache.commons.cli.CommandLineParser) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) Set(java.util.Set) IOException(java.io.IOException) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SamReader(htsjdk.samtools.SamReader) Collectors(java.util.stream.Collectors) File(java.io.File) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator) List(java.util.List) QueryInterval(htsjdk.samtools.QueryInterval) ParseException(org.apache.commons.cli.ParseException) StructuralVariantType(htsjdk.variant.variantcontext.StructuralVariantType) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) VariantContext(htsjdk.variant.variantcontext.VariantContext) NotNull(org.jetbrains.annotations.NotNull) SamReaderFactory(htsjdk.samtools.SamReaderFactory) Options(org.apache.commons.cli.Options) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) Set(java.util.Set) HashMap(java.util.HashMap) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) QueryInterval(htsjdk.samtools.QueryInterval) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator) SamReader(htsjdk.samtools.SamReader) Arrays.asList(java.util.Arrays.asList) List(java.util.List) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) CommandLine(org.apache.commons.cli.CommandLine) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 22 with BiConsumer

use of java.util.function.BiConsumer in project crypto-bot by jnidzwetzki.

the class BarMegerTest method testBarAlignment1.

/**
 * Test the alignment of the Bars
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 10000)
public void testBarAlignment1() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(3);
    final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
        Assert.assertTrue(t.getEndTime().getSecond() == 59);
        latch.countDown();
    };
    final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
    BarMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:02:33").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("02:03:53").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
    BarMerger.close();
    latch.await();
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BiConsumer(java.util.function.BiConsumer) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Test(org.junit.Test) ParseException(java.text.ParseException) Assert(org.junit.Assert) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) Bar(org.ta4j.core.Bar) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 23 with BiConsumer

use of java.util.function.BiConsumer in project crypto-bot by jnidzwetzki.

the class BarMegerTest method testBarAlignment2.

/**
 * Test the alignment of the Bars
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 10000)
public void testBarAlignment2() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(4);
    final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
        Assert.assertTrue(t.getEndTime().getMinute() == 14 || t.getEndTime().getMinute() == 29 || t.getEndTime().getMinute() == 44 || t.getEndTime().getMinute() == 59);
        Assert.assertEquals(59, t.getEndTime().getSecond());
        latch.countDown();
    };
    final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_15, BarConsumer);
    BarMerger.addNewPrice(parser.parse("01:01:00").getTime(), 1.0, 5.0);
    BarMerger.addNewPrice(parser.parse("02:41:33").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("10:33:11").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
    BarMerger.close();
    latch.await();
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BiConsumer(java.util.function.BiConsumer) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Test(org.junit.Test) ParseException(java.text.ParseException) Assert(org.junit.Assert) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) Bar(org.ta4j.core.Bar) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 24 with BiConsumer

use of java.util.function.BiConsumer in project crypto-bot by jnidzwetzki.

the class BarMegerTest method testBarMerger4.

/**
 * Test three Bar merge with other timestamps
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 6000)
public void testBarMerger4() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(2);
    final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
        latch.countDown();
    };
    final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
    BarMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:33").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("02:02:53").getTime(), 2.0, 5.0);
    BarMerger.close();
    latch.await();
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BiConsumer(java.util.function.BiConsumer) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Test(org.junit.Test) ParseException(java.text.ParseException) Assert(org.junit.Assert) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) Bar(org.ta4j.core.Bar) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 25 with BiConsumer

use of java.util.function.BiConsumer in project crypto-bot by jnidzwetzki.

the class BarMegerTest method testBarMerger2.

/**
 * Test two Bar merge
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 5000)
public void testBarMerger2() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(1);
    final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
        Assert.assertEquals(10, t.getVolume().doubleValue(), DELTA);
        Assert.assertEquals(1.0, t.getMinPrice().doubleValue(), DELTA);
        Assert.assertEquals(2.0, t.getMaxPrice().doubleValue(), DELTA);
        latch.countDown();
    };
    final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
    BarMerger.addNewPrice(parser.parse("01:01:13").getTime(), 1.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:23").getTime(), 2.0, 5.0);
    BarMerger.close();
    latch.await();
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BiConsumer(java.util.function.BiConsumer) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Test(org.junit.Test) ParseException(java.text.ParseException) Assert(org.junit.Assert) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) Bar(org.ta4j.core.Bar) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

BiConsumer (java.util.function.BiConsumer)264 Test (org.junit.Test)111 List (java.util.List)109 Map (java.util.Map)79 IOException (java.io.IOException)78 ArrayList (java.util.ArrayList)74 Consumer (java.util.function.Consumer)71 HashMap (java.util.HashMap)66 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)54 Collectors (java.util.stream.Collectors)53 CountDownLatch (java.util.concurrent.CountDownLatch)52 Collections (java.util.Collections)46 Set (java.util.Set)46 Collection (java.util.Collection)45 Arrays (java.util.Arrays)44 TimeUnit (java.util.concurrent.TimeUnit)43 Function (java.util.function.Function)43 Assert (org.junit.Assert)43 Optional (java.util.Optional)41 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)35