Commit 21e8f210 authored by Hervé  MENAGER's avatar Hervé MENAGER
Browse files

add a 2nd manager to the Compound class filtering validated entries

this includes the corresponding test in the contribution tests
module
WIP on #68
parent 4a603b0e
......@@ -457,86 +457,24 @@ class PpiComplex(models.Model):
def __str__(self):
return 'PPI {}, Complex {} ({})'.format(self.ppi, self.complex, self.cc_nb)
class CompoundManager(models.Manager):
class ValidatedCompoundsManager(models.Manager):
"""
CompoundManager adds automatically a number of annotations to the results
of the database query, used for filters and compound card
ValidatedCompoundManager filters only compounds from validated
contributions (or not coming from contributions) in the results
of the database query
"""
# def get_queryset(self):
# return super().get_queryset().exclude(compoundaction__ppi__contribution__validated=False)
def get_queryset_old(self):
# @formatter:off
qs = super().get_queryset()
# with number of publications
qs = qs.annotate(pubs=Count('refcompoundbiblio', distinct=True))
# with best activity
qs = qs.annotate(best_activity=Max('compoundactivityresult__activity'))
# with LE
qs = qs.annotate(le=Cast(1.37 * Max('compoundactivityresult__activity') / F('nb_atom_non_h'), FloatField()))
# with LLE
qs = qs.annotate(lle=Cast(Max('compoundactivityresult__activity') - F('a_log_p'), FloatField()))
# Lipinsky MW (<=500)
qs = qs.annotate(lipinsky_mw=Case(When(molecular_weight__lte=500, then=True), default=False, output_field=BooleanField()))
# Lipinsky hba (<=10)
qs = qs.annotate(lipinsky_hba=Case(When(nb_acceptor_h__lte=10, then=True), default=False, output_field=BooleanField()))
# Lipinsky hbd (<5)
qs = qs.annotate(lipinsky_hbd=Case(When(nb_donor_h__lte=5, then=True), default=False, output_field=BooleanField()))
# Lipinsky a_log_p (<5)
qs = qs.annotate(lipinsky_a_log_p=Case(When(a_log_p__lte=5, then=True), default=False, output_field=BooleanField()))
# Lipinsky global
qs = qs.annotate(lipinsky_score=Cast(F('lipinsky_mw'), IntegerField()) + Cast(F('lipinsky_hba'), IntegerField()) +
Cast(F('lipinsky_hbd'), IntegerField()) + Cast(F('lipinsky_a_log_p'), IntegerField()))
qs = qs.annotate(lipinsky=Case(When(lipinsky_score__gte=3, then=True), default=False, output_field=BooleanField()))
# Veber hba_hbd (<=12)
qs = qs.annotate(hba_hbd=F('nb_acceptor_h')+F('nb_donor_h'))
qs = qs.annotate(veber_hba_hbd=Case(When(hba_hbd__lte=12, then=True), default=False, output_field=BooleanField()))
# Veber TPSA (<=140)
qs = qs.annotate(veber_tpsa=Case(When(tpsa__lte=140, then=True), default=False, output_field=BooleanField()))
# Veber Rotatable Bonds (<=10)
qs = qs.annotate(veber_rb=Case(When(nb_rotatable_bonds__lte=10, then=True), default=False, output_field=BooleanField()))
# Veber global (Rotatable bonds and (hba_hbd or tpsa))
qs = qs.annotate(veber=Case(When(Q(Q(nb_rotatable_bonds__lte=10) & (Q(hba_hbd__lte=12) | Q(tpsa__lte=140))), then=True), default=False, output_field=BooleanField()))
# Pfizer AlogP (<=3)
qs = qs.annotate(pfizer_a_log_p=Case(When(a_log_p__lte=3, then=True), default=False, output_field=BooleanField()))
# Pfizer TPSA (>=75)
qs = qs.annotate(pfizer_tpsa=Case(When(tpsa__gte=75, then=True), default=False, output_field=BooleanField()))
# Pfizer global (AlogP and TPSA)
qs = qs.annotate(pfizer=Case(When(Q(Q(a_log_p__lte=3) & Q(tpsa__gte=75)), then=True), default=False, output_field=BooleanField()))
# PDB ligand available
qs = qs.annotate(pdb_ligand_av=Cast(Max(Case(When(ligand_id__isnull=False, then=1), default=0, output_field=IntegerField())), BooleanField()))
# inhibition role
qs = qs.annotate(inhibition_role=Case(When(compoundactivityresult__modulation_type='I', then=True), default=False, output_field=BooleanField()))
# binding role
qs = qs.annotate(binding_role=Case(When(compoundactivityresult__modulation_type='B', then=True), default=False, output_field=BooleanField()))
# stabilisation role
qs = qs.annotate(stabilisation_role=Case(When(compoundactivityresult__modulation_type='S', then=True), default=False, output_field=BooleanField()))
# cellular tests performed
qs = qs.annotate(celltest_av=Cast(Max(Case(When(compoundactivityresult__test_activity_description__test_type='CELL', then=1), default=0, output_field=IntegerField())), BooleanField()))
# inhibition tests performed
qs = qs.annotate(inhitest_av=Cast(Max(Case(When(compoundactivityresult__test_activity_description__test_modulation_type='I', then=1), default=0, output_field=IntegerField())), BooleanField()))
# stabilisation tests performed
qs = qs.annotate(stabtest_av=Cast(Max(Case(When(compoundactivityresult__test_activity_description__test_modulation_type='S', then=1), default=0, output_field=IntegerField())), BooleanField()))
# binding tests performed
qs = qs.annotate(bindtest_av=Cast(Max(Case(When(compoundactivityresult__test_activity_description__test_modulation_type='B', then=1), default=0, output_field=IntegerField())), BooleanField()))
# pharmacokinetic tests performed
qs = qs.annotate(pktest_av=Cast(Max(Case(When(refcompoundbiblio__bibliography__pharmacokinetic=True, then=1), default=0, output_field=IntegerField())), BooleanField()))
# cytotoxicity tests performedudy
qs = qs.annotate(cytoxtest_av=Cast(Max(Case(When(refcompoundbiblio__bibliography__cytotox=True, then=1), default=0, output_field=IntegerField())), BooleanField()))
# in silico st performed
qs = qs.annotate(insilico_av=Cast(Max(Case(When(refcompoundbiblio__bibliography__in_silico=True, then=1), default=0, output_field=IntegerField())), BooleanField()))
# number of tests available
qs = qs.annotate(tests_av=Count('compoundactivityresult', distinct=True))
# @formatter:on
return qs
def get_queryset(self):
return super().get_queryset().exclude(compoundaction__ppi__contribution__validated=False)
class Compound(AutoFillableModel):
"""
Chemical compound
"""
objects = CompoundManager()
objects = models.Manager()
validated = ValidatedCompoundsManager()
canonical_smile = models.TextField(
verbose_name='Canonical Smiles',
unique=True,
......@@ -888,6 +826,7 @@ class Compound(AutoFillableModel):
models.Index(fields=['le']),
models.Index(fields=['lle']),
]
#default_manager_name = 'ippidb.models.ValidatedCompoundsManager'
# indexes = [
# models.Index(fields=['lipinsky']),
# models.Index(fields=['veber']),
......
......@@ -53,6 +53,11 @@ class ContributionViewsTestCase(TestCase):
models.Compound.objects.count() + len(entry_data["compounds"]),
"Compounds count",
),
(
models.Compound.validated.count,
models.Compound.validated.count(),
"Validated Compounds count",
),
(
models.CompoundAction.objects.count,
models.CompoundAction.objects.count() + len(entry_data["compounds"]),
......@@ -77,6 +82,19 @@ class ContributionViewsTestCase(TestCase):
self._process_contribution_wizard_without_sanity_check(entry_data)
for fcn, results, msg in future_expected_equals:
self.assertEqual(fcn(), results, msg=msg)
post_validation_expected_equals = [
(
models.Compound.validated.count,
models.Compound.objects.count(),
"Validated Compounds count",
),
]
contribution_to_be_validated = models.Contribution.objects.get(validated=False)
contribution_to_be_validated.validated = True
contribution_to_be_validated.save()
for fcn, results, msg in post_validation_expected_equals:
self.assertEqual(fcn(), results, msg=msg)
def _process_contribution_wizard_without_sanity_check(self, entry_data):
"""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment