Search in sources :

Example 1 with BioMaterialValueObject

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

the class AssayViewTag method addNovelBiomaterialSlots.

/**
 * Add places for completely new biomaterials to be added. These are the row labels.
 */
private void addNovelBiomaterialSlots(StringBuilder buf, Set<ArrayDesignValueObject> designs, Map<String, String> assayToMaterial, int count, int emptyCount) {
    if (designs.size() == 1) {
        return;
    }
    for (int i = 1; i <= NUM_EXTRA_BIOMATERIALS; i++) {
        if (count % 2 == 0) {
            buf.append("<tr class='even' align=justify>");
        } else {
            buf.append("<tr class='odd' align=justify>");
        }
        // FIXME this is a kludge: use negative ids to distinguish the new biomaterials.
        BioMaterialValueObject material = new BioMaterialValueObject(0L - i);
        material.setName("[New biomaterial " + i + "]");
        buf.append("<td>" + material.getName() + "</td>");
        String image = "<img height=10 width=20 src='" + Settings.getRootContext() + "/images/arrow_out.png' />";
        for (ArrayDesignValueObject design : designs) {
            emptyCount = addEmpty(buf, assayToMaterial, emptyCount, material, image, design);
        }
        buf.append("</tr>");
        count++;
    }
}
Also used : ArrayDesignValueObject(ubic.gemma.model.expression.arrayDesign.ArrayDesignValueObject) BioMaterialValueObject(ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)

Example 2 with BioMaterialValueObject

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

the class AssayViewTag method doStartTag.

