use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class PersistentDummyObjectHelper method getTestPersistentExpressionExperiment.
/**
* Convenience method to provide an ExpressionExperiment that can be used to fill non-nullable associations in test
* objects. This implementation does NOT fill in associations of the created object except for the creation of
* persistent BioMaterials and BioAssays so that database taxon lookups for this experiment will work.
*
* @param taxon the experiment will have this taxon
* @return EE
*/
public ExpressionExperiment getTestPersistentExpressionExperiment(Taxon taxon) {
BioAssay ba;
BioMaterial bm;
ArrayDesign ad;
bm = this.getTestPersistentBioMaterial(taxon);
ad = this.getTestPersistentArrayDesign(4, true, true);
ba = this.getTestPersistentBioAssay(ad, bm);
Set<BioAssay> bas1 = new HashSet<>();
bas1.add(ba);
ExpressionExperiment ee = ExpressionExperiment.Factory.newInstance();
ee.setName(RandomStringUtils.randomNumeric(PersistentDummyObjectHelper.RANDOM_STRING_LENGTH) + "_testee");
ee.setShortName(RandomStringUtils.randomNumeric(PersistentDummyObjectHelper.RANDOM_STRING_LENGTH) + "_testee");
ee.setBioAssays(bas1);
Collection<FactorValue> allFactorValues = new HashSet<>();
ExperimentalDesign ed = this.getExperimentalDesign(allFactorValues);
ee.setExperimentalDesign(ed);
ee.setOwner(this.getTestPersistentContact());
log.debug("expression experiment => design element data vectors");
Collection<RawExpressionDataVector> vectors = new HashSet<>();
Collection<QuantitationType> quantitationTypes = this.addQuantitationTypes(new HashSet<QuantitationType>());
assert quantitationTypes.size() > 0;
ee.setQuantitationTypes(quantitationTypes);
ee.setRawExpressionDataVectors(vectors);
ArrayDesignsForExperimentCache c = persisterHelper.prepare(ee);
return persisterHelper.persist(ee, c);
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExperimentalDesignVisualizationServiceImpl method getExperimentalDesignLayout.
/**
* @param bds a BioAssayDimension that represents the BioAssayDimensionValueObject. This is only needed to avoid
* making ExpressionMatrix use value objects, otherwise we could use the BioAssayDimensionValueObject
* @return A "Layout": a map of bioassays to map of factors to doubles that represent the position in the layout.
*/
private LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> getExperimentalDesignLayout(ExpressionExperiment experiment, Collection<BioAssayDimension> bds) {
LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>> result = new LinkedHashMap<>();
ExpressionDataMatrix<Object> mat = new EmptyExpressionMatrix(bds);
// This is the place the actual sort order is determined.
List<BioMaterial> bms = ExpressionDataMatrixColumnSort.orderByExperimentalDesign(mat);
Map<Long, Double> fvV = new HashMap<>();
assert experiment != null;
assert experiment.getExperimentalDesign() != null;
if (experiment.getExperimentalDesign().getExperimentalFactors().isEmpty()) {
// Case of no experimental design; just put in a dummy factor.
ExperimentalFactor dummyFactor = ExperimentalFactor.Factory.newInstance();
dummyFactor.setName("No factors");
for (BioMaterial bm : bms) {
int j = mat.getColumnIndex(bm);
Collection<BioAssay> bas = mat.getBioAssaysForColumn(j);
for (BioAssay ba : bas) {
BioAssayValueObject baVo = new BioAssayValueObject(ba, false);
result.put(baVo, new LinkedHashMap<ExperimentalFactor, Double>());
result.get(baVo).put(dummyFactor, 0.0);
}
}
return result;
}
assert !experiment.getExperimentalDesign().getExperimentalFactors().isEmpty();
// Map<ExperimentalFactor, Map<FactorValue, Double>> continuousRanges = new HashMap<>();
for (ExperimentalFactor ef : experiment.getExperimentalDesign().getExperimentalFactors()) {
if (ef.getFactorValues().isEmpty()) {
// this can happen if the design isn't complete.
continue;
}
for (FactorValue fv : ef.getFactorValues()) {
assert fv.getId() != null;
// the id is just used as a convenience.
fvV.put(fv.getId(), new Double(fv.getId()));
}
}
assert !fvV.isEmpty();
assert !bms.isEmpty();
// either bioassay dimension.
for (BioMaterial bm : bms) {
int j = mat.getColumnIndex(bm);
Collection<BioAssay> bas = mat.getBioAssaysForColumn(j);
Collection<FactorValue> fvs = bm.getFactorValues();
for (BioAssay ba : bas) {
BioAssayValueObject baVo = new BioAssayValueObject(ba, false);
result.put(baVo, new LinkedHashMap<ExperimentalFactor, Double>(fvs.size()));
for (FactorValue fv : fvs) {
assert fv.getId() != null;
assert fvV.containsKey(fv.getId());
ExperimentalFactor ef = fv.getExperimentalFactor();
Double value;
if (fv.getMeasurement() != null) {
try {
value = Double.parseDouble(fv.getMeasurement().getValue());
} catch (NumberFormatException e) {
// not good.
value = fvV.get(fv.getId());
}
} else {
value = fvV.get(fv.getId());
}
assert result.containsKey(baVo);
assert value != null;
result.get(baVo).put(ef, value);
}
}
}
return result;
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class CharacteristicUpdateTaskImpl method addToParent.
private void addToParent(Characteristic c, Object parent) {
if (parent instanceof ExpressionExperiment) {
ExpressionExperiment ee = (ExpressionExperiment) parent;
ee = expressionExperimentService.thawLite(ee);
ee.getCharacteristics().add(c);
expressionExperimentService.update(ee);
} else if (parent instanceof BioMaterial) {
BioMaterial bm = (BioMaterial) parent;
bm.getCharacteristics().add(c);
bioMaterialService.update(bm);
} else if (parent instanceof FactorValue) {
FactorValue fv = (FactorValue) parent;
fv.getCharacteristics().add(c);
factorValueService.update(fv);
}
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExperimentalDesignWriter method write.
/**
* @param writeBaseHeader comments
* @param writeHeader column names
* @param ee ee
* @param bioAssays bas
* @param writer writer
* @throws IOException when the write failed
*/
public void write(Writer writer, ExpressionExperiment ee, Collection<BioAssay> bioAssays, boolean writeBaseHeader, boolean writeHeader) throws IOException {
ExperimentalDesign ed = ee.getExperimentalDesign();
/*
* See BaseExpressionDataMatrix.setUpColumnElements() for how this is constructed for the DataMatrix, and for
* some notes about complications.
*/
Map<BioMaterial, Collection<BioAssay>> bioMaterials = new HashMap<>();
for (BioAssay bioAssay : bioAssays) {
BioMaterial bm = bioAssay.getSampleUsed();
if (!bioMaterials.containsKey(bm)) {
bioMaterials.put(bm, new HashSet<BioAssay>());
}
bioMaterials.get(bm).add(bioAssay);
}
Collection<ExperimentalFactor> efs = ed.getExperimentalFactors();
List<ExperimentalFactor> orderedFactors = new ArrayList<>(efs);
StringBuffer buf = new StringBuffer();
if (writeHeader) {
this.writeHeader(ee, orderedFactors, writeBaseHeader, buf);
}
for (BioMaterial bioMaterial : bioMaterials.keySet()) {
/* column 0 of the design matrix */
String rowName = ExpressionDataWriterUtils.constructBioAssayName(bioMaterial, bioMaterials.get(bioMaterial));
buf.append(rowName);
buf.append("\t");
/* column 1 */
String externalId = ExpressionDataWriterUtils.getExternalId(bioMaterial, bioMaterials.get(bioMaterial));
buf.append(externalId);
/* columns 2 ... n where n+1 is the number of factors */
Collection<FactorValue> candidateFactorValues = bioMaterial.getFactorValues();
for (ExperimentalFactor ef : orderedFactors) {
buf.append("\t");
for (FactorValue candidateFactorValue : candidateFactorValues) {
if (candidateFactorValue.getExperimentalFactor().equals(ef)) {
log.debug(candidateFactorValue.getExperimentalFactor() + " matched.");
String matchedFactorValue = ExpressionDataWriterUtils.constructFactorValueName(candidateFactorValue);
buf.append(matchedFactorValue);
break;
}
log.debug(candidateFactorValue.getExperimentalFactor() + " didn't match ... trying the next factor.");
}
}
buf.append("\n");
}
if (log.isDebugEnabled())
log.debug(buf.toString());
writer.write(buf.toString());
writer.flush();
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionDataMatrixColumnSort method chunkOnFactor.
/**
* Divide the biomaterials up into chunks based on the experimental factor given, keeping everybody in order. If the
* factor is continuous, there is just one chunk.
*
* @return ordered map of fv->bm where fv is of ef, or null if it couldn't be done properly.
*/
private static LinkedHashMap<FactorValue, List<BioMaterial>> chunkOnFactor(ExperimentalFactor ef, List<BioMaterial> bms) {
if (bms == null) {
return null;
}
LinkedHashMap<FactorValue, List<BioMaterial>> chunks = new LinkedHashMap<>();
/*
* Get the factor values in the order we have things right now
*/
for (BioMaterial bm : bms) {
for (FactorValue fv : bm.getFactorValues()) {
if (!ef.getFactorValues().contains(fv)) {
continue;
}
if (chunks.keySet().contains(fv)) {
continue;
}
chunks.put(fv, new ArrayList<BioMaterial>());
}
}
/*
* What if bm doesn't have a value for the factorvalue. Need a dummy value.
*/
FactorValue dummy = FactorValue.Factory.newInstance(ef);
dummy.setValue("");
dummy.setId(-1L);
chunks.put(dummy, new ArrayList<BioMaterial>());
for (BioMaterial bm : bms) {
boolean found = false;
for (FactorValue fv : bm.getFactorValues()) {
if (ef.getFactorValues().contains(fv)) {
found = true;
assert chunks.containsKey(fv);
chunks.get(fv).add(bm);
}
}
if (!found) {
if (ExpressionDataMatrixColumnSort.log.isDebugEnabled())
ExpressionDataMatrixColumnSort.log.debug(bm + " has no value for factor=" + ef + "; using dummy value");
chunks.get(dummy).add(bm);
}
}
if (chunks.get(dummy).size() == 0) {
if (ExpressionDataMatrixColumnSort.log.isDebugEnabled())
ExpressionDataMatrixColumnSort.log.debug("removing dummy");
chunks.remove(dummy);
}
ExpressionDataMatrixColumnSort.log.debug(chunks.size() + " chunks for " + ef + ", from current chunk of size " + bms.size());
/*
* Sanity check
*/
int total = 0;
for (FactorValue fv : chunks.keySet()) {
List<BioMaterial> chunk = chunks.get(fv);
total += chunk.size();
}
assert total == bms.size() : "expected " + bms.size() + ", got " + total;
return chunks;
}
Aggregations