enum Chem::Format

Overview

List of the registered file formats.

This enum is populated based on the RegisterFormat annotation. Methods that deals with extensions and file patterns uses the information declared in the annotations.

@[Chem::RegisterFormat(ext: %w(.jpg .jpeg .jpe)), names: %w(IMG*)]
module JPEG; end

@[Chem::RegisterFormat(ext: %w(.tiff .tif))]
module TIFF; end

Chem::Format.names                 # => ["JPEG", "TIFF"]
Chem::Format::JPEG                 # => JPEG
Chem::Format::JPEG.extnames        # => [".jpg", ".jpeg", ".jpe"]
Chem::Format::JPEG.file_patterns   # => ["IMG*"]
Chem::Format::TIFF.extnames        # => [".tiff", ".tif"]
Chem::Format::TIFF.file_patterns   # => []

Chem::Format.from_ext("foo.jpg")   # => JPEG
Chem::Format.from_ext("foo.tiff")  # => TIFF
Chem::Format.from_stem("IMG_2015") # => JPEG

Defined in:

chem/format.cr

Enum Members

Chgcar = 0

The Chgcar format implemented by Chem::VASP::Chgcar.

Cube = 1

The Cube format implemented by Chem::Cube.

DX = 2

The DX format implemented by Chem::DX.

Gen = 3

The Gen format implemented by Chem::Gen.

Locpot = 4

The Locpot format implemented by Chem::VASP::Locpot.

Mol = 5

The Mol format implemented by Chem::Mol.

Mol2 = 6

The Mol2 format implemented by Chem::Mol2.

PDB = 7

The PDB format implemented by Chem::PDB.

PSF = 8

The PSF format implemented by Chem::PSF.

Poscar = 9

The Poscar format implemented by Chem::VASP::Poscar.

PyMOL = 10

The PyMOL format implemented by Chem::PyMOL.

SDF = 11

The SDF format implemented by Chem::SDF.

Stride = 12

The Stride format implemented by Chem::Protein::Stride.

VMD = 13

The VMD format implemented by Chem::VMD.

XYZ = 14

The XYZ format implemented by Chem::XYZ.

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.from_ext(extname : String) : self #

Returns the file format registered to the file extension, or raises ArgumentError otherwise.

@[Chem::RegisterFormat(ext: %w(.jpg .jpeg .jpe))]
module JPEG; end

Chem::Format.from_ext(".jpg")  # => JPEG
Chem::Format.from_ext(".JPG")  # => JPEG
Chem::Format.from_ext(".jpeg") # => JPEG
Chem::Format.from_ext(".jpe")  # => JPEG
Chem::Format.from_ext(".txt")  # raises ArgumentError

NOTE It performs a case-insensitive search so .jpg and .JPG return the same.


[View source]
def self.from_filename(filename : Path | String) : self #

Returns the file format associated with filename, or raises ArgumentError otherwise.

It first looks up the file format associated with the extension of filename via the .from_ext? method. If the latter returns nil, then it executes a case-insensitive search with the stem of filename via .from_stem?.

@[Chem::RegisterFormat(ext: %w(.jpg .jpeg .jpe), names: %w(IMG*))]
module JPEG; end

Chem::Format.from_filename("foo.jpg")      # => JPEG
Chem::Format.from_filename("foo.JPG")      # => JPEG
Chem::Format.from_filename("IMG_2314.jpg") # => JPEG
Chem::Format.from_filename("IMG_2314.png") # => JPEG
Chem::Format.from_filename("IMG_2314")     # => JPEG
Chem::Format.from_filename("img_2314")     # => JPEG
Chem::Format.from_filename("img2314")      # => JPEG
Chem::Format.from_filename("foo")          # raises ArgumentError

[View source]
def self.from_stem(stem : Path | String) : self #

Returns the file format that matches the file stem, or raises ArgumentError otherwise.

The file stem is matched against the file patterns registered by the file formats until one match is found. File patterns can contain valid filename characters and the * wildcard, which matches an unlimited number of arbitrary characters:

  • "c*" matches file stems beginning with c.
  • "*c" matches file stems ending with c.
  • "*c*" matches file stems that have c in them (including at the beginning or end).
@[Chem::RegisterFormat(names: %w(IMG*))]
module JPEG; end

Chem::Format.from_stem("IMG_2314") # => JPEG
Chem::Format.from_stem("img_2314") # => JPEG
Chem::Format.from_stem("img2314")  # => JPEG
Chem::Format.from_stem("himg")     # raises ArgumentError
Chem::Format.from_stem("foo")      # raises ArgumentError

NOTE The comparison is made using String#camelcase and String#downcase, so the file pattern FooBar will match FOOBAR, FooBar, foobar, FOO_BAR and foo_bar.


[View source]

Class Method Detail

def self.from_ext?(extname : String) : self | Nil #

Returns the file format registered to the file extension, or nil otherwise.

@[Chem::RegisterFormat(ext: %w(.jpg .jpeg .jpe))]
module JPEG; end

