Search in sources :

Example 81 with Gene

use of ubic.gemma.model.genome.Gene in project Gemma by PavlidisLab.

the class SymbolChangeAndLoggingAbstract method findGeneUsingSymbolandTaxon.

// sometimes we dont have the gene nbci, so we use taxon and gene symbol to find the correct gene
Gene findGeneUsingSymbolandTaxon(String officialSymbol, String evidenceTaxon) throws IOException {
    String officialSym = officialSymbol.replaceAll("@", "");
    Collection<Gene> genes = this.geneService.findByOfficialSymbol(officialSym);
    Collection<Gene> genesWithTaxon = new HashSet<>();
    for (Gene gene : genes) {
        if (gene.getTaxon().getCommonName().equalsIgnoreCase(evidenceTaxon)) {
            if (gene.getNcbiGeneId() != null) {
                genesWithTaxon.add(gene);
            }
        }
    }
    if (genesWithTaxon.isEmpty()) {
        return this.checkForSymbolChange(officialSym, evidenceTaxon);
    } else // too many results found, to check why
    if (genesWithTaxon.size() >= 2) {
        Gene g = this.treatGemmaMultipleGeneSpeacialCases(officialSym, genesWithTaxon, evidenceTaxon);
        if (g != null) {
            return g;
        }
        String error = "Found more than 1 gene using Symbol: " + officialSym + "   and taxon: " + evidenceTaxon;
        for (Gene geneWithTaxon : genesWithTaxon) {
            logMessages.add(error + "\tGene NCBI: " + geneWithTaxon.getNcbiGeneId());
        }
    }
    return genesWithTaxon.iterator().next();
}
Also used : Gene(ubic.gemma.model.genome.Gene) HashSet(java.util.HashSet)

Example 82 with Gene

use of ubic.gemma.model.genome.Gene in project Gemma by PavlidisLab.

the class GeneSearchServiceImpl method searchMultipleGenesGetMap.

@Override
public Map<String, GeneValueObject> searchMultipleGenesGetMap(Collection<String> query, Long taxonId) {
    Taxon taxon = taxonService.load(taxonId);
    if (taxon == null)
        throw new IllegalArgumentException("No such taxon with id=" + taxonId);
    // this deals with the simple cases. For remainder we look a little harder
    Map<String, GeneValueObject> queryToGenes = geneService.findByOfficialSymbols(query, taxonId);
    for (String line : query) {
        line = StringUtils.strip(line);
        if (StringUtils.isBlank(line)) {
            continue;
        }
        String queryAsKey = line.toLowerCase();
        if (queryToGenes.containsKey(queryAsKey)) {
            // already found.
            continue;
        }
        if (queryToGenes.size() >= GeneSearchServiceImpl.MAX_GENES_PER_QUERY) {
            GeneSearchServiceImpl.log.warn("Too many genes, stopping (limit=" + GeneSearchServiceImpl.MAX_GENES_PER_QUERY + ')');
            break;
        }
        // searching one gene at a time is a bit slow; we do a quick search for symbols.
        SearchSettings settings = SearchSettingsImpl.geneSearch(line, taxon);
        List<SearchResult> geneSearchResults = searchService.speedSearch(settings).get(Gene.class);
        if (geneSearchResults == null || geneSearchResults.isEmpty()) {
            // an empty set is an indication of no results.
            queryToGenes.put(queryAsKey, null);
        } else if (geneSearchResults.size() == 1) {
            // Just one result so add it
            Gene g = (Gene) geneSearchResults.iterator().next().getResultObject();
            queryToGenes.put(queryAsKey, new GeneValueObject(g));
        } else {
            // like grin1, grin2, grin3, grin (given the search term was grin)
            for (SearchResult sr : geneSearchResults) {
                Gene srGene = (Gene) sr.getResultObject();
                if (srGene.getTaxon().equals(taxon) && srGene.getOfficialSymbol().equalsIgnoreCase(line)) {
                    queryToGenes.put(queryAsKey, new GeneValueObject(srGene));
                    // found so done
                    break;
                }
            }
        }
    }
    return queryToGenes;
}
Also used : Gene(ubic.gemma.model.genome.Gene) Taxon(ubic.gemma.model.genome.Taxon) SearchSettings(ubic.gemma.model.common.search.SearchSettings) SearchResult(ubic.gemma.core.search.SearchResult)

Example 83 with Gene

use of ubic.gemma.model.genome.Gene 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 84 with Gene

use of ubic.gemma.model.genome.Gene in project Gemma by PavlidisLab.

the class GeneServiceImpl method loadFullyPopulatedValueObject.

