Search in sources :

Example 1 with SearchResultDisplayObject

use of ubic.gemma.core.search.SearchResultDisplayObject in project Gemma by PavlidisLab.

the class GeneSearchServiceImpl method searchGenesAndGeneGroups.

@Override
public Collection<SearchResultDisplayObject> searchGenesAndGeneGroups(String query, Long taxonId) {
    Taxon taxon = null;
    String taxonName = "";
    if (taxonId != null) {
        taxon = taxonService.load(taxonId);
        if (taxon == null) {
            GeneSearchServiceImpl.log.warn("No such taxon with id=" + taxonId);
        } else {
            taxonName = taxon.getCommonName();
        }
    }
    List<SearchResultDisplayObject> displayResults = new ArrayList<>();
    // session-bound sets
    if (StringUtils.isBlank(query)) {
        return this.searchGenesAndGeneGroupsBlankQuery(taxonId);
    }
    Integer maxGeneralSearchResults = 500;
    /*
         * GET GENES AND GENE SETS
         */
    // SearchSettings settings = SearchSettings.geneSearch( query, taxon );
    SearchSettings settings = SearchSettings.Factory.newInstance();
    settings.setQuery(query);
    settings.noSearches();
    // add searching for genes
    settings.setSearchGenes(true);
    // add searching for geneSets
    settings.setSearchGeneSets(true);
    settings.setMaxResults(maxGeneralSearchResults);
    if (taxon != null)
        // this doesn't work yet
        settings.setTaxon(taxon);
    GeneSearchServiceImpl.log.debug("getting results from searchService for " + query);
    Map<Class<?>, List<SearchResult>> results = searchService.speedSearch(settings);
    List<SearchResult> geneSetSearchResults = new ArrayList<>();
    List<SearchResult> geneSearchResults = new ArrayList<>();
    boolean exactGeneSymbolMatch = false;
    if (!results.isEmpty()) {
        if (results.get(GeneSet.class) != null) {
            geneSetSearchResults.addAll(results.get(GeneSet.class));
        }
        if (results.get(Gene.class) != null) {
            geneSearchResults.addAll(results.get(Gene.class));
        }
        // Check to see if we have an exact match, if so, return earlier abstaining from doing other searches
        for (SearchResult geneResult : results.get(Gene.class)) {
            Gene g = (Gene) geneResult.getResultObject();
            // aliases too?
            if (g != null && g.getOfficialSymbol() != null && g.getOfficialSymbol().startsWith(query.trim())) {
                exactGeneSymbolMatch = true;
                break;
            }
        }
    }
    Collection<SearchResultDisplayObject> genes = new ArrayList<>();
    Collection<SearchResultDisplayObject> geneSets;
    Map<Long, Boolean> isSetOwnedByUser = new HashMap<>();
    if (taxon != null) {
        // filter search results by taxon
        List<SearchResult> taxonCheckedGenes = this.retainGenesOfThisTaxon(taxonId, geneSearchResults);
        // convert result object to a value object to a SearchResultDisplayObject
        for (SearchResult sr : taxonCheckedGenes) {
            genes.add(new SearchResultDisplayObject(sr));
        }
        List<SearchResult> taxonCheckedSets = this.retainGeneSetsOfThisTaxon(taxonId, geneSetSearchResults, isSetOwnedByUser);
        // convert result object to a value object
        for (SearchResult sr : taxonCheckedSets) {
            GeneSet g = (GeneSet) sr.getResultObject();
            DatabaseBackedGeneSetValueObject gsVo = geneSetValueObjectHelper.convertToValueObject(g);
            sr.setResultObject(gsVo);
        }
        geneSets = SearchResultDisplayObject.convertSearchResults2SearchResultDisplayObjects(taxonCheckedSets);
        for (SearchResultDisplayObject srDo : geneSets) {
            // geneSets were filtered by taxon above:
            // if taxonId for geneSet != taxonId param, then gene set was already removed
            srDo.setTaxonId(taxonId);
            srDo.setTaxonName(taxonName);
        }
    } else {
        for (SearchResult sr : geneSearchResults) {
            genes.add(new SearchResultDisplayObject(sr));
        }
        geneSets = new ArrayList<>();
        SearchResultDisplayObject srDo;
        for (SearchResult sr : geneSetSearchResults) {
            GeneSet gs = (GeneSet) sr.getResultObject();
            isSetOwnedByUser.put(gs.getId(), securityService.isOwnedByCurrentUser(gs));
            taxon = geneSetService.getTaxon((GeneSet) sr.getResultObject());
            GeneSetValueObject gsVo = geneSetValueObjectHelper.convertToValueObject(gs);
            srDo = new SearchResultDisplayObject(gsVo);
            srDo.setTaxonId(taxon.getId());
            srDo.setTaxonName(taxon.getCommonName());
            geneSets.add(srDo);
        }
        taxon = null;
    }
    // if a geneSet is owned by the user, mark it as such (used for giving it a special background colour in
    // search results)
    this.setUserOwnedForGeneSets(geneSets, isSetOwnedByUser);
    if (exactGeneSymbolMatch) {
        // get summary results
        GeneSearchServiceImpl.log.info("getting Summary results for " + query);
        List<SearchResultDisplayObject> summaries = this.addEntryForAllResults(query, genes, geneSets, new ArrayList<SearchResultDisplayObject>(), new ArrayList<SearchResultDisplayObject>());
        displayResults.addAll(summaries);
        displayResults.addAll(genes);
        displayResults.addAll(geneSets);
        return displayResults;
    }
    List<SearchResultDisplayObject> srDos;
    // get GO group results
    GeneSearchServiceImpl.log.debug("Getting GO group results for " + query);
    srDos = this.getGOGroupResults(query, taxon);
    List<SearchResultDisplayObject> phenotypeSrDos = new ArrayList<>();
    if (!query.toUpperCase().startsWith("GO")) {
        GeneSearchServiceImpl.log.info("getting Phenotype Association results for " + query);
        phenotypeSrDos = this.getPhenotypeAssociationSearchResults(query, taxon);
    }
    // }
    // get summary results
    GeneSearchServiceImpl.log.debug("Getting Summary results for " + query);
    List<SearchResultDisplayObject> summaryEntries = this.addEntryForAllResults(query, genes, geneSets, srDos, phenotypeSrDos);
    // add all results, keeping order of result types
    displayResults.addAll(summaryEntries);
    displayResults.addAll(geneSets);
    displayResults.addAll(srDos);
    displayResults.addAll(phenotypeSrDos);
    displayResults.addAll(genes);
    if (displayResults.isEmpty()) {
        GeneSearchServiceImpl.log.info("No results for search: " + query + " taxon=" + ((taxon == null) ? null : taxon.getCommonName()));
        return new HashSet<>();
    }
    GeneSearchServiceImpl.log.info("Results for search: " + query + ", size=" + displayResults.size());
    return displayResults;
}
Also used : Gene(ubic.gemma.model.genome.Gene) SearchSettings(ubic.gemma.model.common.search.SearchSettings) Taxon(ubic.gemma.model.genome.Taxon) SearchResult(ubic.gemma.core.search.SearchResult) SearchResultDisplayObject(ubic.gemma.core.search.SearchResultDisplayObject)

