Class: Alumina::Molecule

Inherits:
Object show all
Includes:
HIN::Writer::Molecule
Defined in:
lib/alumina/molecule.rb

Overview

A molecule as represented by HIN data, consisting of multiple Atoms.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HIN::Writer::Molecule

#to_hin

Constructor Details

#initialize(id, label = nil) ⇒ Molecule

Creates a new instance.

this method.

Parameters:

  • id (Fixnum)

    The molecule’s unique ID. Uniqueness is not checked in

  • label (String) (defaults to: nil)

    A label to give the molecule.



19
20
21
22
23
# File 'lib/alumina/molecule.rb', line 19

def initialize(id, label=nil)
  @id = id
  @label = label
  @atoms = Hash.new
end

Instance Attribute Details

#idFixnum

Returns The unique numerical identifier for this molecule.

Returns:

  • (Fixnum)

    The unique numerical identifier for this molecule.



9
10
11
# File 'lib/alumina/molecule.rb', line 9

def id
  @id
end

#labelString?

Returns The optional label given to this molecule.

Returns:

  • (String, nil)

    The optional label given to this molecule.



11
12
13
# File 'lib/alumina/molecule.rb', line 11

def label
  @label
end

Instance Method Details

#<<(atom) ⇒ Object

Adds an atom to this molecule. If there is already an atom in this molecule sharing this atom’s ID, it will be replaced by this atom.

Parameters:

  • atom (Atom)

    The atom to add.



31
32
33
# File 'lib/alumina/molecule.rb', line 31

def <<(atom)
  @atoms[atom.id] = atom
end

#atom(ident) ⇒ Atom? Also known as: []

Returns an atom for a given unique identifier.

atom was found.

Parameters:

  • ident (Fixnum)

    The identifier to search.

Returns:

  • (Atom, nil)

    The atom with that identifier, or @nil@ if no such



47
48
49
# File 'lib/alumina/molecule.rb', line 47

def atom(ident)
  @atoms[ident]
end

#atomsArray<Atom>

Returns An array of atoms in this molecule.

Returns:

  • (Array<Atom>)

    An array of atoms in this molecule.



37
38
39
# File 'lib/alumina/molecule.rb', line 37

def atoms
  @atoms.values
end

#inspectObject



62
63
64
65
66
67
68
# File 'lib/alumina/molecule.rb', line 62

def inspect
  if label then
    "#<Molecule ##{id} (#{label}): #{molecular_formula}>"
  else
    "#<Molecule ##{id}: #{molecular_formula}>"
  end
end

#molecular_formulaString

for example, @C7H5N3O6@ for TNT.

Returns:

  • (String)

    Returns the plain-text molecular formula for this atom;



56
57
58
59
# File 'lib/alumina/molecule.rb', line 56

def molecular_formula
  counts = atoms.map(&:element).inject(Hash.new(0)) { |hsh, cur| hsh[cur] += 1 ; hsh }
  counts.keys.sort.reverse.map { |element| "#{element.symbol}#{num_for counts[element]}" }.join
end