Example usage for org.apache.commons.lang ArrayUtils toPrimitive

List of usage examples for org.apache.commons.lang ArrayUtils toPrimitive

Introduction

In this page you can find the example usage for org.apache.commons.lang ArrayUtils toPrimitive.

Prototype

public static boolean[] toPrimitive(Boolean[] array) 

Source Link

Document

Converts an array of object Booleans to primitives.

Usage

From source file:org.broadinstitute.gatk.tools.walkers.genotyper.afcalc.ExactCallLogger.java

/**
 * Read in a list of ExactCall objects from reader, keeping only those
 * with starts in startsToKeep or all sites (if this is empty)
 *
 * @param reader a just-opened reader sitting at the start of the file
 * @param startsToKeep a list of start position of the calls to keep, or empty if all calls should be kept
 * @param parser a genome loc parser to create genome locs
 * @return a list of ExactCall objects in reader
 * @throws IOException//from w w w. j  a  v a 2s  .c o  m
 */
public static List<ExactCall> readExactLog(final BufferedReader reader, final List<Integer> startsToKeep,
        GenomeLocParser parser) throws IOException {
    if (reader == null)
        throw new IllegalArgumentException("reader cannot be null");
    if (startsToKeep == null)
        throw new IllegalArgumentException("startsToKeep cannot be null");
    if (parser == null)
        throw new IllegalArgumentException("GenomeLocParser cannot be null");

    List<ExactCall> calls = new LinkedList<ExactCall>();

    // skip the header line
    reader.readLine();

    // skip the first "type" line
    reader.readLine();

    while (true) {
        final VariantContextBuilder builder = new VariantContextBuilder();
        final List<Allele> alleles = new ArrayList<Allele>();
        final List<Genotype> genotypes = new ArrayList<Genotype>();
        final double[] posteriors = new double[2];
        final double[] priors = MathUtils.normalizeFromLog10(new double[] { 0.5, 0.5 }, true);
        final List<Integer> mle = new ArrayList<Integer>();
        final Map<Allele, Double> log10pRefByAllele = new HashMap<Allele, Double>();
        long runtimeNano = -1;

        GenomeLoc currentLoc = null;
        while (true) {
            final String line = reader.readLine();
            if (line == null)
                return calls;

            final String[] parts = line.split("\t");
            final GenomeLoc lineLoc = parser.parseGenomeLoc(parts[0]);
            final String variable = parts[1];
            final String key = parts[2];
            final String value = parts[3];

            if (currentLoc == null)
                currentLoc = lineLoc;

            if (variable.equals("type")) {
                if (startsToKeep.isEmpty() || startsToKeep.contains(currentLoc.getStart())) {
                    builder.alleles(alleles);
                    final int stop = currentLoc.getStart() + alleles.get(0).length() - 1;
                    builder.chr(currentLoc.getContig()).start(currentLoc.getStart()).stop(stop);
                    builder.genotypes(genotypes);
                    final int[] mleInts = ArrayUtils.toPrimitive(mle.toArray(new Integer[] {}));
                    final AFCalculationResult result = new AFCalculationResult(mleInts, 1, alleles, posteriors,
                            priors, log10pRefByAllele);
                    calls.add(new ExactCall(builder.make(), runtimeNano, result));
                }
                break;
            } else if (variable.equals("allele")) {
                final boolean isRef = key.equals("0");
                alleles.add(Allele.create(value, isRef));
            } else if (variable.equals("PL")) {
                final GenotypeBuilder gb = new GenotypeBuilder(key);
                gb.PL(GenotypeLikelihoods.fromPLField(value).getAsPLs());
                genotypes.add(gb.make());
            } else if (variable.equals("log10PosteriorOfAFEq0")) {
                posteriors[0] = Double.valueOf(value);
            } else if (variable.equals("log10PosteriorOfAFGt0")) {
                posteriors[1] = Double.valueOf(value);
            } else if (variable.equals("MLE")) {
                mle.add(Integer.valueOf(value));
            } else if (variable.equals("pRefByAllele")) {
                final Allele a = Allele.create(key);
                log10pRefByAllele.put(a, Double.valueOf(value));
            } else if (variable.equals("runtime.nano")) {
                runtimeNano = Long.valueOf(value);
            } else {
                // nothing to do
            }
        }
    }
}