Example 2 with SearchResultDisplayObject

use of ubic.gemma.core.search.SearchResultDisplayObject in project Gemma by PavlidisLab.

the class GeneSearchServiceImpl method searchGenesAndGeneGroupsBlankQuery.

/**
 * if query is blank, return list of public sets, user-owned sets (if logged in) and user's recent session-bound
 * sets called by ubic.gemma.web.controller.genome.gene.GenePickerController.searchGenesAndGeneGroups(String, Long)
 *
 * @param taxonId taxon id
 * @return Collection<SearchResultDisplayObject>
 */
private Collection<SearchResultDisplayObject> searchGenesAndGeneGroupsBlankQuery(Long taxonId) {
    Taxon taxon = null;
    if (taxonId != null) {
        taxon = taxonService.load(taxonId);
        if (taxon == null) {
            GeneSearchServiceImpl.log.warn("No such taxon with id=" + taxonId);
        }
    }
    // if query is blank, return list of auto generated sets, user-owned sets (if logged in) and user's recent
    // session-bound sets
    // right now, no public gene sets are useful so we don't want to prompt them
    StopWatch watch = new StopWatch();
    watch.start();
    // get all public sets
    // filtered by security.
    Collection<GeneSet> sets = new ArrayList<>();
    if (!SecurityUtil.isUserLoggedIn()) {
        try {
            sets = geneSetService.loadAll(taxon);
            if (watch.getTime() > 100)
                GeneSearchServiceImpl.log.info(sets.size() + " sets loaded for taxon =" + taxon + " took: " + watch.getTime() + "ms");
        } catch (AccessDeniedException e) {
        // okay, they just aren't allowed to see those.
        }
    } else if (SecurityUtil.isUserLoggedIn()) {
        /*
             * actually, loadMyGeneSets and loadAll point to the same method (they just use different spring security
             * filters)
             */
        sets = (taxon != null) ? geneSetService.loadMyGeneSets(taxon) : geneSetService.loadMyGeneSets();
        if (watch.getTime() > 100)
            GeneSearchServiceImpl.log.info("Loading the user's gene sets took: " + watch.getTime() + "ms");
    }
    if (sets.isEmpty()) {
        return new ArrayList<>();
    }
    // separate these out because they go at the top of the list
    List<SearchResultDisplayObject> displayResultsPrivate = new ArrayList<>();
    List<SearchResultDisplayObject> displayResultsPublic = new ArrayList<>();
    SearchResultDisplayObject newSrDo;
    List<DatabaseBackedGeneSetValueObject> valueObjects = geneSetValueObjectHelper.convertToValueObjects(sets, false);
    if (watch.getTime() > 500)
        GeneSearchServiceImpl.log.info("Database stage done: " + watch.getTime() + "ms");
    for (DatabaseBackedGeneSetValueObject set : valueObjects) {
        newSrDo = new SearchResultDisplayObject(set);
        newSrDo.setTaxonId(((GeneSetValueObject) newSrDo.getResultValueObject()).getTaxonId());
        newSrDo.setTaxonName(((GeneSetValueObject) newSrDo.getResultValueObject()).getTaxonName());
        newSrDo.setUserOwned(!set.getIsPublic());
        ((GeneSetValueObject) newSrDo.getResultValueObject()).setIsPublic(!newSrDo.isUserOwned());
        if (newSrDo.isUserOwned()) {
            displayResultsPrivate.add(newSrDo);
        } else {
            displayResultsPublic.add(newSrDo);
        }
    }
    // keep sets in proper order (user's groups first, then public ones)
    Collections.sort(displayResultsPrivate);
    Collections.sort(displayResultsPublic);
    List<SearchResultDisplayObject> displayResults = new ArrayList<>();
    displayResults.addAll(displayResultsPrivate);
    displayResults.addAll(displayResultsPublic);
    GeneSearchServiceImpl.log.info("Results for blank query: " + displayResults.size() + " items, " + watch.getTime() + "ms");
    return displayResults;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) Taxon(ubic.gemma.model.genome.Taxon) SearchResultDisplayObject(ubic.gemma.core.search.SearchResultDisplayObject) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 3 with SearchResultDisplayObject

