Search in sources :

Example 61 with Gene

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

the class GeneDaoImpl method find.

@Override
public Gene find(Gene gene) {
    Criteria queryObject = this.getSessionFactory().getCurrentSession().createCriteria(Gene.class);
    BusinessKey.checkKey(gene);
    BusinessKey.createQueryObject(queryObject, gene);
    // noinspection unchecked,unchecked
    List<Gene> results = queryObject.list();
    Object result;
    if (results.isEmpty()) {
        return null;
    } else if (results.size() > 1) {
        /*
             * As a side-effect, we remove relics. This is a bit ugly, but takes care of the problem! It was put in
             * place to help in the cleanup of duplicated genes. But this can happen fairly routinely when NCBI
             * information changes in messy ways.
             *
             * FIXME this can fail because 'find' methods are read-only; it will be okay if it is a nested call from a
             * read-write method.
             */
        Collection<Gene> toDelete = new HashSet<>();
        for (Gene foundGene : results) {
            if (StringUtils.isBlank(foundGene.getPreviousNcbiId()))
                continue;
            // Note hack we used to allow multiple previous ids.
            for (String previousId : StringUtils.split(foundGene.getPreviousNcbiId(), ",")) {
                try {
                    if (gene.getNcbiGeneId().equals(Integer.parseInt(previousId))) {
                        toDelete.add(foundGene);
                    }
                } catch (NumberFormatException e) {
                // no action
                }
            }
        }
        if (!toDelete.isEmpty()) {
            // it shouldn't be everything!
            assert toDelete.size() < results.size();
            AbstractDao.log.warn("Deleting gene(s) that use a deprecated NCBI ID: " + StringUtils.join(toDelete, " | "));
            // WARNING this might fail due to constraints.
            this.remove(toDelete);
        }
        results.removeAll(toDelete);
        for (Gene foundGene : results) {
            if (foundGene.getNcbiGeneId() != null && gene.getNcbiGeneId() != null && foundGene.getNcbiGeneId().equals(gene.getNcbiGeneId())) {
                return foundGene;
            }
        }
        /*
             * This should be quite a rare situation if the database is kept tidy.
             */
        if (results.size() > 1) {
            AbstractDao.log.error("Multiple genes found for " + gene + ":");
            this.debug(results);
            Collections.sort(results, new Comparator<Gene>() {

                @Override
                public int compare(Gene arg0, Gene arg1) {
                    return arg0.getId().compareTo(arg1.getId());
                }
            });
            result = results.iterator().next();
            AbstractDao.log.error("Returning arbitrary gene: " + result);
        } else {
            result = results.get(0);
        }
    } else {
        result = results.get(0);
    }
    return (Gene) result;
}
Also used : Gene(ubic.gemma.model.genome.Gene) GeneValueObject(ubic.gemma.model.genome.gene.GeneValueObject) Criteria(org.hibernate.Criteria)

Example 62 with Gene

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

the class GeneDaoImpl method loadThawedLiter.

@Override
public Collection<Gene> loadThawedLiter(Collection<Long> ids) {
    Collection<Gene> result = new HashSet<>();
    if (ids.isEmpty())
        return result;
    StopWatch timer = new StopWatch();
    timer.start();
    for (Collection<Long> batch : new BatchIterator<>(ids, GeneDaoImpl.BATCH_SIZE)) {
        result.addAll(this.doLoadThawedLiter(batch));
    }
    if (timer.getTime() > 1000) {
        AbstractDao.log.debug("Load+thawRawAndProcessed " + result.size() + " genes: " + timer.getTime() + "ms");
    }
    return result;
}
Also used : Gene(ubic.gemma.model.genome.Gene) BatchIterator(ubic.basecode.util.BatchIterator) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 63 with Gene

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

the class BioSequenceDaoImpl method findByGenes.

