Search in sources :

Example 6 with BioAssayValueObject

use of ubic.gemma.model.expression.bioAssay.BioAssayValueObject in project Gemma by PavlidisLab.

the class DEDVController method makeDiffVisCollection.

/**
 * Takes the DEDVs and put them in point objects and normalize the values. returns a map of eeid to visValueObject.
 * Currently removes multiple hits for same gene. Tries to pick best DEDV. Organizes the experiments from lowest to
 * higest p-value
 *
 * @param validatedProbes (bad name)
 */
private VisualizationValueObject[] makeDiffVisCollection(Collection<DoubleVectorValueObject> dedvs, List<Long> genes, Map<Long, Collection<DifferentialExpressionValueObject>> validatedProbes, Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> layouts) {
    StopWatch watch = new StopWatch();
    watch.start();
    Map<Long, Collection<DoubleVectorValueObject>> vvoMap = new HashMap<>();
    Map<Long, ExpressionExperimentValueObject> eeMap = new HashMap<>();
    // Organize by expression experiment
    for (DoubleVectorValueObject dvvo : dedvs) {
        ExpressionExperimentValueObject ee = dvvo.getExpressionExperiment();
        eeMap.put(ee.getId(), ee);
        if (!vvoMap.containsKey(ee.getId())) {
            vvoMap.put(ee.getId(), new HashSet<DoubleVectorValueObject>());
        }
        vvoMap.get(ee.getId()).add(dvvo);
    }
    class EE2PValue implements Comparable<EE2PValue> {

        Long EEId;

        double pValue;

        public EE2PValue() {
            super();
        }

        public EE2PValue(Long eeid, double pValue) {
            this();
            this.EEId = eeid;
            this.pValue = pValue;
        }

        @Override
        public int compareTo(EE2PValue o) {
            if (this.pValue > o.getPValue())
                return 1;
            else if (this.pValue > o.getPValue())
                return -1;
            else
                return 0;
        }

        public Long getEEId() {
            return EEId;
        }

        public double getPValue() {
            return pValue;
        }
    }
    List<EE2PValue> sortedEE = new ArrayList<>();
    // Need to sort the expression experiments by lowest p-value
    for (Long eeId : vvoMap.keySet()) {
        Collection<DifferentialExpressionValueObject> devos = validatedProbes.get(eeId);
        double minP = 1;
        if (devos != null && !devos.isEmpty()) {
            for (DifferentialExpressionValueObject devo : devos) {
                if (minP > devo.getP()) {
                    minP = devo.getP();
                }
            }
        }
        sortedEE.add(new EE2PValue(eeId, minP));
    }
    Collections.sort(sortedEE);
    VisualizationValueObject[] result = new VisualizationValueObject[vvoMap.keySet().size()];
    List<GeneValueObject> geneValueObjects = getGeneValueObjectList(genes);
    // Create collection of visualizationValueObject for flotr on js side
    int i = 0;
    for (EE2PValue ee2P : sortedEE) {
        VisualizationValueObject vvo = new VisualizationValueObject(vvoMap.get(ee2P.getEEId()), geneValueObjects, ee2P.getPValue(), validatedProbes.get(ee2P.getEEId()));
        getSampleNames(vvoMap.get(ee2P.getEEId()), vvo, layouts);
        if (layouts != null && !layouts.isEmpty() && layouts.containsKey(ee2P.getEEId())) {
            LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> layout = layouts.get(ee2P.getEEId());
            this.prepareFactorsForFrontEndDisplay(vvo, layout);
        }
        if (layouts != null) {
            ExpressionExperimentValueObject ee = eeMap.get(ee2P.getEEId());
            log.debug("setup experimental design layout profiles for " + ee);
            vvo.setUpFactorProfiles(layouts.get(ee.getId()));
        }
        result[i] = vvo;
        i++;
    }
    Long time = watch.getTime();
    if (time > 1000) {
        log.info("Created vis value objects in: " + time);
    }
    return result;
}
Also used : VisualizationValueObject(ubic.gemma.web.controller.visualization.VisualizationValueObject) DifferentialExpressionValueObject(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionValueObject) StopWatch(org.apache.commons.lang3.time.StopWatch) GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 7 with BioAssayValueObject

use of ubic.gemma.model.expression.bioAssay.BioAssayValueObject in project Gemma by PavlidisLab.

the class DEDVController method prepareFactorsForFrontEndDisplay.

