
-- Creates a Compid database with default values for Apache Derby DBMS

CREATE SCHEMA COMPID;
SET SCHEMA COMPID;

call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize','32768');

CREATE TABLE PROTDBINFO(
    PROTDBINFO_ID INTEGER        GENERATED ALWAYS AS IDENTITY
                                 (START WITH 1, INCREMENT BY 1),
    ACCPREFIX     VARCHAR(32)    UNIQUE NOT NULL,
    DBNAME        VARCHAR(64)    NOT NULL,
    DBDESCR       VARCHAR(512),
    CONSTRAINT PROTDBINFO_PK PRIMARY KEY (PROTDBINFO_ID)
);

CREATE TABLE DATASETINFO(
    DATASETINFO_ID INTEGER        GENERATED ALWAYS AS IDENTITY
                                  (START WITH 1, INCREMENT BY 1),
    DSNAME         VARCHAR(128)   UNIQUE NOT NULL,
    DATACOUNTS     VARCHAR(128),
    DSDESCR        VARCHAR(1024),
    PEPDB          VARCHAR(128),
    SOURCESTR      VARCHAR(256),
    ALGORITHM      VARCHAR(32),
    PARAMS         VARCHAR(512),
    ADDTIME        DATE,
    CONSTRAINT DATASETINFO_PK PRIMARY KEY (DATASETINFO_ID)
);

CREATE TABLE PROTSEQ(
    PROTSEQ_ID INTEGER        GENERATED ALWAYS AS IDENTITY
                              (START WITH 1, INCREMENT BY 1),
    DIGEST CHAR(32) UNIQUE NOT NULL,
    SEQUENCE VARCHAR(32000) NOT NULL,
    STATUS INTEGER NOT NULL,
    CONSTRAINT PROTSEQ_PK PRIMARY KEY (PROTSEQ_ID)
);
-- DIGEST: MD5 in hex (lowercase) of SEQUENCE
-- STATUS: bitwise mask: 1: truncated, 2: altered, 32768: unknown

CREATE TABLE PROTEIN(
    PROTEIN_ID  INTEGER       GENERATED ALWAYS AS IDENTITY
                              (START WITH 1, INCREMENT BY 1),
    PROTSEQ_ID  INTEGER,
    PROTDBINFO_ID INTEGER,
    ACCESSION   VARCHAR(128)  UNIQUE NOT NULL,
    DESCRIPTION VARCHAR(2048),
    SPECIES     VARCHAR(256),
    CONSTRAINT PROTEIN_PK PRIMARY KEY (PROTEIN_ID),
    CONSTRAINT PROTEIN_FK FOREIGN KEY (PROTSEQ_ID)
                          REFERENCES PROTSEQ (PROTSEQ_ID)
                          ON DELETE RESTRICT,
    CONSTRAINT PROTEIN_FK2 FOREIGN KEY (PROTDBINFO_ID)
                           REFERENCES PROTDBINFO (PROTDBINFO_ID)
                           ON DELETE RESTRICT
);

CREATE TABLE PROTEINHIT(
    PROTEINHIT_ID INTEGER GENERATED ALWAYS AS IDENTITY
                          (START WITH 1, INCREMENT BY 1),
    PROTEIN_ID     INTEGER NOT NULL,
    DATASETINFO_ID INTEGER NOT NULL,
    GROUPID        INTEGER NOT NULL,
    GROUPMEMBERSTATUS INTEGER,
    MS_PROTSCORE  DOUBLE,
    PG_UNUSED     DOUBLE,
    PG_TOTAL      DOUBLE,
    CONSTRAINT PROTEINHIT_PK PRIMARY KEY (PROTEINHIT_ID),
    CONSTRAINT PROTEINHIT_FK FOREIGN KEY (PROTEIN_ID)
                             REFERENCES PROTEIN (PROTEIN_ID)
                             ON DELETE RESTRICT,
    CONSTRAINT PROTEINHIT_FK2 FOREIGN KEY (DATASETINFO_ID)
                              REFERENCES DATASETINFO (DATASETINFO_ID)
                              ON DELETE CASCADE
);
-- GROUPID: group id *within* a dataset
-- GROUPMEMBERSTATUS: 0: unknown (do not use!), 1: winner, >1: sub protein

