From 744874f425d48e86c8a6687df6006e1b7fe4594c Mon Sep 17 00:00:00 2001 From: Pablo Martinez Bulit Date: Thu, 1 May 2025 15:17:30 +0100 Subject: [PATCH 1/2] Added filter for chirality NO_JIRA --- .../entry_property_calculator.py | 38 +++++++++++++++++++ .../refcodes_with_properties.py | 4 +- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/scripts/refcodes_with_properties/entry_property_calculator.py b/scripts/refcodes_with_properties/entry_property_calculator.py index 588a17e..8300cbf 100644 --- a/scripts/refcodes_with_properties/entry_property_calculator.py +++ b/scripts/refcodes_with_properties/entry_property_calculator.py @@ -85,6 +85,20 @@ def __call__(self, theobject): return value >= self.minimum and value <= self.maximum +class _ValueFilter(_Filter): + def __init__(self, args): + values = [p for p in args.split()] + #To do: add option for two values? + self.expected_value = values[0] + + def value(self, theobject): + raise NotImplementedError # override this + + def __call__(self, theobject): + value = self.value(theobject) + return value == self.expected_value + + class AllowedAtomicNumbersFilter(_Filter): def __init__(self, args): self.allowed_atomic_numbers = [int(atomic_number) for atomic_number in args.strip().split()] @@ -413,6 +427,30 @@ def value(self, entry): register(SpacegroupNumberFilter) +class ChiralityFilter(_ValueFilter): + def __init__(self, args): + super().__init__(args) + + @staticmethod + def name(): + return "chirality" + + @staticmethod + def helptext(): + return "specify the chirality value to be used as filter" + + def value(self, entry): + try: + molecule = entry.crystal.molecule + chirality = next((atom.chirality for atom in molecule.atoms if atom.is_chiral), None) + return chirality + except TypeError: + return 0 + + +register(ChiralityFilter) + + class FilterEvaluation(object): def __init__(self): self._methods = [] diff --git a/scripts/refcodes_with_properties/refcodes_with_properties.py b/scripts/refcodes_with_properties/refcodes_with_properties.py index e98b7ea..df92dd6 100644 --- a/scripts/refcodes_with_properties/refcodes_with_properties.py +++ b/scripts/refcodes_with_properties/refcodes_with_properties.py @@ -46,7 +46,7 @@ outfile = sys.stdout if args.output_file is not None: - outfile = open(args.output_file, 'wb') + outfile = open(args.output_file, 'w', encoding='utf-8') filterer = entry_property_calculator.parse_control_file(open(control_file, "r").readlines()) @@ -73,4 +73,4 @@ else: for entry in reader: if filterer.evaluate(entry): - outfile.write(entry.identifier + "\n") + outfile.write(entry.identifier + "\n") \ No newline at end of file From 6c174d51b768e5a1158a63420d2b2ee0167861d5 Mon Sep 17 00:00:00 2001 From: Pablo Martinez Bulit Date: Fri, 2 May 2025 10:42:53 +0100 Subject: [PATCH 2/2] Added filter 3D structures and fixed SG bug NO_JIRA --- .../entry_property_calculator.py | 28 +++++++++++++++++-- .../refcodes_with_properties.py | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/scripts/refcodes_with_properties/entry_property_calculator.py b/scripts/refcodes_with_properties/entry_property_calculator.py index 8300cbf..aa1ee9f 100644 --- a/scripts/refcodes_with_properties/entry_property_calculator.py +++ b/scripts/refcodes_with_properties/entry_property_calculator.py @@ -89,7 +89,10 @@ class _ValueFilter(_Filter): def __init__(self, args): values = [p for p in args.split()] #To do: add option for two values? - self.expected_value = values[0] + if values[0] == 'None': + self.expected_value = None + else: + self.expected_value = values[0] def value(self, theobject): raise NotImplementedError # override this @@ -214,6 +217,25 @@ def value(self, entry): register(AllHaveSitesFilter) +class Has3DStructure(_ComparativeFilter): + def __init__(self, args): + super().__init__(args) + + @staticmethod + def name(): + return "has 3D structure" + + @staticmethod + def helptext(): + return "whether 3D coordinates have been determined for the structure" + + def value(self, entry): + return entry.has_3d_structure + + +register(Has3DStructure) + + class DisorderedFilter(_ComparativeFilter): def __init__(self, args): super().__init__(args) @@ -445,7 +467,7 @@ def value(self, entry): chirality = next((atom.chirality for atom in molecule.atoms if atom.is_chiral), None) return chirality except TypeError: - return 0 + return None register(ChiralityFilter) @@ -463,7 +485,7 @@ def evaluate(self, entry): try: if not method(entry): return False - except TypeError: + except (TypeError, RuntimeError): return False return True diff --git a/scripts/refcodes_with_properties/refcodes_with_properties.py b/scripts/refcodes_with_properties/refcodes_with_properties.py index df92dd6..474fc68 100644 --- a/scripts/refcodes_with_properties/refcodes_with_properties.py +++ b/scripts/refcodes_with_properties/refcodes_with_properties.py @@ -73,4 +73,4 @@ else: for entry in reader: if filterer.evaluate(entry): - outfile.write(entry.identifier + "\n") \ No newline at end of file + outfile.write(entry.identifier + "\n")