use of ubic.basecode.ontology.model.OntologyResource in project Gemma by PavlidisLab.
the class GeneSetSearchImpl method goTermToGeneSets.
private Collection<GeneSet> goTermToGeneSets(OntologyTerm term, Integer maxGeneSetSize) {
if (term == null)
return null;
if (term.getUri() == null)
return null;
Collection<OntologyResource> allMatches = new HashSet<>();
allMatches.add(term);
allMatches.addAll(this.geneOntologyService.getAllChildren(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);
}
Map<Taxon, Collection<Gene>> genesByTaxon = this.gene2GoService.findByGOTermsPerTaxon(termsToFetch);
Collection<GeneSet> results = new HashSet<>();
for (Taxon t : genesByTaxon.keySet()) {
Collection<Gene> genes = genesByTaxon.get(t);
if (genes.isEmpty() || (maxGeneSetSize != null && genes.size() > maxGeneSetSize)) {
continue;
}
GeneSet transientGeneSet = GeneSet.Factory.newInstance();
transientGeneSet.setName(this.uri2goid(term));
transientGeneSet.setDescription(term.getLabel());
for (Gene gene : genes) {
GeneSetMember gmember = GeneSetMember.Factory.newInstance();
gmember.setGene(gene);
transientGeneSet.getMembers().add(gmember);
}
results.add(transientGeneSet);
}
return results;
}
use of ubic.basecode.ontology.model.OntologyResource 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.OntologyResource in project Gemma by PavlidisLab.
the class GeneOntologyServiceImpl method addTerms.
private void addTerms(Collection<OntologyResource> newTerms) {
for (OntologyResource term : newTerms) {
if (term.getUri() == null)
continue;
if (term instanceof OntologyTerm) {
OntologyTerm ontTerm = (OntologyTerm) term;
GeneOntologyServiceImpl.uri2Term.put(term.getUri(), ontTerm);
for (String alternativeID : ontTerm.getAlternativeIds()) {
GeneOntologyServiceImpl.log.debug(GeneOntologyServiceImpl.toUri(alternativeID));
GeneOntologyServiceImpl.uri2Term.put(GeneOntologyServiceImpl.toUri(alternativeID), ontTerm);
}
}
}
}
use of ubic.basecode.ontology.model.OntologyResource in project Gemma by PavlidisLab.
the class GeneOntologyServiceImpl method findTerm.
@Override
public Collection<OntologyTerm> findTerm(String queryString) {
if (!this.isReady())
return new HashSet<>();
if (GeneOntologyServiceImpl.log.isDebugEnabled())
GeneOntologyServiceImpl.log.debug("Searching Gene Ontology for '" + queryString + "'");
// make sure we are all-inclusive
queryString = queryString.trim();
queryString = queryString.replaceAll("\\s+", " AND ");
StopWatch timer = new StopWatch();
timer.start();
Collection<OntologyResource> rawMatches = new HashSet<>();
for (SearchIndex index : this.indices) {
rawMatches.addAll(OntologySearch.matchIndividuals(model, index, queryString));
}
if (timer.getTime() > 100) {
GeneOntologyServiceImpl.log.info("Find " + rawMatches.size() + " raw go terms from " + queryString + ": " + timer.getTime() + " ms");
}
timer.reset();
timer.start();
/*
* Required to make sure the descriptions are filled in.
*/
Collection<OntologyTerm> matches = new HashSet<>();
for (OntologyResource r : rawMatches) {
if (StringUtils.isBlank(r.getUri()))
continue;
OntologyTerm termForURI = GeneOntologyServiceImpl.getTermForURI(r.getUri());
if (termForURI == null) {
GeneOntologyServiceImpl.log.warn("No term for : " + r);
continue;
}
matches.add(termForURI);
}
if (timer.getTime() > 100) {
GeneOntologyServiceImpl.log.info("Convert " + rawMatches.size() + " raw go terms to terms: " + timer.getTime() + " ms");
}
return matches;
}
use of ubic.basecode.ontology.model.OntologyResource 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;
}
Aggregations