Search in sources :

Example 51 with BioAssay

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);
}
Also used : BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 52 with BioAssay

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());
    }
}
Also used : BioSequence(ubic.gemma.model.genome.biosequence.BioSequence) ArrayDesign(ubic.gemma.model.expression.arrayDesign.ArrayDesign) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 53 with BioAssay

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);
    }
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 54 with BioAssay

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 55 with BioAssay

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;
}
Also used : BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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