Molecular weight as sum of atomic weights

Use atoms as theoretical (obvious) fragment types for molecular weight

atomic_weights
smarts weight symbol
[#1] 1.01 H
[*;h1] 1.01 H1
[*;h2] 2.02 H2
[#6] 12.01 C
[#7] 14.01 N
[#8] 16.00 O
... ... ...

Select symbol, weight, count_matches('c1cc(c(cc1CCN)O)O', smarts) from atomic_weights;

symbol weight count_matches
C 12.01 8
N 14.01 1
O 16.00 2
H1 1.01 5
H2 2.02 3
 

Select
sum(weight*count_matches('c1cc(c(cc1CCN)O)O', smarts))
from atomic_weights;

sum
153.20

 

Create Function amw(character varying) Returns numeric AS
'Select sum(weight*count_matches($1,smarts))
from atomic_weights;
' Language SQL;

 

Select amw('c1cc(c(cc1CCN)O)O');

amw
153.20