Chemistry::Atom(3pm)
π NAME π
Chemistry::Atom β chemical atoms as objects in molecules π§ͺβ¨ (this is the Chemistry::Atom(3pm) page from the User Contributed Perl Documentation, and bestie, it's about to serve)
πΈ SYNOPSIS πΈ
use Chemistry::Atom;
my $atom = new Chemistry::Atom(
id => 'a1',
coords => [$x, $y, $z],
symbol => 'Br'
);
print $atom->print;
π¦ DESCRIPTION π¦
Okay so this module is literally a whole little object system for describing chemical atoms. An atom is defined by its symbol and its coordinates, plus a bunch of other attributes that we'll get into. And here's the cute part: the atomic coordinates are described by a Math::VectorReal object, so you can just yeet them straight into vector operations. π
π Atom Attributes π
Beyond the regular everyday attributes like id, name, and type, atoms also carry these: bonds, coords, internal_coords, Z, symbol, etc. (the "etc." is doing a lot of work there, but slay).
The pattern, so you don't have to guess:
- To get a property, call
$atom->methodwith no parameters. - To set it, call
$atom->method($new_value). - And when you set something, the accessor returns the atom object itself, which means accessor chaining is absolutely valid and honestly kinda iconic:
$atom->symbol("C")->name("CA")->coords(1,2,3);
β¨ METHODS β¨
Chemistry::Atom->new(name => value, ...)β bestie, this makes you a whole new Atom object with whatever attributes you hand it. π±$atom->Z($new_Z)β sets and returns the atomic number (Z). If the atom's symbol doesn't match up with a known element, thenZ = undef. Sorry, no guesses. π$atom->symbol($new_symbol)β sets and returns the atomic symbol. β$atom->mass($new_mass)β sets and returns the atomic mass in atomic mass units. You can set any arbitrary mass explicitly with this method, no questions asked. BUT β and this is the important bit β if no mass was ever set explicitly and you call this as an accessor, here's what you get back, in order:- If the mass number is undefined (peep the
mass_numbermethod below), you get the relative atomic mass from the 1995 IUPAC recommendation. (Table stolen from theChemistry::MolecularMassmodule by Maksim A. Khrapov β credit where credit is due. π«Ά) - If the mass number is defined, and the
Chemistry::Isotopemodule is available and it knows the mass for that isotope, you get the exact mass of the isotope. Otherwise, you just get the mass number back.
- If the mass number is undefined (peep the
$atom->mass_number($new_mass_number)β sets or gets the mass number. Heads up: the mass number is undefined unless you set it explicitly, because this module does not try to guess a default based on the natural isotope distribution. It's not out here speculating about isotopes. π$atom->coordsβ sets or gets the atom's coordinates. It happily accepts aMath::VectorRealobject, a reference to an array, or just the list of coordinates. Check the vibes:
my $vector = $atom->coords; # get a Math::VectorReal object
$atom->coords($vector); # set a Math::VectorReal object
$atom->coords([$x, $y, $z]); # also accepts array refs
$atom->coords($x, $y, $z); # also accepts lists
$atom->internal_coordsβ sets or gets the atom's internal coordinates. It takes aChemistry::InternalCoordsobject, an array ref, or a plain list. For those last two options, the elements of the list go in this order: atom number or reference for distance, distance, atom number or reference for angle, angle in degrees, atom number or reference for dihedral, dihedral in degrees. Like so:
# get a Chemistry::InternalCoords object
my $ic = $atom->internal_coords;
# set a Chemistry::InternalCoords object
$atom->internal_coords($vic);
# also accepts array refs
$atom->internal_coords([8, 1.54, 7, 109.47, 6, 120.0]);
# also accepts lists
$atom->internal_coords(8, 1.54, 7, 109.47, 6, 120.0);
$atom->x3,$atom->y3,$atom->z3β get the x, y or z 3D coordinate of the atom. These are just accessors and they don't change the coordinates at all.$atom->x3is short for($atom->coords->array)[0], and so on down the line. π$atom->formal_charge($charge)β set or get the atom's formal charge. β‘$atom->formal_radical($radical)β set or get the formal radical multiplicity for the atom.$atom->implicit_hydrogens($h_count)β set or get the number of implicit hydrogen atoms bonded to the atom.$atom->hydrogens($h_count)β set or get the number of implicit hydrogen atoms bonded to the atom. π¨ DEPRECATED: USE"implicit_hydrogens"INSTEAD. Like, for real, use the other one.$atom->total_hydrogens($h_count)β get the total number of hydrogen atoms bonded to the atom (implicit + explicit). Yes, they get added together. Math! β¨$atom->sprout_hydrogensβ converts all the implicit hydrogens for this atom into explicit hydrogens. β οΈ Note: it does not generate coordinates for the new atoms, so don't come crying about missing geometry, bestie.$atom->collapse_hydrogensβ deletes the neighboring hydrogen atoms and adds them back as implicit hydrogens on this atom. The reverse glow-down. π¦$atom->calc_implicit_hydrogensβ uses heuristics to figure out how many implicit hydrogens the atom should have to satisfy its normal "organic" valence. It returns that number but does not touch the atom object. Just a lil peek, no commitment. π$atom->add_implicit_hydrogensβ same energy ascalc_implicit_hydrogens, except it also sets the number of implicit hydrogens on the atom to the newly calculated value. It's equivalent to:
$atom->implicit_hydrogens($atom->calc_implicit_hydrogens);
And it returns the atom object.
$atom->aromatic($bool)β set or get whether the atom is considered aromatic. And listen: this property may be set arbitrarily β it does not imply any kind of "intelligent aromaticity detection"! π¨ (If that's what you want, go look at theChemistry::Ringmodule.) It's giving "you decide, I just hold the flag."$atom->valenceβ returns the sum of the bond orders of all the bonds the atom is participating in, including implicit hydrogens (which are assumed to have bond orders of one).$atom->explicit_valenceβ like"valence", but excluding implicit hydrogen atoms. And if you want the raw number of bonds without counting bond orders at all, call$atom->bondsin scalar context.$atom->deleteβ calls$mol->delete_atom($atom)on the atom's parent molecule. bye bestie π$atom->parentβ returns the atom's containing object (the molecule it belongs to). An atom can only have one parent. No complicated family trees here.$atom->neighbors($from)β returns a list of neighbors. If you pass an atom object as$from, that one gets excluded from the list. This is super useful when an atom wants to know who its neighbor's neighbors are without counting itself. iykyk. π$atom->bonds($from)β returns a list of bonds. If you pass an atom object as$from, the bonds to that atom get excluded from the list.$atom->bonds_neighbors($from)β returns a list of hash references representing the bonds and neighbors of the atom. If you pass an atom object as$from, it gets excluded from the list. The elements of each hash are'to', an atom reference, and'bond', a bond reference. Like:
for my $bn ($atom->bonds_neighbors) {
print "bond $bn->{bond} point to atom $bn->{to}\n";
}
($distance, $closest_atom) = $atom->distance($obj)β returns the minimum distance to$obj, which can be an atom, a molecule, or a vector. In scalar context you only get the distance; in list context you also get the closest atom found. π― It can also be called as a function,Chemistry::Atom::distance(which can be exported).$atom->angle($atom2, $atom3)β returns the angle in radians between the atoms involved.$atom2is the atom in the middle. Can also be called asChemistry::Atom::angle($atom1, $atom2, $atom3). This function can be exported. β οΈ Note: if you override this method, you may also need to overrideangle_deg, or strange things may happen. Consider yourself warned.$atom->angle_deg($atom2, $atom3)β same asangle(), but returns the value in degrees. May be exported. π$atom->dihedral($atom2, $atom3, $atom4)β returns the dihedral angle in radians between the atoms involved. Can also be called asChemistry::Atom::dihedral($atom1, $atom2, $atom3, $atom4). May be exported. β οΈ Note: if you override this method, you may also need to overridedihedral_degandangle, or strange things may happen. Same warning, different method.$atom->dihedral_deg($atom2, $atom3, $atom4)β same asdihedral(), but returns the value in degrees. May be exported.$atom->printβ converts the atom to a string representation (used for debugging). πmy $info = $atom->sprintf($format)β formats interesting atomic information in a concise way, as specified by a printf-like format. The whole menu of conversions:%sβ symbol%Zβ atomic number%nβ name%qβ formal charge%hβ implicit hydrogen count%vβ valence%iβ id%8.3mβ mass, formatted as%8.3fwith core sprintf%8.3xβ x coordinate, formatted as%8.3fwith core sprintf%8.3yβ y coordinate, formatted as%8.3fwith core sprintf%8.3zβ z coordinate, formatted as%8.3fwith core sprintf%%β%
$atom->printf($format)β same as$atom->sprintf, but it prints to standard output automatically. Used for quick and dirty atomic information dumping. π
π₯ SOURCE CODE REPOSITORY π₯
https://github.com/perlmol/Chemistry-Mol
π SEE ALSO π
Chemistry::Mol, Chemistry::Bond, Math::VectorReal, Chemistry::Tutorial, Chemistry::InternalCoords
π«Ά AUTHOR π«Ά
Ivan Tubert-Brohman <itub@cpan.org>
π COPYRIGHT π
Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
And that's the whole page, bestie β no cap. Signing off: this was Chemistry::Atom(3pm), served up by perl v5.40.1, dated 2025-10-13. Go build some molecules. π§π
π¦ where this page comes from
- ubuntu resolute: libchemistry-mol-perl 0.40-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libchemistry-mol-perl 0.40-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms