Search in sources :

Example 6 with AnnotationAssociation

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

the class GenomePersister method removeGeneProducts.

private void removeGeneProducts(Collection<GeneProduct> toRemove) {
    Collection<BlatAssociation> associations = this.blatAssociationDao.find(toRemove);
    if (!associations.isEmpty()) {
        AbstractPersister.log.info("Removing " + associations.size() + " blat associations involving up to " + toRemove.size() + " products.");
        this.blatAssociationDao.remove(associations);
    }
    Collection<AnnotationAssociation> annotationAssociations = this.annotationAssociationDao.find(toRemove);
    if (!annotationAssociations.isEmpty()) {
        AbstractPersister.log.info("Removing " + annotationAssociations.size() + " annotationAssociations involving up to " + toRemove.size() + " products.");
        this.annotationAssociationDao.remove(annotationAssociations);
    }
    // remove associations to database entries that are still associated with sequences.
    for (GeneProduct gp : toRemove) {
        Collection<DatabaseEntry> accessions = gp.getAccessions();
        Collection<DatabaseEntry> toRelease = new HashSet<>();
        for (DatabaseEntry de : accessions) {
            if (this.bioSequenceDao.findByAccession(de) != null) {
                toRelease.add(de);
            }
        }
        gp.getAccessions().removeAll(toRelease);
        this.geneProductDao.remove(gp);
    }
}
Also used : BioSequence2GeneProduct(ubic.gemma.model.association.BioSequence2GeneProduct) GeneProduct(ubic.gemma.model.genome.gene.GeneProduct) AnnotationAssociation(ubic.gemma.model.genome.sequenceAnalysis.AnnotationAssociation) DatabaseEntry(ubic.gemma.model.common.description.DatabaseEntry) BlatAssociation(ubic.gemma.model.genome.sequenceAnalysis.BlatAssociation) HashSet(java.util.HashSet)

Example 7 with AnnotationAssociation

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

the class AnnotationAssociationDaoImpl method find.

@Override
public Collection<AnnotationAssociation> find(Gene gene) {
    if (gene.getProducts().size() == 0) {
        throw new IllegalArgumentException("Gene has no products");
    }
    Collection<AnnotationAssociation> result = new HashSet<>();
    for (GeneProduct geneProduct : gene.getProducts()) {
        BusinessKey.checkValidKey(geneProduct);
        Criteria queryObject = this.getSessionFactory().getCurrentSession().createCriteria(AnnotationAssociation.class);
        Criteria innerQuery = queryObject.createCriteria("geneProduct");
        if (StringUtils.isNotBlank(geneProduct.getNcbiGi())) {
            innerQuery.add(Restrictions.eq("ncbiGi", geneProduct.getNcbiGi()));
        }
        if (StringUtils.isNotBlank(geneProduct.getName())) {
            innerQuery.add(Restrictions.eq("name", geneProduct.getName()));
        }
        // noinspection unchecked
        result.addAll(queryObject.list());
    }
    return result;
}
Also used : GeneProduct(ubic.gemma.model.genome.gene.GeneProduct) AnnotationAssociation(ubic.gemma.model.genome.sequenceAnalysis.AnnotationAssociation) Criteria(org.hibernate.Criteria) HashSet(java.util.HashSet)

Example 8 with AnnotationAssociation

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

the class GenericGenelistDesignGenerator method doWork.

