Search in sources :

Example 71 with OntologyTerm

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

the class GeneSetSearchImpl method goTermToGeneSet.

/**
 * Convert a GO term to a 'GeneSet', including genes from all child terms. Divide up by taxon.
 */
private GeneSet goTermToGeneSet(OntologyResource term, Taxon taxon, Integer maxGeneSetSize) {
    assert taxon != null;
    if (term == null)
        return null;
    if (term.getUri() == null)
        return null;
    Collection<OntologyResource> allMatches = new HashSet<>();
    allMatches.add(term);
    assert term instanceof OntologyTerm;
    allMatches.addAll(this.geneOntologyService.getAllChildren((OntologyTerm) term));
    GeneSetSearchImpl.log.info(term);
    /*
         * Gather up uris
         */
    Collection<String> termsToFetch = new HashSet<>();
    for (OntologyResource t : allMatches) {
        String goId = this.uri2goid(t);
        termsToFetch.add(goId);
    }
    Collection<Gene> genes = this.gene2GoService.findByGOTerms(termsToFetch, taxon);
    if (genes.isEmpty() || (maxGeneSetSize != null && genes.size() > maxGeneSetSize)) {
        return null;
    }
    GeneSet transientGeneSet = GeneSet.Factory.newInstance();
    transientGeneSet.setName(this.uri2goid(term));
    if (term.getLabel() == null) {
        GeneSetSearchImpl.log.warn(" Label for term " + term.getUri() + " was null");
    }
    // noinspection StatementWithEmptyBody // FIXME this is an individual or a 'resource', not a 'class', but it's a real GO term. How to get the text.
    if (term.getLabel() != null && term.getLabel().toUpperCase().startsWith("GO_")) {
    }
    transientGeneSet.setDescription(term.getLabel());
    for (Gene gene : genes) {
        GeneSetMember gmember = GeneSetMember.Factory.newInstance();
        gmember.setGene(gene);
        transientGeneSet.getMembers().add(gmember);
    }
    return transientGeneSet;
}
Also used : Gene(ubic.gemma.model.genome.Gene) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) GeneSetMember(ubic.gemma.model.genome.gene.GeneSetMember) GeneSet(ubic.gemma.model.genome.gene.GeneSet) OntologyResource(ubic.basecode.ontology.model.OntologyResource) HashSet(java.util.HashSet)

Example 72 with OntologyTerm

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

the class PhenotypeAssociationDaoImpl method findGenesForPhenotype.

@Override
public Map<GeneValueObject, OntologyTerm> findGenesForPhenotype(OntologyTerm term, Long taxon, boolean includeIEA) {
    Collection<OntologyTerm> children = term.getChildren(false);
    Map<String, OntologyTerm> uris = new HashMap<>();
    uris.put(term.getUri(), term);
    for (OntologyTerm c : children) {
        uris.put(c.getUri(), c);
    }
    assert !uris.isEmpty();
    Session sess = this.getSessionFactory().getCurrentSession();
    String q = "select distinct ph.gene, p.valueUri, p.evidenceCode " + " from PhenotypeAssociation ph join ph.phenotypes p where p.valueUri in (:t)";
    Query query = sess.createQuery(q);
    Map<GeneValueObject, OntologyTerm> result = new HashMap<>();
    query.setParameterList("t", uris.keySet());
    List<?> list = query.list();
    for (Object o : list) {
        Object[] oa = (Object[]) o;
        Gene g = (Gene) oa[0];
        if (!taxon.equals(g.getTaxon().getId()))
            continue;
        String uri = (String) oa[1];
        GOEvidenceCode ev = (GOEvidenceCode) oa[2];
        if (!includeIEA && ev != null && ev.equals(GOEvidenceCode.IEA)) {
            continue;
        }
        GeneValueObject gvo = new GeneValueObject(g);
        OntologyTerm otForUri = uris.get(uri);
        assert otForUri != null;
        /*
             * only clobber if this term is more specific
             */
        if (result.containsKey(gvo) && otForUri.getParents(false).contains(otForUri)) {
            continue;
        }
        result.put(gvo, otForUri);
    }
    return result;
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) Gene(ubic.gemma.model.genome.Gene) GOEvidenceCode(ubic.gemma.model.association.GOEvidenceCode) ExternalDatabaseStatisticsValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.ExternalDatabaseStatisticsValueObject) GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) PhenotypeValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.PhenotypeValueObject) GeneEvidenceValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.GeneEvidenceValueObject) CharacteristicValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject)

Example 73 with OntologyTerm

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

the class Gene2GOdescriptionEndpoint method invokeInternal.

/**
 * Reads the given <code>requestElement</code>, and sends a the response back.
 *
 * @param requestElement the contents of the SOAP message as DOM elements
 * @param document a DOM document to be used for constructing <code>Node</code>s
 * @return the response element
 */
@Override
protected Element invokeInternal(Element requestElement, Document document) {
    StopWatch watch = new StopWatch();
    watch.start();
    setLocalName(LOCAL_NAME);
    Collection<String> geneInput = getArrayValues(requestElement, "gene_id");
    // String geneString = geneInput.iterator().next();
    log.info("XML input read: " + geneInput.size() + " gene ids ");
    Element responseWrapper = document.createElementNS(NAMESPACE_URI, LOCAL_NAME);
    Element responseElement = document.createElementNS(NAMESPACE_URI, LOCAL_NAME + RESPONSE);
    responseWrapper.appendChild(responseElement);
    Collection<Long> geneIds = parseLongCollection(geneInput);
    Collection<Gene> geneCol = geneService.load(geneIds);
    if (geneCol == null || geneCol.isEmpty()) {
        String msg = "No gene with the given ids can be found.";
        return buildBadResponse(document, msg);
    }
    for (Gene gene : geneCol) {
        Collection<OntologyTerm> terms = geneOntologyService.getGOTerms(gene);
        // get the labels for the gene and store them
        if (terms != null) {
            for (OntologyTerm ot : terms) {
                // gene id output
                Element e1 = document.createElement("gene_id");
                e1.appendChild(document.createTextNode(gene.getId().toString()));
                responseElement.appendChild(e1);
                String goTerm = GeneOntologyServiceImpl.asRegularGoId(ot);
                Element e2 = document.createElement("go_id");
                e2.appendChild(document.createTextNode(goTerm));
                responseElement.appendChild(e2);
                Element e3 = document.createElement("go_description");
                e3.appendChild(document.createTextNode(ot.getLabel()));
                responseElement.appendChild(e3);
            }
        } else {
            // gene id output
            Element e1 = document.createElement("gene_id");
            e1.appendChild(document.createTextNode(gene.getId().toString()));
            responseElement.appendChild(e1);
            Element e2 = document.createElement("go_id");
            e2.appendChild(document.createTextNode("NaN"));
            responseElement.appendChild(e2);
            Element e3 = document.createElement("go_description");
            e3.appendChild(document.createTextNode("NaN"));
            responseElement.appendChild(e3);
        }
    }
    watch.stop();
    Long time = watch.getTime();
    log.info("XML response for GO Term results built in " + time + "ms.");
    return responseWrapper;
}
Also used : Gene(ubic.gemma.model.genome.Gene) Element(org.w3c.dom.Element) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) StopWatch(org.apache.commons.lang3.time.StopWatch)

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