Search in sources :

Example 46 with BiFunction

use of java.util.function.BiFunction in project gatk-protected by broadinstitute.

the class CoverageModelEMWorkspace method getCopyRatioSegmentsSpark.

/**
     * Fetch copy ratio segments from compute blocks (Spark implementation)
     *
     * @return a list of {@link CopyRatioHMMResults}
     */
private List<List<HiddenStateSegmentRecord<STATE, Target>>> getCopyRatioSegmentsSpark() {
    /* local final member variables for lambda capture */
    final List<Target> processedTargetList = new ArrayList<>();
    processedTargetList.addAll(this.processedTargetList);
    final List<SexGenotypeData> processedSampleSexGenotypeData = new ArrayList<>();
    processedSampleSexGenotypeData.addAll(this.processedSampleSexGenotypeData);
    final List<String> processedSampleNameList = new ArrayList<>();
    processedSampleNameList.addAll(this.processedSampleNameList);
    final INDArray sampleReadDepths = Transforms.exp(sampleMeanLogReadDepths, true);
    final CopyRatioExpectationsCalculator<CoverageModelCopyRatioEmissionData, STATE> copyRatioExpectationsCalculator = this.copyRatioExpectationsCalculator;
    final BiFunction<SexGenotypeData, Target, STATE> referenceStateFactory = this.referenceStateFactory;
    return fetchCopyRatioEmissionDataSpark().mapPartitionsToPair(it -> {
        final List<Tuple2<Integer, CopyRatioHMMResults<CoverageModelCopyRatioEmissionData, STATE>>> newPartitionData = new ArrayList<>();
        while (it.hasNext()) {
            final Tuple2<Integer, List<CoverageModelCopyRatioEmissionData>> prevDatum = it.next();
            final int sampleIndex = prevDatum._1;
            final CopyRatioCallingMetadata copyRatioCallingMetadata = CopyRatioCallingMetadata.builder().sampleName(processedSampleNameList.get(sampleIndex)).sampleSexGenotypeData(processedSampleSexGenotypeData.get(sampleIndex)).sampleCoverageDepth(sampleReadDepths.getDouble(sampleIndex)).emissionCalculationStrategy(EmissionCalculationStrategy.HYBRID_POISSON_GAUSSIAN).build();
            newPartitionData.add(new Tuple2<>(sampleIndex, copyRatioExpectationsCalculator.getCopyRatioHMMResults(copyRatioCallingMetadata, processedTargetList, prevDatum._2)));
        }
        return newPartitionData.iterator();
    }, true).mapPartitionsToPair(it -> {
        final List<Tuple2<Integer, List<HiddenStateSegmentRecord<STATE, Target>>>> newPartitionData = new ArrayList<>();
        while (it.hasNext()) {
            final Tuple2<Integer, CopyRatioHMMResults<CoverageModelCopyRatioEmissionData, STATE>> prevDatum = it.next();
            final int sampleIndex = prevDatum._1;
            final CopyRatioHMMResults<CoverageModelCopyRatioEmissionData, STATE> result = prevDatum._2;
            final HMMSegmentProcessor<CoverageModelCopyRatioEmissionData, STATE, Target> processor = new HMMSegmentProcessor<>(Collections.singletonList(result.getMetaData().getSampleName()), Collections.singletonList(result.getMetaData().getSampleSexGenotypeData()), referenceStateFactory, Collections.singletonList(new HashedListTargetCollection<>(processedTargetList)), Collections.singletonList(result.getForwardBackwardResult()), Collections.singletonList(result.getViterbiResult()));
            newPartitionData.add(new Tuple2<>(sampleIndex, processor.getSegmentsAsList()));
        }
        return newPartitionData.iterator();
    }).collect().stream().sorted(Comparator.comparingInt(t -> t._1)).map(t -> t._2).collect(Collectors.toList());
}
Also used : ScalarProducer(org.broadinstitute.hellbender.utils.hmm.interfaces.ScalarProducer) Function2(org.apache.spark.api.java.function.Function2) HMMSegmentProcessor(org.broadinstitute.hellbender.utils.hmm.segmentation.HMMSegmentProcessor) GermlinePloidyAnnotatedTargetCollection(org.broadinstitute.hellbender.tools.exome.sexgenotyper.GermlinePloidyAnnotatedTargetCollection) HiddenStateSegmentRecordWriter(org.broadinstitute.hellbender.utils.hmm.segmentation.HiddenStateSegmentRecordWriter) BiFunction(java.util.function.BiFunction) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) SexGenotypeData(org.broadinstitute.hellbender.tools.exome.sexgenotyper.SexGenotypeData) ParamUtils(org.broadinstitute.hellbender.utils.param.ParamUtils) CallStringProducer(org.broadinstitute.hellbender.utils.hmm.interfaces.CallStringProducer) StorageLevel(org.apache.spark.storage.StorageLevel) SynchronizedUnivariateSolver(org.broadinstitute.hellbender.tools.coveragemodel.math.SynchronizedUnivariateSolver) CopyRatioExpectationsCalculator(org.broadinstitute.hellbender.tools.coveragemodel.interfaces.CopyRatioExpectationsCalculator) UnivariateSolverSpecifications(org.broadinstitute.hellbender.tools.coveragemodel.math.UnivariateSolverSpecifications) IndexRange(org.broadinstitute.hellbender.utils.IndexRange) Broadcast(org.apache.spark.broadcast.Broadcast) ExitStatus(org.broadinstitute.hellbender.tools.coveragemodel.linalg.IterativeLinearSolverNDArray.ExitStatus) SexGenotypeDataCollection(org.broadinstitute.hellbender.tools.exome.sexgenotyper.SexGenotypeDataCollection) HashPartitioner(org.apache.spark.HashPartitioner) Predicate(java.util.function.Predicate) GeneralLinearOperator(org.broadinstitute.hellbender.tools.coveragemodel.linalg.GeneralLinearOperator) Nd4j(org.nd4j.linalg.factory.Nd4j) INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex) FastMath(org.apache.commons.math3.util.FastMath) org.broadinstitute.hellbender.tools.exome(org.broadinstitute.hellbender.tools.exome) Tuple2(scala.Tuple2) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AbstractUnivariateSolver(org.apache.commons.math3.analysis.solvers.AbstractUnivariateSolver) FourierLinearOperatorNDArray(org.broadinstitute.hellbender.tools.coveragemodel.linalg.FourierLinearOperatorNDArray) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) UserException(org.broadinstitute.hellbender.exceptions.UserException) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) TooManyEvaluationsException(org.apache.commons.math3.exception.TooManyEvaluationsException) Utils(org.broadinstitute.hellbender.utils.Utils) Function(org.apache.spark.api.java.function.Function) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) IntStream(java.util.stream.IntStream) java.util(java.util) NDArrayIndex(org.nd4j.linalg.indexing.NDArrayIndex) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) AlleleMetadataProducer(org.broadinstitute.hellbender.utils.hmm.interfaces.AlleleMetadataProducer) EmissionCalculationStrategy(org.broadinstitute.hellbender.tools.coveragemodel.CoverageModelCopyRatioEmissionProbabilityCalculator.EmissionCalculationStrategy) RobustBrentSolver(org.broadinstitute.hellbender.tools.coveragemodel.math.RobustBrentSolver) IntervalUtils(org.broadinstitute.hellbender.utils.IntervalUtils) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) HiddenStateSegmentRecord(org.broadinstitute.hellbender.utils.hmm.segmentation.HiddenStateSegmentRecord) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) IterativeLinearSolverNDArray(org.broadinstitute.hellbender.tools.coveragemodel.linalg.IterativeLinearSolverNDArray) GATKProtectedMathUtils(org.broadinstitute.hellbender.utils.GATKProtectedMathUtils) Nd4jIOUtils(org.broadinstitute.hellbender.tools.coveragemodel.nd4jutils.Nd4jIOUtils) IOException(java.io.IOException) JavaPairRDD(org.apache.spark.api.java.JavaPairRDD) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) File(java.io.File) INDArray(org.nd4j.linalg.api.ndarray.INDArray) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Transforms(org.nd4j.linalg.ops.transforms.Transforms) LogManager(org.apache.logging.log4j.LogManager) NoBracketingException(org.apache.commons.math3.exception.NoBracketingException) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Tuple2(scala.Tuple2) SexGenotypeData(org.broadinstitute.hellbender.tools.exome.sexgenotyper.SexGenotypeData) HMMSegmentProcessor(org.broadinstitute.hellbender.utils.hmm.segmentation.HMMSegmentProcessor)

