Search in sources :

Example 11 with VocabCharacteristic

use of ubic.gemma.model.common.description.VocabCharacteristic in project Gemma by PavlidisLab.

the class CharacteristicUpdateTaskImpl method convertToFactorValuesWithCharacteristics.

/**
 * This is used to handle the special case of FactorValues that are being updated to have a characteristic.
 *
 * @return for each given AnnotationValueObject, the corresponding FactorValue with an associated persistent
 * Characteristic.
 * @throws IllegalStateException if the corresponding FactorValue already has at least one Characteristic. This
 *                               method is just intended for filling that in if it's empty.
 */
private Collection<FactorValue> convertToFactorValuesWithCharacteristics(Collection<AnnotationValueObject> avos) {
    Collection<FactorValue> result = new HashSet<>();
    for (AnnotationValueObject avo : avos) {
        if (avo.getObjectClass() == null || !avo.getObjectClass().equals(FactorValue.class.getSimpleName()))
            continue;
        if (avo.getId() == null) {
            log.warn("No id");
            continue;
        }
        /*
             * load the factor value, and create a characteristic
             */
        FactorValue fv = factorValueService.load(avo.getId());
        if (fv == null)
            continue;
        if (!fv.getCharacteristics().isEmpty()) {
            throw new IllegalStateException("Don't use the annotator to update factor values that already have characteristics");
        }
        VocabCharacteristic vc = convertAvo2Characteristic(avo);
        vc.setId(null);
        if (vc.getEvidenceCode() == null) {
            vc.setEvidenceCode(GOEvidenceCode.IC);
        }
        vc = (VocabCharacteristic) characteristicService.create(vc);
        fv.setValue(vc.getValue());
        fv.getCharacteristics().add(vc);
        result.add(fv);
    }
    return result;
}
Also used : FactorValue(ubic.gemma.model.expression.experiment.FactorValue) AnnotationValueObject(ubic.gemma.model.common.description.AnnotationValueObject) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) HashSet(java.util.HashSet)

Example 12 with VocabCharacteristic

use of ubic.gemma.model.common.description.VocabCharacteristic in project Gemma by PavlidisLab.

the class CharacteristicUpdateTaskImpl method doUpdate.