CREATE TABLE PEPTIDE(
    PEPTIDE_ID  INTEGER      GENERATED ALWAYS AS IDENTITY
                             (START WITH 1, INCREMENT BY 1),
    PEPSEQUENCE VARCHAR(256) UNIQUE NOT NULL,
    CONSTRAINT PEPTIDE_PK PRIMARY KEY (PEPTIDE_ID)
);

CREATE TABLE QUERY(
    QUERY_ID       INTEGER  GENERATED ALWAYS AS IDENTITY
                            (START WITH 1, INCREMENT BY 1),
    DATASETINFO_ID INTEGER  NOT NULL,
    SUBMITTEDMASS  DOUBLE,
    CHARGE         INTEGER,
    SCANTITLE      VARCHAR(128),
    CONSTRAINT QUERY_PK PRIMARY KEY (QUERY_ID),
    CONSTRAINT QUERY_FK FOREIGN KEY (DATASETINFO_ID)
                        REFERENCES DATASETINFO (DATASETINFO_ID)
                        ON DELETE CASCADE
);

CREATE TABLE PEPTIDEHIT(
    PEPTIDEHIT_ID INTEGER GENERATED ALWAYS AS IDENTITY
                          (START WITH 1, INCREMENT BY 1),
    PEPTIDE_ID    INTEGER NOT NULL,
    QUERY_ID      INTEGER NOT NULL,
--    CALCMASS      DOUBLE  NOT NULL,
    PG_CONF       DOUBLE  NOT NULL,
    MS_IONSSCORE  DOUBLE  NOT NULL,
    CONSTRAINT PEPTIDEHIT_PK PRIMARY KEY (PEPTIDEHIT_ID),
    CONSTRAINT PEPTIDEHIT_FK FOREIGN KEY (PEPTIDE_ID)
                             REFERENCES PEPTIDE (PEPTIDE_ID)
                             ON DELETE CASCADE,
    CONSTRAINT PEPTIDEHIT_FK2 FOREIGN KEY (QUERY_ID)
                              REFERENCES QUERY (QUERY_ID)
                              ON DELETE CASCADE
);

CREATE TABLE PROTPEPTHITSJOIN (
    PROTPEPTHITSJOIN_ID INTEGER  GENERATED ALWAYS AS IDENTITY
                                 (START WITH 1, INCREMENT BY 1),
    PROTEINHIT_ID       INTEGER    NOT NULL,
    PEPTIDEHIT_ID       INTEGER    NOT NULL,
    CONSTRAINT PROTPEPTHITSJOIN_PK PRIMARY KEY (PROTPEPTHITSJOIN_ID),
    CONSTRAINT PROTPEPTHITSJOIN_FK1 FOREIGN KEY (PROTEINHIT_ID)
                                    REFERENCES PROTEINHIT
                                               (PROTEINHIT_ID)
                                    ON DELETE CASCADE,
    CONSTRAINT PROTPEPTHITSJOIN_FK2 FOREIGN KEY (PEPTIDEHIT_ID)
                                    REFERENCES PEPTIDEHIT
                                               (PEPTIDEHIT_ID)
                                    ON DELETE CASCADE
);

CREATE TABLE SETTINGS (
    SETTINGS_ID INTEGER  GENERATED ALWAYS AS IDENTITY
                         (START WITH 1, INCREMENT BY 1),
    SETNAME         VARCHAR(128) UNIQUE NOT NULL,
    SETVALUE        VARCHAR(4096),
    SETDESCR        VARCHAR(512),
    SETACTIVE       INTEGER,
    SETUSEREDITABLE INTEGER,
    SETFLAGS        INTEGER
);


-- indexes

CREATE INDEX IDX_QUERY ON QUERY (SCANTITLE, SUBMITTEDMASS, CHARGE);
CREATE INDEX IDX_PROTEINHIT ON PROTEINHIT (GROUPMEMBERSTATUS);
-- DO NOT remove IDX_QUERY or separate it: slows insertions considerably
-- DO NOT add index for PEPTIDEHIT: seems to slow down a lot



-- insert basic values

INSERT INTO PROTDBINFO (ACCPREFIX, DBNAME, DBDESCR)
VALUES ('','UNKNOWN','Protein entries without known accession prefix');

INSERT INTO PROTSEQ (DIGEST, SEQUENCE, STATUS)
VALUES ('d41d8cd98f00b204e9800998ecf8427e','', 32768);
-- note: ^ is MD5 of an empty string, do not modify