Example 47 with BiFunction

use of java.util.function.BiFunction in project gatk by broadinstitute.

the class ReadCountRecordUnitTest method testAppendCountsToBeyondEnd.

@Test(dataProvider = "testNonZeroCountsData", dependsOnMethods = "testAppendCountsTo", expectedExceptions = IllegalStateException.class)
public void testAppendCountsToBeyondEnd(@SuppressWarnings("unused") final String testName, final BiFunction<Target, double[], ? extends ReadCountRecord> constructor, final int size) {
    final double[] counts = generateCounts(size);
    final ReadCountRecord record = constructor.apply(TEST_TARGET, counts);
    final List<String> columnNames = Stream.concat(Stream.concat(IntStream.range(0, 10).mapToObj(i -> "pre-padding_" + i), IntStream.range(0, counts.length).mapToObj(i -> "column_" + i)), IntStream.range(0, 10).mapToObj(i -> "post-padding_" + i)).collect(Collectors.toList());
    final TableColumnCollection columns = new TableColumnCollection(columnNames);
    final DataLine dataLine = new DataLine(columns, RuntimeException::new);
    final double[] copiedCounts = new double[counts.length + 20];
    Arrays.fill(copiedCounts, -11);
    dataLine.seek(columnNames.size());
    record.appendCountsTo(dataLine);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) BiFunction(java.util.function.BiFunction) Test(org.testng.annotations.Test) Random(java.util.Random) Collectors(java.util.stream.Collectors) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) Assert(org.testng.Assert) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) Test(org.testng.annotations.Test)

