use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.
the class GeeqServiceImpl method leastReplicates.
/**
* Checks for all combinations of factor values in the experiments bio assays, and counts the amount of
* their occurrences, then checks what the lowest amount is. The method only combines factor values from
* first two experimental factors it encounters, and always disregards values from batch factors.
*
* @param ee an expression experiment to get the count for.
* @return the lowest number of replicates (ignoring factor value combinations with only one replicate),
* or 1, if all factor value combinations were present only once, or -1, if there were no factor values to
* begin with.
*/
private int leastReplicates(ExpressionExperiment ee) {
HashMap<String, Integer> factors = new HashMap<>();
Collection<BioAssay> bas = ee.getBioAssays();
List<ExperimentalFactor> keepEfs = new ArrayList<>(GeeqServiceImpl.MAX_EFS_REPLICATE_CHECK);
for (BioAssay ba : bas) {
Collection<FactorValue> fvs = ba.getSampleUsed().getFactorValues();
// only keep two factors, remove batch factor
Collection<FactorValue> removeFvs = new LinkedList<>();
for (FactorValue fv : fvs) {
ExperimentalFactor ef = fv.getExperimentalFactor();
if (ExperimentalDesignUtils.isBatch(ef)) {
// always remove batch factor values
removeFvs.add(fv);
} else {
if (!keepEfs.contains(ef) && keepEfs.size() <= GeeqServiceImpl.MAX_EFS_REPLICATE_CHECK) {
// keep first two encountered factors
keepEfs.add(ef);
} else if (!keepEfs.contains(ef)) {
// if from different factor, remove the value
removeFvs.add(fv);
}
}
}
fvs.removeAll(removeFvs);
// sort so the keys in the hash map are consistent
Collection<Long> ids = EntityUtils.getIds(fvs);
Long[] arr = ids.toArray(new Long[ids.size()]);
Arrays.sort(arr);
String key = Arrays.toString(arr);
// add new key or increment counter of existing one
Integer cnt = factors.get(key);
factors.put(key, cnt == null ? 1 : ++cnt);
}
// Hash-maps value collection can be almost anything
List<Integer> counts = factors.values() instanceof List ? (List<Integer>) factors.values() : new ArrayList<>(factors.values());
int totalSize = counts.size();
// noinspection StatementWithEmptyBody // because java standard libraries suck
while (counts.remove((Integer) 1)) {
}
Collections.sort(counts);
return (totalSize < 1 ? -1 : counts.size() > 0 ? counts.get(0) : 1);
}
use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.
the class DesignElementDataVectorDaoImpl method thaw.
@Override
public void thaw(T designElementDataVector) {
Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
BioSequence seq = designElementDataVector.getDesignElement().getBiologicalCharacteristic();
if (seq != null) {
session.buildLockRequest(LockOptions.NONE).lock(seq);
Hibernate.initialize(seq);
}
ArrayDesign arrayDesign = designElementDataVector.getDesignElement().getArrayDesign();
Hibernate.initialize(arrayDesign);
// thawRawAndProcessed the bioassays.
for (BioAssay ba : designElementDataVector.getBioAssayDimension().getBioAssays()) {
ba = (BioAssay) session.get(BioAssay.class, ba.getId());
Hibernate.initialize(ba.getArrayDesignUsed());
Hibernate.initialize(ba.getSampleUsed());
Hibernate.initialize(ba.getDerivedDataFiles());
}
}
use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.
the class BioAssayServiceImpl method handleRemoveBioMaterialAssociation.
// TODO: Refactor so that it accepts ids and does security check later.
private void handleRemoveBioMaterialAssociation(BioAssay bioAssay, BioMaterial bioMaterial) {
BioAssay bioAssayTemp = this.bioAssayDao.load(bioAssay.getId());
BioMaterial biomaterialToBeRemoved = this.bioMaterialDao.load(bioMaterial.getId());
BioMaterial currentBioMaterials = bioAssayTemp.getSampleUsed();
bioAssayTemp.setSampleUsed(currentBioMaterials);
// Remove bioAssay from bioMaterial
Collection<BioAssay> currentBioAssays = biomaterialToBeRemoved.getBioAssaysUsedIn();
currentBioAssays.remove(bioAssayTemp);
biomaterialToBeRemoved.setBioAssaysUsedIn(currentBioAssays);
this.bioMaterialDao.update(biomaterialToBeRemoved);
this.update(bioAssayTemp);
// If it is, remove it; if not, update it.
if (currentBioAssays.size() == 0) {
this.bioMaterialDao.remove(biomaterialToBeRemoved);
}
}
use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.
the class BioAssayServiceImpl method handleAddBioMaterialAssociation.
private void handleAddBioMaterialAssociation(BioAssay bioAssay, BioMaterial bioMaterial) {
// add bioMaterial to bioAssay
bioAssay.setSampleUsed(bioMaterial);
// add bioAssay to bioMaterial
Collection<BioAssay> currentBioAssays = bioMaterial.getBioAssaysUsedIn();
currentBioAssays.add(bioAssay);
bioMaterial.setBioAssaysUsedIn(currentBioAssays);
// update bioMaterial name - remove text after pipes
// this should not be necessary going forward
// build regular expression - match only text before the first pipe
Pattern pattern = Pattern.compile("^(.+)|");
String bmName = bioMaterial.getName();
Matcher matcher = pattern.matcher(bmName);
if (matcher.find()) {
String shortName = matcher.group();
bioMaterial.setName(shortName);
}
this.update(bioAssay);
this.bioMaterialDao.update(bioMaterial);
}
use of ubic.gemma.model.expression.bioAssay.BioAssay in project Gemma by PavlidisLab.
the class BioAssayDimensionDaoImpl method find.
@Override
public BioAssayDimension find(BioAssayDimension bioAssayDimension) {
Criteria queryObject = this.getSessionFactory().getCurrentSession().createCriteria(BioAssayDimension.class);
queryObject.setReadOnly(true);
queryObject.setFlushMode(FlushMode.MANUAL);
if (StringUtils.isNotBlank(bioAssayDimension.getName())) {
queryObject.add(Restrictions.eq("name", bioAssayDimension.getName()));
}
if (StringUtils.isNotBlank(bioAssayDimension.getDescription())) {
queryObject.add(Restrictions.eq("description", bioAssayDimension.getDescription()));
}
queryObject.add(Restrictions.sizeEq("bioAssays", bioAssayDimension.getBioAssays().size()));
Collection<String> names = new HashSet<>();
assert bioAssayDimension.getBioAssays().size() > 0;
for (BioAssay bioAssay : bioAssayDimension.getBioAssays()) {
names.add(bioAssay.getName());
}
queryObject.createCriteria("bioAssays").add(Restrictions.in("name", names));
BioAssayDimension candidate = (BioAssayDimension) queryObject.uniqueResult();
if (candidate == null)
return null;
// Now check that the bioassays and order are exactly the same.
Collection<BioAssay> desiredBioAssays = bioAssayDimension.getBioAssays();
Collection<BioAssay> candidateBioAssays = candidate.getBioAssays();
assert desiredBioAssays.size() == candidateBioAssays.size();
Iterator<BioAssay> dit = desiredBioAssays.iterator();
Iterator<BioAssay> cit = candidateBioAssays.iterator();
while (dit.hasNext()) {
BioAssay d = dit.next();
BioAssay c = cit.next();
if (!c.equals(d))
return null;
}
return candidate;
}
Aggregations