Skip to content

Commit 020bc5e

Browse files
authored
Chirality and 3D structure filters added NO_JIRA
Added option to filter for one specific chirality value, might extend this at some point if anyone requests it. Added 3D structure bool filter Added exception when RuntimeError is raised for some space group searches
2 parents b05f1db + 6c174d5 commit 020bc5e

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

scripts/refcodes_with_properties/entry_property_calculator.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ def __call__(self, theobject):
8585
return value >= self.minimum and value <= self.maximum
8686

8787

88+
class _ValueFilter(_Filter):
89+
def __init__(self, args):
90+
values = [p for p in args.split()]
91+
#To do: add option for two values?
92+
if values[0] == 'None':
93+
self.expected_value = None
94+
else:
95+
self.expected_value = values[0]
96+
97+
def value(self, theobject):
98+
raise NotImplementedError # override this
99+
100+
def __call__(self, theobject):
101+
value = self.value(theobject)
102+
return value == self.expected_value
103+
104+
88105
class AllowedAtomicNumbersFilter(_Filter):
89106
def __init__(self, args):
90107
self.allowed_atomic_numbers = [int(atomic_number) for atomic_number in args.strip().split()]
@@ -200,6 +217,25 @@ def value(self, entry):
200217
register(AllHaveSitesFilter)
201218

202219

220+
class Has3DStructure(_ComparativeFilter):
221+
def __init__(self, args):
222+
super().__init__(args)
223+
224+
@staticmethod
225+
def name():
226+
return "has 3D structure"
227+
228+
@staticmethod
229+
def helptext():
230+
return "whether 3D coordinates have been determined for the structure"
231+
232+
def value(self, entry):
233+
return entry.has_3d_structure
234+
235+
236+
register(Has3DStructure)
237+
238+
203239
class DisorderedFilter(_ComparativeFilter):
204240
def __init__(self, args):
205241
super().__init__(args)
@@ -413,6 +449,30 @@ def value(self, entry):
413449
register(SpacegroupNumberFilter)
414450

415451

452+
class ChiralityFilter(_ValueFilter):
453+
def __init__(self, args):
454+
super().__init__(args)
455+
456+
@staticmethod
457+
def name():
458+
return "chirality"
459+
460+
@staticmethod
461+
def helptext():
462+
return "specify the chirality value to be used as filter"
463+
464+
def value(self, entry):
465+
try:
466+
molecule = entry.crystal.molecule
467+
chirality = next((atom.chirality for atom in molecule.atoms if atom.is_chiral), None)
468+
return chirality
469+
except TypeError:
470+
return None
471+
472+
473+
register(ChiralityFilter)
474+
475+
416476
class FilterEvaluation(object):
417477
def __init__(self):
418478
self._methods = []
@@ -425,7 +485,7 @@ def evaluate(self, entry):
425485
try:
426486
if not method(entry):
427487
return False
428-
except TypeError:
488+
except (TypeError, RuntimeError):
429489
return False
430490

431491
return True

scripts/refcodes_with_properties/refcodes_with_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
outfile = sys.stdout
4848
if args.output_file is not None:
49-
outfile = open(args.output_file, 'wb')
49+
outfile = open(args.output_file, 'w', encoding='utf-8')
5050

5151
filterer = entry_property_calculator.parse_control_file(open(control_file, "r").readlines())
5252

0 commit comments

Comments
 (0)