Example 48 with BiFunction

use of java.util.function.BiFunction in project gatk by broadinstitute.

the class ReadCountRecordUnitTest method testAppendCountsTo.

@Test(dataProvider = "testData", dependsOnMethods = "testCreation")
public void testAppendCountsTo(@SuppressWarnings("unused") final String testName, final BiFunction<Target, double[], ? extends ReadCountRecord> constructor, final int size) {
    final double[] counts = generateCounts(size);
    final boolean round = testName.equals("long[]");
    final ReadCountRecord record = constructor.apply(TEST_TARGET, counts);
    final List<String> columnNames = Stream.concat(Stream.concat(IntStream.range(0, 10).mapToObj(i -> "pre-padding_" + i), IntStream.range(0, counts.length).mapToObj(i -> "column_" + i)), IntStream.range(0, 10).mapToObj(i -> "post-padding_" + i)).collect(Collectors.toList());
    final TableColumnCollection columns = new TableColumnCollection(columnNames);
    final DataLine dataLine = new DataLine(columns, RuntimeException::new);
    final double[] copiedCounts = new double[counts.length + 20];
    Arrays.fill(copiedCounts, -11);
    for (int i = 0; i < 10 + 10 + counts.length; i++) {
        dataLine.append("-11");
    }
    dataLine.seek(10);
    record.appendCountsTo(dataLine);
    // Check the copied values.
    if (!round) {
        for (int i = 0; i < counts.length; i++) {
            Assert.assertEquals(dataLine.getDouble(10 + i), counts[i], 0.0);
        }
    } else {
        for (int i = 0; i < counts.length; i++) {
            Assert.assertEquals(dataLine.getDouble(10 + i), Math.round(counts[i]), 0.00001);
        }
    }
    // Check that the padding remains intact:
    for (int i = 0; i < 10; i++) {
        Assert.assertEquals(dataLine.get(i), "-11");
    }
    for (int i = counts.length + 10; i < copiedCounts.length; i++) {
        Assert.assertEquals(dataLine.get(i), "-11");
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) BiFunction(java.util.function.BiFunction) Test(org.testng.annotations.Test) Random(java.util.Random) Collectors(java.util.stream.Collectors) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) Assert(org.testng.Assert) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) Test(org.testng.annotations.Test)

