Skip to content

Commit

Permalink
materialise (fetch) lazyily referenced mongo embeds when accessing re…
Browse files Browse the repository at this point in the history
…lated attributes
  • Loading branch information
SandyRogers committed Jan 22, 2024
1 parent 0a8f872 commit e10fd33
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions emgapianns/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import cached_property

# Copyright 2019 EMBL - European Bioinformatics Institute
#
Expand Down Expand Up @@ -108,17 +109,22 @@ class AnalysisJobGoTermAnnotation(BaseAnalysisJobAnnotation):

go_term = mongoengine.LazyReferenceField(GoTerm, required=True)


@cached_property
def materialised_go_term(self):
return self.go_term.fetch()

@property
def accession(self):
return self.go_term.accession
return self.materialised_go_term.accession

@property
def description(self):
return self.go_term.description
return self.materialised_go_term.description

@property
def lineage(self):
return self.go_term.lineage
return self.materialised_go_term.lineage


class AnalysisJobInterproIdentifierAnnotation(BaseAnalysisJobAnnotation):
Expand All @@ -128,17 +134,21 @@ class AnalysisJobInterproIdentifierAnnotation(BaseAnalysisJobAnnotation):
required=True
)

@cached_property
def materialised_interpro_identifier(self):
return self.interpro_identifier.fetch()

@property
def accession(self):
return self.interpro_identifier.accession
return self.materialised_interpro_identifier.accession

@property
def description(self):
return self.interpro_identifier.description
return self.materialised_interpro_identifier.description

@property
def lineage(self):
return self.interpro_identifier.lineage
return self.materialised_interpro_identifier.lineage


class AnalysisJobKeggModuleAnnotation(mongoengine.EmbeddedDocument):
Expand Down Expand Up @@ -167,27 +177,35 @@ class AnalysisJobPfamAnnotation(BaseAnalysisJobAnnotation):
"""
pfam_entry = mongoengine.LazyReferenceField(PfamEntry, required=True)

@cached_property
def materialised_pfam_entry(self):
return self.pfam_entry.fetch()

@property
def accession(self):
return self.pfam_entry.accession
return self.materialised_pfam_entry.accession

@property
def description(self):
return self.pfam_entry.description
return self.materialised_pfam_entry.description


class AnalysisJobCOGAnnotation(BaseAnalysisJobAnnotation):
"""COG on a given Analysis Job.
"""
cog = mongoengine.LazyReferenceField(COG, required=True)

@cached_property
def materialised_cog(self):
return self.cog.fetch()

@property
def accession(self):
return self.cog.accession
return self.materialised_cog.accession

@property
def description(self):
return self.cog.description
return self.materialised_cog.description


class AnalysisJobGenomePropAnnotation(mongoengine.EmbeddedDocument):
Expand Down Expand Up @@ -218,27 +236,35 @@ class AnalysisJobKeggOrthologAnnotation(BaseAnalysisJobAnnotation):
"""
ko = mongoengine.LazyReferenceField(KeggOrtholog, required=True)

@cached_property
def materialised_ko(self):
return self.ko.fetch()

@property
def accession(self):
return self.ko.accession
return self.materialised_ko.accession

@property
def description(self):
return self.ko.description
return self.materialised_ko.description


class AnalysisJobAntiSmashGCAnnotation(BaseAnalysisJobAnnotation):
"""antiSMASH gene cluster on a given Analysis Job
"""
gene_cluster = mongoengine.LazyReferenceField(AntiSmashGeneCluster, required=True)

@cached_property
def materialised_gene_cluster(self):
return self.gene_cluster.fetch()

@property
def accession(self):
return self.gene_cluster.accession
return self.materialised_gene_cluster.accession

@property
def description(self):
return self.gene_cluster.description
return self.materialised_gene_cluster.description


class BaseAnalysisJob(mongoengine.Document):
Expand Down

0 comments on commit e10fd33

Please sign in to comment.