use of ubic.gemma.core.search.SearchResultDisplayObject in project Gemma by PavlidisLab.

the class ExpressionExperimentSearchServiceImpl method searchExperimentsAndExperimentGroups.

@Override
public List<SearchResultDisplayObject> searchExperimentsAndExperimentGroups(String query, Long taxonId) {
    List<SearchResultDisplayObject> displayResults = new LinkedList<>();
    // session-bound sets (not autogen sets until handling of large searches is fixed)
    if (StringUtils.isBlank(query)) {
        return this.searchExperimentsAndExperimentGroupBlankQuery(taxonId);
    }
    Map<Class<?>, List<SearchResult>> results = this.initialSearch(query, taxonId);
    List<SearchResultDisplayObject> experimentSets = this.getExpressionExperimentSetResults(results);
    List<SearchResultDisplayObject> experiments = this.getExpressionExperimentResults(results);
    if (experimentSets.isEmpty() && experiments.isEmpty()) {
        return displayResults;
    }
    /*
         * ALL RESULTS BY TAXON GROUPS
         */
    // if >1 result, add a group whose members are all experiments returned from search
    Map<Long, Set<Long>> eeIdsByTaxonId = new HashMap<>();
    // add every individual experiment to the set, grouped by taxon and also altogether.
    for (SearchResultDisplayObject srdo : experiments) {
        // group by the Parent Taxon, for things like salmonid - see bug 3286
        Long taxId;
        if (srdo.getParentTaxonId() != null) {
            taxId = srdo.getParentTaxonId();
        } else {
            taxId = srdo.getTaxonId();
        }
        if (!eeIdsByTaxonId.containsKey(taxId)) {
            eeIdsByTaxonId.put(taxId, new HashSet<Long>());
        }
        ExpressionExperimentValueObject eevo = (ExpressionExperimentValueObject) srdo.getResultValueObject();
        eeIdsByTaxonId.get(taxId).add(eevo.getId());
    }
    // for each group
    for (SearchResultDisplayObject eesSRO : experimentSets) {
        ExpressionExperimentSetValueObject set = (ExpressionExperimentSetValueObject) eesSRO.getResultValueObject();
        /*
             * This is security filtered.
             */
        Collection<Long> ids = EntityUtils.getIds(expressionExperimentSetService.getExperimentValueObjectsInSet(set.getId()));
        // to account for security filtering.
        set.setSize(ids.size());
        if (!eeIdsByTaxonId.containsKey(set.getTaxonId())) {
            eeIdsByTaxonId.put(set.getTaxonId(), new HashSet<Long>());
        }
        eeIdsByTaxonId.get(set.getTaxonId()).addAll(ids);
    }
    // make an entry for each taxon
    Long taxonId2;
    for (Map.Entry<Long, Set<Long>> entry : eeIdsByTaxonId.entrySet()) {
        taxonId2 = entry.getKey();
        Taxon taxon = taxonService.load(taxonId2);
        if (taxon != null && entry.getValue().size() > 0) {
            FreeTextExpressionExperimentResultsValueObject ftvo = new FreeTextExpressionExperimentResultsValueObject("All " + taxon.getCommonName() + " results for '" + query + "'", "All " + taxon.getCommonName() + " experiments found for your query", taxon.getId(), taxon.getCommonName(), entry.getValue(), query);
            int numWithDifferentialExpressionAnalysis = differentialExpressionAnalysisService.getExperimentsWithAnalysis(entry.getValue()).size();
            assert numWithDifferentialExpressionAnalysis <= entry.getValue().size();
            int numWithCoexpressionAnalysis = coexpressionAnalysisService.getExperimentsWithAnalysis(entry.getValue()).size();
            ftvo.setNumWithCoexpressionAnalysis(numWithCoexpressionAnalysis);
            ftvo.setNumWithDifferentialExpressionAnalysis(numWithDifferentialExpressionAnalysis);
            displayResults.add(new SearchResultDisplayObject(ftvo));
        }
    }
    displayResults.addAll(experimentSets);
    displayResults.addAll(experiments);
    if (displayResults.isEmpty()) {
        ExpressionExperimentSearchServiceImpl.log.info("No results for search: " + query);
    } else {
        ExpressionExperimentSearchServiceImpl.log.info("Results for search: " + query + " size=" + displayResults.size() + " entry0: " + ((SearchResultDisplayObject) (displayResults.toArray())[0]).getName() + " valueObject:" + ((SearchResultDisplayObject) (displayResults.toArray())[0]).getResultValueObject().toString());
    }
    return displayResults;
}
Also used : ExpressionExperimentSet(ubic.gemma.model.analysis.expression.ExpressionExperimentSet) Taxon(ubic.gemma.model.genome.Taxon) FreeTextExpressionExperimentResultsValueObject(ubic.gemma.model.expression.experiment.FreeTextExpressionExperimentResultsValueObject) ExpressionExperimentSetValueObject(ubic.gemma.model.expression.experiment.ExpressionExperimentSetValueObject) ExpressionExperimentValueObject(ubic.gemma.model.expression.experiment.ExpressionExperimentValueObject) SearchResultDisplayObject(ubic.gemma.core.search.SearchResultDisplayObject)