private TaskResult doUpdate() {
    Collection<AnnotationValueObject> avos = taskCommand.getAnnotationValueObjects();
    if (avos.size() == 0)
        return new TaskResult(taskCommand, false);
    log.info("Updating " + avos.size() + " characteristics or uncharacterized factor values...");
    StopWatch timer = new StopWatch();
    timer.start();
    Collection<Characteristic> asChars = convertToCharacteristic(avos);
    Collection<FactorValue> factorValues = convertToFactorValuesWithCharacteristics(avos);
    if (asChars.size() == 0 && factorValues.size() == 0) {
        log.info("Nothing to update");
        return new TaskResult(taskCommand, false);
    }
    for (FactorValue factorValue : factorValues) {
        factorValueService.update(factorValue);
    }
    if (asChars.size() == 0)
        return new TaskResult(taskCommand, true);
    Map<Characteristic, Object> charToParent = characteristicService.getParents(asChars);
    for (Characteristic cFromClient : asChars) {
        Long characteristicId = cFromClient.getId();
        if (characteristicId == null) {
            continue;
        }
        Characteristic cFromDatabase = characteristicService.load(characteristicId);
        if (cFromDatabase == null) {
            log.warn("No such characteristic with id=" + characteristicId);
            continue;
        }
        VocabCharacteristic vcFromClient = (cFromClient instanceof VocabCharacteristic) ? (VocabCharacteristic) cFromClient : null;
        VocabCharacteristic vcFromDatabase = (cFromDatabase instanceof VocabCharacteristic) ? (VocabCharacteristic) cFromDatabase : null;
        /*
             * if one of the characteristics is a VocabCharacteristic and the other is not, we have to change the
             * characteristic in the database so that it matches the one from the client; since we can't change the
             * class of the object, we have to remove the old characteristic and make a new one of the appropriate
             * class.
             */
        Object parent = charToParent.get(cFromDatabase);
        /*
             * Check needed because Characteristics are not securable.
             */
        if (parent != null && Securable.class.isAssignableFrom(parent.getClass()) && !securityService.isEditable((Securable) parent)) {
            throw new AccessDeniedException("Access is denied");
        }
        if (vcFromClient != null && vcFromDatabase == null) {
            VocabCharacteristic vc = VocabCharacteristic.Factory.newInstance();
            vc.setValue(StringUtils.strip(cFromDatabase.getValue()));
            vc.setEvidenceCode(cFromDatabase.getEvidenceCode());
            vc.setDescription(cFromDatabase.getDescription());
            vc.setCategory(cFromDatabase.getCategory());
            vc.setName(cFromDatabase.getName());
            vcFromDatabase = (VocabCharacteristic) characteristicService.create(vc);
            removeFromParent(cFromDatabase, parent);
            characteristicService.remove(cFromDatabase);
            addToParent(vcFromDatabase, parent);
            cFromDatabase = vcFromDatabase;
        } else if (vcFromClient == null && vcFromDatabase != null) {
            // don't copy AuditTrail or Status to avoid cascade error... vcFromDatabase.getAuditTrail()
            cFromDatabase = characteristicService.create(Characteristic.Factory.newInstance(vcFromDatabase.getName(), vcFromDatabase.getDescription(), null, StringUtils.strip(vcFromDatabase.getValue()), vcFromDatabase.getCategory(), vcFromDatabase.getCategoryUri(), vcFromDatabase.getEvidenceCode()));
            removeFromParent(vcFromDatabase, parent);
            characteristicService.remove(vcFromDatabase);
            addToParent(cFromDatabase, parent);
        }
        /*
             * at this point, cFromDatabase points to the class-corrected characteristic in the database that must be
             * updated with the information coming from the client.
             */
        assert cFromDatabase != null;
        cFromDatabase.setValue(cFromClient.getValue());
        cFromDatabase.setCategory(cFromClient.getCategory());
        if (cFromDatabase instanceof VocabCharacteristic) {
            vcFromDatabase = (VocabCharacteristic) cFromDatabase;
            if (vcFromClient != null) {
                if (vcFromDatabase.getValueUri() == null || vcFromDatabase.getValueUri() == null || !vcFromDatabase.getValueUri().equals(vcFromClient.getValueUri())) {
                    log.info("Characteristic value update: " + vcFromDatabase + " " + vcFromDatabase.getValueUri() + " -> " + vcFromClient.getValueUri() + " associated with " + parent);
                    vcFromDatabase.setValueUri(vcFromClient.getValueUri());
                }
                if (vcFromDatabase.getCategory() == null || vcFromDatabase.getCategoryUri() == null || !vcFromDatabase.getCategoryUri().equals(vcFromClient.getCategoryUri())) {
                    log.info("Characteristic category update: " + vcFromDatabase + " " + vcFromDatabase.getCategoryUri() + " -> " + vcFromClient.getCategoryUri() + " associated with " + parent);
                    vcFromDatabase.setCategoryUri(vcFromClient.getCategoryUri());
                }
            }
        }
        if (cFromClient.getEvidenceCode() == null) {
            // characteristic has been manually updated
            cFromDatabase.setEvidenceCode(GOEvidenceCode.IC);
        } else {
            if (!cFromDatabase.getEvidenceCode().equals(cFromClient.getEvidenceCode())) {
                log.info("Characteristic evidence code update: " + cFromDatabase + " " + cFromDatabase.getEvidenceCode() + " -> " + cFromClient.getEvidenceCode());
            }
            // let them change it.
            cFromDatabase.setEvidenceCode(cFromClient.getEvidenceCode());
        }
        characteristicService.update(cFromDatabase);
    }
    timer.stop();
    if (timer.getTime() > 1000) {
        log.info("Update took: " + timer.getTime());
    }
    return new TaskResult(taskCommand, true);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) AnnotationValueObject(ubic.gemma.model.common.description.AnnotationValueObject) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) StopWatch(org.apache.commons.lang3.time.StopWatch) TaskResult(ubic.gemma.core.job.TaskResult) AnnotationValueObject(ubic.gemma.model.common.description.AnnotationValueObject)

Example 13 with VocabCharacteristic

use of ubic.gemma.model.common.description.VocabCharacteristic in project Gemma by PavlidisLab.

the class CharacteristicUpdateTaskImpl method convertToCharacteristic.

/**
 * Convert incoming AVOs into Characteristics (if the AVO objectClass is not FactorValue)
 */