Example 49 with BiFunction

use of java.util.function.BiFunction in project samza by apache.

the class StreamGraphImpl method getIntermediateStream.

/**
   * Internal helper for {@link MessageStreamImpl} to add an intermediate {@link MessageStream} to the graph.
   * An intermediate {@link MessageStream} is both an output and an input stream.
   *
   * @param streamName the name of the stream to be created. Will be prefixed with job name and id to generate the
   *                   logical streamId.
   * @param keyExtractor the {@link Function} to extract the outgoing key from the intermediate message
   * @param msgExtractor the {@link Function} to extract the outgoing message from the intermediate message
   * @param msgBuilder the {@link BiFunction} to convert the incoming key and message to a message
   *                   in the intermediate {@link MessageStream}
   * @param <K> the type of key in the intermediate message
   * @param <V> the type of message in the intermediate message
   * @param <M> the type of messages in the intermediate {@link MessageStream}
   * @return  the intermediate {@link MessageStreamImpl}
   */
<K, V, M> MessageStreamImpl<M> getIntermediateStream(String streamName, Function<? super M, ? extends K> keyExtractor, Function<? super M, ? extends V> msgExtractor, BiFunction<? super K, ? super V, ? extends M> msgBuilder) {
    String streamId = String.format("%s-%s-%s", config.get(JobConfig.JOB_NAME()), config.get(JobConfig.JOB_ID(), "1"), streamName);
    if (msgBuilder == null) {
        throw new IllegalArgumentException("msgBuilder cannot be null for an intermediate stream");
    }
    if (keyExtractor == null) {
        throw new IllegalArgumentException("keyExtractor can't be null for an output stream.");
    }
    if (msgExtractor == null) {
        throw new IllegalArgumentException("msgExtractor can't be null for an output stream.");
    }
    StreamSpec streamSpec = runner.getStreamSpec(streamId);
    IntermediateStreamInternalImpl<K, V, M> intStream = (IntermediateStreamInternalImpl<K, V, M>) inStreams.computeIfAbsent(streamSpec, k -> new IntermediateStreamInternalImpl<>(this, streamSpec, (Function<M, K>) keyExtractor, (Function<M, V>) msgExtractor, (BiFunction<K, V, M>) msgBuilder));
    outStreams.putIfAbsent(streamSpec, intStream);
    return intStream;
}
Also used : ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) OutputStreamInternal(org.apache.samza.operators.stream.OutputStreamInternal) Collection(java.util.Collection) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) Set(java.util.Set) StreamSpec(org.apache.samza.system.StreamSpec) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) InputStreamInternal(org.apache.samza.operators.stream.InputStreamInternal) HashSet(java.util.HashSet) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Map(java.util.Map) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Collections(java.util.Collections) StreamSpec(org.apache.samza.system.StreamSpec) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl)

Example 50 with BiFunction

use of java.util.function.BiFunction in project Gargoyle by callakrsos.

the class RequestUtil method request200.

