Skip to content

Commit

Permalink
Merge pull request #528 from xchem/m2ms-1334-short-code-prefix
Browse files Browse the repository at this point in the history
Short code prefix and tooltip read from yaml file (issue 1334)
  • Loading branch information
kaliif authored Feb 14, 2024
2 parents 4a703ed + 632719a commit 654f87c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions viewer/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def filter_qs(self):
).annotate(
target=F("experiment__experiment_upload__target"),
compound_code=F("cmpd__compound_code"),
prefix_tooltip=F("experiment__prefix_tooltip"),
)

return qs
Expand Down
17 changes: 17 additions & 0 deletions viewer/migrations/0043_experiment_prefix_tooltip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.23 on 2024-02-13 15:12

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('viewer', '0042_alter_xtalformsite_xtalform_site_num'),
]

operations = [
migrations.AddField(
model_name='experiment',
name='prefix_tooltip',
field=models.TextField(null=True),
),
]
1 change: 1 addition & 0 deletions viewer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class Experiment(models.Model):
map_info = ArrayField(models.FileField(max_length=255), null=True)
type = models.PositiveSmallIntegerField(null=True)
pdb_sha256 = models.TextField(null=True)
prefix_tooltip = models.TextField(null=True)
compounds = models.ManyToManyField(
"Compound",
through="ExperimentCompound",
Expand Down
1 change: 1 addition & 0 deletions viewer/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,7 @@ class Meta:

class SiteObservationReadSerializer(serializers.ModelSerializer):
compound_code = serializers.StringRelatedField()
prefix_tooltip = serializers.StringRelatedField()

class Meta:
model = models.SiteObservation
Expand Down
38 changes: 26 additions & 12 deletions viewer/target_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ def _enumerate_objects(self, objects: dict, attr: str) -> None:
def process_experiment(
self,
item_data: tuple[str, dict] | None = None,
prefix_tooltips: dict[str, str] | None = None,
validate_files: bool = True,
**kwargs,
) -> ProcessedObject | None:
Expand Down Expand Up @@ -734,6 +735,7 @@ def process_experiment(
"""
del kwargs
assert item_data
assert prefix_tooltips
logger.debug("incoming data: %s", item_data)
experiment_name, data = item_data

Expand Down Expand Up @@ -813,6 +815,9 @@ def process_experiment(
# version int old versions are kept target loader
version = 1

code_prefix = extract(key="code_prefix")
prefix_tooltip = prefix_tooltips.get(code_prefix, "")

fields = {
"code": experiment_name,
}
Expand All @@ -830,6 +835,7 @@ def process_experiment(
"mtz_info": str(self._get_final_path(mtz_info)),
"cif_info": str(self._get_final_path(cif_info)),
"map_info": map_info_paths,
"prefix_tooltip": prefix_tooltip,
# this doesn't seem to be present
# pdb_sha256:
}
Expand All @@ -839,6 +845,7 @@ def process_experiment(
index_fields = {
"xtalform": assigned_xtalform,
"smiles": smiles,
"code_prefix": code_prefix,
}

return ProcessedObject(
Expand Down Expand Up @@ -1437,7 +1444,6 @@ def process_bundle(self):
self.report.log(logging.ERROR, msg)
raise KeyError(msg) from exc

# moved this bit from init
self.target, target_created = Target.objects.get_or_create(
title=self.target_name,
display_name=self.target_name,
Expand Down Expand Up @@ -1475,6 +1481,7 @@ def process_bundle(self):
self.version_number = meta["version_number"]
self.version_dir = meta["version_dir"]
self.previous_version_dirs = meta["previous_version_dirs"]
prefix_tooltips = meta["code_prefix_tooltips"]

# check transformation matrix files
( # pylint: disable=unbalanced-tuple-unpacking
Expand Down Expand Up @@ -1533,7 +1540,9 @@ def process_bundle(self):
),
)

experiment_objects = self.process_experiment(yaml_data=crystals)
experiment_objects = self.process_experiment(
yaml_data=crystals, prefix_tooltips=prefix_tooltips
)
compound_objects = self.process_compound(
yaml_data=crystals, experiments=experiment_objects
)
Expand Down Expand Up @@ -1643,16 +1652,14 @@ def process_bundle(self):
canon_site_confs=canon_site_conf_objects,
)

values = ["xtalform_site__xtalform", "canon_site_conf__canon_site", "cmpd"]
# values = ["canon_site_conf__canon_site", "cmpd"]
values = ["experiment"]
qs = (
SiteObservation.objects.values(*values)
.order_by(*values)
.annotate(obvs=ArrayAgg("id"))
.values_list("obvs", flat=True)
)
current_list = SiteObservation.objects.filter(
experiment__experiment_upload__target=self.target
).values_list('code', flat=True)
for elem in qs:
# objects in this group should be named with same scheme
so_group = SiteObservation.objects.filter(pk__in=elem)
Expand Down Expand Up @@ -1681,20 +1688,27 @@ def process_bundle(self):
# technically it should be validated in previous try-catch block
logger.error("Non-standard SiteObservation code 2: %s", last)

logger.debug("iter_pos: %s", iter_pos)

# ... and create new one starting from next item
suffix = alphanumerator(start_from=iter_pos)
for so in so_group.filter(code__isnull=True):
code = f"{so.experiment.code.split('-')[1]}{next(suffix)}"
code_prefix = experiment_objects[so.experiment.code].index_data[
"code_prefix"
]
code = f"{code_prefix}{so.experiment.code.split('-')[1]}{next(suffix)}"

# test uniqueness for target
# TODO: this should ideally be solved by db engine, before
# rushing to write the trigger, have think about the
# loader concurrency situations
prefix = alphanumerator()
while code in current_list:
code = f"{next(prefix)}{code}"
if SiteObservation.objects.filter(
experiment__experiment_upload__target=self.target,
code=code,
).exists():
msg = (
f"short code {code} already exists for this target; "
+ "specify a code_prefix to resolve this conflict"
)
self.report.log(logging.ERROR, msg)

so.code = code
so.save()
Expand Down

0 comments on commit 654f87c

Please sign in to comment.