Search in sources :

Example 1 with OntologyTerm

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

the class Gene2GoTermEndpoint 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(GENE2GO_LOCAL_NAME);
    Collection<String> geneResult = getArrayValues(requestElement, "gene_ids");
    log.info("XML input read: " + geneResult.size() + " gene ids");
    // start building the wrapper
    // build xml manually for mapped result rather than use buildWrapper inherited from AbstractGemmeEndpoint
    // log.info( "Building " + GENE2GO_LOCAL_NAME + " XML response" );
    String elementName1 = "gene_id";
    String elementName2 = "goIdList";
    Element responseWrapper = document.createElementNS(NAMESPACE_URI, GENE2GO_LOCAL_NAME);
    Element responseElement = document.createElementNS(NAMESPACE_URI, GENE2GO_LOCAL_NAME + RESPONSE);
    responseWrapper.appendChild(responseElement);
    for (String geneString : geneResult) {
        Long geneId = Long.parseLong(geneString);
        Gene gene = geneService.load(geneId);
        if (gene == null) {
            String msg = "No gene with ids, " + geneId + " can be found.";
            return buildBadResponse(document, msg);
        }
        Collection<OntologyTerm> terms = geneOntologyService.getGOTerms(gene);
        // get the labels and store them
        Collection<String> goTerms = new HashSet<String>();
        if (terms != null) {
            for (OntologyTerm ot : terms) {
                goTerms.add(GeneOntologyServiceImpl.asRegularGoId(ot));
            }
        } else
            goTerms.add("NaN");
        String elementString1 = geneId.toString();
        String elementString2 = encode(retainNumericIds(goTerms).toArray());
        Element e1 = document.createElement(elementName1);
        e1.appendChild(document.createTextNode(elementString1));
        responseElement.appendChild(e1);
        Element e2 = document.createElement(elementName2);
        e2.appendChild(document.createTextNode(elementString2));
        responseElement.appendChild(e2);
    }
    watch.stop();
    Long time = watch.getTime();
    // log.info( "Finished generating result. Sending response to client." );
    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) HashSet(java.util.HashSet)

Example 2 with OntologyTerm

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

the class GeneSetSearchImpl method findByGoTermName.

@Override
public Collection<GeneSet> findByGoTermName(String goTermName, Taxon taxon, Integer maxGoTermsProcessed, Integer maxGeneSetSize) {
    Collection<? extends OntologyResource> matches = this.geneOntologyService.findTerm(StringUtils.strip(goTermName));
    Collection<GeneSet> results = new HashSet<>();
    for (OntologyResource t : matches) {
        assert t instanceof OntologyTerm;
        if (taxon == null) {
            Collection<GeneSet> sets = this.goTermToGeneSets((OntologyTerm) t, maxGeneSetSize);
            results.addAll(sets);
            // noinspection StatementWithEmptyBody // FIXME should we count each species as one go?
            if (maxGoTermsProcessed != null && results.size() > maxGoTermsProcessed) {
            // return results;
            }
        } else {
            GeneSet converted = this.goTermToGeneSet(t, taxon, maxGeneSetSize);
            // converted will be null if its size is more than maxGeneSetSize
            if (converted != null) {
                results.add(converted);
            }
        }
        if (maxGoTermsProcessed != null && results.size() > maxGoTermsProcessed) {
            return results;
        }
    }
    return results;
}
Also used : OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) GeneSet(ubic.gemma.model.genome.gene.GeneSet) OntologyResource(ubic.basecode.ontology.model.OntologyResource) HashSet(java.util.HashSet)

Example 3 with OntologyTerm

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

the class SearchServiceImpl method databaseCharacteristicExactUriSearchForOwners.

/**
 * Takes a list of ontology terms, and classes of objects of interest to be returned. Looks through the
 * characteristic table for an exact match with the given ontology terms. Only tries to match the uri's.
 *
 * @param classes Class of objects to restrict the search to (typically ExpressionExperiment.class, for
 *                example).
 * @param terms   A list of ontology terms to search for
 * @return Collection of search results for the objects owning the found characteristics, where the owner is of
 * class clazz
 */
private Collection<SearchResult> databaseCharacteristicExactUriSearchForOwners(Collection<Class<?>> classes, Collection<OntologyTerm> terms) {
    // Collection<Characteristic> characteristicValueMatches = new ArrayList<Characteristic>();
    Collection<Characteristic> characteristicURIMatches = new ArrayList<>();
    for (OntologyTerm term : terms) {
        // characteristicValueMatches.addAll( characteristicService.findByValue( term.getUri() ));
        characteristicURIMatches.addAll(characteristicService.findByUri(classes, term.getUri()));
    }
    Map<Characteristic, Object> parentMap = characteristicService.getParents(classes, characteristicURIMatches);
    return this.filterCharacteristicOwnersByClass(classes, parentMap);
}
Also used : Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) BibliographicReferenceValueObject(ubic.gemma.model.common.description.BibliographicReferenceValueObject) SearchSettingsValueObject(ubic.gemma.model.common.search.SearchSettingsValueObject) BioSequenceValueObject(ubic.gemma.model.genome.sequenceAnalysis.BioSequenceValueObject) GeneEvidenceValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.GeneEvidenceValueObject) CharacteristicValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm)

Example 4 with OntologyTerm

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

the class SearchServiceImpl method ontologyUriSearch.

/**
 * @return results, if the settings.termUri is populated. This includes gene uris.
 */
private Map<Class<?>, List<SearchResult>> ontologyUriSearch(SearchSettings settings) {
    Map<Class<?>, List<SearchResult>> results = new HashMap<>();
    // 1st check to see if the query is a URI (from an ontology).
    // Do this by seeing if we can find it in the loaded ontologies.
    // Escape with general utilities because might not be doing a lucene backed search. (just a hibernate one).
    String termUri = settings.getTermUri();
    if (StringUtils.isBlank(termUri)) {
        termUri = settings.getQuery();
    }
    if (!termUri.startsWith("http://")) {
        return results;
    }
    OntologyTerm matchingTerm;
    String uriString;
    uriString = StringEscapeUtils.escapeJava(StringUtils.strip(termUri));
    if (StringUtils.containsIgnoreCase(uriString, SearchServiceImpl.NCBI_GENE)) {
        // Perhaps is a valid gene URL. Want to search for the gene in gemma.
        // 1st get objects tagged with the given gene identifier
        Collection<Class<?>> classesToFilterOn = new HashSet<>();
        classesToFilterOn.add(ExpressionExperiment.class);
        Collection<Characteristic> foundCharacteristics = characteristicService.findByUri(classesToFilterOn, uriString);
        Map<Characteristic, Object> parentMap = characteristicService.getParents(classesToFilterOn, foundCharacteristics);
        Collection<SearchResult> characteristicOwnerResults = this.filterCharacteristicOwnersByClass(classesToFilterOn, parentMap);
        if (!characteristicOwnerResults.isEmpty()) {
            results.put(ExpressionExperiment.class, new ArrayList<SearchResult>());
            results.get(ExpressionExperiment.class).addAll(characteristicOwnerResults);
        }
        if (settings.getSearchGenes()) {
            // Get the gene
            String ncbiAccessionFromUri = StringUtils.substringAfterLast(uriString, "/");
            Gene g = null;
            try {
                g = geneService.findByNCBIId(Integer.parseInt(ncbiAccessionFromUri));
            } catch (NumberFormatException e) {
            // ok
            }
            if (g != null) {
                results.put(Gene.class, new ArrayList<SearchResult>());
                results.get(Gene.class).add(new SearchResult(g));
            }
        }
        return results;
    }
    /*
         * Not searching for a gene.
         */
    Collection<SearchResult> matchingResults;
    Collection<Class<?>> classesToSearch = new HashSet<>();
    if (settings.getSearchExperiments()) {
        // not sure ...
        classesToSearch.add(ExpressionExperiment.class);
        classesToSearch.add(BioMaterial.class);
        classesToSearch.add(FactorValue.class);
    }
    // this doesn't seem to be implemented yet, LiteratureEvidence and GenericEvidence aren't handled in the
    // fillValueObjects method downstream
    /*
         * if ( settings.getSearchPhenotypes() ) { classesToSearch.add( PhenotypeAssociation.class ); }
         */
    matchingTerm = this.ontologyService.getTerm(uriString);
    if (matchingTerm == null || matchingTerm.getUri() == null) {
        /*
             * Maybe the ontology isn't loaded. Look anyway.
             */
        Map<Characteristic, Object> parentMap = characteristicService.getParents(classesToSearch, characteristicService.findByUri(classesToSearch, uriString));
        matchingResults = this.filterCharacteristicOwnersByClass(classesToSearch, parentMap);
    } else {
        SearchServiceImpl.log.info("Found ontology term: " + matchingTerm);
        // Was a URI from a loaded ontology soo get the children.
        Collection<OntologyTerm> terms2Search4 = matchingTerm.getChildren(true);
        terms2Search4.add(matchingTerm);
        matchingResults = this.databaseCharacteristicExactUriSearchForOwners(classesToSearch, terms2Search4);
    }
    for (SearchResult searchR : matchingResults) {
        if (results.containsKey(searchR.getResultClass())) {
            results.get(searchR.getResultClass()).add(searchR);
        } else {
            List<SearchResult> rs = new ArrayList<>();
            rs.add(searchR);
            results.put(searchR.getResultClass(), rs);
        }
    }
    return results;
}
Also used : Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) OntologyTerm(ubic.basecode.ontology.model.OntologyTerm) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) Gene(ubic.gemma.model.genome.Gene) BibliographicReferenceValueObject(ubic.gemma.model.common.description.BibliographicReferenceValueObject) SearchSettingsValueObject(ubic.gemma.model.common.search.SearchSettingsValueObject) BioSequenceValueObject(ubic.gemma.model.genome.sequenceAnalysis.BioSequenceValueObject) GeneEvidenceValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.GeneEvidenceValueObject) CharacteristicValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject)

