Skip to content

Commit

Permalink
Merge pull request #693 from xchem/m2ms-1524-rhs-new-bug
Browse files Browse the repository at this point in the history
Fix incrementing numeric code on new compound (issue 1524)
  • Loading branch information
kaliif authored Nov 26, 2024
2 parents b8561c1 + 3412a83 commit a7e70a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
23 changes: 12 additions & 11 deletions viewer/cset_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,25 +288,25 @@ def get_site_observation(

return site_obvs

def create_mol(self, inchi, target, name=None) -> Compound:
def create_mol(self, inchi, target, name=None) -> tuple[Compound, str]:
# check for an existing compound, returning a Compound

sanitized_mol = Chem.MolFromInchi(inchi, sanitize=True)
Chem.RemoveStereochemistry(sanitized_mol)
inchi = Chem.inchi.MolToInchi(sanitized_mol)
inchi_key = Chem.InchiToInchiKey(inchi)

qs = Compound.objects.filter(
computedmolecule__computed_set__target=target,
)
cpd_number = '1'
try:
# NB! Max said there could be thousands of compounds per
# target so this distinct() here may become a problem
cpd = qs.distinct().get(inchi_key=inchi_key)

# fmt: off
cpd = Compound.objects.filter(
computedmolecule__computed_set__target=target,
).distinct().get(
inchi_key=inchi_key,
)
# fmt: on
# memo to self: I'm not setting cpd_number here, because
# it's read from computedmol name
except Compound.DoesNotExist:
cpd = Compound(
smiles=Chem.MolToSmiles(sanitized_mol),
Expand All @@ -319,6 +319,7 @@ def create_mol(self, inchi, target, name=None) -> Compound:
# This is a new compound.
# We must now set relationships to the Proposal that it applies to.
cpd.project_id.add(target.project)
cpd_number = str(qs.count() + 1)
except MultipleObjectsReturned as exc:
# NB! when processing new uploads, Compound is always
# fetched by inchi_key, so this shouldn't ever create
Expand All @@ -334,7 +335,7 @@ def create_mol(self, inchi, target, name=None) -> Compound:
)
raise MultipleObjectsReturned from exc

return cpd
return cpd, cpd_number

def set_props(self, cpd, props, score_descriptions) -> List[ScoreDescription]:
if 'ref_mols' and 'ref_pdb' not in list(props.keys()):
Expand Down Expand Up @@ -374,7 +375,7 @@ def set_mol(
Chem.RemoveStereochemistry(mol)
flat_inchi = Chem.inchi.MolToInchi(flattened_copy)

compound: Compound = self.create_mol(
compound, number = self.create_mol(
inchi, compound_set.target, name=molecule_name
)

Expand Down Expand Up @@ -448,7 +449,7 @@ def set_mol(
suffix = next(alphanumerator(start_from=groups.groups()[2])) # type: ignore [index]
else:
suffix = 'a'
number = 1
# number = 1

name = f'v{number}{suffix}'

Expand Down
3 changes: 3 additions & 0 deletions viewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,13 +1502,16 @@ def get_view_name(self):

def create(self, request, *args, **kwargs):
logger.info("+ UploadTargetExperiments.create called")
logger.debug("UploadTargetExperiments serializer data: %s", request.data)
del args, kwargs

serializer = self.get_serializer_class()(data=request.data)
if not serializer.is_valid():
logger.debug("serializer not valid: %s", serializer.errors)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

logger.debug("Serializer validated_data=%s", serializer.validated_data)
logger.debug("User=%s", self.request.user)

target_access_string = serializer.validated_data['target_access_string']
contact_email = serializer.validated_data['contact_email']
Expand Down

0 comments on commit a7e70a3

Please sign in to comment.