From source file:org.broadinstitute.gatk.tools.walkers.variantrecalibration.VariantDataManager.java

public void normalizeData() {
    boolean foundZeroVarianceAnnotation = false;
    for (int iii = 0; iii < meanVector.length; iii++) {
        final double theMean = mean(iii, true);
        final double theSTD = standardDeviation(theMean, iii, true);
        logger.info(annotationKeys.get(iii)
                + String.format(": \t mean = %.2f\t standard deviation = %.2f", theMean, theSTD));
        if (Double.isNaN(theMean)) {
            throw new UserException.BadInput("Values for " + annotationKeys.get(iii)
                    + " annotation not detected for ANY training variant in the input callset. VariantAnnotator may be used to add these annotations.");
        }//from w w  w.j  a  v  a2  s  .com

        foundZeroVarianceAnnotation = foundZeroVarianceAnnotation || (theSTD < 1E-5);
        meanVector[iii] = theMean;
        varianceVector[iii] = theSTD;
        for (final VariantDatum datum : data) {
            // Transform each data point via: (x - mean) / standard deviation
            datum.annotations[iii] = (datum.isNull[iii] ? 0.1 * Utils.getRandomGenerator().nextGaussian()
                    : (datum.annotations[iii] - theMean) / theSTD);
        }
    }
    if (foundZeroVarianceAnnotation) {
        throw new UserException.BadInput(
                "Found annotations with zero variance. They must be excluded before proceeding.");
    }

    // trim data by standard deviation threshold and mark failing data for exclusion later
    for (final VariantDatum datum : data) {
        boolean remove = false;
        for (final double val : datum.annotations) {
            remove = remove || (Math.abs(val) > VRAC.STD_THRESHOLD);
        }
        datum.failingSTDThreshold = remove;
    }

    // re-order the data by increasing standard deviation so that the results don't depend on the order things were specified on the command line
    // standard deviation over the training points is used as a simple proxy for information content, perhaps there is a better thing to use here
    final List<Integer> theOrder = calculateSortOrder(meanVector);
    annotationKeys = reorderList(annotationKeys, theOrder);
    varianceVector = ArrayUtils.toPrimitive(reorderArray(ArrayUtils.toObject(varianceVector), theOrder));
    meanVector = ArrayUtils.toPrimitive(reorderArray(ArrayUtils.toObject(meanVector), theOrder));
    for (final VariantDatum datum : data) {
        datum.annotations = ArrayUtils
                .toPrimitive(reorderArray(ArrayUtils.toObject(datum.annotations), theOrder));
        datum.isNull = ArrayUtils.toPrimitive(reorderArray(ArrayUtils.toObject(datum.isNull), theOrder));
    }
    logger.info("Annotations are now ordered by their information content: " + annotationKeys.toString());
}

From source file:org.broadinstitute.gatk.tools.walkers.variantrecalibration.VariantDataManagerUnitTest.java

@Test
public final void testCalculateSortOrder() {
    final double passingQual = 400.0;
    final VariantRecalibratorArgumentCollection VRAC = new VariantRecalibratorArgumentCollection();

    VariantDataManager vdm = new VariantDataManager(new ArrayList<String>(), VRAC);

    final List<VariantDatum> theData = new ArrayList<>();
    final VariantDatum datum1 = new VariantDatum();
    datum1.atTrainingSite = true;//  w w w  . j a  va  2s . c  o m
    datum1.failingSTDThreshold = false;
    datum1.originalQual = passingQual;
    datum1.annotations = new double[] { 0.0, -10.0, 10.0 };
    datum1.isNull = new boolean[] { false, false, false };
    theData.add(datum1);

    final VariantDatum datum2 = new VariantDatum();
    datum2.atTrainingSite = true;
    datum2.failingSTDThreshold = false;
    datum2.originalQual = passingQual;
    datum2.annotations = new double[] { 0.0, -9.0, 15.0 };
    datum2.isNull = new boolean[] { false, false, false };
    theData.add(datum2);

    final VariantDatum datum3 = new VariantDatum();
    datum3.atTrainingSite = false;
    datum3.failingSTDThreshold = false;
    datum3.originalQual = passingQual;
    datum3.annotations = new double[] { 0.0, 1.0, 999.0 };
    datum3.isNull = new boolean[] { false, false, false };
    theData.add(datum3);

    final VariantDatum datum4 = new VariantDatum();
    datum4.atTrainingSite = false;
    datum4.failingSTDThreshold = false;
    datum4.originalQual = passingQual;
    datum4.annotations = new double[] { 0.015, 2.0, 1001.11 };
    datum4.isNull = new boolean[] { false, false, false };
    theData.add(datum4);

    vdm.setData(theData);

    final double[] meanVector = new double[3];
    for (int iii = 0; iii < meanVector.length; iii++) {
        meanVector[iii] = vdm.mean(iii, true);
    }
    final List<Integer> order = vdm.calculateSortOrder(meanVector);
    Assert.assertTrue(Arrays.equals(new int[] { 2, 1, 0 },
            ArrayUtils.toPrimitive(order.toArray(new Integer[order.size()]))));
}

