Skip to content

Commit

Permalink
fix: Corrected use of lhs and ref pdb molecule properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Christie authored and alanbchristie committed Jan 11, 2024
1 parent 1f25b16 commit a3aa0c4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
69 changes: 59 additions & 10 deletions viewer/cset_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,22 @@ def process_pdb(self, pdb_code, target, zfile, zfile_hashvals) -> SiteObservatio

# use zfile object for pdb files uploaded in zip
def get_site_observation(
self, mol, target, compound_set, zfile, zfile_hashvals
self, property_name, mol, target, compound_set, zfile, zfile_hashvals
) -> Optional[SiteObservation]:
# The returned protein object may be None
# Get a SiteObservation from the molecule using
# a named property (i.e. lhs_pdb or ref_pdb for example)

pdb_fn = mol.GetProp('ref_pdb').split('/')[-1]
if not mol.HasProp(property_name):
logger.warning(
'Molecule %s has no "%s" property (%s, %s)',
mol,
property_name,
target,
compound_set,
)
return None

pdb_fn = mol.GetProp(property_name).split('/')[-1]
site_obvs = None

if zfile:
Expand Down Expand Up @@ -294,7 +305,6 @@ def set_mol(
self, mol, target, compound_set, filename, zfile=None, zfile_hashvals=None
) -> ComputedMolecule:
# Don't need...
assert mol
assert target
assert compound_set

Expand Down Expand Up @@ -343,19 +353,57 @@ def set_mol(

insp_frags.append(ref)

# Try to get the SiteObservation.
# Try to get the LHS SiteObservation,
# This will be used to set the ComputedMolecule.site_observation_code.
# This may fail.
lhs_property = 'lhs_pdb'
lhs_so = self.get_site_observation(
lhs_property,
mol,
target,
compound_set,
zfile,
zfile_hashvals=zfile_hashvals,
)
if not lhs_so:
logger.warning(
'Failed to get a LHS SiteObservation (%s) for %s, %s, %s',
lhs_property,
mol,
target,
compound_set,
)

# Try to get the reference SiteObservation,
# This will be used to set the ComputedMolecule.reference_code.
# This may fail.
prot = self.get_site_observation(
mol, target, compound_set, zfile, zfile_hashvals=zfile_hashvals
ref_property = 'ref_pdb'
ref_so = self.get_site_observation(
ref_property,
mol,
target,
compound_set,
zfile,
zfile_hashvals=zfile_hashvals,
)
if not prot:
if not ref_so:
logger.warning(
'Failed to get a SiteObservation for %s, %s, %s',
'Failed to get a Reference SiteObservation (%s) for %s, %s, %s',
ref_property,
mol,
target,
compound_set,
)

# A LHS or Reference protein must be provided.
# (Part of "Fix behaviour of RHS [P] button - also RHS upload change", issue #1249)
if not lhs_so and not ref_so:
logger.error(
'ComputedMolecule has no LHS (%s) or Reference (%s) property',
lhs_property,
ref_property,
)

# Need a ComputedMolecule before saving.
# Check if anything exists already...
existing_computed_molecules = ComputedMolecule.objects.filter(
Expand All @@ -382,7 +430,8 @@ def set_mol(
computed_molecule.compound = compound
computed_molecule.computed_set = compound_set
computed_molecule.sdf_info = Chem.MolToMolBlock(mol)
computed_molecule.site_observation_code = prot.code if prot else None
computed_molecule.site_observation_code = lhs_so.code if lhs_so else None
computed_molecule.reference_code = ref_so.code if ref_so else None
computed_molecule.molecule_name = molecule_name
computed_molecule.name = f"{target}-{computed_molecule.identifier}"
computed_molecule.smiles = smiles
Expand Down
30 changes: 30 additions & 0 deletions viewer/migrations/0029_add_cm_reference_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.2.23 on 2024-01-11 12:03

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('viewer', '0028_auto_20240108_1114'),
]

operations = [
migrations.AddField(
model_name='computedmolecule',
name='reference_code',
field=models.TextField(
blank=True,
help_text='The computed reference SiteObservation (the corresponding ref_pdb value if it has one)',
null=True,
),
),
migrations.AlterField(
model_name='computedmolecule',
name='site_observation_code',
field=models.TextField(
blank=True,
help_text='The LHS SiteObservation (the corresponding lhs_pdb value if it has one)',
null=True,
),
),
]
7 changes: 6 additions & 1 deletion viewer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,12 @@ class ComputedMolecule(models.Model):
compound = models.ForeignKey(Compound, on_delete=models.CASCADE)
sdf_info = models.TextField(help_text="The 3D coordinates for the molecule")
site_observation_code = models.TextField(
help_text="The LHS SiteObservation for this molecule",
help_text="The LHS SiteObservation (the corresponding lhs_pdb value if it has one)",
null=True,
blank=True,
)
reference_code = models.TextField(
help_text="The computed reference SiteObservation (the corresponding ref_pdb value if it has one)",
null=True,
blank=True,
)
Expand Down

0 comments on commit a3aa0c4

Please sign in to comment.