Search in sources :

Example 1 with Characteristic

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

the class ExperimentAnnotationEndpoint 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> eeResult = getArrayValues(requestElement, "ee_ids");
    log.info("XML Input read: " + eeResult.size() + " expression experiment(s)");
    // start building the wrapper
    // build xml manually for mapped result rather than use buildWrapper inherited from AbstractGemmeEndpoint
    Element responseWrapper = document.createElementNS(NAMESPACE_URI, LOCAL_NAME);
    Element responseElement = document.createElementNS(NAMESPACE_URI, LOCAL_NAME + RESPONSE);
    responseWrapper.appendChild(responseElement);
    Long eeId = null;
    for (String eeString : eeResult) {
        eeId = Long.parseLong(eeString);
        ExpressionExperiment ee = expressionExperimentService.load(eeId);
        if (ee == null) {
            String msg = "No expression experiment with id, " + eeId + ", can be found.";
            return buildBadResponse(document, msg);
        }
        ee = expressionExperimentService.thawLite(ee);
        Collection<Characteristic> characterCol = ee.getCharacteristics();
        for (Characteristic character : characterCol) {
            String elementString1 = eeId.toString();
            String elementString2 = character.getValue();
            String elementString4 = character.getEvidenceCode().getValue();
            String elementString3 = character.getCategory();
            Element e1 = document.createElement("ee_id");
            e1.appendChild(document.createTextNode(elementString1));
            responseElement.appendChild(e1);
            Element e2 = document.createElement("Category");
            e2.appendChild(document.createTextNode(elementString3));
            responseElement.appendChild(e2);
            Element e3 = document.createElement("Terms");
            e3.appendChild(document.createTextNode(elementString2));
            responseElement.appendChild(e3);
            Element e4 = document.createElement("EvidenceCode");
            e4.appendChild(document.createTextNode(elementString4));
            responseElement.appendChild(e4);
        }
    }
    watch.stop();
    Long time = watch.getTime();
    // log.info( "Finished generating result. Sending response to client." );
    log.info("XML response for Experiment Annotation result built in " + time + "ms.");
    return responseWrapper;
}
Also used : Element(org.w3c.dom.Element) Characteristic(ubic.gemma.model.common.description.Characteristic) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 2 with Characteristic

use of ubic.gemma.model.common.description.Characteristic 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 3 with Characteristic

use of ubic.gemma.model.common.description.Characteristic 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 4 with Characteristic

use of ubic.gemma.model.common.description.Characteristic 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)

Example 5 with Characteristic

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

the class SearchServiceImpl method filterCharacteristicOwnersByClass.

private Collection<SearchResult> filterCharacteristicOwnersByClass(Collection<Class<?>> classes, Map<Characteristic, Object> characteristic2entity) {
    Collection<BioMaterial> biomaterials = new HashSet<>();
    Collection<FactorValue> factorValues = new HashSet<>();
    Collection<SearchResult> results = new HashSet<>();
    for (Characteristic c : characteristic2entity.keySet()) {
        Object o = characteristic2entity.get(c);
        for (Class<?> clazz : classes) {
            if (clazz.isAssignableFrom(o.getClass())) {
                String matchedText = c.getValue();
                if (o instanceof BioMaterial) {
                    biomaterials.add((BioMaterial) o);
                } else if (o instanceof FactorValue) {
                    factorValues.add((FactorValue) o);
                } else {
                    if (c instanceof VocabCharacteristic && c.getValueUri() != null) {
                        matchedText = "Ontology term: <a href=\"" + Settings.getRootContext() + "/searcher.html?query=" + c.getValueUri() + "\">" + matchedText + "</a>";
                    }
                    results.add(new SearchResult(o, 1.0, matchedText));
                }
            }
        }
    }
    this.addEEeByFactorvalues(results, factorValues);
    if (biomaterials.size() > 0) {
        Collection<ExpressionExperiment> ees = expressionExperimentService.findByBioMaterials(biomaterials);
        for (ExpressionExperiment ee : ees) {
            results.add(new SearchResult(ee, SearchServiceImpl.INDIRECT_DB_HIT_PENALTY, "BioMaterial characteristic"));
        }
    }
    return results;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) FactorValue(ubic.gemma.model.expression.experiment.FactorValue) Characteristic(ubic.gemma.model.common.description.Characteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) VocabCharacteristic(ubic.gemma.model.common.description.VocabCharacteristic) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) 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)

Aggregations

Characteristic (ubic.gemma.model.common.description.Characteristic)46 VocabCharacteristic (ubic.gemma.model.common.description.VocabCharacteristic)32 StopWatch (org.apache.commons.lang3.time.StopWatch)7 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)7 CharacteristicValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject)7 AnnotationValueObject (ubic.gemma.model.common.description.AnnotationValueObject)6 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)6 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)6 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 SearchSettingsValueObject (ubic.gemma.model.common.search.SearchSettingsValueObject)4 BioSequenceValueObject (ubic.gemma.model.genome.sequenceAnalysis.BioSequenceValueObject)4 OntologyTerm (ubic.basecode.ontology.model.OntologyTerm)3 BibliographicReferenceValueObject (ubic.gemma.model.common.description.BibliographicReferenceValueObject)3 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)3 GeneEvidenceValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.GeneEvidenceValueObject)3 ConcurrentHashSet (org.compass.core.util.concurrent.ConcurrentHashSet)2 TaskResult (ubic.gemma.core.job.TaskResult)2 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)2 Treatment (ubic.gemma.model.expression.biomaterial.Treatment)2