| 1 |
# Using CIF dictionaries with PyCIFRW |
| 2 |
|
| 3 |
## Introduction |
| 4 |
|
| 5 |
CIF dictionaries describe the meaning of the datanames found in CIF data files |
| 6 |
in a machine-readable format - the CIF format. Each block in a CIF dictionary |
| 7 |
defines a single dataname by assigning values to a limited set of attributes. |
| 8 |
This set of attributes used by a dictionary is called its 'Dictionary Definition |
| 9 |
Language' or DDL. Three languages have been used in IUCr-supported CIF dictionaries: |
| 10 |
DDL1 (the original language), DDL2 (heavily developed by the macromolecular community), and |
| 11 |
DDLm (a new standard that aims to unite the best of DDL1 and DDL2). DDL2 and DDLm both |
| 12 |
allow algorithms to be defined for datanames. These algorithms describe how to derive values for |
| 13 |
datanames from other quantities in the data file. |
| 14 |
|
| 15 |
Knowing the dictionary that a given datafile is written with reference to thus allows |
| 16 |
us to do two things: to *validate* that datanames and values match the constraints imposed |
| 17 |
by the definition; and, in the case of DDL2 and DDLm, to *calculate* values which might |
| 18 |
then be used for checking or simply to fill in missing information. |
| 19 |
|
| 20 |
## Dictionaries |
| 21 |
|
| 22 |
DDL dictionaries can be read into `CifFile` objects just like CIF data |
| 23 |
files. For this purpose, `CifFile` objects automatically support save |
| 24 |
frames (used in DDL2 and DDLm dictionaries), which are accessed just |
| 25 |
like `CifBlock`s using their save frame name. By default save frames |
| 26 |
are not listed as keys in `CifFile`s as they do not form part of the |
| 27 |
CIF standard. |
| 28 |
|
| 29 |
The more powerful `CifDic` object creats a unified interface to DDL1, |
| 30 |
DDL2 and DDLm dictionaries. A `CifDic` is initialised with a single file name |
| 31 |
or `CifFile` object, and will accept the grammar keyword: |
| 32 |
|
| 33 |
~~~{.python} |
| 34 |
cd = CifFile.CifDic("cif_core.dic",grammar='1.1') |
| 35 |
~~~ |
| 36 |
|
| 37 |
Definitions are accessed using the usual notation, e.g. |
| 38 |
`cd['_atom_site_aniso_label']`. Return values are always `CifBlock` |
| 39 |
objects. Additionally, the `CifDic` object contains a number of |
| 40 |
instance variables derived from dictionary global data: |
| 41 |
|
| 42 |
`dicname` |
| 43 |
: The dictionary name + version as given in the dictionary |
| 44 |
|
| 45 |
`diclang` |
| 46 |
: 'DDL1','DDL2', or 'DDLm' |
| 47 |
|
| 48 |
|
| 49 |
`CifDic` objects provide a large number of validation functions, |
| 50 |
which all return a Python dictionary which contains at least the |
| 51 |
key `result`. `result` takes the values `True`, `False` or `None` depending |
| 52 |
on the success, failure or non-applicability of each test. In |
| 53 |
case of failure, additional keys are returned depending on the |
| 54 |
nature of the error. |
| 55 |
|
| 56 |
## Validation with PyCIFRW |
| 57 |
|
| 58 |
A top level function is provided for convenient validation of CIF |
| 59 |
files: |
| 60 |
|
| 61 |
~~~{.python} |
| 62 |
CifFile.Validate("mycif.cif",dic = "cif_core.dic") |
| 63 |
~~~ |
| 64 |
|
| 65 |
This returns a tuple `(valid_result, no_matches)`. `valid_result` and |
| 66 |
`no_matches` are Python dictionaries indexed by block name. For |
| 67 |
`valid_result`, the value for each block is itself a dictionary |
| 68 |
indexed by item_name. The value attached to each item name is a |
| 69 |
list of `(check_function, check_result)` tuples, with `check_result` |
| 70 |
a small dictionary containing at least the key `result`. All tests |
| 71 |
which passed or were not applicable are removed from this |
| 72 |
dictionary, so `result` is always `False`. Additional keys contain |
| 73 |
auxiliary information depending on the test. Each of the items in |
| 74 |
`no_matches` is a simple list of item names which were not found in |
| 75 |
the dictionary. |
| 76 |
|
| 77 |
If a simple validation report is required, the function |
| 78 |
`validate_report` can be called on the output of the above |
| 79 |
function, printing a simple ASCII report. This function can be |
| 80 |
studied as an example of how to process the structure returned by |
| 81 |
the `validate` function. |
| 82 |
|
| 83 |
A somewhat nicer interface to validation is provided in the |
| 84 |
`ValidationResult` class (thanks to Boris Dusek), which is |
| 85 |
initialised with the return value from validate: |
| 86 |
|
| 87 |
~~~{.python} |
| 88 |
val_report = ValidationResult(validate("mycif.cif",dic="cif_core.dic")) |
| 89 |
~~~ |
| 90 |
|
| 91 |
This class provides the `report` method, producing a human-readable |
| 92 |
report, as well as boolean methods which return whether or not |
| 93 |
the block is valid or if items appear in the block that are not |
| 94 |
present in the dictionary - `is_valid` and `has_no_match_items` |
| 95 |
respectively. |
| 96 |
|
| 97 |
### Limitations on validation |
| 98 |
|
| 99 |
1. (DDL2 only) When validating data dictionaries themselves, no |
| 100 |
checks are made on group and subgroup consistency (e.g. that a |
| 101 |
specified subgroup is actually defined). |
| 102 |
|
| 103 |
2. (DDL1 only) Some `_type_construct` attributes in the DDL1 spec |
| 104 |
file are not machine-readable, so values cannot be checked for |
| 105 |
consistency |
| 106 |
|
| 107 |
3. DDLm validation methods are still in development so are not |
| 108 |
comprehensive. |