/**
 * Prepare vvo for display on front end. Uses factors and factor values from layouts
 *
 * @param vvo Note: This will be modified! It will be updated with the factorNames and factorValuesToNames
 */
private void prepareFactorsForFrontEndDisplay(VisualizationValueObject vvo, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> eeLayouts) {
    if (eeLayouts == null || eeLayouts.isEmpty()) {
        log.warn("No layouts, bail");
        vvo.setFactorNames(null);
        vvo.setFactorValuesToNames(null);
        return;
    }
    LinkedHashSet<ExperimentalFactor> factorNames = getFactorNames(eeLayouts);
    // colours for conditions/factor values bar chart FIXME make continuous maps different.
    Map<ExperimentalFactor, Queue<String>> factorColoursMap = createFactorNameToColoursMap(factorNames);
    String missingValueColour = "#DCDCDC";
    Random random = new Random();
    LinkedHashMap<String, LinkedHashMap<String, String>> factorToValueNames = new LinkedHashMap<>();
    // list of maps with entries: key = factorName, value=array of factor values
    // 1 entry per sample
    List<LinkedHashMap<String, String[]>> factorValueMaps = new ArrayList<>();
    Collection<String> factorsMissingValues = new HashSet<>();
    // if same sample was run more than once on
    Collection<BioMaterialValueObject> seenSamples = new HashSet<>();
    // diff platforms.
    // avoid loading repeatedly.
    Map<Long, FactorValue> fvs = new HashMap<>();
    Collection<ExperimentalFactor> seenFactors = new HashSet<>();
    for (BioAssayValueObject ba : eeLayouts.keySet()) {
        if (seenSamples.contains(ba.getSample())) {
            continue;
        }
        seenSamples.add(ba.getSample());
        // double should be the factorValue id, defined in
        // ubic.gemma.core.visualization.ExperimentalDesignVisualizationService.getExperimentalDesignLayout(ExpressionExperiment,
        // BioAssayDimension)
        LinkedHashMap<ExperimentalFactor, Double> factorMap = eeLayouts.get(ba);
        LinkedHashMap<String, String[]> factorNamesToValueColourPairs = new LinkedHashMap<>(factorNames.size());
        // for every factor, add a missing-value entry (guards against missing data messing up the layout)
        for (ExperimentalFactor factor : factorNames) {
            String[] facValAndColour = new String[] { "No value", missingValueColour };
            factorNamesToValueColourPairs.put(getUniqueFactorName(factor), facValAndColour);
        }
        // for each experimental factor, store the name and value
        for (Entry<ExperimentalFactor, Double> pair : factorMap.entrySet()) {
            ExperimentalFactor factor = pair.getKey();
            Double valueOrId = pair.getValue();
            /*
                 * the double is only a double because it is meant to hold measurements when the factor is continuous if
                 * the factor is categorical, the double value is set to the value's id see
                 * ubic.gemma.core.visualization.ExperimentalDesignVisualizationService.getExperimentalDesignLayout(
                 * ExpressionExperiment, BioAssayDimension)
                 */
            if (valueOrId == null || factor.getType() == null || (factor.getType().equals(FactorType.CATEGORICAL) && factor.getFactorValues().isEmpty())) {
                factorsMissingValues.add(getUniqueFactorName(factor));
                continue;
            }
            if (!seenFactors.contains(factor) && factor.getType().equals(FactorType.CATEGORICAL)) {
                for (FactorValue fv : factor.getFactorValues()) {
                    fvs.put(fv.getId(), fv);
                }
            }
            String facValsStr = getFacValsStr(fvs, factor, valueOrId);
            if (!factorToValueNames.containsKey(getUniqueFactorName(factor))) {
                factorToValueNames.put(getUniqueFactorName(factor), new LinkedHashMap<String, String>());
            }
            // assign colour if unassigned or fetch it if already assigned
            String colourString = "";
            if (!factorToValueNames.get(getUniqueFactorName(factor)).containsKey(facValsStr)) {
                if (factorColoursMap.containsKey(factor)) {
                    colourString = factorColoursMap.get(factor).poll();
                }
                if (colourString == null || Objects.equals(colourString, "")) {
                    // ran out of predefined colours
                    colourString = getRandomColour(random);
                }
                factorToValueNames.get(getUniqueFactorName(factor)).put(facValsStr, colourString);
            } else {
                colourString = factorToValueNames.get(getUniqueFactorName(factor)).get(facValsStr);
            }
            String[] facValAndColour = new String[] { facValsStr, colourString };
            factorNamesToValueColourPairs.put(getUniqueFactorName(factor), facValAndColour);
        }
        factorValueMaps.add(factorNamesToValueColourPairs);
    }
    // add missing value entries here so they show up at the end of the legend's value lists
    if (!factorsMissingValues.isEmpty()) {
        for (String factorName : factorsMissingValues) {
            if (!factorToValueNames.containsKey(factorName)) {
                factorToValueNames.put(factorName, new LinkedHashMap<String, String>());
            }
            factorToValueNames.get(factorName).put("No value", missingValueColour);
        }
    }
    // this is summary of values & colours by factor, used for legend
    vvo.setFactorNames(factorToValueNames);
    // this is list of maps for each sample
    vvo.setFactorValuesToNames(factorValueMaps);
}
Also used : BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) BioMaterialValueObject(ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)