@Override
public Map<Gene, Collection<BioSequence>> findByGenes(Collection<Gene> genes) {
    if (genes == null || genes.isEmpty())
        return new HashMap<>();
    Map<Gene, Collection<BioSequence>> results = new HashMap<>();
    int batchSize = 500;
    if (genes.size() <= batchSize) {
        this.findByGenesBatch(genes, results);
        return results;
    }
    Collection<Gene> batch = new HashSet<>();
    for (Gene gene : genes) {
        batch.add(gene);
        if (batch.size() == batchSize) {
            this.findByGenesBatch(genes, results);
            batch.clear();
        }
    }
    if (!batch.isEmpty()) {
        this.findByGenesBatch(genes, results);
    }
    return results;
}
Also used : Gene(ubic.gemma.model.genome.Gene)

Example 64 with Gene

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

the class BioSequenceDaoImpl method findByGenesBatch.

private void findByGenesBatch(Collection<Gene> genes, Map<Gene, Collection<BioSequence>> results) {
    // noinspection unchecked
    List<Object[]> qr = this.getSessionFactory().getCurrentSession().createQuery("select distinct gene,bs from Gene gene inner join fetch gene.products ggp," + " BioSequenceImpl bs inner join bs.bioSequence2GeneProduct bs2gp inner join bs2gp.geneProduct bsgp" + " where ggp=bsgp and gene in (:genes)").setParameterList("genes", genes).list();
    for (Object[] oa : qr) {
        Gene g = (Gene) oa[0];
        BioSequence b = (BioSequence) oa[1];
        if (!results.containsKey(g)) {
            results.put(g, new HashSet<BioSequence>());
        }
        results.get(g).add(b);
    }
}
Also used : Gene(ubic.gemma.model.genome.Gene) BioSequence(ubic.gemma.model.genome.biosequence.BioSequence) BioSequenceValueObject(ubic.gemma.model.genome.sequenceAnalysis.BioSequenceValueObject)

Example 65 with Gene

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

the class ProcessedExpressionDataVectorServiceImpl method addExperimentGeneVectors.

/**
 * Creates an ExperimentExpressionLevelValueObject for the given experiment and collection of double vector VOs, and
 * adds it to the given vos collection.
 *
 * @param vos     the collection to add the result to.
 * @param ee      the experiment the vectors belong to.
 * @param vectors the vectors to create the new ExperimentExpressionLevelsVO with.
 */
private void addExperimentGeneVectors(Collection<ExperimentExpressionLevelsValueObject> vos, ExpressionExperiment ee, Collection<DoubleVectorValueObject> vectors, boolean keepGeneNonSpecific, String consolidateMode) {
    Map<Gene, List<DoubleVectorValueObject>> vectorsPerGene = new HashMap<>();
    if (vectors == null) {
        return;
    }
    for (DoubleVectorValueObject v : vectors) {
        if (!v.getExpressionExperiment().getId().equals(ee.getId())) {
            continue;
        }
        if (v.getGenes() == null || v.getGenes().isEmpty()) {
            if (!vectorsPerGene.containsKey(null)) {
                vectorsPerGene.put(null, new LinkedList<DoubleVectorValueObject>());
            }
            vectorsPerGene.get(null).add(v);
        }
        for (Long gId : v.getGenes()) {
            Gene g = geneService.load(gId);
            if (g != null) {
                if (!vectorsPerGene.containsKey(g)) {
                    vectorsPerGene.put(g, new LinkedList<DoubleVectorValueObject>());
                }
                vectorsPerGene.get(g).add(v);
            }
        }
    }
    vos.add(new ExperimentExpressionLevelsValueObject(ee.getId(), vectorsPerGene, keepGeneNonSpecific, consolidateMode));
}
Also used : Gene(ubic.gemma.model.genome.Gene) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject) ExperimentExpressionLevelsValueObject(ubic.gemma.model.expression.bioAssayData.ExperimentExpressionLevelsValueObject)

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