private Collection<Characteristic> convertToCharacteristic(Collection<AnnotationValueObject> avos) {
    Collection<Characteristic> result = new HashSet<>();
    for (AnnotationValueObject avo : avos) {
        if (avo.getObjectClass() != null && avo.getObjectClass().equals(FactorValue.class.getSimpleName()))
            continue;
        VocabCharacteristic vc = convertAvo2Characteristic(avo);
        result.add(vc);
    }
    return result;
}
Also used : Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) AnnotationValueObject(ubic.gemma.model.common.description.AnnotationValueObject) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) HashSet(java.util.HashSet)

Example 14 with VocabCharacteristic

use of ubic.gemma.model.common.description.VocabCharacteristic in project Gemma by PavlidisLab.

the class Gene2GOAssociationDaoImpl method fetchBatch.

private Map<? extends Gene, ? extends Collection<VocabCharacteristic>> fetchBatch(Set<Gene> batch) {
    Map<Long, Gene> giMap = EntityUtils.getIdMap(batch);
    // language=HQL
    final String queryString = "select g.id, geneAss.ontologyEntry from Gene2GOAssociationImpl as geneAss join geneAss.gene g where g.id in (:genes)";
    Map<Gene, Collection<VocabCharacteristic>> results = new HashMap<>();
    Query query = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
    query.setFetchSize(batch.size());
    query.setParameterList("genes", giMap.keySet());
    List<?> o = query.list();
    for (Object object : o) {
        Object[] oa = (Object[]) object;
        Long g = (Long) oa[0];
        VocabCharacteristic vc = (VocabCharacteristic) oa[1];
        Gene gene = giMap.get(g);
        assert gene != null;
        if (!results.containsKey(gene)) {
            results.put(gene, new HashSet<VocabCharacteristic>());
        }
        results.get(gene).add(vc);
    }
    return results;
}
Also used : Query(org.hibernate.Query) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) Gene(ubic.gemma.model.genome.Gene)

Example 15 with VocabCharacteristic

use of ubic.gemma.model.common.description.VocabCharacteristic in project Gemma by PavlidisLab.

the class FactorValueValueObject method init.

private void init(FactorValue val, Characteristic c) {
    this.setFactorValue(FactorValueValueObject.getSummaryString(val));
    this.setFactorId(val.getExperimentalFactor().getId());
    this.isBaseline = val.getIsBaseline() != null ? val.getIsBaseline() : this.isBaseline;
    if (val.getMeasurement() != null) {
        this.setMeasurement(true);
        this.value = val.getMeasurement().getValue();
        this.setCharId(val.getMeasurement().getId());
    } else if (c != null && c.getId() != null) {
        this.setCharId(c.getId());
    } else {
        this.value = val.getValue();
    }
    if (c != null) {
        this.setCategory(c.getCategory());
        // clobbers if we set it already
        this.setValue(c.getValue());
        if (c instanceof VocabCharacteristic) {
            VocabCharacteristic vc = (VocabCharacteristic) c;
            this.setCategoryUri(vc.getCategoryUri());
            this.setValueUri(vc.getValueUri());
        }
    }
    /*
         * Make sure we fill in the Category for this.
         */
    Characteristic factorCategory = val.getExperimentalFactor().getCategory();
    if (this.getCategory() == null && factorCategory != null) {
        this.setCategory(factorCategory.getCategory());
        if (factorCategory instanceof VocabCharacteristic) {
            VocabCharacteristic vc = (VocabCharacteristic) factorCategory;
            this.setCategoryUri(vc.getCategoryUri());
        }
    }
}
Also used : Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic)

Aggregations

VocabCharacteristic (ubic.gemma.model.common.description.VocabCharacteristic)44 Characteristic (ubic.gemma.model.common.description.Characteristic)15 OntologyTerm (ubic.basecode.ontology.model.OntologyTerm)10 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)7 Test (org.junit.Test)6 AnnotationValueObject (ubic.gemma.model.common.description.AnnotationValueObject)4 Gene (ubic.gemma.model.genome.Gene)4 HashSet (java.util.HashSet)3 Gene2GOAssociation (ubic.gemma.model.association.Gene2GOAssociation)3 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 ConcurrentHashSet (org.compass.core.util.concurrent.ConcurrentHashSet)2 OntologyIndividual (ubic.basecode.ontology.model.OntologyIndividual)2 OntologyResource (ubic.basecode.ontology.model.OntologyResource)2 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)2 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)2 CharacteristicValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject)2 AclObjectIdentity (gemma.gsec.acl.domain.AclObjectIdentity)1 InputStream (java.io.InputStream)1