Search in sources :

Example 6 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class SearchServiceImpl method getDirectChildTerms.

/**
 * Returns children one step down.
 *
 * @param term starting point
 */
@SuppressWarnings("unchecked")
private Collection<OntologyTerm> getDirectChildTerms(OntologyTerm term) {
    String uri = term.getUri();
    /*
         * getChildren can be very slow for 'high-level' classes like "neoplasm", so we use a cache.
         */
    Collection<OntologyTerm> children = null;
    if (StringUtils.isBlank(uri)) {
        // shouldn't happen, but just in case
        if (SearchServiceImpl.log.isDebugEnabled())
            SearchServiceImpl.log.debug("Blank uri for " + term);
    }
    Element cachedChildren = this.childTermCache.get(uri);
    if (cachedChildren == null) {
        try {
            children = term.getChildren(true);
            childTermCache.put(new Element(uri, children));
        } catch (com.hp.hpl.jena.ontology.ConversionException ce) {
            SearchServiceImpl.log.warn("getting children for term: " + term + " caused com.hp.hpl.jena.ontology.ConversionException. " + ce.getMessage());
        }
    } else {
        children = (Collection<OntologyTerm>) cachedChildren.getObjectValue();
    }
    return children;
}
Also used : Element(net.sf.ehcache.Element) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Example 7 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class GoMetric method computePercentOverlap.

/**
 * @param gene1         gene 1
 * @param gene2         gene 2
 * @param includePartOf include part of
 * @return percent of overlapping terms wrt to the gene with the lower number of GO terms
 */