@Override
public int doStartTag() throws JspException {
    StringBuilder buf = new StringBuilder();
    buf.append("<div>");
    // create table
    Map<BioMaterialValueObject, Map<ArrayDesignValueObject, Collection<BioAssayValueObject>>> bioAssayMap = new HashMap<>();
    Set<ArrayDesignValueObject> designs = new HashSet<>();
    Map<ArrayDesignValueObject, Long> arrayMaterialCount = new HashMap<>();
    // package all of this information into JSON for javascript dynamic retrieval
    Map<String, String> assayToMaterial = new HashMap<>();
    for (BioAssayValueObject assay : bioAssays) {
        // map for bioassays linked to a specific arraydesign
        // map for the bioassays linked to a specific biomaterial
        BioMaterialValueObject material = assay.getSample();
        ArrayDesignValueObject design = assay.getArrayDesign();
        designs.add(design);
        // check if the assay list is initialized yet
        Map<ArrayDesignValueObject, Collection<BioAssayValueObject>> assayMap;
        if (bioAssayMap.containsKey(material)) {
            assayMap = bioAssayMap.get(material);
        } else {
            assayMap = new HashMap<>();
            bioAssayMap.put(material, assayMap);
        }
        if (assayMap.containsKey(design)) {
            assayMap.get(design).add(assay);
        } else {
            Collection<BioAssayValueObject> assayList = new ArrayList<>();
            assayList.add(assay);
            assayMap.put(design, assayList);
        }
        if (arrayMaterialCount.containsKey(design)) {
            Long count = arrayMaterialCount.get(design);
            count++;
            arrayMaterialCount.put(design, count);
        } else {
            Long count = new Long(1);
            arrayMaterialCount.put(design, count);
        }
    }
    int materialCount = bioAssayMap.keySet().size();
    buf.append("<table class='detail row-separated odd-gray'><tr>");
    buf.append("<th>" + materialCount + " BioMaterials</th>");
    // display arraydesigns
    for (ArrayDesignValueObject design : designs) {
        Long count = arrayMaterialCount.get(design);
        buf.append("<th>" + count + " BioAssays on<br /><a target='_blank' href=\"" + Settings.getRootContext() + "/arrays/showArrayDesign.html?id=" + design.getId() + "\" title=\"" + design.getName() + "\" >" + (design.getShortName() == null ? design.getName() : design.getShortName()) + "</a></th>");
    }
    buf.append("</tr>");
    // display bioMaterials and the corresponding bioAssays
    int count = 1;
    Iterator<BioMaterialValueObject> iter = bioAssayMap.keySet().iterator();
    List<BioMaterialValueObject> materials = new ArrayList<>();
    while (iter.hasNext()) {
        materials.add(iter.next());
    }
    Comparator<BioMaterialValueObject> comparator = new BioMaterialComparator();
    Collections.sort(materials, comparator);
    int elementCount = 1;
    int emptyCount = 0;
    for (BioMaterialValueObject material : materials) {
        if (count % 2 == 0) {
            buf.append("<tr class='even' align=justify>");
        } else {
            buf.append("<tr class='odd' align=justify>");
        }
        String bmLink = "<a href='" + Settings.getRootContext() + "/bioMaterial/showBioMaterial.html?id=" + material.getId() + "'> " + material.getName() + "</a>";
        buf.append("<td>" + bmLink + "</td>");
        Map<ArrayDesignValueObject, Collection<BioAssayValueObject>> assayMap = bioAssayMap.get(material);
        String image = "&nbsp;&nbsp;&nbsp;<img height=16 width=16 src='" + Settings.getRootContext() + "/images/icons/arrow_switch.png' />&nbsp;&nbsp;&nbsp;";
        for (ArrayDesignValueObject design : designs) {
            if (assayMap.containsKey(design)) {
                Collection<BioAssayValueObject> assays = assayMap.get(design);
                Collection<Long> ids = new ArrayList<>();
                Collection<String> tooltips = new ArrayList<>();
                for (BioAssayValueObject assay : assays) {
                    ids.add(assay.getId());
                    tooltips.add(StringUtils.abbreviate(assay.getName() + assay.getDescription(), 120));
                    this.addMaterial(assayToMaterial, assay.getId(), material.getId());
                }
                if (assayMap.get(design).size() > 1) {
                    String link = "<a title='" + StringUtils.join(tooltips.toArray(), "\n") + "' href='" + Settings.getRootContext() + "/bioAssay/showAllBioAssays.html?id=" + StringUtils.join(ids.toArray(), ",") + "'> (list) </a>";
                    buf.append("<td>" + assayMap.get(design).size() + link + "&nbsp;" + elementCount + "</td>\n");
                } else {
                    /*
                         * Each bioassay has a unique id; the div it sits in is identified by the class 'dragitem'. See
                         * expressionExperiment.edit.jsp.
                         */
                    BioAssayValueObject assay = ((List<BioAssayValueObject>) assayMap.get(design)).get(0);
                    String shortDesc = StringUtils.abbreviate(assay.getDescription(), 60);
                    String link = "<a target=\"_blank\" title='" + shortDesc + "' href='" + Settings.getRootContext() + "/bioAssay/showBioAssay.html?id=" + assay.getId() + "'>" + assay.getName() + "</a>";
                    String editAttributes = " align='left' class='dragItem' id='bioassay." + assay.getId() + "' material='" + material.getId() + "' assay='" + assay.getId() + "' arrayDesign='" + design.getId() + "'";
                    if (edit && designs.size() > 1) {
                        buf.append("\n<td><span " + editAttributes + ">" + image + link);
                    } else {
                        buf.append("\n<td ><span>" + link + "&nbsp;");
                    }
                    buf.append("</span></td>\n");
                }
            } else {
                emptyCount = addEmpty(buf, assayToMaterial, emptyCount, material, image, design);
            }
        }
        buf.append("</tr>");
        count++;
        elementCount++;
    }
    // add a few blanks, but only if we are editing.
    if (edit) {
        addNovelBiomaterialSlots(buf, designs, assayToMaterial, count, emptyCount);
    }
    buf.append("</table>");
    if (edit) {
        // append JSON serialization
        try {
            String jsonSerialization = JSONMapper.toJSON(assayToMaterial).render(false);
            buf.append("<input type='hidden' id='assayToMaterialMap' name='assayToMaterialMap' value='" + jsonSerialization + "'/>");
        } catch (MapperException e) {
        // cannot serialize
        }
    }
    buf.append("</div>");
    try {
        pageContext.getOut().print(buf.toString());
    } catch (Exception ex) {
        throw new JspException("assayViewTag: " + ex.getMessage());
    }
    return SKIP_BODY;
}
Also used : JspException(javax.servlet.jsp.JspException) ArrayDesignValueObject(ubic.gemma.model.expression.arrayDesign.ArrayDesignValueObject) MapperException(com.sdicons.json.mapper.MapperException) MapperException(com.sdicons.json.mapper.MapperException) JspException(javax.servlet.jsp.JspException) BioAssayValueObject(ubic.gemma.model.expression.bioAssay.BioAssayValueObject) BioMaterialValueObject(ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)

