Search in sources :

Example 1 with PrimitiveType

use of ubic.gemma.model.common.quantitationtype.PrimitiveType in project Gemma by PavlidisLab.

the class VectorMergingServiceImpl method makeMergedData.

/**
 * @param sortedOldDims sorted old dims
 * @param newBioAd      new BA dims
 * @param type          type
 * @param de            de
 * @param dedvs         dedvs
 * @param mergedData    starts out empty, is initalized to the new data.
 * @return number of values missing
 */
private int makeMergedData(List<BioAssayDimension> sortedOldDims, BioAssayDimension newBioAd, QuantitationType type, CompositeSequence de, Collection<RawExpressionDataVector> dedvs, List<Object> mergedData) {
    int totalMissingInVector = 0;
    PrimitiveType representation = type.getRepresentation();
    for (BioAssayDimension oldDim : sortedOldDims) {
        // careful, the 'new' bioAssayDimension might be one of the old ones that we're reusing.
        if (oldDim.equals(newBioAd))
            continue;
        boolean found = false;
        for (RawExpressionDataVector oldV : dedvs) {
            assert oldV.getDesignElement().equals(de);
            assert oldV.getQuantitationType().equals(type);
            if (oldV.getBioAssayDimension().equals(oldDim)) {
                found = true;
                this.convertFromBytes(mergedData, representation, oldV);
                break;
            }
        }
        if (!found) {
            int missing = this.fillMissingValues(de, mergedData, oldDim, representation);
            totalMissingInVector += missing;
        }
    }
    return totalMissingInVector;
}
Also used : BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) RawExpressionDataVector(ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector) PrimitiveType(ubic.gemma.model.common.quantitationtype.PrimitiveType)

Example 2 with PrimitiveType

use of ubic.gemma.model.common.quantitationtype.PrimitiveType in project Gemma by PavlidisLab.

the class ExpressionExperimentPlatformSwitchService method vectorReWrite.

/**
 * Rearrange/expand a vector as necessary to use the given BioAssayDimension. Only used for multiplatform case of
 * samples run on multiple platforms.
 *
 * @param vector vector
 * @param bad    to be used as the replacement.
 */
private void vectorReWrite(DesignElementDataVector vector, BioAssayDimension bad) {
    List<BioAssay> desiredOrder = bad.getBioAssays();
    List<BioAssay> currentOrder = vector.getBioAssayDimension().getBioAssays();
    if (this.equivalent(currentOrder, desiredOrder)) {
        // Easy, we can just switch it.
        vector.setBioAssayDimension(bad);
        return;
    }
    /*
         * We remake the data vector following the new ordering.
         */
    PrimitiveType representation = vector.getQuantitationType().getRepresentation();
    Object missingVal;
    if (representation.equals(PrimitiveType.DOUBLE)) {
        missingVal = Double.NaN;
    } else if (representation.equals(PrimitiveType.STRING)) {
        missingVal = "";
    } else if (representation.equals(PrimitiveType.INT)) {
        missingVal = 0;
    } else if (representation.equals(PrimitiveType.BOOLEAN)) {
        missingVal = false;
    } else {
        throw new UnsupportedOperationException("Missing values in data vectors of type " + representation + " not supported (when processing " + vector);
    }
    List<Object> oldData = new ArrayList<>();
    super.convertFromBytes(oldData, vector.getQuantitationType().getRepresentation(), vector);
    /*
         * Now data has the old data, so we need to rearrange it to match, inserting missings as necessary.
         */
    Map<BioMaterial, Integer> bm2loc = new HashMap<>();
    int i = 0;
    List<Object> newData = new ArrayList<>();
    // initialize
    for (BioAssay ba : desiredOrder) {
        bm2loc.put(ba.getSampleUsed(), i++);
        newData.add(missingVal);
    }
    // Put data into new locations
    int j = 0;
    for (BioAssay ba : currentOrder) {
        Integer loc = bm2loc.get(ba.getSampleUsed());
        assert loc != null;
        newData.set(loc, oldData.get(j++));
    }
    byte[] newDataAr = converter.toBytes(newData.toArray());
    vector.setData(newDataAr);
    vector.setBioAssayDimension(bad);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) PrimitiveType(ubic.gemma.model.common.quantitationtype.PrimitiveType) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 3 with PrimitiveType