private Double computePercentOverlap(Gene gene1, Gene gene2, boolean includePartOf) {
    if (!geneOntologyService.isReady())
        GoMetric.log.error("computeSimpleOverlap called before geneOntologyService is ready!!!");
    Double avgScore = 0.0;
    Collection<OntologyTerm> masterGO = geneOntologyService.getGOTerms(gene1, includePartOf, null);
    Collection<OntologyTerm> coExpGO = geneOntologyService.getGOTerms(gene2, includePartOf, null);
    Collection<OntologyTerm> overlappingTerms = new HashSet<>();
    for (OntologyTerm o : masterGO) {
        if (coExpGO.contains(o) && !this.isRoot(o))
            overlappingTerms.add(o);
    }
    if (masterGO.size() < coExpGO.size()) {
        avgScore = (double) overlappingTerms.size() / masterGO.size();
    }
    if (coExpGO.size() < masterGO.size()) {
        avgScore = (double) overlappingTerms.size() / coExpGO.size();
    }
    if (coExpGO.size() == masterGO.size()) {
        avgScore = (double) overlappingTerms.size() / coExpGO.size();
    }
    return avgScore;
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Example 8 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class GoMetric method computeMaxSimilarity.

/**
 * @param metric     metric
 * @param GOProbMap  go prob map
 * @param queryGene  query gene
 * @param targetGene target gene
 * @return the MAX overlap score between two genes
 */
public Double computeMaxSimilarity(Gene queryGene, Gene targetGene, Map<String, Double> GOProbMap, Metric metric) {
    Collection<OntologyTerm> masterGO = this.getOntologyTerms(queryGene);
    if ((masterGO == null) || masterGO.isEmpty())
        return 0.0;
    Collection<OntologyTerm> coExpGO = this.getOntologyTerms(targetGene);
    if ((coExpGO == null) || coExpGO.isEmpty())
        return 0.0;
    double checkScore = 0.0;
    for (OntologyTerm ontoM : masterGO) {
        if (this.isRoot(ontoM))
            continue;
        if (!GOProbMap.containsKey(ontoM.getUri()))
            continue;
        double probM = GOProbMap.get(ontoM.getUri());
        for (OntologyTerm ontoC : coExpGO) {
            if (this.isRoot(ontoC))
                continue;
            if (!GOProbMap.containsKey(ontoC.getUri()))
                continue;
            Double probC = GOProbMap.get(ontoC.getUri());
            Double pMin;
            Double score;
            if (ontoM.getUri().equalsIgnoreCase(ontoC.getUri()))
                pMin = GOProbMap.get(ontoM.getUri());
            else
                pMin = this.checkParents(ontoM, ontoC, GOProbMap);
            if (pMin < 1) {
                score = this.getMetric(metric, pMin, probM, probC);
                if (score > checkScore)
                    checkScore = score;
            }
        }
    }
    GoMetric.log.info("score for " + queryGene + " and " + targetGene + " is " + checkScore);
    return checkScore;
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Example 9 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class GoMetric method computeSimpleOverlap.

/**
 * @param gene1         gene 1
 * @param gene2         gene 2
 * @param includePartOf include part of
 * @return number of overlapping terms
 */
private Double computeSimpleOverlap(Gene gene1, Gene gene2, boolean includePartOf) {
    if (!geneOntologyService.isReady())
        GoMetric.log.error("computeSimpleOverlap called before geneOntologyService is ready!!!");
    Collection<OntologyTerm> masterGO = geneOntologyService.getGOTerms(gene1, includePartOf, null);
    Collection<OntologyTerm> coExpGO = geneOntologyService.getGOTerms(gene2, includePartOf, null);
    Collection<OntologyTerm> overlappingTerms = new HashSet<>();
    for (OntologyTerm o : masterGO) {
        if (coExpGO.contains(o) && !this.isRoot(o))
            overlappingTerms.add(o);
    }
    return (double) overlappingTerms.size();
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Example 10 with OntologyTerm

use of ubic.basecode.ontology.model.OntologyTerm in project Gemma by PavlidisLab.

the class GoMetric method computeSimilarity.

/**
 * @param metric     metric
 * @param GOProbMap  go prob map
 * @param queryGene  query gene
 * @param targetGene target gene
 * @return the overlap score between two genes
 */
public Double computeSimilarity(Gene queryGene, Gene targetGene, Map<String, Double> GOProbMap, Metric metric) {
    if (metric.equals(GoMetric.Metric.simple)) {
        return this.computeSimpleOverlap(queryGene, targetGene, partOf);
    }
    if (metric.equals(GoMetric.Metric.percent)) {
        return this.computePercentOverlap(queryGene, targetGene, partOf);
    }
    Collection<OntologyTerm> masterGO = this.getOntologyTerms(queryGene);
    if ((masterGO == null) || masterGO.isEmpty())
        return 0.0;
    Collection<OntologyTerm> coExpGO = this.getOntologyTerms(targetGene);
    if ((coExpGO == null) || coExpGO.isEmpty())
        return 0.0;
    double total = 0;
    int count = 0;
    for (OntologyTerm ontoM : masterGO) {
        if (this.isRoot(ontoM))
            continue;
        if (!GOProbMap.containsKey(ontoM.getUri()))
            continue;
        double probM = GOProbMap.get(ontoM.getUri());
        for (OntologyTerm ontoC : coExpGO) {
            if (this.isRoot(ontoC))
                continue;
            if (!GOProbMap.containsKey(ontoC.getUri()))
                continue;
            Double probC = GOProbMap.get(ontoC.getUri());
            Double pMin;
            Double score;
            if (ontoM.getUri().equalsIgnoreCase(ontoC.getUri()))
                pMin = GOProbMap.get(ontoM.getUri());
            else
                pMin = this.checkParents(ontoM, ontoC, GOProbMap);
            if (pMin < 1) {
                score = this.getMetric(metric, pMin, probM, probC);
                total += score;
                count++;
            }
        }
    }
    if (total > 0) {
        double avgScore = total / count;
        GoMetric.log.info("score for " + queryGene + " and " + targetGene + " is " + avgScore);
        return avgScore;
    }
    GoMetric.log.info("NO score for " + queryGene + " and " + targetGene);
    return 0.0;
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Aggregations

OntologyTerm (ubic.basecode.ontology.model.OntologyTerm)73 Test (org.junit.Test)13 VocabCharacteristic (ubic.gemma.model.common.description.VocabCharacteristic)13 Gene (ubic.gemma.model.genome.Gene)11 OntologyResource (ubic.basecode.ontology.model.OntologyResource)8 HashSet (java.util.HashSet)6 StopWatch (org.apache.commons.lang3.time.StopWatch)6 CharacteristicValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject)6 BufferedReader (java.io.BufferedReader)3 ConcurrentHashSet (org.compass.core.util.concurrent.ConcurrentHashSet)3 Element (org.w3c.dom.Element)3 OntologyIndividual (ubic.basecode.ontology.model.OntologyIndividual)3 EntityNotFoundException (ubic.gemma.core.association.phenotype.PhenotypeExceptions.EntityNotFoundException)3 Characteristic (ubic.gemma.model.common.description.Characteristic)3 GeneEvidenceValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.GeneEvidenceValueObject)3 Resource (com.hp.hpl.jena.rdf.model.Resource)2 FileReader (java.io.FileReader)2 SocketException (java.net.SocketException)2 Collection (java.util.Collection)2 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)2