nuri.fmt
- class nuri.fmt.MoleculeReader
- __iter__(self: MoleculeReader) MoleculeReader
Returns itself.
- __next__(self: MoleculeReader) Molecule
Returns the next molecule.
CIF Format Support
from nuri.fmt import cif
CIF format-specific handlers and utilities.
- class nuri.fmt.cif.Block
- __init__(self: Block, data: Frame, save: Iterable[Frame] = ()) None
Construct a CIF block.
- Parameters:
data – The main frame of the block; its name is used as the block name.
save – The save frames contained in the block.
- Raises:
ValueError – If a save frame has an empty name or two share a name.
- property data
- property is_global
- property name
- property save_frames
- class nuri.fmt.cif.Frame
- __init__(self: Frame, name: str, tables: Sequence[Table]) None
Construct a CIF frame (data block body or save frame) from tables.
- Parameters:
name – The frame name.
tables – The tables that make up the frame.
- Raises:
ValueError – If a column key is duplicated across the tables.
- as_ddl2_dict(self: Frame) dict[str, list[dict[str, str | None]]]
Convert this DDL2 (mmCIF) frame to a dictionary of lists of dictionaries.
- Returns:
A dictionary of lists of dictionaries, where the keys are the parent keys and the values are the rows of the table.
- as_mols(self: Frame) list[Molecule]
Load this frame as a list of molecules.
- Returns:
A list of molecules loaded from the frame.
- prefix_search_first(self: Frame, prefix: str) Table | None
Search for the first table containing a column starting with the given prefix.
- Parameters:
prefix – The prefix to search for.
- Returns:
The first table containing the given prefix, or None if not found.
- property name
- class nuri.fmt.cif.Table
- __init__(self: Table, keys: Sequence[str], rows: Sequence[Sequence[str | int | float | bool | Value | None]], category: str = '', column_formats: dict[str, ColumnFormat] = {}) None
Construct a CIF table from column keys and rows of values.
- Parameters:
keys – The column keys, given without the leading underscore (e.g.
["atom_site.id", "atom_site.type_symbol"]); a_is prepended to each.rows – The rows of the table. Each row must have exactly
len(keys)cells. A cell may be astr,int,bool,float,None, or aValue.category – Optional DDL2 category. When non-empty, each key is formed as
_<category>.<key>, sokeysmay list just the attribute names (e.g.category="atom_site", keys=["id", "type_symbol"]). Empty (the default) leaves keys as given.column_formats – Optional per-column formatting options, mapping a column key (as given in
keys) to aColumnFormatapplied when a plain scalar (orNone) cell in that column is converted. Each option applies only to cells of its matching type (seeColumnFormat); columns not listed use the defaults. ExplicitValuecells are unaffected.
- Raises:
ValueError – If a row length differs from the number of keys, a key is not a valid or unique CIF data name, or
column_formatsnames an unknown key.TypeError – If a cell has an unsupported type.
- class nuri.fmt.cif.Value
An explicit CIF value, for use as a
Tablecell.Most cells can be given as plain Python objects (
str,int,bool,float, orNone); construct aValuedirectly only when you need control over the formatting. The constructor is overloaded on the type ofvalue.- __init__(*args, **kwargs)
Overloaded function.
__init__(self: nuri.fmt.cif.Value, value: str, raw: bool = False) -> None
Store text as a CIF value.
- Parameters:
value – The text to store.
raw – If
True, store the text as an unquoted generic literal (e.g. a standard-uncertainty token like1.234(5)). Defaults toFalse: a string, quoted on write only if its content requires it.
__init__(self: nuri.fmt.cif.Value, value: bool, short_form: bool = False) -> None
Store a boolean CIF value.
- Parameters:
value – The boolean to store.
short_form – Use
y/ninstead ofyes/no.
__init__(self: nuri.fmt.cif.Value, value: int, width: int = 0) -> None
Store an integer CIF value.
- Parameters:
value – The integer to store.
width – If positive, zero-pad the number to at least this many digits.
__init__(self: nuri.fmt.cif.Value, value: float, precision: Optional[int] = None, coerce_nonfinite: bool = False, null_token: str = ‘?’) -> None
Store a floating-point CIF value.
- Parameters:
value – The number to store.
precision – Digits after the decimal point; if
None(the default), yields at most 6 significant digits. Must be non-negative if provided.coerce_nonfinite – How to handle a non-finite
value. IfFalse(the default), a non-finite value has no valid CIF representation and raisesValueErrorwhen serialized. IfTrue,NaNbecomes the null value chosen bynull_tokenand+/-Infbecomes the sentinel+/-8e+88888888, which any IEEE-conformant parser (up tobinary256) reads back as the original infinity.null_token – The CIF null token that
NaNis coerced to whencoerce_nonfiniteisTrue:"?"(the default) for unknown, or"."for inapplicable. Ignored for finite values.
- Raises:
ValueError – If
null_tokenis not"?"or".".
__init__(self: nuri.fmt.cif.Value, value: None, null_token: str = ‘?’) -> None
Store a null CIF value.
- Parameters:
value –
None.null_token – The CIF null token:
"?"(the default) for the unknown value, or"."for the inapplicable value.
- Raises:
ValueError – If
null_tokenis not"?"or".".
- class nuri.fmt.cif.ColumnFormat
- property coerce_nonfinite
- property null_token
- property precision
- property raw
- property short_form
- property width
- nuri.fmt.cif.read_blocks(path: PathLike) Iterator[Block]
Create a parser object from a CIF file path.
- Parameters:
path – The path to the CIF file.
- Returns:
An iterator over the blocks in the file.
- nuri.fmt.cif.write(*args, **kwargs)
Overloaded function.
write(frame: nuri.fmt.cif.Frame, align: bool = False) -> str
Serialize a CIF frame to a CIF 1.1 string.
- Parameters:
frame – The
Frameto serialize, written as adata_block. Both freshly constructed and parsed objects are accepted.align – Whether to pad the columns of
loop_tables so that values line up. Defaults toFalse.
- Returns:
The serialized CIF string.
- Raises:
ValueError – If a value cannot be represented in CIF 1.1.
write(block: nuri.fmt.cif.Block, align: bool = False) -> str
Serialize a CIF block to a CIF 1.1 string.
- Parameters:
block – The
Blockto serialize.align – Whether to pad the columns of
loop_tables so that values line up. Defaults toFalse.
- Returns:
The serialized CIF string.
- Raises:
ValueError – If a value cannot be represented in CIF 1.1.
Note
A
global_block (only ever produced by the parser, from a STAR file) is written back as-is, i.e. as aglobal_block. This is a STAR construct and is not valid CIF 1.1; a warning is logged in that case.>>> import nuri >>> table = nuri.fmt.cif.Table(["x.a", "x.b"], [[1, "two words"], [None, nuri.fmt.cif.Value(None, null_token=".")]]) >>> block = nuri.fmt.cif.Block(nuri.fmt.cif.Frame("demo", [table])) >>> print(nuri.fmt.cif.write(block)) data_demo loop_ _x.a _x.b 1 'two words' ? .
PDB Format Support
from nuri.fmt import pdb
PDB format-specific handlers and utilities.
- class nuri.fmt.pdb.Atom
- property element
- property formal_charge
- property hetero
- property name
- property res_id
- property sites
- class nuri.fmt.pdb.AtomSite
- property altloc
- property occupancy
- property pos
- property tempfactor
- class nuri.fmt.pdb.Model
-
- property atoms
- property chains
- property major_conf
- property props
- property residues
- nuri.fmt.pdb.read_models(path: PathLike, skip_on_error: bool = False) list[Model]
Read PDB models from a file.
- Parameters:
path – The path to the PDB file.
skip_on_error – Whether to skip a model if an error occurs, instead of raising an exception.
- Raises:
OSError – If any file-related error occurs.
ValueError – If reading a model fails, unless skip_on_error is set.