use of ubic.gemma.model.genome.gene.GeneProduct 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;
}
use of ubic.gemma.model.genome.gene.GeneProduct in project Gemma by PavlidisLab.
the class BlatAssociationServiceTest method setup.
@Before
public void setup() {
int numSequencesToCreate = 20;
for (int i = 0; i < numSequencesToCreate; i++) {
BioSequence bs = this.getTestPersistentBioSequence();
if (i == 11) {
bs.setSequence(testSequence);
bs.setName(testSequenceName);
this.bioSequenceService.update(bs);
}
BlatResult br = this.getTestPersistentBlatResult(bs);
br.setQuerySequence(bs);
blatResultService.update(br);
BlatAssociation ba = BlatAssociation.Factory.newInstance();
Gene g = this.getTestPersistentGene();
GeneProduct gp = this.getTestPersistentGeneProduct(g);
if (i == 10) {
g.setOfficialName(testGeneIdentifier);
gp.setGene(g);
gp.setName(testGeneIdentifier);
this.geneProductService.update(gp);
}
ba.setGeneProduct(gp);
ba.setBlatResult(br);
ba.setBioSequence(bs);
blatAssociationService.create(ba);
}
}
use of ubic.gemma.model.genome.gene.GeneProduct in project Gemma by PavlidisLab.
the class BlatAssociationServiceTest method testFindGene.
@Test
public final void testFindGene() {
Gene g = Gene.Factory.newInstance();
g.setOfficialName(testGeneIdentifier);
GeneProduct gp = GeneProduct.Factory.newInstance();
gp.setName(testGeneIdentifier);
g.getProducts().add(gp);
gp.setGene(g);
Collection<BlatAssociation> res = this.blatAssociationService.find(g);
assertEquals(1, res.size());
}
use of ubic.gemma.model.genome.gene.GeneProduct in project Gemma by PavlidisLab.
the class GenomePersisterTest method testPersistGeneProduct.
/*
* Going the opposite way as the other test.
*
*/
@Test
public void testPersistGeneProduct() {
Gene gene = Gene.Factory.newInstance();
gene.setName(RandomStringUtils.randomAlphabetic(10));
gene.setNcbiGeneId(Integer.parseInt(RandomStringUtils.randomNumeric(8)));
GeneProduct gp = GeneProduct.Factory.newInstance();
gp.setName(RandomStringUtils.randomAlphabetic(10));
gp.setGene(gene);
gp.setNcbiGi(RandomStringUtils.randomAlphabetic(10));
gene.getProducts().add(gp);
gp = (GeneProduct) this.persisterHelper.persist(gp);
assertNotNull(gp.getId());
assertNotNull(gp.getGene().getId());
}
use of ubic.gemma.model.genome.gene.GeneProduct in project Gemma by PavlidisLab.
the class GenomePersisterTest method testPersistGene.
@Test
public void testPersistGene() {
Gene gene = Gene.Factory.newInstance();
gene.setName(RandomStringUtils.randomAlphabetic(10));
gene.setNcbiGeneId(Integer.parseInt(RandomStringUtils.randomNumeric(8)));
Collection<GeneProduct> gps = new HashSet<>();
for (int i = 0; i < 10; i++) {
GeneProduct gp = GeneProduct.Factory.newInstance();
gp.setName(RandomStringUtils.randomAlphabetic(10));
gp.setGene(gene);
gp.setNcbiGi(RandomStringUtils.randomAlphabetic(10));
gps.add(gp);
}
gene.setProducts(gps);
gene = (Gene) this.persisterHelper.persistOrUpdate(gene);
assertNotNull(gene.getId());
assertNotNull(gene.getName());
assertNotNull(gene.getProducts());
for (GeneProduct product : gene.getProducts()) {
assertNotNull(product.getId());
assertNotNull(product.getName());
}
}
Aggregations