Example 8 with BioAssayValueObject

use of ubic.gemma.model.expression.bioAssay.BioAssayValueObject in project Gemma by PavlidisLab.

the class ProcessedExpressionDataCreateServiceTest method testComputeDevRankForExpressionExperimentMultiArrayWithGaps.

/**
 * Three platforms, one sample was not run on GPL81. It's 'Norm-1a', but the name we use for the sample is random.
 */
@SuppressWarnings("unchecked")
@Test
public void testComputeDevRankForExpressionExperimentMultiArrayWithGaps() throws Exception {
    try {
        geoService.setGeoDomainObjectGenerator(new GeoDomainObjectGeneratorLocal(this.getTestFileBasePath("gse482short")));
        Collection<ExpressionExperiment> results = (Collection<ExpressionExperiment>) geoService.fetchAndLoad("GSE482", false, true, false);
        this.ee = results.iterator().next();
    } catch (AlreadyExistsInSystemException e) {
        this.ee = ((Collection<ExpressionExperiment>) e.getData()).iterator().next();
    }
    ee = this.eeService.thawLite(ee);
    processedExpressionDataVectorService.computeProcessedExpressionData(ee);
    Collection<ProcessedExpressionDataVector> preferredVectors = this.processedExpressionDataVectorService.getProcessedDataVectors(ee);
    ee = eeService.load(ee.getId());
    ee = this.eeService.thawLite(ee);
    processedExpressionDataVectorService.thaw(preferredVectors);
    ExpressionDataDoubleMatrix mat = new ExpressionDataDoubleMatrix(preferredVectors);
    assertEquals(10, mat.columns());
    boolean found = false;
    for (int i = 0; i < mat.rows(); i++) {
        Double[] row = mat.getRow(i);
        // debugging
        if (i == 0) {
            for (int j = 0; j < row.length; j++) {
                BioAssay ba = mat.getBioAssaysForColumn(j).iterator().next();
                System.err.println(ba.getName());
            }
        }
        System.err.print(mat.getRowElement(i).getDesignElement().getName() + "\t");
        for (double d : row) {
            System.err.print(String.format("%4.2f\t", d));
        }
        System.err.print("\n");
        CompositeSequence el = mat.getDesignElementForRow(i);
        for (int j = 0; j < row.length; j++) {
            BioAssay ba = mat.getBioAssaysForColumn(j).iterator().next();
            if (ba.getName().matches("PGA-MurLungHyper-Norm-1a[ABC]v2-s2") && (el.getName().equals("100001_at") || el.getName().equals("100002_at") || el.getName().equals("100003_at") || el.getName().equals("100004_at") || el.getName().equals("100005_at") || el.getName().equals("100006_at") || el.getName().equals("100007_at") || el.getName().equals("100009_r_at") || el.getName().equals("100010_at") || el.getName().equals("100011_at"))) {
                assertEquals(Double.NaN, row[j], 0.0001);
                found = true;
            } else {
                assertTrue("Got unexpected NA value for " + ba.getName() + " for " + el.getName(), !Double.isNaN(row[j]));
            }
        }
    }
    assertTrue(found);
    /*
         * Now do this through the processedExpressionDataVectorService
         */
    Collection<DoubleVectorValueObject> da = this.processedExpressionDataVectorService.getProcessedDataArrays(ee);
    assertEquals(30, da.size());
    found = false;
    boolean first = true;
    for (DoubleVectorValueObject v : da) {
        CompositeSequenceValueObject el = v.getDesignElement();
        double[] row = v.getData();
        // debugging
        if (first) {
            for (int j = 0; j < row.length; j++) {
                BioAssayValueObject ba = v.getBioAssays().get(j);
                System.err.println(ba.getName());
            }
            first = false;
        }
        System.err.print(el.getName() + "\t");
        for (double d : row) {
            System.err.print(String.format("%4.2f\t", d));
        }
        System.err.print("\n");
        assertEquals(10, row.length);
        for (int j = 0; j < row.length; j++) {
            assertNotNull(v.getBioAssays());
            BioAssayValueObject ba = v.getBioAssays().get(j);
            if (ba.getName().startsWith("Missing bioassay for biomaterial") && (el.getName().equals("100001_at") || el.getName().equals("100002_at") || el.getName().equals("100003_at") || el.getName().equals("100004_at") || el.getName().equals("100005_at") || el.getName().equals("100006_at") || el.getName().equals("100007_at") || el.getName().equals("100009_r_at") || el.getName().equals("100010_at") || el.getName().equals("100011_at"))) {
                assertEquals(Double.NaN, row[j], 0.0001);
                found = true;
            } else {
                assertTrue("Got unexpected NA value for " + ba.getName() + " for " + el.getName(), !Double.isNaN(row[j]));
            }
        }
    }
    assertTrue(found);
}
Also used : ExpressionDataDoubleMatrix(ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix) ProcessedExpressionDataVector(ubic.gemma.model.expression.bioAssayData.ProcessedExpressionDataVector) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) CompositeSequenceValueObject(ubic.gemma.model.expression.designElement.CompositeSequenceValueObject) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) AlreadyExistsInSystemException(ubic.gemma.core.loader.util.AlreadyExistsInSystemException) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) GeoDomainObjectGeneratorLocal(ubic.gemma.core.loader.expression.geo.GeoDomainObjectGeneratorLocal) AbstractGeoServiceTest(ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest) Test(org.junit.Test)

