File: token_reader.py

package info (click to toggle)
python-ihm 2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,368 kB
  • sloc: python: 30,422; ansic: 5,990; sh: 24; makefile: 20
file content (30 lines) | stat: -rw-r--r-- 1,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# This example demonstrates the use of the Python IHM library at a very
# low level, to perform housekeeping tasks on an mmCIF file without
# making large changes to its structure, and preserving whitespace,
# case, and comments.

# Note that unlike higher-level interfaces, the tokenizer can generate
# invalid mmCIF if used incorrectly. It is recommended that the resulting
# mmCIF files are run through a validator, as in the `validate_pdb_dev.py`
# example.

import ihm.format

filters = [
    # Change chain ID 'B' to 'Z' by altering the _struct_asym table
    ihm.format.ChangeValueFilter('_struct_asym.id', old='B', new='Z'),
    # Note that the tokenizer does not parse parent-child relationships
    # or understand the underlying dictionary. So we must also change other
    # tables that reference chain IDs. Here we change the label_asym_id keyword
    # in *any* table (typically in _atom_site).
    ihm.format.ChangeValueFilter('.label_asym_id', old='B', new='Z'),
    # Remove the non-standard _modeller.version data item from the file
    ihm.format.RemoveItemFilter('_modeller.version')]

# Read the input file as a set of tokens, modify them using the filters
# above, and write a new file:
with open('mini.cif') as fh_in:
    r = ihm.format.CifTokenReader(fh_in)
    with open('output.cif', 'w') as fh_out:
        for token in r.read_file(filters):
            fh_out.write(token.as_mmcif())