Example 4 with SearchResultDisplayObject

use of ubic.gemma.core.search.SearchResultDisplayObject in project Gemma by PavlidisLab.

the class GenePickerController method searchGenesAndGeneGroups.

/**
 * AJAX (used by GeneAndGeneGroupCombo.js)
 *
 * @param query   query
 * @param taxonId can be null
 * @return Collection of SearchResultDisplayObject
 */
public Collection<SearchResultDisplayObject> searchGenesAndGeneGroups(String query, Long taxonId) {
    // get any session-bound groups
    Collection<SessionBoundGeneSetValueObject> sessionResult = (taxonId != null) ? sessionListManager.getModifiedGeneSets(taxonId) : sessionListManager.getModifiedGeneSets();
    List<SearchResultDisplayObject> sessionSets = new ArrayList<>();
    // create SearchResultDisplayObjects
    if (sessionResult != null && sessionResult.size() > 0) {
        for (SessionBoundGeneSetValueObject gvo : sessionResult) {
            SearchResultDisplayObject srDo = new SearchResultDisplayObject(gvo);
            srDo.setUserOwned(true);
            sessionSets.add(srDo);
        }
    }
    Collections.sort(sessionSets);
    // maintain order: session sets first
    Collection<SearchResultDisplayObject> results = new ArrayList<>();
    results.addAll(sessionSets);
    results.addAll(geneSearchService.searchGenesAndGeneGroups(query, taxonId));
    for (SearchResultDisplayObject r : results) {
        r.setOriginalQuery(query);
    }
    return results;
}
Also used : SearchResultDisplayObject(ubic.gemma.core.search.SearchResultDisplayObject) SessionBoundGeneSetValueObject(ubic.gemma.core.genome.gene.SessionBoundGeneSetValueObject)