From source file:org.broadinstitute.gatk.utils.MathUtilsUnitTest.java

@Test(dataProvider = "ArrayMinData")
public void testArrayMinIntArray(final List<Integer> values, final int expected) {
    final int[] asArray = ArrayUtils.toPrimitive(values.toArray(new Integer[values.size()]));
    final int actual = MathUtils.arrayMin(asArray);
    Assert.assertEquals(actual, expected, "Failed with " + values);
}

From source file:org.broadinstitute.sting.gatk.walkers.genotyper.afcalc.ExactCallLogger.java

/**
 * Read in a list of ExactCall objects from reader, keeping only those
 * with starts in startsToKeep or all sites (if this is empty)
 *
 * @param reader a just-opened reader sitting at the start of the file
 * @param startsToKeep a list of start position of the calls to keep, or empty if all calls should be kept
 * @param parser a genome loc parser to create genome locs
 * @return a list of ExactCall objects in reader
 * @throws IOException//from  w ww .  ja va 2 s  .  co  m
 */
public static List<ExactCall> readExactLog(final BufferedReader reader, final List<Integer> startsToKeep,
        GenomeLocParser parser) throws IOException {
    if (reader == null)
        throw new IllegalArgumentException("reader cannot be null");
    if (startsToKeep == null)
        throw new IllegalArgumentException("startsToKeep cannot be null");
    if (parser == null)
        throw new IllegalArgumentException("GenomeLocParser cannot be null");

    List<ExactCall> calls = new LinkedList<ExactCall>();

    // skip the header line
    reader.readLine();

    // skip the first "type" line
    reader.readLine();

    while (true) {
        final VariantContextBuilder builder = new VariantContextBuilder();
        final List<Allele> alleles = new ArrayList<Allele>();
        final List<Genotype> genotypes = new ArrayList<Genotype>();
        final double[] posteriors = new double[2];
        final double[] priors = MathUtils.normalizeFromLog10(new double[] { 0.5, 0.5 }, true);
        final List<Integer> mle = new ArrayList<Integer>();
        final Map<Allele, Double> log10pRefByAllele = new HashMap<Allele, Double>();
        long runtimeNano = -1;

        GenomeLoc currentLoc = null;
        while (true) {
            final String line = reader.readLine();
            if (line == null)
                return calls;

            final String[] parts = line.split("\t");
            final GenomeLoc lineLoc = parser.parseGenomeLoc(parts[0]);
            final String variable = parts[1];
            final String key = parts[2];
            final String value = parts[3];

            if (currentLoc == null)
                currentLoc = lineLoc;

            if (variable.equals("type")) {
                if (startsToKeep.isEmpty() || startsToKeep.contains(currentLoc.getStart())) {
                    builder.alleles(alleles);
                    final int stop = currentLoc.getStart() + alleles.get(0).length() - 1;
                    builder.chr(currentLoc.getContig()).start(currentLoc.getStart()).stop(stop);
                    builder.genotypes(genotypes);
                    final int[] mleInts = ArrayUtils.toPrimitive(mle.toArray(new Integer[] {}));
                    final AFCalcResult result = new AFCalcResult(mleInts, 1, alleles, posteriors, priors,
                            log10pRefByAllele);
                    calls.add(new ExactCall(builder.make(), runtimeNano, result));
                }
                break;
            } else if (variable.equals("allele")) {
                final boolean isRef = key.equals("0");
                alleles.add(Allele.create(value, isRef));
            } else if (variable.equals("PL")) {
                final GenotypeBuilder gb = new GenotypeBuilder(key);
                gb.PL(GenotypeLikelihoods.fromPLField(value).getAsPLs());
                genotypes.add(gb.make());
            } else if (variable.equals("log10PosteriorOfAFEq0")) {
                posteriors[0] = Double.valueOf(value);
            } else if (variable.equals("log10PosteriorOfAFGt0")) {
                posteriors[1] = Double.valueOf(value);
            } else if (variable.equals("MLE")) {
                mle.add(Integer.valueOf(value));
            } else if (variable.equals("pRefByAllele")) {
                final Allele a = Allele.create(key);
                log10pRefByAllele.put(a, Double.valueOf(value));
            } else if (variable.equals("runtime.nano")) {
                runtimeNano = Long.valueOf(value);
            } else {
                // nothing to do
            }
        }
    }
}

From source file:org.broadinstitute.sting.gatk.walkers.variantrecalibration.VariantDataManager.java

public void normalizeData() {
    boolean foundZeroVarianceAnnotation = false;
    for (int iii = 0; iii < meanVector.length; iii++) {
        final double theMean = mean(iii, true);
        final double theSTD = standardDeviation(theMean, iii, true);
        logger.info(annotationKeys.get(iii)
                + String.format(": \t mean = %.2f\t standard deviation = %.2f", theMean, theSTD));
        if (Double.isNaN(theMean)) {
            throw new UserException.BadInput("Values for " + annotationKeys.get(iii)
                    + " annotation not detected for ANY training variant in the input callset. VariantAnnotator may be used to add these annotations. See "
                    + HelpConstants.forumPost("discussion/49/using-variant-annotator"));
        }//from  w  ww .ja v  a 2 s.  c om

        foundZeroVarianceAnnotation = foundZeroVarianceAnnotation || (theSTD < 1E-5);
        meanVector[iii] = theMean;
        varianceVector[iii] = theSTD;
        for (final VariantDatum datum : data) {
            // Transform each data point via: (x - mean) / standard deviation
            datum.annotations[iii] = (datum.isNull[iii]
                    ? 0.1 * GenomeAnalysisEngine.getRandomGenerator().nextGaussian()
                    : (datum.annotations[iii] - theMean) / theSTD);
        }
    }
    if (foundZeroVarianceAnnotation) {
        throw new UserException.BadInput(
                "Found annotations with zero variance. They must be excluded before proceeding.");
    }

    // trim data by standard deviation threshold and mark failing data for exclusion later
    for (final VariantDatum datum : data) {
        boolean remove = false;
        for (final double val : datum.annotations) {
            remove = remove || (Math.abs(val) > VRAC.STD_THRESHOLD);
        }
        datum.failingSTDThreshold = remove;
    }

    // re-order the data by increasing standard deviation so that the results don't depend on the order things were specified on the command line
    // standard deviation over the training points is used as a simple proxy for information content, perhaps there is a better thing to use here
    final List<Integer> theOrder = calculateSortOrder(meanVector);
    annotationKeys = reorderList(annotationKeys, theOrder);
    varianceVector = ArrayUtils.toPrimitive(reorderArray(ArrayUtils.toObject(varianceVector), theOrder));
    meanVector = ArrayUtils.toPrimitive(reorderArray(ArrayUtils.toObject(meanVector), theOrder));
    for (final VariantDatum datum : data) {
        datum.annotations = ArrayUtils
                .toPrimitive(reorderArray(ArrayUtils.toObject(datum.annotations), theOrder));
        datum.isNull = ArrayUtils.toPrimitive(reorderArray(ArrayUtils.toObject(datum.isNull), theOrder));
    }
    logger.info("Annotations are now ordered by their information content: " + annotationKeys.toString());
}

From source file:org.broadinstitute.sting.gatk.walkers.variantrecalibration.VariantDataManagerUnitTest.java