use of ubic.gemma.model.common.quantitationtype.PrimitiveType in project Gemma by PavlidisLab.

the class GeoConverterImpl method convertData.

/**
 * Convert a vector of strings into a byte[] for saving in the database. . Blanks(missing values) are treated as NAN
 * (double), 0 (integer), false (booleans) or just empty strings (strings). Other invalid values are treated the
 * same way as missing data (to keep the parser from failing when dealing with strange GEO files that have values
 * like "Error" for an expression value).
 *
 * @param vector of Strings to be converted to primitive values (double, int etc)
 * @param qt     The quantitation type for the values to be converted.
 */
@Override
public byte[] convertData(List<Object> vector, QuantitationType qt) {
    if (vector == null || vector.size() == 0)
        return null;
    boolean containsAtLeastOneNonNull = false;
    for (Object string : vector) {
        if (string != null) {
            containsAtLeastOneNonNull = true;
            break;
        }
    }
    if (!containsAtLeastOneNonNull) {
        if (GeoConverterImpl.log.isDebugEnabled()) {
            GeoConverterImpl.log.debug("No data for " + qt + " in vector of length " + vector.size());
        }
        return null;
    }
    List<Object> toConvert = new ArrayList<>();
    PrimitiveType pt = qt.getRepresentation();
    int numMissing = 0;
    for (Object rawValue : vector) {
        if (rawValue == null) {
            numMissing++;
            this.handleMissing(toConvert, pt);
        } else if (rawValue instanceof String) {
            // needs to be coverted.
            String valueString = (String) rawValue;
            if (StringUtils.isBlank(valueString)) {
                numMissing++;
                this.handleMissing(toConvert, pt);
                continue;
            }
            try {
                if (pt.equals(PrimitiveType.DOUBLE)) {
                    toConvert.add(Double.parseDouble(valueString));
                } else if (pt.equals(PrimitiveType.STRING)) {
                    toConvert.add(rawValue);
                } else if (pt.equals(PrimitiveType.CHAR)) {
                    if (valueString.length() != 1) {
                        throw new IllegalStateException("Attempt to cast a string of length " + valueString.length() + " to a char: " + rawValue + "(quantitation type =" + qt);
                    }
                    toConvert.add(valueString.toCharArray()[0]);
                } else if (pt.equals(PrimitiveType.INT)) {
                    toConvert.add(Integer.parseInt(valueString));
                } else if (pt.equals(PrimitiveType.BOOLEAN)) {
                    toConvert.add(Boolean.parseBoolean(valueString));
                } else {
                    throw new UnsupportedOperationException("Data vectors of type " + pt + " not supported");
                }
            } catch (NumberFormatException e) {
                numMissing++;
                this.handleMissing(toConvert, pt);
            }
        } else {
            // use as is.
            toConvert.add(rawValue);
        }
    }
    if (numMissing == vector.size()) {
        return null;
    }
    byte[] bytes = byteArrayConverter.toBytes(toConvert.toArray());
    /*
         * Debugging - absolutely make sure we can convert the data back.
         */
    if (pt.equals(PrimitiveType.DOUBLE)) {
        double[] byteArrayToDoubles = byteArrayConverter.byteArrayToDoubles(bytes);
        if (byteArrayToDoubles.length != vector.size()) {
            throw new IllegalStateException("Expected " + vector.size() + " got " + byteArrayToDoubles.length + " doubles");
        }
    } else if (pt.equals(PrimitiveType.INT)) {
        int[] byteArrayToInts = byteArrayConverter.byteArrayToInts(bytes);
        if (byteArrayToInts.length != vector.size()) {
            throw new IllegalStateException("Expected " + vector.size() + " got " + byteArrayToInts.length + " ints");
        }
    } else if (pt.equals(PrimitiveType.BOOLEAN)) {
        boolean[] byteArrayToBooleans = byteArrayConverter.byteArrayToBooleans(bytes);
        if (byteArrayToBooleans.length != vector.size()) {
            throw new IllegalStateException("Expected " + vector.size() + " got " + byteArrayToBooleans.length + " booleans");
        }
    }
    return bytes;
}
Also used : PrimitiveType(ubic.gemma.model.common.quantitationtype.PrimitiveType)

Aggregations

PrimitiveType (ubic.gemma.model.common.quantitationtype.PrimitiveType)3 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)1 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)1 RawExpressionDataVector (ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector)1 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)1