Example 3 with BioMaterialValueObject

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

the class BioMaterialServiceImpl method updateBioMaterials.

@Override
@Transactional
public Collection<BioMaterial> updateBioMaterials(Collection<BioMaterialValueObject> valueObjects) {
    Collection<BioMaterial> bms = new HashSet<>();
    for (BioMaterialValueObject bioMaterialValueObject : valueObjects) {
        BioMaterial updatedBm = this.update(bioMaterialValueObject);
        // the map FactorIdToFactorValueId contains values for all factors, including empty ones.
        assert bioMaterialValueObject.getFactorIdToFactorValueId().size() >= updatedBm.getFactorValues().size();
        bms.add(updatedBm);
    }
    return bms;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioMaterialValueObject(ubic.gemma.model.expression.biomaterial.BioMaterialValueObject) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with BioMaterialValueObject

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

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

the class ExperimentalDesignControllerImpl method updateBioMaterials.

@Override
public void updateBioMaterials(BioMaterialValueObject[] bmvos) {
    if (bmvos == null || bmvos.length == 0)
        return;
    Collection<BioMaterial> biomaterials = bioMaterialService.updateBioMaterials(Arrays.asList(bmvos));
    if (biomaterials.isEmpty())
        return;
    BioMaterial bm = biomaterials.iterator().next();
    ExpressionExperiment ee = expressionExperimentService.findByBioMaterial(bm);
    if (ee == null)
        throw new IllegalStateException("No Experiment for biomaterial: " + bm);
    ee = expressionExperimentService.thawLite(ee);
    for (ExperimentalFactor ef : ee.getExperimentalDesign().getExperimentalFactors()) {
        if (ef.getType().equals(FactorType.CONTINUOUS)) {
            /*
                 * Check for unused factorValues
                 */
            Collection<FactorValue> usedFactorValues = new HashSet<>();
            LinearModelAnalyzer.populateFactorValuesFromBASet(ee, ef, usedFactorValues);
            Collection<FactorValue> toDelete = new HashSet<>();
            for (FactorValue fv : ef.getFactorValues()) {
                if (!usedFactorValues.contains(fv)) {
                    /*
                         * remove it.
                         */
                    toDelete.add(fv);
                }
            }
            if (!toDelete.isEmpty()) {
                log.info("Deleting " + toDelete.size() + " unused factorvalues for " + ef);
                factorValueDeletion.deleteFactorValues(EntityUtils.getIds(toDelete));
            }
        }
    }
    StringBuilder details = new StringBuilder("Updated bio materials:\n");
    for (BioMaterialValueObject vo : bmvos) {
        if (vo == null) {
            continue;
        }
        BioMaterial ba = bioMaterialService.load(vo.getId());
        if (ba != null) {
            details.append("id: ").append(ba.getId()).append(" - ").append(ba.getName()).append("\n");
        }
    }
    this.auditTrailService.addUpdateEvent(ee, ExperimentalDesignUpdatedEvent.class, "BioMaterials updated (" + bmvos.length + " items)", details.toString());
    this.experimentReportService.evictFromCache(ee.getId());
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioMaterialValueObject(ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)

Aggregations

BioMaterialValueObject (ubic.gemma.model.expression.biomaterial.BioMaterialValueObject)9 BioAssayValueObject (ubic.gemma.model.expression.bioAssay.BioAssayValueObject)5 ArrayDesignValueObject (ubic.gemma.model.expression.arrayDesign.ArrayDesignValueObject)4 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)4 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 MapperException (com.sdicons.json.mapper.MapperException)1 HashSet (java.util.HashSet)1 JspException (javax.servlet.jsp.JspException)1 Transactional (org.springframework.transaction.annotation.Transactional)1 IdentifiableValueObject (ubic.gemma.model.IdentifiableValueObject)1 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)1 DoubleVectorValueObject (ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)1 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)1 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)1 FactorValueValueObject (ubic.gemma.model.expression.experiment.FactorValueValueObject)1