Create Schema perlmol; Create Or Replace Function perlmol.valid(text) Returns Boolean As $EOPERL$ use Chemistry::File ':auto'; #-- return true if input smi can be parsed my ($smi) = @_; my $mol = Chemistry::Mol->parse($smi, format => 'smiles', fatal => 0); if ($mol->atoms(1)) { #-- $mol has as least one valid atom return true; } else { return false; } $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.cansmiles(text) Returns Text As $EOPERL$ use Chemistry::File ':auto'; #-- return canonicalized version of input smi my ($smi) = @_; my $mol = Chemistry::Mol->parse($smi, format => 'smiles'); return $mol->sprintf('%S'); $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.keksmiles(text) Returns Text As $EOPERL$ use Chemistry::File ':auto'; #-- return kekulized version of input smi my ($smi) = @_; my $mol = Chemistry::Mol->parse($smi, format => 'smiles', kekulize => 1); return $mol->sprintf('%s'); $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.smiles_to_molfile(text) Returns Text As $EOPERL$ use Chemistry::File ':auto'; #--use Chemistry::3DBuilder qw(build_3d); #-- convert smi to molfile format my ($smi) = @_; my $mol = Chemistry::Mol->parse($smi, format => smiles); #-- compute 3D coords #--build_3d($mol); return $mol->print(format => sdf); $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.molfile_to_smiles(text) Returns Text As $EOPERL$ use Chemistry::File ':auto'; my ($molfile) = @_; my $mol = Chemistry::Mol->parse($molfile, format => sdf); return $mol->print(format => smiles); $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.matches(text, text) Returns Boolean As $EOPERL$ use Chemistry::File::SMILES; use Chemistry::File::SMARTS; use Chemistry::Ring 'aromatize_mol'; #-- return true if smi is matched by sma my ($smi, $sma) = @_; my $mol = Chemistry::Mol->parse($smi, format => smiles); aromatize_mol($mol); my $patt = Chemistry::Pattern->parse($sma, format => smarts); if ( $patt->match($mol) ) { return 't'; } else { return 'f'; } $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.count_matches(text, text) Returns Integer As $EOPERL$ use Chemistry::File::SMILES; use Chemistry::File::SMARTS; use Chemistry::Ring 'aromatize_mol'; #-- return how many times smi is matched by sma my ($smi, $sma) = @_; my $mol = Chemistry::Mol->parse($smi, format => smiles); aromatize_mol($mol); my $patt = Chemistry::Pattern->parse($sma, format => smarts); my $nmatches = 0; while ( $patt->match($mol) ) { ++$nmatches; } return $nmatches; $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.list_matches(text, text, integer) Returns Integer[] As $EOPERL$ use Chemistry::File::SMILES; use Chemistry::File::SMARTS; use Chemistry::Ring 'aromatize_mol'; #-- return list of atoms in smi matched by sma #-- return imatch'th match; all matches if imatch=0 my ($smi, $sma, $imatch) = @_; my $mol = Chemistry::Mol->parse($smi, format => smiles); aromatize_mol($mol); #---- map atom ids (returned from $patt->atom_map below) to atom numbers in mol my %atom_number; my $iatom = 1; foreach ( $mol->atoms ) { $atom_number{$_} = $iatom; ++$iatom; } my $patt = Chemistry::Pattern->parse($sma, format => smarts); my @all_matches = (); my $nmatches = 0; while ( $patt->match($mol) ) { my @matches = (); $natoms = @matches = $patt->atom_map; for (my $i=0; $i<$natoms; ++$i) { #---- convert atom ids from $patt- 0 && $imatch == $nmatches); push @all_matches, [ @matches ]; } return undef unless ($nmatches > 0); return [ @all_matches ]; $EOPERL$ Language plperlu Immutable; Create Or Replace Function perlmol.list_matches(text, text) Returns Integer[] As $EOSQL$ -- Convenience function to return first match Select perlmol.list_matches($1, $2, 1); $EOSQL$ Language SQL Immutable; -- not part of the core, but useful and instructive Create Or Replace Function perlmol.mf(text) Returns Text As $EOPERL$ use Chemistry::File::SMILES; use Chemistry::File::Formula; my ($smi) = @_; my $mol = Chemistry::Mol->parse($smi, format => 'smiles'); return $mol->print(format => 'formula'); $EOPERL$ Language plperlu Immutable;