Search in sources :

Example 1 with JSONInteger

use of com.sdicons.json.model.JSONInteger in project Gemma by PavlidisLab.

the class ExpressionExperimentFormController method updateBioMaterialMap.

/**
 * Change the relationship between bioassays and biomaterials.
 *
 * @param request              request
 * @param expressionExperiment ee
 * @return true if there were changes
 */
private boolean updateBioMaterialMap(HttpServletRequest request, ExpressionExperiment expressionExperiment) {
    // parse JSON-serialized map
    String jsonSerialization = request.getParameter("assayToMaterialMap");
    // convert back to a map
    Map<String, JSONValue> bioAssayMap;
    try (StringInputStream aStream = new StringInputStream(jsonSerialization)) {
        JSONParser parser = new JSONParser(aStream);
        bioAssayMap = ((JSONObject) parser.nextValue()).getValue();
    } catch (IOException | ANTLRException e) {
        throw new RuntimeException(e);
    }
    Map<BioAssay, BioMaterial> deleteAssociations = new HashMap<>();
    Set<Entry<String, JSONValue>> bioAssays = bioAssayMap.entrySet();
    boolean anyChanges = false;
    int newBioMaterialCount = 0;
    for (Entry<String, JSONValue> entry : bioAssays) {
        // if it is, skip over this entry
        if (entry.getKey().equalsIgnoreCase("nullElement")) {
            continue;
        }
        Long bioAssayId = Long.parseLong(entry.getKey());
        JSONValue value = entry.getValue();
        Long newMaterialId;
        if (value.isString()) {
            newMaterialId = Long.parseLong(((JSONString) value).getValue());
        } else {
            newMaterialId = ((JSONInteger) value).getValue().longValue();
        }
        // maybe we need to do
        BioAssay bioAssay = bioAssayService.load(bioAssayId);
        if (bioAssay == null) {
            throw new IllegalArgumentException("Bioassay with id=" + bioAssayId + " was not associated with the experiment");
        }
        BioMaterial currentBioMaterial = bioAssay.getSampleUsed();
        if (newMaterialId.equals(currentBioMaterial.getId())) {
            // / no change
            continue;
        }
        BioMaterial newMaterial;
        if (newMaterialId < 0) {
            // This kludge signifies that it is a 'brand new' biomaterial.
            newMaterial = bioMaterialService.copy(currentBioMaterial);
            newMaterial.setName("Modeled after " + currentBioMaterial.getName());
            newMaterial.getFactorValues().clear();
            newMaterial = (BioMaterial) persisterHelper.persist(newMaterial);
            newBioMaterialCount++;
        } else {
            // FIXME can we just use this from the experiment, probably no need to fetch it again.
            newMaterial = bioMaterialService.load(newMaterialId);
            if (newMaterial == null) {
                throw new IllegalArgumentException("BioMaterial with id=" + newMaterialId + " could not be loaded");
            }
        }
        anyChanges = true;
        BaseFormController.log.info("Associating " + bioAssay + " with " + newMaterial);
        bioAssayService.addBioMaterialAssociation(bioAssay, newMaterial);
    }
    if (anyChanges) {
        /*
             * FIXME Decide if we need to remove the biomaterial -> factor value associations, it could be completely
             * fouled up.
             */
        BaseFormController.log.info("There were changes to the BioMaterial -> BioAssay map");
        this.audit(expressionExperiment, BioMaterialMappingUpdate.Factory.newInstance(), // remove unnecessary biomaterial associations
        newBioMaterialCount + " biomaterials");
        Collection<BioAssay> deleteKeys = deleteAssociations.keySet();
        for (BioAssay assay : deleteKeys) {
            /*
                 * BUG: if this fails, we end up with a useless extra biomaterial associated with the bioassay.
                 */
            bioAssayService.removeBioMaterialAssociation(assay, deleteAssociations.get(assay));
        }
    } else {
        BaseFormController.log.info("There were no changes to the BioMaterial -> BioAssay map");
    }
    return anyChanges;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) JSONInteger(com.sdicons.json.model.JSONInteger) JSONString(com.sdicons.json.model.JSONString) IOException(java.io.IOException) JSONValue(com.sdicons.json.model.JSONValue) ANTLRException(antlr.ANTLRException) StringInputStream(org.apache.tools.ant.filters.StringInputStream) Entry(java.util.Map.Entry) JSONParser(com.sdicons.json.parser.JSONParser) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) JSONString(com.sdicons.json.model.JSONString)

Aggregations

ANTLRException (antlr.ANTLRException)1 JSONInteger (com.sdicons.json.model.JSONInteger)1 JSONString (com.sdicons.json.model.JSONString)1 JSONValue (com.sdicons.json.model.JSONValue)1 JSONParser (com.sdicons.json.parser.JSONParser)1 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 StringInputStream (org.apache.tools.ant.filters.StringInputStream)1 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)1 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)1