@Test
public final void testCalculateSortOrder() {
    final double passingQual = 400.0;
    final VariantRecalibratorArgumentCollection VRAC = new VariantRecalibratorArgumentCollection();

    VariantDataManager vdm = new VariantDataManager(new ArrayList<String>(), VRAC);

    final List<VariantDatum> theData = new ArrayList<>();
    final VariantDatum datum1 = new VariantDatum();
    datum1.atTrainingSite = true;/* w  ww.j a v a 2s.co  m*/
    datum1.failingSTDThreshold = false;
    datum1.originalQual = passingQual;
    datum1.annotations = new double[] { 0.0, -10.0, 10.0 };
    datum1.isNull = new boolean[] { false, false, false };
    theData.add(datum1);

    final VariantDatum datum2 = new VariantDatum();
    datum2.atTrainingSite = true;
    datum2.failingSTDThreshold = false;
    datum2.originalQual = passingQual;
    datum2.annotations = new double[] { 0.0, -9.0, 15.0 };
    datum2.isNull = new boolean[] { false, false, false };
    theData.add(datum2);

    final VariantDatum datum3 = new VariantDatum();
    datum3.atTrainingSite = false;
    datum3.failingSTDThreshold = false;
    datum3.originalQual = passingQual;
    datum3.annotations = new double[] { 0.0, 1.0, 999.0 };
    datum3.isNull = new boolean[] { false, false, false };
    theData.add(datum3);

    final VariantDatum datum4 = new VariantDatum();
    datum4.atTrainingSite = false;
    datum4.failingSTDThreshold = false;
    datum4.originalQual = passingQual;
    datum4.annotations = new double[] { 0.015, 2.0, 1001.11 };
    datum4.isNull = new boolean[] { false, false, false };
    theData.add(datum4);

    vdm.setData(theData);

    final double[] meanVector = new double[3];
    for (int iii = 0; iii < meanVector.length; iii++) {
        meanVector[iii] = vdm.mean(iii, true);
    }
    final List<Integer> order = vdm.calculateSortOrder(meanVector);
    Assert.assertArrayEquals(new int[] { 2, 1, 0 },
            ArrayUtils.toPrimitive(order.toArray(new Integer[order.size()])));
}

From source file:org.broadinstitute.sting.utils.codecs.bcf2.BCF2EncoderDecoderUnitTest.java

@Test(dataProvider = "BestIntTypeTests")
public void determineBestEncoding(final List<Integer> ints, final BCF2Type expectedType) throws IOException {
    BCF2Encoder encoder = new BCF2Encoder();
    Assert.assertEquals(BCF2Utils.determineIntegerType(ints), expectedType);
    Assert.assertEquals(BCF2Utils.determineIntegerType(ArrayUtils.toPrimitive(ints.toArray(new Integer[0]))),
            expectedType);//from  www .  j  a va 2 s  . c om
}

From source file:org.carbondata.core.util.CarbonUtil.java

/**
 * This API will record the indexes of the dimension which doesnt have
 * Dictionary values.// ww  w.  j  a  va 2 s .c  o m
 *
 * @param currentDims .
 * @return
 */
public static int[] getNoDictionaryColIndex(Dimension[] currentDims) {
    List<Integer> dirSurrogateList = new ArrayList<Integer>(currentDims.length);
    for (Dimension dim : currentDims) {
        if (dim.isNoDictionaryDim()) {
            dirSurrogateList.add(dim.getOrdinal());
        }
    }
    int[] noDictionaryValIndex = ArrayUtils
            .toPrimitive(dirSurrogateList.toArray(new Integer[dirSurrogateList.size()]));
    return noDictionaryValIndex;
}

From source file:org.carbondata.core.util.CarbonUtil.java

/**
 * Below method will be used to get the dimension
 *
 * @param tableDimensionList table dimension list
 * @return boolean array specifying true if dimension is dictionary
 * and false if dimension is not a dictionary column
 *//* w  w w. ja  va 2  s . c  o  m*/
public static boolean[] identifyDimensionType(List<CarbonDimension> tableDimensionList) {
    List<Boolean> isDictionaryDimensions = new ArrayList<Boolean>();
    Set<Integer> processedColumnGroup = new HashSet<Integer>();
    for (CarbonDimension carbonDimension : tableDimensionList) {
        if (carbonDimension.isColumnar() && hasEncoding(carbonDimension.getEncoder(), Encoding.DICTIONARY)) {
            isDictionaryDimensions.add(true);
        } else if (!carbonDimension.isColumnar()) {
            if (processedColumnGroup.add(carbonDimension.columnGroupId())) {
                isDictionaryDimensions.add(true);
            }
        } else {
            isDictionaryDimensions.add(false);
        }
    }
    boolean[] primitive = ArrayUtils
            .toPrimitive(isDictionaryDimensions.toArray(new Boolean[isDictionaryDimensions.size()]));
    return primitive;
}