Search in sources :

Example 71 with BioAssay

use of ubic.gemma.model.expression.bioAssay.BioAssay 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)

Example 72 with BioAssay

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

the class ExpressionExperimentQCController method identifyPossibleOutliers.

@RequestMapping("/expressionExperiment/possibleOutliers.html")
public ModelAndView identifyPossibleOutliers(Long id) throws IOException {
    if (id == null) {
        log.warn("No id!");
        return null;
    }
    ExpressionExperiment ee = expressionExperimentService.load(id);
    if (ee == null) {
        log.warn("Could not load experiment with id " + id);
        return null;
    }
    // identify outliers
    if (!sampleCoexpressionMatrixService.hasMatrix(ee)) {
        log.warn("Experiment doesn't have correlation matrix computed (will not create right now)");
        return null;
    }
    DoubleMatrix<BioAssay, BioAssay> sampleCorrelationMatrix = sampleCoexpressionMatrixService.findOrCreate(ee);
    Collection<OutlierDetails> outliers = outlierDetectionService.identifyOutliersByMedianCorrelation(ee);
    Collection<BioAssay> bioAssays = new HashSet<>();
    if (!outliers.isEmpty()) {
        for (OutlierDetails details : outliers) {
            bioAssays.add(details.getBioAssay());
        }
    }
    // and write it out
    StringWriter writer = new StringWriter();
    StringBuffer buf = writer.getBuffer();
    ExpressionDataWriterUtils.appendBaseHeader(ee, "Sample outlier", buf);
    ExperimentalDesignWriter edWriter = new ExperimentalDesignWriter();
    ee = expressionExperimentService.thawLiter(ee);
    edWriter.write(writer, ee, bioAssays, false, true);
    ModelAndView mav = new ModelAndView(new TextView());
    mav.addObject(TextView.TEXT_PARAM, buf.toString());
    return mav;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) TextView(ubic.gemma.web.view.TextView) ExperimentalDesignWriter(ubic.gemma.core.datastructure.matrix.ExperimentalDesignWriter) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) OutlierDetails(ubic.gemma.core.analysis.preprocess.OutlierDetails) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 73 with BioAssay

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

the class BioMaterialController method getBioMaterialsForEE.

/**
 * @param id of experiment
 */
public Collection<BioMaterial> getBioMaterialsForEE(Long id) {
    ExpressionExperiment expressionExperiment = expressionExperimentService.load(id);
    if (expressionExperiment == null) {
        throw new EntityNotFoundException("Expression experiment with id=" + id + " not found");
    }
    expressionExperiment = expressionExperimentService.thawLite(expressionExperiment);
    Collection<BioAssay> bioAssays = expressionExperiment.getBioAssays();
    Collection<BioMaterial> bioMaterials = new ArrayList<>();
    for (BioAssay assay : bioAssays) {
        BioMaterial material = assay.getSampleUsed();
        if (material != null) {
            bioMaterials.add(material);
        }
    }
    return bioMaterials;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) ArrayList(java.util.ArrayList) EntityNotFoundException(ubic.gemma.web.util.EntityNotFoundException) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 74 with BioAssay

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

the class ExpressionExperimentController method unmatchAllBioAssays.

/**
 * Completely reset the pairing of bioassays to biomaterials so they are no longer paired. New biomaterials are
 * constructed where necessary; they retain the characteristics of the original. Experimental design might need to
 * be redone after this operation. (AJAX)
 *
 * @param eeId ee id
 */
public void unmatchAllBioAssays(Long eeId) {
    ExpressionExperiment ee = this.expressionExperimentService.load(eeId);
    if (ee == null) {
        throw new IllegalArgumentException("Could not load experiment with id=" + eeId);
    }
    ee = expressionExperimentService.thawLite(ee);
    Collection<BioMaterial> needToProcess = new HashSet<>();
    for (BioAssay ba : ee.getBioAssays()) {
        BioMaterial bm = ba.getSampleUsed();
        this.bioMaterialService.thaw(bm);
        Collection<BioAssay> bioAssaysUsedIn = bm.getBioAssaysUsedIn();
        if (bioAssaysUsedIn.size() > 1) {
            needToProcess.add(bm);
        }
    }
    // FIXME this should be in a transaction!
    for (BioMaterial bm : needToProcess) {
        int i = 0;
        for (BioAssay baU : bm.getBioAssaysUsedIn()) {
            if (i > 0) {
                BioMaterial newMaterial = bioMaterialService.copy(bm);
                this.bioMaterialService.thaw(newMaterial);
                newMaterial.setName("Modeled after " + bm.getName());
                newMaterial.getFactorValues().clear();
                newMaterial.getBioAssaysUsedIn().add(baU);
                newMaterial = (BioMaterial) persisterHelper.persist(newMaterial);
                baU.setSampleUsed(newMaterial);
                bioAssayService.update(baU);
            }
            i++;
        }
    }
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 75 with BioAssay

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

the class SampleCoexpressionMatrixServiceImpl method create.

@Override
public DoubleMatrix<BioAssay, BioAssay> create(ExpressionExperiment ee, boolean useRegression, boolean removeOutliers) {
    // Load data and create matrix
    ExpressionDataDoubleMatrix mat = this.loadDataMatrix(ee, useRegression, this.loadVectors(ee));
    DoubleMatrix<BioAssay, BioAssay> cormat = this.loadCorMat(removeOutliers, mat);
    // Check consistency
    BioAssayDimension bestBioAssayDimension = mat.getBestBioAssayDimension();
    if (cormat.rows() != bestBioAssayDimension.getBioAssays().size()) {
        throw new IllegalStateException("Number of bioassays doesn't match length of the best bioAssayDimension. BAs in dimension: " + bestBioAssayDimension.getBioAssays().size() + ", rows in cormat: " + cormat.rows());
    }
    // Persist
    sampleCoexpressionMatrixHelperService.create(cormat, bestBioAssayDimension, mat.getExpressionExperiment());
    return cormat;
}
Also used : BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) ExpressionDataDoubleMatrix(ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Aggregations

BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)144 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)67 Test (org.junit.Test)29 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)29 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)24 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)20 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)18 RawExpressionDataVector (ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector)16 AbstractGeoServiceTest (ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest)15 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)14 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)14 InputStream (java.io.InputStream)11 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)10 HashSet (java.util.HashSet)9 AlreadyExistsInSystemException (ubic.gemma.core.loader.util.AlreadyExistsInSystemException)8 DesignElementDataVector (ubic.gemma.model.expression.bioAssayData.DesignElementDataVector)8 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)7