Example 9 with BioAssayValueObject

use of ubic.gemma.model.expression.bioAssay.BioAssayValueObject in project Gemma by PavlidisLab.

the class BioAssayDimensionValueObject method makeDummyBioAssayDimension.

private BioAssayDimension makeDummyBioAssayDimension() {
    assert this.id == null;
    BioAssayDimension fakeBd = BioAssayDimension.Factory.newInstance("Placeholder representing: " + name, description, new ArrayList<BioAssay>());
    Map<Long, ExperimentalFactor> fakeEfs = new HashMap<>();
    for (BioAssayValueObject bav : this.bioAssays) {
        BioAssay ba = BioAssay.Factory.newInstance();
        ba.setId(bav.getId());
        ba.setName(bav.getName());
        ba.setDescription("Fake placeholder");
        BioMaterial sampleUsed = BioMaterial.Factory.newInstance();
        BioMaterialValueObject bmVo = bav.getSample();
        assert bmVo != null;
        sampleUsed.setId(bmVo.getId());
        sampleUsed.setName(bmVo.getName());
        sampleUsed.setDescription("Fake placeholder");
        for (IdentifiableValueObject iVo : bmVo.getFactorValueObjects()) {
            FactorValueValueObject fvVo = (FactorValueValueObject) iVo;
            FactorValue fv = FactorValue.Factory.newInstance();
            assert fvVo.getId() != null;
            fv.setId(fvVo.getId());
            assert fvVo.getValue() != null;
            fv.setValue(fvVo.getValue());
            Long efId = fvVo.getFactorId();
            ExperimentalFactor ef;
            if (fakeEfs.containsKey(efId)) {
                ef = fakeEfs.get(efId);
            } else {
                ef = ExperimentalFactor.Factory.newInstance();
                ef.setId(efId);
                ef.setName(fvVo.getCategory());
                ef.setType(fvVo.isMeasurement() ? FactorType.CONTINUOUS : FactorType.CATEGORICAL);
                fakeEfs.put(efId, ef);
            }
            ef.getFactorValues().add(fv);
            fv.setExperimentalFactor(ef);
            sampleUsed.getFactorValues().add(fv);
        }
        ba.setSampleUsed(sampleUsed);
        ArrayDesign ad = ArrayDesign.Factory.newInstance();
        ArrayDesignValueObject adVo = bav.getArrayDesign();
        assert adVo != null;
        ad.setId(adVo.getId());
        ad.setShortName(adVo.getShortName());
        ad.setDescription("Fake placeholder");
        ba.setArrayDesignUsed(ad);
        fakeBd.getBioAssays().add(ba);
    }
    return fakeBd;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) FactorValueValueObject(ubic.gemma.model.expression.experiment.FactorValueValueObject) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) ArrayDesign(ubic.gemma.model.expression.arrayDesign.ArrayDesign) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) ArrayDesignValueObject(ubic.gemma.model.expression.arrayDesign.ArrayDesignValueObject) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) IdentifiableValueObject(ubic.gemma.model.IdentifiableValueObject) BioMaterialValueObject(ubic.gemma.model.expression.biomaterial.BioMaterialValueObject) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 10 with BioAssayValueObject

