Search in sources :

Example 91 with BioMaterial

use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.

the class ExpressionExperimentDaoImpl method findByBioMaterials.

@Override
public Collection<ExpressionExperiment> findByBioMaterials(Collection<BioMaterial> bms) {
    if (bms == null || bms.size() == 0) {
        return new HashSet<>();
    }
    // language=HQL
    final String queryString = "select distinct ee from ExpressionExperiment as ee " + "inner join ee.bioAssays as ba inner join ba.sampleUsed as sample where sample in (:bms)";
    Collection<ExpressionExperiment> results = new HashSet<>();
    Collection<BioMaterial> batch = new HashSet<>();
    for (BioMaterial o : bms) {
        batch.add(o);
        if (batch.size() == ExpressionExperimentDaoImpl.BATCH_SIZE) {
            // noinspection unchecked
            results.addAll(this.getSessionFactory().getCurrentSession().createQuery(queryString).setParameterList("bms", batch).list());
            batch.clear();
        }
    }
    if (batch.size() > 0) {
        // noinspection unchecked
        results.addAll(this.getSessionFactory().getCurrentSession().createQuery(queryString).setParameterList("bms", batch).list());
    }
    return results;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial)

Example 92 with BioMaterial

use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.

the class ExperimentalFactorDaoImpl method remove.

@Override
public void remove(ExperimentalFactor experimentalFactor) {
    Long experimentalDesignId = experimentalFactor.getExperimentalDesign().getId();
    ExperimentalDesign ed = (ExperimentalDesign) this.getSessionFactory().getCurrentSession().load(ExperimentalDesign.class, experimentalDesignId);
    // language=HQL
    final String queryString = "select distinct ee from ExpressionExperiment as ee where ee.experimentalDesign = :ed";
    List<?> results = this.getHibernateTemplate().findByNamedParam(queryString, "ed", ed);
    if (results.size() == 0) {
        throw new IllegalArgumentException("No expression experiment for experimental design " + ed);
    }
    ExpressionExperiment ee = (ExpressionExperiment) results.iterator().next();
    for (BioAssay ba : ee.getBioAssays()) {
        BioMaterial bm = ba.getSampleUsed();
        Collection<FactorValue> factorValuesToRemoveFromBioMaterial = new HashSet<>();
        for (FactorValue factorValue : bm.getFactorValues()) {
            if (experimentalFactor.equals(factorValue.getExperimentalFactor())) {
                factorValuesToRemoveFromBioMaterial.add(factorValue);
                this.getSessionFactory().getCurrentSession().evict(factorValue.getExperimentalFactor());
            }
        }
        // if there are factor values to remove
        if (factorValuesToRemoveFromBioMaterial.size() > 0) {
            bm.getFactorValues().removeAll(factorValuesToRemoveFromBioMaterial);
        // this.getSessionFactory().getCurrentSession().update( bm ); // needed? see bug 4341
        }
    }
    // ed.getExperimentalFactors().remove( experimentalFactor );
    // remove the experimental factor this cascades to values.
    // this.getExperimentalDesignDao().update( ed );
    this.getHibernateTemplate().delete(experimentalFactor);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 93 with BioMaterial

use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.

the class ExperimentalDesignUtils method buildDesignMatrix.

/**
 * Convert factors to a matrix usable in R. The rows are in the same order as the columns of our data matrix
 * (defined by samplesUsed).
 *
 * @param factors in the order they will be used
 * @return a design matrix
 */
public static ObjectMatrix<String, String, Object> buildDesignMatrix(List<ExperimentalFactor> factors, List<BioMaterial> samplesUsed, Map<ExperimentalFactor, FactorValue> baselines) {
    ObjectMatrix<String, String, Object> designMatrix = new ObjectMatrixImpl<>(samplesUsed.size(), factors.size());
    Map<ExperimentalFactor, String> factorNamesInR = new LinkedHashMap<>();
    for (ExperimentalFactor factor : factors) {
        factorNamesInR.put(factor, ExperimentalDesignUtils.nameForR(factor));
    }
    designMatrix.setColumnNames(new ArrayList<>(factorNamesInR.values()));
    List<String> rowNames = new ArrayList<>();
    int row = 0;
    for (BioMaterial samp : samplesUsed) {
        rowNames.add("biomat_" + samp.getId());
        int col = 0;
        for (ExperimentalFactor factor : factors) {
            Object value = ExperimentalDesignUtils.extractFactorValueForSample(baselines, samp, factor);
            designMatrix.set(row, col, value);
            // if the value is null, we have to skip this factor, actually, but we do it later.
            if (value == null) {
                throw new IllegalStateException("Missing values not tolerated in design matrix");
            }
            col++;
        }
        row++;
    }
    // 
    // /*
    // * Drop columns that have missing values.
    // */
    // List<String> toKeep = new ArrayList<String>();
    // for ( int i = 0; i < designMatrix.columns(); i++ ) {
    // boolean skip = false;
    // Object[] column = designMatrix.getColumn( i );
    // for ( Object o : column ) {
    // if ( o == null ) {
    // skip = true;
    // }
    // }
    // 
    // if ( !skip ) {
    // toKeep.add( designMatrix.getColName( i ) );
    // }
    // }
    // 
    // if ( toKeep.isEmpty() ) {
    // throw new IllegalStateException( "Design matrix had no columns without missing values" );
    // }
    // 
    // if ( toKeep.size() < designMatrix.columns() ) {
    // designMatrix = designMatrix.subsetColumns( toKeep );
    // }
    designMatrix.setRowNames(rowNames);
    return designMatrix;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ExperimentalFactorValueObject(ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject) ObjectMatrixImpl(ubic.basecode.dataStructure.matrix.ObjectMatrixImpl)

Example 94 with BioMaterial

use of ubic.gemma.model.expression.biomaterial.BioMaterial 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 95 with BioMaterial

use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.

the class GeoConverterImpl method addFactorValueToBioMaterial.

private void addFactorValueToBioMaterial(ExpressionExperiment expExp, GeoSubset geoSubSet, FactorValue factorValue) {
    // fill in biomaterial-->factorvalue.
    for (GeoSample sample : geoSubSet.getSamples()) {
        // find the matching biomaterial(s) in the expression experiment.
        for (BioAssay bioAssay : expExp.getBioAssays()) {
            if (bioAssay.getAccession().getAccession().equals(sample.getGeoAccession())) {
                BioMaterial material = bioAssay.getSampleUsed();
                if (GeoConverterImpl.log.isDebugEnabled()) {
                    GeoConverterImpl.log.debug("Adding " + factorValue.getExperimentalFactor() + " : " + factorValue + " to " + material);
                }
                material.getFactorValues().add(factorValue);
            }
        }
    }
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Aggregations

BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)132 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)67 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)27 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)22 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)19 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)15 HashSet (java.util.HashSet)13 Test (org.junit.Test)13 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)12 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)12 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)10 InputStream (java.io.InputStream)7 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)7 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)7 QuantitationType (ubic.gemma.model.common.quantitationtype.QuantitationType)7 Characteristic (ubic.gemma.model.common.description.Characteristic)6 RawExpressionDataVector (ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)5 ArrayList (java.util.ArrayList)5