Search in sources :

Example 91 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project repseqio by repseqio.

the class CategoricalIndependentIntModel method create.

@Override
public IndependentIntGenerator create(RandomGenerator random) {
    int[] intValues = new int[distribution.size()];
    // ~ probability, will be normalized in distribution constructor
    double[] weights = new double[distribution.size()];
    int i = 0;
    for (Map.Entry<Integer, Double> e : distribution.entrySet()) {
        intValues[i] = e.getKey();
        weights[i] = e.getValue();
        ++i;
    }
    final EnumeratedIntegerDistribution dist = new EnumeratedIntegerDistribution(random, intValues, weights);
    return new IndependentIntGenerator() {

        @Override
        public int sample() {
            return dist.sample();
        }
    };
}
Also used : EnumeratedIntegerDistribution(org.apache.commons.math3.distribution.EnumeratedIntegerDistribution) Map(java.util.Map)

Example 92 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project repseqio by repseqio.

the class CommonNormalDTrimmingModel method create.

@Override
public DTrimmingGenerator create(RandomGenerator random, VDJCGene gene) {
    final int dLength = gene.getPartitioning().getLength(GeneFeature.DRegion);
    final EnumeratedIntegerDistribution dist3 = parameters3.truncatedDistribution(random, -dLength, Math.min(dLength, maxPLength3));
    final EnumeratedIntegerDistribution dist5 = parameters5.truncatedDistribution(random, -dLength, Math.min(dLength, maxPLength5));
    return new DTrimmingGenerator() {

        @Override
        public DTrimming sample() {
            int trimming3 = dist3.sample();
            int trimming5 = dist5.sample();
            if (trimming3 + trimming5 < -dLength) {
                int excess = dLength - trimming3 - trimming5;
                trimming3 += excess / 2;
                trimming5 += excess - (excess / 2);
            }
            return new DTrimming(trimming3, trimming5);
        }
    };
}
Also used : EnumeratedIntegerDistribution(org.apache.commons.math3.distribution.EnumeratedIntegerDistribution) DTrimming(io.repseq.gen.DTrimming)

Example 93 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project repseqio by repseqio.

the class ExportCloneSequencesActionTest method randomRoundTest.

@Test
public void randomRoundTest() throws Exception {
    double value = 12.34;
    double sum = 0.0;
    RandomGenerator random = new Well19937c(1232434);
    for (int i = 0; i < 100000; i++) sum += ExportCloneSequencesAction.randomizedRound(value, random);
    assertEquals(value, sum / 100000, 0.1);
}
Also used : Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) Test(org.junit.Test)

Example 94 with RandomGenerator

use of org.apache.commons.math3.random.RandomGenerator in project mixcr by milaboratory.

the class PartialAlignmentsAssemblerTest method createTestData.

public static InputTestData createTestData(long seed) throws Exception {
    EnumMap<GeneType, String> geneNames = new EnumMap<GeneType, String>(GeneType.class) {

        {
            put(Variable, "TRBV20-1*00");
            put(Diversity, "TRBD2*00");
            put(Joining, "TRBJ2-6*00");
            put(Constant, "TRBC2*00");
        }
    };
    // config
    RandomGenerator rnd = RandomUtil.getThreadLocalRandom();
    rnd.setSeed(seed);
    final VDJCAlignerParameters defaultFeatures = VDJCParametersPresets.getByName("default");
    defaultFeatures.getVAlignerParameters().setGeneFeatureToAlign(VRegion);
    defaultFeatures.getDAlignerParameters().setGeneFeatureToAlign(DRegion);
    defaultFeatures.getJAlignerParameters().setGeneFeatureToAlign(JRegion);
    // used alleles
    EnumMap<GeneType, VDJCGene> genes = new EnumMap<>(GeneType.class);
    // germline parts of sequences
    EnumMap<GeneType, NucleotideSequence> germlineRegions = gtMap();
    // left, right cut of germline
    EnumMap<GeneType, int[]> germlineCuts = gtMap();
    // begin, end positions in assembled sequence
    EnumMap<GeneType, int[]> refPositions = gtMap();
    // single assembled sequence
    SequenceBuilder<NucleotideSequence> referenceBuilder = NucleotideSequence.ALPHABET.createBuilder();
    NucleotideSequence VDJunction = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 3, 10);
    NucleotideSequence DJJunction = TestUtil.randomSequence(NucleotideSequence.ALPHABET, 3, 10);
    for (GeneType gt : GeneType.VDJC_REFERENCE) {
        VDJCGene gene = VDJCLibraryRegistry.getDefault().getLibrary("default", "hs").get(geneNames.get(gt));
        NucleotideSequence seq = gene.getFeature(defaultFeatures.getFeatureToAlign(gt));
        int[] cuts = null;
        switch(gt) {
            case Variable:
                cuts = new int[] { 0, rnd.nextInt(gene.getFeature(GermlineVCDR3Part).size() - 5) };
                break;
            case Diversity:
                cuts = new int[] { rnd.nextInt(seq.size() / 3), rnd.nextInt(seq.size() / 3) };
                break;
            case Joining:
                cuts = new int[] { rnd.nextInt(gene.getFeature(GermlineJCDR3Part).size() - 5), 0 };
                break;
            case Constant:
                cuts = new int[] { 0, rnd.nextInt(seq.size() / 2) };
                break;
        }
        NucleotideSequence gSeq = seq.getRange(cuts[0], seq.size() - cuts[1]);
        int[] positions = new int[2];
        positions[0] = referenceBuilder.size();
        referenceBuilder.append(gSeq);
        positions[1] = referenceBuilder.size();
        if (gt == Variable)
            referenceBuilder.append(VDJunction);
        if (gt == Diversity)
            referenceBuilder.append(DJJunction);
        genes.put(gt, gene);
        germlineCuts.put(gt, cuts);
        germlineRegions.put(gt, gSeq);
        refPositions.put(gt, positions);
    }
    NucleotideSequence VJJunction = NucleotideSequence.ALPHABET.createBuilder().append(VDJunction).append(germlineRegions.get(Diversity)).append(DJJunction).createAndDestroy();
    NucleotideSequence reference = referenceBuilder.createAndDestroy();
    return new InputTestData(genes, germlineRegions, germlineCuts, refPositions, VDJunction, DJJunction, reference, VJJunction);
}
Also used : VDJCAlignerParameters(com.milaboratory.mixcr.vdjaligners.VDJCAlignerParameters) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCGene(io.repseq.core.VDJCGene) GeneType(io.repseq.core.GeneType) EnumMap(java.util.EnumMap)

Aggregations

RandomGenerator (org.apache.commons.math3.random.RandomGenerator)82 Well19937c (org.apache.commons.math3.random.Well19937c)27 Random (java.util.Random)20 Test (org.testng.annotations.Test)18 RandomGeneratorFactory (org.apache.commons.math3.random.RandomGeneratorFactory)16 Assert (org.testng.Assert)16 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)14 Test (org.junit.Test)14 Collectors (java.util.stream.Collectors)12 IntStream (java.util.stream.IntStream)12 Arrays (java.util.Arrays)10 List (java.util.List)10 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)10 ArrayList (java.util.ArrayList)9 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)8 ModeledSegment (org.broadinstitute.hellbender.tools.exome.ModeledSegment)8 AllelicCountCollection (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection)8 java.util (java.util)6 GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)6 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)6