Chem::Format.from_ext?(".jpg")  # => JPEG
Chem::Format.from_ext?(".JPG")  # => JPEG
Chem::Format.from_ext?(".jpeg") # => JPEG
Chem::Format.from_ext?(".jpe")  # => JPEG
Chem::Format.from_ext?(".txt")  # => nil

NOTE It performs a case-insensitive search so .jpg and .JPG return the same.


[View source]
def self.from_filename?(filename : Path | String) : self | Nil #

Returns the file format associated with filename, or nil otherwise.

It first looks up the file format associated with the extension of filename via the .from_ext? method. If the latter returns nil, then it executes a case-insensitive search with the stem of filename via .from_stem?.

@[Chem::RegisterFormat(ext: %w(.jpg .jpeg .jpe), names: %w(IMG*))]
module JPEG; end

Chem::Format.from_filename?("foo.jpg")      # => JPEG
Chem::Format.from_filename?("foo.JPG")      # => JPEG
Chem::Format.from_filename?("IMG_2314.jpg") # => JPEG
Chem::Format.from_filename?("IMG_2314.png") # => JPEG
Chem::Format.from_filename?("IMG_2314")     # => JPEG
Chem::Format.from_filename?("img_2314")     # => JPEG
Chem::Format.from_filename?("img2314")      # => JPEG
Chem::Format.from_filename?("foo")          # => nil

[View source]
def self.from_stem?(stem : String) : self | Nil #

Returns the file format that matches the file stem, or nil otherwise.

The file stem is matched against the file patterns registered by the file formats until one match is found. File patterns can contain valid filename characters and the * wildcard, which matches an unlimited number of arbitrary characters:

  • "c*" matches file stems beginning with c.
  • "*c" matches file stems ending with c.
  • "*c*" matches file stems that have c in them (including at the beginning or end).
@[Chem::RegisterFormat(names: %w(IMG*))]
module JPEG; end

Chem::Format.from_stem?("IMG_2314") # => JPEG
Chem::Format.from_stem?("img_2314") # => JPEG
Chem::Format.from_stem?("img2314")  # => JPEG
Chem::Format.from_stem?("himg")     # => nil
Chem::Format.from_stem?("foo")      # => nil

NOTE The comparison is made using String#camelcase and String#downcase, so the file pattern FooBar will match FOOBAR, FooBar, foobar, FOO_BAR and foo_bar.


[View source]

Instance Method Detail

def chgcar? : Bool #

Returns true if the member is the Chgcar format.


[View source]
def cube? : Bool #

Returns true if the member is the Cube format.


[View source]
def dx? : Bool #

Returns true if the member is the DX format.


[View source]
def encodes?(type : Array(T).class) : Bool forall T #

Returns true if the format can write an instance of type.

Chem::Format::XYZ.encodes?(Chem::AtomCollection)      # => true
Chem::Format::XYZ.encodes?(Chem::Structure)           # => true
Chem::Format::XYZ.encodes?(Array(Chem::Structure))    # => true
Chem::Format::Poscar.encodes?(Chem::AtomCollection)   # => false
Chem::Format::Poscar.encodes?(Chem::Structure)        # => true
Chem::Format::Poscar.encodes?(Array(Chem::Structure)) # => false
Chem::Format::XYZ.encodes?(Int32)                     # => false
Chem::Format::XYZ.encodes?(Array(Int32))              # => false

[View source]
def encodes?(type : T.class) : Bool forall T #

Returns true if the format can write an instance of type.

Chem::Format::XYZ.encodes?(Chem::AtomCollection)      # => true
Chem::Format::XYZ.encodes?(Chem::Structure)           # => true
Chem::Format::XYZ.encodes?(Array(Chem::Structure))    # => true
Chem::Format::Poscar.encodes?(Chem::AtomCollection)   # => false
Chem::Format::Poscar.encodes?(Chem::Structure)        # => true
Chem::Format::Poscar.encodes?(Array(Chem::Structure)) # => false
Chem::Format::XYZ.encodes?(Int32)                     # => false
Chem::Format::XYZ.encodes?(Array(Int32))              # => false

[View source]
def extnames : Array(String) #

Returns the file extensions associated with the file format.

@[Chem::RegisterFormat(ext: %w(.jpg .jpeg .jpe))]
module JPEG; end

Chem::Format::JPEG.extnames # => [".jpg", ".jpeg", ".jpe"]

[View source]
def file_patterns : Array(String) #

Returns the file patterns associated with the file format.

@[Chem::RegisterFormat(names: %w(IMG*))]
module JPEG; end

Chem::Format::JPEG.file_patterns # => ["IMG*"]

[View source]
def gen? : Bool #

Returns true if the member is the Gen format.


[View source]
def locpot? : Bool #

Returns true if the member is the Locpot format.


[View source]
def mol2? : Bool #

Returns true if the member is the Mol2 format.


[View source]
def mol? : Bool #

Returns true if the member is the Mol format.


[View source]
def pdb? : Bool #

Returns true if the member is the PDB format.


[View source]
def poscar? : Bool #

Returns true if the member is the Poscar format.


[View source]
def psf? : Bool #

Returns true if the member is the PSF format.


[View source]
def py_mol? #

