Create advanced and robust postgresql database schema for pubchem web application
- User
Create advanced and robust postgresql database schema for pubchem web application
- Assistant
Create advanced and robust postgresql database schema for pubchem web application
Creating an advanced and robust PostgreSQL database schema for a PubChem-like web application involves modeling complex relationships between chemical compounds, their properties, biological activities, references, and more. Below is a comprehensive schema design that captures these elements effectively. This design emphasizes normalization, scalability, performance optimization through indexing, and data integrity through constraints.
Below are the SQL CREATE TABLE statements for each of these components, along with explanations and indexing strategies to ensure robustness and performance.
CREATE TABLE Compounds (
compound_id SERIAL PRIMARY KEY,
standard_inchikey CHAR(27) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
molecular_weight NUMERIC(10, 4),
formula VARCHAR(100),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
Explanation:
compound_id: Unique identifier for each compound.standard_inchikey: Standard InChIKey for the compound, ensuring unique chemical identity.name: Primary name of the compound.molecular_weight and formula: Basic molecular properties.Indexes:
compound_id.standard_inchikey to prevent duplicates.CREATE TABLE Synonyms (
synonym_id SERIAL PRIMARY KEY,
compound_id INTEGER REFERENCES Compounds(compound_id) ON DELETE CASCADE,
synonym VARCHAR(255) NOT NULL,
source VARCHAR(255),
UNIQUE (compound_id, synonym)
);
Explanation:
source can indicate the origin of the synonym (e.g., database, publication).Indexes:
compound_id.CREATE TABLE Molecular_Properties (
property_id SERIAL PRIMARY KEY,
compound_id INTEGER REFERENCES Compounds(compound_id) ON DELETE CASCADE,
property_name VARCHAR(100) NOT NULL,
property_value NUMERIC(20, 6),
property_unit VARCHAR(50),
UNIQUE (compound_id, property_name)
);
Explanation:
Indexes:
compound_id.(compound_id, property_name) to ensure one value per property per compound.CREATE TABLE Structures (
structure_id SERIAL PRIMARY KEY,
compound_id INTEGER REFERENCES Compounds(compound_id) ON DELETE CASCADE,
format VARCHAR(50) NOT NULL CHECK (format IN ('SMILES', 'InChI', 'MOL')),
structure_text TEXT NOT NULL,
source VARCHAR(255),
UNIQUE (compound_id, format)
);
Explanation:
format specifies the type of structure representation.Indexes:
compound_id.(compound_id, format) to prevent multiple entries of the same format for a compound.CREATE TABLE Assays (
assay_id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
assay_type VARCHAR(100),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
Explanation:
Indexes:
assay_id.name for quick lookup.CREATE TABLE Assay_Results (
result_id SERIAL PRIMARY KEY,
assay_id INTEGER REFERENCES Assays(assay_id) ON DELETE CASCADE,
compound_id INTEGER REFERENCES Compounds(compound_id) ON DELETE CASCADE,
value NUMERIC(20, 6),
unit VARCHAR(50),
result_type VARCHAR(50), -- e.g., IC50, EC50
measured_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
UNIQUE (assay_id, compound_id, result_type)
);
Explanation:
result_type specifies the type of measurement (e.g., concentration values).Indexes:
assay_id and compound_id.(assay_id, compound_id, result_type) to prevent duplicate results.CREATE TABLE References (
reference_id SERIAL PRIMARY KEY,
title VARCHAR(512) NOT NULL,
authors TEXT,
journal VARCHAR(255),
year INTEGER,
doi VARCHAR(100) UNIQUE,
url TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
Explanation:
Indexes:
reference_id.doi.CREATE TABLE Compound_References (
compound_id INTEGER REFERENCES Compounds(compound_id) ON DELETE CASCADE,
reference_id INTEGER REFERENCES References(reference_id) ON DELETE CASCADE,
PRIMARY KEY (compound_id, reference_id)
);
Explanation:
Indexes:
(compound_id, reference_id).CREATE TABLE Users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP WITH TIME ZONE
);
Explanation:
Indexes:
user_id.username and email.CREATE TABLE Annotations (
annotation_id SERIAL PRIMARY KEY,
compound_id INTEGER REFERENCES Compounds(compound_id) ON DELETE CASCADE,
user_id INTEGER REFERENCES Users(user_id) ON DELETE SET NULL,
comment TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
Explanation:
Indexes:
compound_id and user_id.CREATE TABLE Tags (
tag_id SERIAL PRIMARY KEY,
tag_name VARCHAR(50) UNIQUE NOT NULL
);
Explanation:
Indexes:
tag_id.tag_name.CREATE TABLE Compound_Tags (
compound_id INTEGER REFERENCES Compounds(compound_id) ON DELETE CASCADE,
tag_id INTEGER REFERENCES Tags(tag_id) ON DELETE CASCADE,
PRIMARY KEY (compound_id, tag_id)
);
Explanation:
Indexes:
(compound_id, tag_id).compound_id ranges or assay types can improve performance.JSONB Fields: For storing flexible or semi-structured data, such as experimental conditions in assays.
ALTER TABLE Assays ADD COLUMN additional_info JSONB;
Full-Text Search: Implement full-text search on textual fields like name, synonyms, and comments.
CREATE INDEX idx_compounds_fulltext ON Compounds USING GIN (to_tsvector('english', name));
Materialized Views: Precompute and store complex queries for faster access.
CREATE MATERIALIZED VIEW mv_compound_summary AS
SELECT c.compound_id, c.name, COUNT(ar.result_id) AS assay_count
FROM Compounds c
LEFT JOIN Assay_Results ar ON c.compound_id = ar.compound_id
GROUP BY c.compound_id, c.name;
a. Retrieve Compound Details with Synonyms and Structures
SELECT
c.compound_id,
c.name,
c.molecular_weight,
c.formula,
s.format,
s.structure_text,
syn.synonym
FROM
Compounds c
LEFT JOIN
Structures s ON c.compound_id = s.compound_id
LEFT JOIN
Synonyms syn ON c.compound_id = syn.compound_id
WHERE
c.compound_id = $1;
b. Find Compounds with a Specific Assay Result Type Below a Threshold
SELECT
c.compound_id,
c.name,
ar.value,
ar.unit
FROM
Compounds c
JOIN
Assay_Results ar ON c.compound_id = ar.compound_id
JOIN
Assays a ON ar.assay_id = a.assay_id
WHERE
a.name = 'Inhibition Assay'
AND ar.result_type = 'IC50'
AND ar.value < 50;
c. Search Compounds by Synonym Using Full-Text Search
SELECT
c.compound_id,
c.name
FROM
Compounds c
JOIN
Synonyms syn ON c.compound_id = syn.compound_id
WHERE
to_tsvector('english', syn.synonym) @@ plainto_tsquery('english', 'aspirin');
The above PostgreSQL schema provides a robust foundation for a PubChem-like web application, encompassing comprehensive data representation for chemical compounds, their properties, biological activities, and related metadata. By leveraging PostgreSQL's advanced features and adhering to best practices in database design, this schema ensures scalability, performance, and data integrity, facilitating complex queries and large-scale data management essential for scientific applications.
To implement this schema:
CREATE TABLE statements in your PostgreSQL environment.Feel free to extend or modify this schema based on specific requirements or additional features of your web application.