Example 5 with OntologyTerm

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

the class SearchServiceImpl method characteristicSearchTerm.

/**
 * Perform a search on a query - it does not have to be one word, it could be "parkinson's disease"
 */
private Collection<SearchResult> characteristicSearchTerm(Collection<Class<?>> classes, String query) {
    if (SearchServiceImpl.log.isDebugEnabled())
        SearchServiceImpl.log.debug("Starting search for " + query);
    StopWatch watch = this.startTiming();
    Collection<Characteristic> cs = new HashSet<>();
    Collection<OntologyIndividual> individuals = ontologyService.findIndividuals(query);
    for (Collection<OntologyIndividual> individualbatch : BatchIterator.batches(individuals, 10)) {
        Collection<String> uris = new HashSet<>();
        for (OntologyIndividual individual : individualbatch) {
            uris.add(individual.getUri());
        }
        Collection<SearchResult> dbhits = this.dbHitsToSearchResult(characteristicService.findByUri(classes, uris), null);
        for (SearchResult crs : dbhits) {
            cs.add((Characteristic) crs.getResultObject());
        }
        if (cs.size() >= SearchServiceImpl.MAX_CHARACTERISTIC_SEARCH_RESULTS) {
            break;
        }
    }
    if (individuals.size() > 0 && watch.getTime() > 1000) {
        SearchServiceImpl.log.info("Found " + individuals.size() + " individuals matching '" + query + "' in " + watch.getTime() + "ms");
    }
    /*
         * Add characteristics that have values matching the query; this pulls in items not associated with ontology
         * terms (free text). We do this here so we can apply the query logic to the matches.
         */
    if (cs.size() < SearchServiceImpl.MAX_CHARACTERISTIC_SEARCH_RESULTS) {
        // note I changed the order of search operations so
        String dbQueryString = query.replaceAll("\\*", "");
        // this might not be wanted.
        Collection<Characteristic> valueMatches = characteristicService.findByValue(classes, dbQueryString);
        if (valueMatches != null && !valueMatches.isEmpty()) {
            cs.addAll(valueMatches);
            if (watch.getTime() > 1000) {
                SearchServiceImpl.log.info("Found " + valueMatches.size() + " characteristics matching value '" + query + "' in " + watch.getTime() + "ms");
            }
            watch.reset();
            watch.start();
        }
    }
    if (cs.size() < SearchServiceImpl.MAX_CHARACTERISTIC_SEARCH_RESULTS) {
        /*
             * Identify initial set of matches to the query.
             */
        Collection<OntologyTerm> matchingTerms = ontologyService.findTerms(query);
        if (watch.getTime() > 1000) {
            SearchServiceImpl.log.info("Found " + matchingTerms.size() + " ontology classes matching '" + query + "' in " + watch.getTime() + "ms");
        }
        /*
             * Search for child terms.
             */
        if (!matchingTerms.isEmpty()) {
            for (OntologyTerm term : matchingTerms) {
                /*
                     * In this loop, each term is a match directly to our query, and we do a depth-first fetch of the
                     * children.
                     */
                String uri = term.getUri();
                if (StringUtils.isBlank(uri))
                    continue;
                int sizeBefore = cs.size();
                this.getCharacteristicsAnnotatedToChildren(classes, term, cs);
                if (SearchServiceImpl.log.isDebugEnabled() && cs.size() > sizeBefore) {
                    SearchServiceImpl.log.debug((cs.size() - sizeBefore) + " characteristics matching children term of " + term);
                }
                if (cs.size() >= SearchServiceImpl.MAX_CHARACTERISTIC_SEARCH_RESULTS) {
                    break;
                }
            }
            if (watch.getTime() > 1000) {
                SearchServiceImpl.log.info("Found " + cs.size() + " characteristics for '" + query + "' including child terms in " + watch.getTime() + "ms");
            }
            watch.reset();
            watch.start();
        }
    }
    /*
         * Retrieve the owner objects
         */
    watch.reset();
    watch.start();
    Collection<SearchResult> matchingEntities = this.getAnnotatedEntities(classes, cs);
    if (watch.getTime() > 1000) {
        SearchServiceImpl.log.info("Retrieved " + matchingEntities.size() + " entities via characteristics for '" + query + "' in " + watch.getTime() + "ms");
    }
    if (SearchServiceImpl.log.isDebugEnabled())
        SearchServiceImpl.log.debug("End search for " + query);
    return matchingEntities;
}
Also used : Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) OntologyIndividual(ubic.basecode.ontology.model.OntologyIndividual) 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