public static <T> T request200(URL url, byte[] out, BiFunction<InputStream, Charset, T> response, boolean autoClose) throws Exception {
    URLConnection openConnection = url.openConnection();
    HttpURLConnection conn = (HttpURLConnection) openConnection;
    InputStream is = null;
    T result = null;
    try {
        conn.setDefaultUseCaches(true);
        conn.setUseCaches(true);
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
        conn.setRequestProperty("Accept-Encoding", "UTF-8");
        // conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Accept-Encoding", "UTF-8");
        conn.setRequestProperty("Accept-Language", "KR");
        conn.setConnectTimeout(6000);
        conn.setReadTimeout(6000);
        if (out != null) {
            conn.setDoOutput(true);
            OutputStream stream = conn.getOutputStream();
            stream.write(out);
        }
        conn.connect();
        is = conn.getInputStream();
        String contentType = conn.getContentType();
        String contentEncoding = conn.getContentEncoding();
        //			int contentLength = conn.getContentLength();
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        try {
            Optional<String> findAny = headerFields.keySet().stream().filter(f -> f != null).filter(str -> {
                if ("Accept-Encoding".equals(str))
                    return true;
                return str.toLowerCase().indexOf("charset") >= 0;
            }).findAny();
            if (findAny.isPresent()) {
                String wow = findAny.get();
                List<String> list = headerFields.get(wow);
                wow = list.get(0);
                if (Charset.isSupported(wow)) {
                    contentEncoding = wow;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        //			String charset = "UTF-8";
        if (ValueUtil.isEmpty(contentEncoding)) {
            if (contentType != null) {
                Optional<String> map = Stream.of(contentType.split(";")).filter(txt -> txt.toLowerCase().contains("charset")).findFirst().map(v -> {
                    return v.substring(v.indexOf("=") + 1);
                });
                if (map.isPresent())
                    contentEncoding = map.get();
                else {
                    String headerField = conn.getHeaderField("Accept-Charset");
                    if (ValueUtil.isNotEmpty(headerField))
                        contentEncoding = headerField;
                }
            }
        }
        if (ValueUtil.isEmpty(contentEncoding)) {
            contentEncoding = "UTF-8";
            LOGGER.debug("force encoding 'UTF-8' -  what is encoding ?????  ########################################");
        }
        LOGGER.debug("code : [{}] [{}] URL : {} ,  ", conn.getResponseCode(), contentEncoding, url.toString());
        if (200 == conn.getResponseCode()) {
            result = response.apply(is, Charset.forName(contentEncoding));
        }
    } finally {
        if (autoClose) {
            if (is != null)
                is.close();
            if (conn != null)
                conn.disconnect();
        }
    }
    return result;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URL(java.net.URL) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) Header(org.apache.http.Header) ArrayList(java.util.ArrayList) EntityUtils(org.apache.http.util.EntityUtils) Charset(java.nio.charset.Charset) URLConnection(java.net.URLConnection) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Map(java.util.Map) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) Cookie(org.apache.http.cookie.Cookie) HostnameVerifier(javax.net.ssl.HostnameVerifier) OutputStream(java.io.OutputStream) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Logger(org.slf4j.Logger) RuntimeException(com.sun.star.uno.RuntimeException) Iterator(java.util.Iterator) HttpEntity(org.apache.http.HttpEntity) StringEntity(org.apache.http.entity.StringEntity) List(java.util.List) Stream(java.util.stream.Stream) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpGet(org.apache.http.client.methods.HttpGet) Optional(java.util.Optional) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) NameValuePair(org.apache.http.NameValuePair) InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) List(java.util.List) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

BiFunction (java.util.function.BiFunction)51 List (java.util.List)25 HashMap (java.util.HashMap)24 Map (java.util.Map)24 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21 Collectors (java.util.stream.Collectors)18 Collections (java.util.Collections)15 Arrays (java.util.Arrays)14 Set (java.util.Set)14 Function (java.util.function.Function)14 Collection (java.util.Collection)13 IOException (java.io.IOException)12 IntStream (java.util.stream.IntStream)12 Mockito.mock (org.mockito.Mockito.mock)12 Before (org.junit.Before)11 Stream (java.util.stream.Stream)10 HashSet (java.util.HashSet)8 TimeUnit (java.util.concurrent.TimeUnit)8 Logger (org.apache.logging.log4j.Logger)8