But what if we need access to the elemental composition as a relation, e.g:
element | count |
---|---|
C | 3 |
N | 1 |
O | 2 |
Fortunately, PostgreSQL is awesome:
CREATE OR REPLACE FUNCTION elemental_composition(molformula TEXT)
RETURNS TABLE(element TEXT, count INTEGER) AS
$BODY$
DECLARE token TEXT[];
DECLARE elements TEXT[];
BEGIN
elements := ARRAY['C','N','O','P','S','Cl']; --expand as needed
molformula := REPLACE(REPLACE(molformula,'-',''),'+','');
FOREACH element IN ARRAY elements LOOP
count := 1;
token := REGEXP_MATCHES(molformula, element || '[\d?]*');
IF (token[1] IS NOT NULL) THEN
token := REGEXP_MATCHES(token[1],'[0-9]+');
IF (token[1] iS NOT NULL) THEN
count := token[1]::INTEGER;
END IF;
RETURN NEXT;
END IF;
END LOOP;
RETURN;
END;
$BODY$
LANGUAGE plpgsql IMMUTABLE STRICT
COST 1000;
SELECT * FROM elemental_composition('C3H6NO2-');
And that's it. Did I already mention that PostgreSQL is awesome? :-)