@Override
@Transactional(readOnly = true)
public GeneValueObject loadFullyPopulatedValueObject(Long id) {
    Gene gene = this.geneDao.load(id);
    if (gene == null) {
        return null;
    }
    gene = this.geneDao.thaw(gene);
    GeneValueObject gvo = GeneValueObject.convert2ValueObject(gene);
    Collection<GeneAlias> aliasObjects = gene.getAliases();
    Collection<String> aliasStrings = new ArrayList<>();
    for (GeneAlias ga : aliasObjects) {
        aliasStrings.add(ga.getAlias());
    }
    gvo.setAliases(aliasStrings);
    if (gene.getMultifunctionality() != null) {
        gvo.setMultifunctionalityRank(gene.getMultifunctionality().getRank());
    }
    Long compositeSequenceCount = this.getCompositeSequenceCountById(id);
    gvo.setCompositeSequenceCount(compositeSequenceCount.intValue());
    Integer platformCount = this.geneDao.getPlatformCountById(id);
    gvo.setPlatformCount(platformCount);
    Collection<GeneSet> geneSets = this.geneSetSearch.findByGene(gene);
    Collection<GeneSetValueObject> gsVos = new ArrayList<>();
    // noinspection CollectionAddAllCanBeReplacedWithConstructor // Constructor can't handle subclasses
    gsVos.addAll(geneSetValueObjectHelper.convertToLightValueObjects(geneSets, false));
    gvo.setGeneSets(gsVos);
    Collection<Gene> geneHomologues = this.homologeneService.getHomologues(gene);
    geneHomologues = this.thawLite(geneHomologues);
    Collection<GeneValueObject> homologues = this.loadValueObjects(geneHomologues);
    gvo.setHomologues(homologues);
    Collection<PhenotypeAssociation> pas = gene.getPhenotypeAssociations();
    Collection<CharacteristicValueObject> cVos = new HashSet<>();
    for (PhenotypeAssociation pa : pas) {
        cVos.addAll(CharacteristicValueObject.characteristic2CharacteristicVO(pa.getPhenotypes()));
    }
    gvo.setPhenotypes(cVos);
    if (gvo.getNcbiId() != null) {
        SearchSettingsImpl s = new SearchSettingsImpl();
        s.setTermUri("http://purl.org/commons/record/ncbi_gene/" + gvo.getNcbiId());
        s.noSearches();
        s.setSearchExperiments(true);
        Map<Class<?>, List<SearchResult>> r = searchService.search(s);
        if (r.containsKey(ExpressionExperiment.class)) {
            List<SearchResult> hits = r.get(ExpressionExperiment.class);
            gvo.setAssociatedExperimentCount(hits.size());
        }
    }
    GeneCoexpressionNodeDegreeValueObject nodeDegree = coexpressionService.getNodeDegree(gene);
    if (nodeDegree != null) {
        gvo.setNodeDegreesPos(nodeDegree.asIntArrayPos());
        gvo.setNodeDegreesNeg(nodeDegree.asIntArrayNeg());
        gvo.setNodeDegreePosRanks(nodeDegree.asDoubleArrayPosRanks());
        gvo.setNodeDegreeNegRanks(nodeDegree.asDoubleArrayNegRanks());
    }
    return gvo;
}
Also used : CharacteristicValueObject(ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject) Gene(ubic.gemma.model.genome.Gene) SearchSettingsImpl(ubic.gemma.model.common.search.SearchSettingsImpl) PhenotypeAssociation(ubic.gemma.model.association.phenotype.PhenotypeAssociation) SearchResult(ubic.gemma.core.search.SearchResult) GeneCoexpressionNodeDegreeValueObject(ubic.gemma.model.association.coexpression.GeneCoexpressionNodeDegreeValueObject) Transactional(org.springframework.transaction.annotation.Transactional)

Example 85 with Gene

use of ubic.gemma.model.genome.Gene in project Gemma by PavlidisLab.

the class GeneServiceImpl method loadGenePhenotypes.

@Override
@Transactional(readOnly = true)
public GeneValueObject loadGenePhenotypes(Long geneId) {
    Gene gene = this.load(geneId);
    gene = this.thaw(gene);
    GeneValueObject initialResult = GeneValueObject.convert2ValueObject(gene);
    GeneValueObject details = new GeneValueObject(initialResult);
    Collection<GeneAlias> aliasObjects = gene.getAliases();
    Collection<String> aliasStrings = new ArrayList<>();
    for (GeneAlias ga : aliasObjects) {
        aliasStrings.add(ga.getAlias());
    }
    details.setAliases(aliasStrings);
    Long compositeSequenceCount = this.getCompositeSequenceCountById(geneId);
    details.setCompositeSequenceCount(compositeSequenceCount.intValue());
    Collection<GeneSet> geneSets = geneSetSearch.findByGene(gene);
    Collection<GeneSetValueObject> gsVos = new ArrayList<>();
    // noinspection CollectionAddAllCanBeReplacedWithConstructor // Constructor can't handle subclasses
    gsVos.addAll(geneSetValueObjectHelper.convertToValueObjects(geneSets, false));
    details.setGeneSets(gsVos);
    Collection<Gene> geneHomologues = homologeneService.getHomologues(gene);
    geneHomologues = this.thawLite(geneHomologues);
    Collection<GeneValueObject> homologues = this.loadValueObjects(geneHomologues);
    details.setHomologues(homologues);
    return details;
}
Also used : Gene(ubic.gemma.model.genome.Gene) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Gene (ubic.gemma.model.genome.Gene)186 Taxon (ubic.gemma.model.genome.Taxon)34 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)32 StopWatch (org.apache.commons.lang3.time.StopWatch)31 Test (org.junit.Test)24 HashSet (java.util.HashSet)23 GeneProduct (ubic.gemma.model.genome.gene.GeneProduct)20 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)18 Element (org.w3c.dom.Element)16 ArrayList (java.util.ArrayList)13 Transactional (org.springframework.transaction.annotation.Transactional)12 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)12 Collection (java.util.Collection)11 OntologyTerm (ubic.basecode.ontology.model.OntologyTerm)11 CharacteristicValueObject (ubic.gemma.model.genome.gene.phenotype.valueObject.CharacteristicValueObject)10 HashMap (java.util.HashMap)8 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)8 BioSequence2GeneProduct (ubic.gemma.model.association.BioSequence2GeneProduct)7 PhysicalLocation (ubic.gemma.model.genome.PhysicalLocation)7 BioSequence (ubic.gemma.model.genome.biosequence.BioSequence)7