Example 5 with SearchResultDisplayObject

use of ubic.gemma.core.search.SearchResultDisplayObject in project Gemma by PavlidisLab.

the class GeneSearchServiceImpl method getSearchResultForSessionBoundGroupValueObject.

private SearchResultDisplayObject getSearchResultForSessionBoundGroupValueObject(Taxon taxonForGS, SessionBoundGeneSetValueObject sbGsVo) {
    if (taxonForGS != null) {
        // GO groups don't seem to have there sbGsVo's taxon info populated
        sbGsVo.setTaxonId(taxonForGS.getId());
        sbGsVo.setTaxonName(taxonForGS.getCommonName());
    } else {
        sbGsVo.setTaxonId(sbGsVo.getTaxonId());
        sbGsVo.setTaxonName(sbGsVo.getTaxonName());
    }
    SearchResultDisplayObject sdo = new SearchResultDisplayObject(sbGsVo);
    sdo.setUserOwned(false);
    sdo.setTaxonId(sbGsVo.getTaxonId());
    sdo.setTaxonName(sbGsVo.getTaxonName());
    return sdo;
}
Also used : SearchResultDisplayObject(ubic.gemma.core.search.SearchResultDisplayObject)

Aggregations

SearchResultDisplayObject (ubic.gemma.core.search.SearchResultDisplayObject)13 Taxon (ubic.gemma.model.genome.Taxon)5 ExpressionExperimentSetValueObject (ubic.gemma.model.expression.experiment.ExpressionExperimentSetValueObject)3 StopWatch (org.apache.commons.lang3.time.StopWatch)2 SearchResult (ubic.gemma.core.search.SearchResult)2 ExpressionExperimentSet (ubic.gemma.model.analysis.expression.ExpressionExperimentSet)2 ExpressionExperimentValueObject (ubic.gemma.model.expression.experiment.ExpressionExperimentValueObject)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 SessionBoundGeneSetValueObject (ubic.gemma.core.genome.gene.SessionBoundGeneSetValueObject)1 SearchSettings (ubic.gemma.model.common.search.SearchSettings)1 FreeTextExpressionExperimentResultsValueObject (ubic.gemma.model.expression.experiment.FreeTextExpressionExperimentResultsValueObject)1 Gene (ubic.gemma.model.genome.Gene)1