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