[View source]
def pymol? : Bool #

Returns true if the member is the PyMOL format.


[View source]
def reader(type : Chem::Spatial::Grid.class) #

Returns the reader associated with the format. Raises ArgumentError if the format does not decode type or it is write only.

Chem::Format::XYZ.reader(Chem::Structure)           # => Chem::XYZ::Reader
Chem::Format::DX.reader(Chem::Spatial::Grid)        # => Chem::DX::Reader
Chem::Format::XYZ.reader(Array(Chem::Structure))    # => Chem::XYZ::Reader
Chem::Format::DX.reader(Array(Chem::Spatial::Grid)) # raises ArgumentError
Chem::Format::VMD.reader(Chem::Structure)           # raises ArgumentError
Chem::Format::XYZ.reader(Int32)                     # raises ArgumentError

[View source]
def reader(type : Chem::Structure.class) #

Returns the reader associated with the format. Raises ArgumentError if the format does not decode type or it is write only.

Chem::Format::XYZ.reader(Chem::Structure)           # => Chem::XYZ::Reader
Chem::Format::DX.reader(Chem::Spatial::Grid)        # => Chem::DX::Reader
Chem::Format::XYZ.reader(Array(Chem::Structure))    # => Chem::XYZ::Reader
Chem::Format::DX.reader(Array(Chem::Spatial::Grid)) # raises ArgumentError
Chem::Format::VMD.reader(Chem::Structure)           # raises ArgumentError
Chem::Format::XYZ.reader(Int32)                     # raises ArgumentError

[View source]
def reader(type : Array(Chem::Structure).class) #

Returns the reader associated with the format. Raises ArgumentError if the format does not decode type or it is write only.

Chem::Format::XYZ.reader(Chem::Structure)           # => Chem::XYZ::Reader
Chem::Format::DX.reader(Chem::Spatial::Grid)        # => Chem::DX::Reader
Chem::Format::XYZ.reader(Array(Chem::Structure))    # => Chem::XYZ::Reader
Chem::Format::DX.reader(Array(Chem::Spatial::Grid)) # raises ArgumentError
Chem::Format::VMD.reader(Chem::Structure)           # raises ArgumentError
Chem::Format::XYZ.reader(Int32)                     # raises ArgumentError

[View source]
def sdf? : Bool #

Returns true if the member is the SDF format.


[View source]
def stride? : Bool #

Returns true if the member is the Stride format.


[View source]
def vmd? : Bool #

Returns true if the member is the VMD format.


[View source]
def writer(type : Chem::Spatial::Grid.class) #

Returns the writer associated with the format. Raises ArgumentError if the format does not encode type or it is read only.

Chem::Format::XYZ.writer(Chem::Structure)           # => Chem::XYZ::Writer
Chem::Format::DX.writer(Chem::Spatial::Grid)        # => Chem::DX::Writer
Chem::Format::XYZ.writer(Array(Chem::Structure))    # => Chem::XYZ::Writer
Chem::Format::DX.writer(Array(Chem::Spatial::Grid)) # raises ArgumentError
Chem::Format::XYZ.writer(Int32)                     # raises ArgumentError

[View source]
def writer(type : Chem::Structure.class) #

Returns the writer associated with the format. Raises ArgumentError if the format does not encode type or it is read only.

Chem::Format::XYZ.writer(Chem::Structure)           # => Chem::XYZ::Writer
Chem::Format::DX.writer(Chem::Spatial::Grid)        # => Chem::DX::Writer
Chem::Format::XYZ.writer(Array(Chem::Structure))    # => Chem::XYZ::Writer
Chem::Format::DX.writer(Array(Chem::Spatial::Grid)) # raises ArgumentError
Chem::Format::XYZ.writer(Int32)                     # raises ArgumentError

[View source]
def writer(type : Chem::AtomCollection.class) #

Returns the writer associated with the format. Raises ArgumentError if the format does not encode type or it is read only.

Chem::Format::XYZ.writer(Chem::Structure)           # => Chem::XYZ::Writer
Chem::Format::DX.writer(Chem::Spatial::Grid)        # => Chem::DX::Writer
Chem::Format::XYZ.writer(Array(Chem::Structure))    # => Chem::XYZ::Writer
Chem::Format::DX.writer(Array(Chem::Spatial::Grid)) # raises ArgumentError
Chem::Format::XYZ.writer(Int32)                     # raises ArgumentError

[View source]
def writer(type : Array(Chem::AtomCollection).class) #

Returns the writer associated with the format. Raises ArgumentError if the format does not encode type or it is read only.

Chem::Format::XYZ.writer(Chem::Structure)           # => Chem::XYZ::Writer
Chem::Format::DX.writer(Chem::Spatial::Grid)        # => Chem::DX::Writer
Chem::Format::XYZ.writer(Array(Chem::Structure))    # => Chem::XYZ::Writer
Chem::Format::DX.writer(Array(Chem::Spatial::Grid)) # raises ArgumentError
Chem::Format::XYZ.writer(Int32)                     # raises ArgumentError

[View source]
def xyz? : Bool #

Returns true if the member is the XYZ format.


[View source]