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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations