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;
}
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;
}
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;
}
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++;
}
}
}
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;
}
Aggregations