use of ubic.basecode.ontology.model.OntologyIndividual 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;
}
use of ubic.basecode.ontology.model.OntologyIndividual in project Gemma by PavlidisLab.
the class OntologyServiceImpl method termsToCharacteristics.
/**
* Convert raw ontology resources into VocabCharacteristics.
*/
@Override
public Collection<VocabCharacteristic> termsToCharacteristics(final Collection<? extends OntologyResource> terms) {
Collection<VocabCharacteristic> results = new HashSet<>();
if ((terms == null) || (terms.isEmpty()))
return results;
for (OntologyResource res : terms) {
if (res == null)
continue;
VocabCharacteristic vc = VocabCharacteristic.Factory.newInstance();
if (res instanceof OntologyTerm) {
OntologyTerm term = (OntologyTerm) res;
vc.setValue(term.getTerm());
vc.setValueUri(term.getUri());
vc.setDescription(term.getComment());
} else if (res instanceof OntologyIndividual) {
OntologyIndividual indi = (OntologyIndividual) res;
vc.setValue(indi.getLabel());
vc.setValueUri(indi.getUri());
vc.setDescription("Individual");
} else {
OntologyServiceImpl.log.warn("What is it? " + res);
continue;
}
if (vc.getValue() == null)
continue;
results.add(vc);
}
OntologyServiceImpl.log.debug("returning " + results.size() + " terms after filter");
return results;
}
use of ubic.basecode.ontology.model.OntologyIndividual in project Gemma by PavlidisLab.
the class OntologyServiceImpl method convert.
/**
* Given a collection of ontology terms converts them to a collection of VocabCharacteristics
*/
private Collection<VocabCharacteristic> convert(final Collection<OntologyResource> resources) {
Collection<VocabCharacteristic> converted = new HashSet<>();
if ((resources == null) || (resources.isEmpty()))
return converted;
for (OntologyResource res : resources) {
VocabCharacteristic vc = VocabCharacteristic.Factory.newInstance();
// If there is no URI we don't want to send it back (ie useless)
if ((res.getUri() == null) || StringUtils.isEmpty(res.getUri()))
continue;
if (res instanceof OntologyTerm) {
OntologyTerm term = (OntologyTerm) res;
vc.setValue(term.getTerm());
vc.setValueUri(term.getUri());
vc.setDescription(term.getComment());
}
if (res instanceof OntologyIndividual) {
OntologyIndividual indi = (OntologyIndividual) res;
vc.setValue(indi.getLabel());
vc.setValueUri(indi.getUri());
vc.setDescription("Individual");
}
converted.add(vc);
}
return converted;
}
Aggregations