@Override
protected Exception doWork(String[] args) {
    Exception exception = super.processCommandLine(args);
    if (exception != null) {
        return exception;
    }
    ExternalDatabase genbank = externalDatabaseService.findByName("Genbank");
    ExternalDatabase ensembl = externalDatabaseService.findByName("Ensembl");
    assert genbank != null;
    assert ensembl != null;
    /*
         * Create the stub array design for the organism. The name and etc. are generated automatically. If the design
         * exists, we update it.
         */
    String shortName = this.generateShortName();
    ArrayDesign arrayDesign = ArrayDesign.Factory.newInstance();
    arrayDesign.setShortName(shortName);
    // common name
    arrayDesign.setPrimaryTaxon(taxon);
    String nameExt = useNCBIIds ? ", indexed by NCBI IDs" : useEnsemblIds ? ", indexed by Ensembl IDs" : "";
    arrayDesign.setName("Generic platform for " + taxon.getScientificName() + nameExt);
    arrayDesign.setDescription("Created by Gemma");
    // this is key
    arrayDesign.setTechnologyType(TechnologyType.NONE);
    if (arrayDesignService.find(arrayDesign) != null) {
        AbstractCLI.log.info("Platform for " + taxon + " already exists, will update");
        arrayDesign = arrayDesignService.find(arrayDesign);
        arrayDesignService.deleteGeneProductAssociations(arrayDesign);
        arrayDesign = arrayDesignService.load(arrayDesign.getId());
    } else {
        AbstractCLI.log.info("Creating new 'generic' platform");
        arrayDesign = arrayDesignService.create(arrayDesign);
    }
    arrayDesign = arrayDesignService.thaw(arrayDesign);
    // temporary: making sure we set it, as it is new.
    arrayDesign.setTechnologyType(TechnologyType.NONE);
    /*
         * Load up the genes for the organism.
         */
    Collection<Gene> knownGenes = geneService.loadAll(taxon);
    AbstractCLI.log.info("Taxon has " + knownGenes.size() + " genes");
    // this would be good for cases where the identifier we are using has changed.
    Map<Gene, CompositeSequence> existingGeneMap = new HashMap<>();
    if (!useNCBIIds && !useEnsemblIds) {
        // only using this for symbol changes.
        existingGeneMap = this.getExistingGeneMap(arrayDesign);
    }
    Map<String, CompositeSequence> existingSymbolMap = this.getExistingProbeNameMap(arrayDesign);
    int count = 0;
    int numWithNoTranscript = 0;
    // int hasGeneAlready = 0;
    // int numNewGenes = 0;
    int numNewElements = 0;
    int numUpdatedElements = 0;
    for (Gene gene : knownGenes) {
        gene = geneService.thaw(gene);
        Collection<GeneProduct> products = gene.getProducts();
        if (products.isEmpty()) {
            numWithNoTranscript++;
            AbstractCLI.log.debug("No transcript for " + gene);
            continue;
        }
        count++;
        CompositeSequence csForGene = null;
        if (useNCBIIds) {
            if (gene.getNcbiGeneId() == null) {
                AbstractCLI.log.debug("No NCBI ID for " + gene + ", skipping");
                continue;
            }
            if (existingSymbolMap.containsKey(gene.getNcbiGeneId().toString())) {
                csForGene = existingSymbolMap.get(gene.getNcbiGeneId().toString());
            }
        } else if (useEnsemblIds) {
            if (gene.getEnsemblId() == null) {
                AbstractCLI.log.debug("No Ensembl ID for " + gene + ", skipping");
                continue;
            }
            if (existingSymbolMap.containsKey(gene.getEnsemblId())) {
                csForGene = existingSymbolMap.get(gene.getEnsemblId());
            }
        } else {
            /*
                 * detect when the symbol has changed
                 */
            if (existingSymbolMap.containsKey(gene.getOfficialSymbol())) {
                csForGene = existingSymbolMap.get(gene.getOfficialSymbol());
            } else if (existingGeneMap.containsKey(gene)) {
                csForGene = existingGeneMap.get(gene);
                AbstractCLI.log.debug("Gene symbol has changed for: " + gene + "? Current element has name=" + csForGene.getName());
                csForGene.setName(gene.getOfficialSymbol());
            }
        }
        assert csForGene == null || csForGene.getId() != null : "Null id for " + csForGene;
        /*
             * We arbitrarily link the "probe" to one of the gene's RNA transcripts. We could consider other strategies
             * to pick the representative, but it generally doesn't matter.
             */
        for (GeneProduct geneProduct : products) {
            if (!GeneProductType.RNA.equals(geneProduct.getType())) {
                continue;
            }
            /*
                 * Name is usually the genbank or ensembl accession
                 */
            String name = geneProduct.getName();
            BioSequence bioSequence = BioSequence.Factory.newInstance();
            Collection<DatabaseEntry> accessions = geneProduct.getAccessions();
            bioSequence.setName(name);
            bioSequence.setTaxon(taxon);
            bioSequence.setPolymerType(PolymerType.RNA);
            bioSequence.setType(SequenceType.mRNA);
            BioSequence existing = null;
            if (accessions.isEmpty()) {
                // this should not be hit.
                AbstractCLI.log.warn("No accession for " + name);
                DatabaseEntry de = DatabaseEntry.Factory.newInstance();
                de.setAccession(name);
                if (name.startsWith("ENS") && name.length() > 10) {
                    de.setExternalDatabase(ensembl);
                } else {
                    if (name.matches("^[A-Z]{1,2}(_?)[0-9]+(\\.[0-9]+)?$")) {
                        de.setExternalDatabase(genbank);
                    } else {
                        AbstractCLI.log.info("Name doesn't look like genbank or ensembl, skipping: " + name);
                        continue;
                    }
                }
                bioSequence.setSequenceDatabaseEntry(de);
            } else {
                bioSequence.setSequenceDatabaseEntry(accessions.iterator().next());
                existing = bioSequenceService.findByAccession(accessions.iterator().next());
            // FIXME It is possible that this sequence will have been aligned to the genome, which is a bit
            // confusing. So it will map to a gene. Worse case: it maps to more than one gene ...
            }
            if (existing == null) {
                bioSequence = (BioSequence) this.getPersisterHelper().persist(bioSequence);
            } else {
                bioSequence = existing;
            }
            assert bioSequence != null && bioSequence.getId() != null;
            if (bioSequence.getSequenceDatabaseEntry() == null) {
                AbstractCLI.log.info("No DB entry for " + bioSequence + "(" + gene + "), will look for a better sequence to use ...");
                continue;
            }
            if (csForGene == null) {
                if (AbstractCLI.log.isDebugEnabled())
                    AbstractCLI.log.debug("New element " + " with " + bioSequence + " for " + gene);
                csForGene = CompositeSequence.Factory.newInstance();
                if (useNCBIIds) {
                    if (gene.getNcbiGeneId() == null) {
                        continue;
                    }
                    csForGene.setName(gene.getNcbiGeneId().toString());
                } else if (useEnsemblIds) {
                    if (gene.getEnsemblId() == null) {
                        continue;
                    }
                    csForGene.setName(gene.getEnsemblId());
                } else {
                    csForGene.setName(gene.getOfficialSymbol());
                }
                csForGene.setArrayDesign(arrayDesign);
                csForGene.setBiologicalCharacteristic(bioSequence);
                csForGene.setDescription("Generic expression element for " + gene);
                csForGene = compositeSequenceService.create(csForGene);
                assert csForGene.getId() != null : "No id for " + csForGene + " for " + gene;
                arrayDesign.getCompositeSequences().add(csForGene);
                numNewElements++;
            } else {
                if (AbstractCLI.log.isDebugEnabled())
                    AbstractCLI.log.debug("Updating existing element: " + csForGene + " with " + bioSequence + " for " + gene);
                csForGene.setArrayDesign(arrayDesign);
                csForGene.setBiologicalCharacteristic(bioSequence);
                csForGene.setDescription("Generic expression element for " + gene);
                assert csForGene.getId() != null : "No id for " + csForGene + " for " + gene;
                compositeSequenceService.update(csForGene);
                // making sure ...
                csForGene = compositeSequenceService.load(csForGene.getId());
                assert csForGene.getId() != null;
                arrayDesign.getCompositeSequences().add(csForGene);
                numUpdatedElements++;
            }
            assert bioSequence.getId() != null;
            assert geneProduct.getId() != null;
            assert csForGene.getBiologicalCharacteristic() != null && csForGene.getBiologicalCharacteristic().getId() != null;
            AnnotationAssociation aa = AnnotationAssociation.Factory.newInstance();
            aa.setGeneProduct(geneProduct);
            aa.setBioSequence(bioSequence);
            annotationAssociationService.create(aa);
            break;
        }
        if (count % 100 == 0)
            AbstractCLI.log.info(count + " genes processed; " + numNewElements + " new elements; " + numUpdatedElements + " updated elements; " + numWithNoTranscript + " genes had no transcript and were skipped.");
    }
    // is this necessary? causes an error sometimes.
    // arrayDesignService.update( arrayDesign );
    AbstractCLI.log.info("Array design has " + arrayDesignService.numCompositeSequenceWithGenes(arrayDesign) + " 'probes' associated with genes.");
    arrayDesignReportService.generateArrayDesignReport(arrayDesign.getId());
    auditTrailService.addUpdateEvent(arrayDesign, AnnotationBasedGeneMappingEvent.Factory.newInstance(), count + " genes processed; " + numNewElements + " new elements; " + numUpdatedElements + " updated elements; " + numWithNoTranscript + " genes had no transcript and were skipped.");
    arrayDesignAnnotationService.deleteExistingFiles(arrayDesign);
    AbstractCLI.log.info("Don't forget to update the annotation files");
    return null;
}
Also used : AnnotationAssociation(ubic.gemma.model.genome.sequenceAnalysis.AnnotationAssociation) HashMap(java.util.HashMap) BioSequence(ubic.gemma.model.genome.biosequence.BioSequence) ArrayDesign(ubic.gemma.model.expression.arrayDesign.ArrayDesign) DatabaseEntry(ubic.gemma.model.common.description.DatabaseEntry) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) GeneProduct(ubic.gemma.model.genome.gene.GeneProduct) ExternalDatabase(ubic.gemma.model.common.description.ExternalDatabase) Gene(ubic.gemma.model.genome.Gene)

Aggregations

AnnotationAssociation (ubic.gemma.model.genome.sequenceAnalysis.AnnotationAssociation)8 GeneProduct (ubic.gemma.model.genome.gene.GeneProduct)5 HashSet (java.util.HashSet)4 DatabaseEntry (ubic.gemma.model.common.description.DatabaseEntry)3 BioSequence (ubic.gemma.model.genome.biosequence.BioSequence)3 BlatAssociation (ubic.gemma.model.genome.sequenceAnalysis.BlatAssociation)3 BioSequence2GeneProduct (ubic.gemma.model.association.BioSequence2GeneProduct)2 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)2 Gene (ubic.gemma.model.genome.Gene)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 Criteria (org.hibernate.Criteria)1 HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate)1 Transactional (org.springframework.transaction.annotation.Transactional)1 GeneMappingSummary (ubic.gemma.core.analysis.sequence.GeneMappingSummary)1 ExternalDatabase (ubic.gemma.model.common.description.ExternalDatabase)1 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)1