use of ubic.gemma.model.expression.bioAssay.BioAssayValueObject in project Gemma by PavlidisLab.

the class DoubleVectorValueObject method addGaps.

private void addGaps(BioAssayDimension dimToMatch) {
    BioAssayDimensionValueObject sourceBioAssayDimension = new BioAssayDimensionValueObject(dimToMatch);
    List<BioAssayValueObject> dimToMatchBioAssays = sourceBioAssayDimension.getBioAssays();
    double[] expandedData = new double[dimToMatch.getBioAssays().size()];
    BioAssayDimension expandedDim = BioAssayDimension.Factory.newInstance();
    expandedDim.setDescription("Expanded bioassay dimension based on " + this.getBioAssayDimension().getName());
    expandedDim.setName("Expanded bioassay dimension based on " + this.getBioAssayDimension().getName());
    Map<BioMaterialValueObject, BioAssayValueObject> bmap = new HashMap<>();
    ArrayDesignValueObject arrayDesign = null;
    for (BioAssayValueObject b : this.getBioAssays()) {
        bmap.put(b.getSample(), b);
        arrayDesign = b.getArrayDesign();
    }
    List<BioAssayValueObject> expandedBioAssays = new ArrayList<>();
    int i = 0;
    int indexInUngappedData = 0;
    for (BioAssayValueObject b : dimToMatchBioAssays) {
        BioMaterialValueObject bm = b.getSample();
        if (!bmap.containsKey(bm)) {
            /*
                 * This is one where we have to put in a gap.
                 */
            expandedData[i] = Double.NaN;
            BioAssayValueObject placeholder = new BioAssayValueObject(-1L);
            placeholder.setName("Missing bioassay for biomaterial=" + bm + " that was not run on " + arrayDesign);
            placeholder.setDescription("This is to represent a biomaterial that was not run on the platform for the rest of the bioassay dimension.");
            placeholder.setArrayDesign(arrayDesign);
            placeholder.setSample(bm);
            expandedBioAssays.add(placeholder);
        } else {
            expandedBioAssays.add(this.getBioAssays().get(indexInUngappedData));
            expandedData[i] = data[indexInUngappedData];
            indexInUngappedData++;
        }
        i++;
    }
    assert dimToMatchBioAssays.size() == expandedBioAssays.size();
    this.data = expandedData;
    this.setBioAssayDimension(new BioAssayDimensionValueObject(-1L));
    this.getBioAssayDimension().setSourceBioAssayDimension(sourceBioAssayDimension);
    // not exactly, but have to make clear it's not real.
    this.getBioAssayDimension().setIsSubset(true);
    this.getBioAssayDimension().clearBioAssays();
    this.getBioAssayDimension().addBioAssays(expandedBioAssays);
    this.getBioAssayDimension().setName("Expanded bioassay dimension based on " + this.getBioAssayDimension().getName());
    assert this.getBioAssays() != null;
}
Also used : BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) ArrayDesignValueObject(ubic.gemma.model.expression.arrayDesign.ArrayDesignValueObject) DoubleArrayList(cern.colt.list.DoubleArrayList) BioMaterialValueObject(ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)

Aggregations

BioAssayValueObject (ubic.gemma.model.expression.bioAssay.BioAssayValueObject)20 DoubleVectorValueObject (ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)7 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)6 BioMaterialValueObject (ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 StopWatch (org.apache.commons.lang3.time.StopWatch)3 ArrayDesignValueObject (ubic.gemma.model.expression.arrayDesign.ArrayDesignValueObject)3 BioAssayDimensionValueObject (ubic.gemma.model.expression.bioAssayData.BioAssayDimensionValueObject)3 HashSet (java.util.HashSet)2 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)2 GeneValueObject (ubic.gemma.model.genome.gene.GeneValueObject)2 VisualizationValueObject (ubic.gemma.web.controller.visualization.VisualizationValueObject)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 MapperException (com.sdicons.json.mapper.MapperException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 JspException (javax.servlet.jsp.JspException)1 Test (org.junit.Test)1