class Chem::Templates::Registry

Overview

Stores a map of residue templates (see Residue).

It behaves as a hash, where residue templates are indexed (registered) by the residue names.

A global registry can be accessed via the Registry.default method, which contains the default templates as well as other templates that have been globally registered. For instance, the topology detection mechanism (see Topology::Detector) uses the global registry unless a registry is specified.

Local registries could be used to selectively detect a few templates, which would speed up the process if existing residues are known beforehand, or for another reason.

Loading residue templates

Residue templates can be parsed and loaded from a file, IO, or string that encodes a registry in the YAML format (refer to the Format specification section).

The following example shows how to create a new registry directly from YAML content:

registry = Chem::Templates::Registry.from_yaml <<-YAML
  templates:
    - description: Phenylalanine
      names: [PHE, PHY]
      code: F
      type: protein
      link_bond: C-N
      spec: '{backbone}-CB-CG%1=CD1-CE1=CZ-CE2=CD2-%1'
      symmetry:
    - [[CD1, CD2], [CE1, CE2]]
  aliases:
    backbone: N(-H)-CA(-HA)(-C=O)
  YAML
registry.size               # => 1
registry["PHE"].description # => "Phenylalanine"

Residue templates can also be loaded into an existing registry using either the #load or #parse methods.

registry = Chem::Templates::Registry.new.parse <<-YAML
  ...
  YAML

NOTE A YAML file can be baked into the executable using the read_file macro at compilation time so it's can be executed anywhere. Otherwise, the hardcoded filepath may be unaccesible at runtime.

registry = Chem::Templates::Registry.from_yaml {{read_file("/path/to/yaml")}}

Registering a new residue template

Residue templates can also be registered using the DSL provided by the Builder type using the #register method:

registry = Chem::Templates::Registry.new
res_t = registry.register do
  description "Phenylalanine"
  names %w(PHE PHY)
  code 'F'
  type :protein
  link_bond "C-N"
  spec "{backbone}-CB-CG%1=CD1-CE1=CZ-CE2=CD2-%1"
  symmetry({CD1, CD2}, {CE1, CE2})
end
registry["PHE"] == res_t # => true

Retrieving a residue template

Residue templates are indexed by every residue name, so they can be accessed using the bracket methods much like a hash.

registry = Chem::Templates::Registry.from_yaml <<-YAML
  names: [CX1, CX2]
  description: "Fake residue"
  spec: CX
  YAML
registry["CX1"].description        # => "Fake residue"
registry["CX1"] == registry["CX2"] # => true
registry["CX3"]                    # Raises Chem::Error
registry["CX3"]?                   # => nil

Format specification

As shown above, residue templates can be encoded in a YAML document.

Residue templates can be defined either under the templates field or at the top-level, and listed as an array of records. A single record can also be specified at the top level.

The following examples are equivalent:

templates:
  - name: LFG
    spec: '[N1H3+]-C2-C3-O4-C5(-C6)=O7'
- name: LFG
  spec: '[N1H3+]-C2-C3-O4-C5(-C6)=O7'
name: LFG
spec: '[N1H3+]-C2-C3-O4-C5(-C6)=O7'

The records are transformed into Residue instances via the Builder type. Each allowed field correspond to a specific method of the builder. The latter dictates the type of data and also handles validation.

Aliases for common residue template specifications (spec aliases) can be defined as a map under the aliases field at the top level:

...
aliases:
  backbone: 'N(-H)-CA(-HA)(-C=O)'

Registered aliases are passed down to the SpecParser type such that they are expanded when parsing the specification of a residue template via the bracket syntax, e.g., '%{backbone}-CB-OH'.

Defined in:

chem/templates/registry.cr

Constructors

Instance Method Summary

Constructor Detail

def self.default : self #

Returns the global residue template registry.

NOTE It loads the default templates including proteinogenic aminoacids and their variants (e.g., HIS, HSE, and HSP), and solvents such as water.


[View source]
def self.from_yaml(content : IO | String) : self #

Returns the residue templates encoded in the given YAML content.


[View source]
def self.new #

Creates a new, empty residue template registry.


[View source]
def self.read(filepath : Path | String) : self #

Returns the residue templates encoded in the given YAML file.


[View source]

Instance Method Detail

def <<(res_t : Residue) : self #

Adds the residue template to the registry. The template is registered under every residue name.

Raises Error if any of the residue names already exists.


[View source]
def <<(ter_t : Ter) : self #

Adds the termination template to the registry. The template is registered under every name.

Raises Error if any of the names already exists.


[View source]
def [](name : String) : Residue #

Returns the residue template registered under name. Raises Error if no template exist with the given name.


[View source]
def []?(name : String) : Residue | Nil #

Returns the residue template registered under name, or nil if no template exist with the given name.


[View source]
def alias(new_name : String, to existing_name : String) : self #

Registers an aliases to a known residue template. Raises Error if name is not registered.


[View source]
def includes?(res_t : Residue) : Bool #

Returns true if the registry contains the given residue template, else false. Both template's name and content are checked for a match.


[View source]
def load(filepath : Path | String) : self #

Loads the residue template(s) encoded in the given YAML or structure file into the registry.

If a valid structure file (checked via Format.from_filename?) is passed, it's read into a Structure instance, and the first residue is transformed into a template by calling Residue.build.

Otherwise, the content of the YAML file is parsed by calling Registry#parse.


[View source]
def parse(io : IO) : self #

Parses the residue templates encoded in the given YAML content into the registry. Refer to the Format specification section above.

Validation on template data is handled by Builder, which may raise Error.


[View source]
def parse(content : String) : self #

Parses the residue templates encoded in the given YAML content into the registry. Refer to the Format specification section above.

Validation on template data is handled by Builder, which may raise Error.


[View source]
def register(structure : Structure) : Residue #

Convenience method that creates and registers a new residue template from a structure. See Residue.build and #<<.


[View source]
def register(&) : Residue #

Convenience method that creates and registers a new residue template. See Builder and #<<.


[View source]
def reject(& : Residue -> _) : self #

Returns a new registry with all the residue templates for which the passed block is falsey.


[View source]
def select(& : Residue -> _) : self #

Returns a new registry with all the residue templates for which the passed block is truthy.


[View source]
def size : Int32 #

Number of residue templates.


[View source]
def spec_alias(name : String, spec : String) : self #

Registers an alias for a residue template specification.

Registered aliases are passed to SpecParser such that they are expanded when parsing the specification of a residue template.


[View source]
def ters : Array(Ter) #

Returns an array containing all the termination templates.


[View source]
def to_a : Array(Residue) #

Returns an array containing all the residue templates.


[View source]