Protein Score

A protein is divided into regions, each made of amino acids, each made of atoms. Regions and atoms have a potential of −1, 0 or +1.

proteins(code, name, function, class)
amino_acids(code, name, function)
atoms(code, name, potential)
regions(code, protein, position, potential)
atom_in_amino(amino, atom, position)
amino_in_region(region, amino, position)

Each atom of a region gets a weight from the region's potential and its own:

region \ atom − 0 +
− +1 −1 −2
0 −1 +1 −1
+ −2 −1 +1

A region's score is the average weight of its atoms; the protein's score is the average of its regions' scores. Write a procedure protein_score(p_protein) that prints both.

Run schema.sql (tables and example data), then solution.sql.

Example

region R1: 0.333
region R2: -1.000
region R3: 0.000
protein P1: -0.222

R1 (potential −) has atoms − − 0 − − 0, weights +1 +1 −1 +1 +1 −1: 2/6.

Approach

The table has a simple rule: +1 when the potentials are equal, −2 when they are opposite (their difference is 2), −1 otherwise. That turns nine cases into one CASE.

The cursor query joins regions → amino acids → atoms (the amino acids are only the path to the atoms), groups by region, and averages the weights. The loop then averages the regions. The average of averages is deliberate: every region counts the same, whatever its number of atoms. A single avg over all atoms of the protein would weight big regions more and give a different number.

Lesson: PL/SQL cursors.

Solutions

solution.sql

SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF
SET LINESIZE 120 PAGESIZE 100

CREATE OR REPLACE PROCEDURE protein_score(p_protein proteins.code%TYPE) IS
    v_sum     NUMBER := 0;
    v_regions NUMBER := 0;
BEGIN
    FOR r IN (
        SELECT r.code,
               avg(CASE WHEN r.potential = a.potential THEN 1
                        WHEN ABS(r.potential - a.potential) = 2 THEN -2
                        ELSE -1 END) AS score
        FROM regions r
        JOIN amino_in_region ar ON ar.region = r.code
        JOIN atom_in_amino aa   ON aa.amino = ar.amino
        JOIN atoms a            ON a.code = aa.atom
        WHERE r.protein = p_protein
        GROUP BY r.code
        ORDER BY r.code
    ) LOOP
        DBMS_OUTPUT.PUT_LINE('region ' || r.code || ': ' || TO_CHAR(r.score, 'FM90.000'));
        v_sum := v_sum + r.score;
        v_regions := v_regions + 1;
    END LOOP;
    IF v_regions = 0 THEN
        DBMS_OUTPUT.PUT_LINE('protein ' || p_protein || ' has no regions');
    ELSE
        DBMS_OUTPUT.PUT_LINE('protein ' || p_protein || ': ' || TO_CHAR(v_sum / v_regions, 'FM90.000'));
    END IF;
END;
/

EXEC protein_score('P1')

schema.sql

CREATE TABLE proteins (code VARCHAR2(10) PRIMARY KEY, name VARCHAR2(40), function VARCHAR2(40), class VARCHAR2(20));
CREATE TABLE amino_acids (code VARCHAR2(10) PRIMARY KEY, name VARCHAR2(40), function VARCHAR2(40));
CREATE TABLE atoms (code VARCHAR2(10) PRIMARY KEY, name VARCHAR2(20), potential NUMBER CHECK (potential IN (-1, 0, 1)));
CREATE TABLE regions (
    code      VARCHAR2(10) PRIMARY KEY,
    protein   VARCHAR2(10) REFERENCES proteins,
    position  NUMBER,
    potential NUMBER CHECK (potential IN (-1, 0, 1))
);
CREATE TABLE atom_in_amino (
    amino    VARCHAR2(10) REFERENCES amino_acids,
    atom     VARCHAR2(10) REFERENCES atoms,
    position NUMBER,
    PRIMARY KEY (amino, position)
);
CREATE TABLE amino_in_region (
    region   VARCHAR2(10) REFERENCES regions,
    amino    VARCHAR2(10) REFERENCES amino_acids,
    position NUMBER,
    PRIMARY KEY (region, position)
);

INSERT INTO proteins VALUES ('P1', 'Demo protein', NULL, NULL);
INSERT INTO atoms VALUES ('N', 'neg',     -1);
INSERT INTO atoms VALUES ('Z', 'neutral',  0);
INSERT INTO atoms VALUES ('P', 'pos',      1);
-- amino acid A: atoms -, -, 0   amino acid B: atoms +, 0
INSERT INTO amino_acids VALUES ('A', 'Alpha', NULL);
INSERT INTO amino_acids VALUES ('B', 'Beta',  NULL);
INSERT INTO atom_in_amino VALUES ('A', 'N', 1);
INSERT INTO atom_in_amino VALUES ('A', 'N', 2);
INSERT INTO atom_in_amino VALUES ('A', 'Z', 3);
INSERT INTO atom_in_amino VALUES ('B', 'P', 1);
INSERT INTO atom_in_amino VALUES ('B', 'Z', 2);
-- R1 (potential -): A, A          atoms - - 0 - - 0        weights +1 +1 -1 +1 +1 -1  -> 2/6
-- R2 (potential +): B, A          atoms + 0 - - 0          weights +1 -1 -2 -2 -1     -> -5/5
-- R3 (potential 0): B             atoms + 0                weights -1 +1              -> 0/2
INSERT INTO regions VALUES ('R1', 'P1', 1, -1);
INSERT INTO regions VALUES ('R2', 'P1', 2,  1);
INSERT INTO regions VALUES ('R3', 'P1', 3,  0);
INSERT INTO amino_in_region VALUES ('R1', 'A', 1);
INSERT INTO amino_in_region VALUES ('R1', 'A', 2);
INSERT INTO amino_in_region VALUES ('R2', 'B', 1);
INSERT INTO amino_in_region VALUES ('R2', 'A', 2);
INSERT INTO amino_in_region VALUES ('R3', 'B', 1);
COMMIT;