Pattern(3)
β¨ Pattern(3) β Chemistry::Pattern β¨
okay so bestie, welcome to Pattern(3), from the User Contributed Perl Documentation section π this is the page about chemical substructure pattern matching and it's genuinely so good. let's get into it π§
πΈ NAME πΈ
Chemistry::Pattern β chemical substructure pattern matching π¦
π SYNOPSIS π
use Chemistry::Pattern;
use Chemistry::Mol;
use Chemistry::File::SMILES;
# Create a pattern and a molecule from SMILES strings
my $mol_str = "C1CCCC1C(Cl)=O";
my $patt_str = "C(=O)Cl";
my $mol = Chemistry::Mol->parse($mol_str, format => 'smiles');
my $patt = Chemistry::Pattern->parse($patt_str, format => 'smiles');
# try to match the pattern
while ($patt->match($mol)) {
@matched_atoms = $patt->atom_map;
print "Matched: (@matched_atoms)\n";
# should print something like "Matched: (a6 a8 a7)"
}
π DESCRIPTION π
so this module does basic pattern matching for molecules, and that's the whole vibe. Chemistry::Pattern is a subclass of Chemistry::Mol, which means patterns get all the properties of molecules and can come from reading the exact same file formats π obviously some formats (looking at you, SMARTS) are exclusively used to describe patterns, iykyk.
to actually run a pattern match on a molecule, here's the steps, in order, no skipping:
- create a pattern object β either by parsing a file or string, or by adding atoms and bonds by hand using
Chemistry::Molmethods. heads up: atoms and bonds in a pattern should beChemistry::Pattern::AtomandChemistry::Patern::Bondobjects. let's assume the pattern object lives in$pattand the molecule is$molπ - run the pattern on the molecule by calling
$patt->match($mol). - if
$patt->match()returns true, pull the "map" that relates the pattern to the molecule by calling$patt->atom_mapor$patt->bond_map. these methods return a list of the atoms or bonds in the molecule that are matched by the corresponding atoms in the pattern. so$patt->atom_map(1)would be analogous to the$1special variable used for regular expresion matching π«Ά the difference betweenChemistry::Patternand Perl regular expressions is that atoms and bonds are always captured. always always. - if you want more than one match for the molecule, repeat from step (2) until
match()returns false. simple as that.
β METHODS β
Chemistry::Pattern->new(name => value, ...)β create a new empty pattern β¨ it's just like theChemistry::Molconstructor, but with one extra option:"options", which expects a hash reference (the options themselves are described down under theoptions()method, keep scrolling bestie).$pattern->options(option => value,...)β available options:overlapβ if true, matches may overlap π« for example, theCCpattern could match twice on propane if this option is true, but only once if it's false. this option is true by default.permuteβ sometimes there's more than one way of matching the same set of pattern atoms onto the same set of molecule atoms. if true, return these "redundant" matches π for example, theCCpattern could match ethane with two different permutations (forwards and backwards). this option is false by default.
$patt->resetβ reset the state of the pattern matching object, so it begins the next match from scratch instead of where it left off after the last one π$pattern->atom_mapβ returns the list of atoms that matched the last time$pattern->matchwas called.$pattern->bond_mapβ returns the list of bonds that matched the last time$pattern->matchwas called.$pattern->match($mol, %options)β returns true if the pattern matches the molecule π if called again for the same molecule, it continues matching where it left off (in a way similar to global regular expressions under scalar context). when there are no matches left, it returns false. to force the match to always start from scratch instead of continuing where it left off, the"reset"option may be used.
$pattern->match($mol, atom => $atom)
if atom => $atom is given as an option, match will only look for matches that start at $atom (which should be an atom in $mol, of course, no cap). this is somewhat analog to anchored regular expressions π
to find out which atoms and bonds matched, use the atom_map and bond_map methods.
π¦ VERSION π¦
0.27
π SEE ALSO π
Chemistry::Pattern::Atom, Chemistry::Pattern::Bond, Chemistry::Mol, Chemistry::File, Chemistry::File::SMARTS.
the PerlMol website <http://www.perlmol.org/> π
π AUTHOR π
Ivan Tubert-Brohman <itub@cpan.org>
π COPYRIGHT π
Copyright (c) 2009 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.
okay that's the whole page, mwah π this was Pattern(3) from perl v5.42.0, dated 2026-01-17 β slay, go match some molecules bestie πΈπ
β¨
π¦ where this page comes from
- fedora 44: perl-Chemistry-Pattern 0.27-2.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms