Skip to main content

Graph Neural Networks for Molecular Machine Learning

Project description

molcraft-logo

Deep Learning on Molecules: Graph Neural Networks for Molecular Machine Learning.

Examples

Context-Aware Graph Neural Network

Implement a context-aware graph neural network by embedding context features in the super node. The super node is a virtual node bidirectionally linked to all atomic nodes, allowing both efficient information propagation and inclusion of context features. Context features may be continuous or discrete (categorical); for discrete context features, specify the number of categories expected via num_categories of the AddContext layer.

from molcraft import features
from molcraft import featurizers 
from molcraft import layers
from molcraft import models

import keras
import pandas as pd

featurizer = featurizers.MolGraphFeaturizer(
    atom_features=[
        features.AtomType(),
        features.NumHydrogens(),
        features.Degree(),
    ],
    bond_features=[
        features.BondType(),
        features.IsRotatable(),
    ],
    super_node=True,
    self_loops=True,
)

df = pd.DataFrame({
    'smiles': [
        'N[C@@H](C)C(=O)O', 'N[C@@H](CS)C(=O)O' 
    ],
    'label': [3.5, -1.5],
    'ph': [7.2, 4.5],
    'temperature': [35., 45.],
})

graph = featurizer(df)

model = models.GraphModel.from_layers(
    [
        layers.Input(graph.spec),
        layers.NodeEmbedding(dim=128),
        layers.EdgeEmbedding(dim=128),
        layers.AddContext(field='ph'),
        layers.AddContext(field='temperature'),
        layers.GraphConv(units=128),
        layers.GraphConv(units=128),
        layers.GraphConv(units=128),
        layers.GraphConv(units=128),
        layers.Readout(mode='mean'),
        keras.layers.Dense(units=1024, activation='elu'),
        keras.layers.Dense(units=1024, activation='elu'),
        keras.layers.Dense(1)
    ]
)

model.compile(
    keras.optimizers.Adam(1e-4), keras.losses.MeanSquaredError()
)
model.fit(graph, epochs=30)
pred = model.predict(graph)

# Uncomment below to save and load model (including featurizer)
# featurizers.save_featurizer(featurizer, '/tmp/featurizer.json')
# models.save_model(model, '/tmp/model.keras')

# loaded_featurizer = featurizers.load_featurizer('/tmp/featurizer.json')
# loaded_model = models.load_model('/tmp/model.keras')

Hybrid Model for Peptides

Implement a GNN-RNN hybrid model for peptides.

from molcraft import features
from molcraft import featurizers 
from molcraft import layers
from molcraft import models

import keras
import pandas as pd

featurizer = featurizers.PeptideGraphFeaturizer(
    atom_features=[
        features.AtomType(),
        features.NumHydrogens(),
        features.Degree(),
    ],
    bond_features=[
        features.BondType(),
        features.IsRotatable(),
    ],
)

# Allow modified amino acids:
# featurizer.monomers.update({
#     "C[Carbamidomethyl]": "N[C@@H](CSCC(=O)N)C(=O)O"
# })

df = pd.DataFrame({
    'sequence': [
        'CYIQNCPLG', 'KTTKS' 
    ],
    'label': [1.0, 0.0],
})

graph = featurizer(df)

model = models.GraphModel.from_layers(
    [
        layers.Input(graph.spec),
        layers.NodeEmbedding(dim=128),
        layers.EdgeEmbedding(dim=128),
        layers.GraphConv(units=128),
        layers.GraphConv(units=128),
        layers.GraphConv(units=128),
        layers.GraphConv(units=128),
        layers.PeptideReadout(),
        keras.layers.Masking(),
        keras.layers.Bidirectional(
            keras.layers.LSTM(units=128, return_sequences=True)
        ),
        keras.layers.GlobalAveragePooling1D(),
        keras.layers.Dense(units=1024, activation='elu'),
        keras.layers.Dense(units=1024, activation='elu'),
        keras.layers.Dense(1, activation='sigmoid')
    ]
)

model.compile(
    keras.optimizers.Adam(1e-4), keras.losses.BinaryCrossentropy()
)
model.fit(graph, epochs=30)
pred = model.predict(graph)

# Uncomment below to save and load model (including featurizer)
# featurizers.save_featurizer(featurizer, '/tmp/featurizer.json')
# models.save_model(model, '/tmp/model.keras')

# loaded_featurizer = featurizers.load_featurizer('/tmp/featurizer.json')
# loaded_model = models.load_model('/tmp/model.keras')

Installation

For CPU users:

pip install molcraft

For GPU users:

pip install molcraft[gpu]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

molcraft-0.3.5.tar.gz (53.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

molcraft-0.3.5-py3-none-any.whl (53.8 kB view details)

Uploaded Python 3

File details

Details for the file molcraft-0.3.5.tar.gz.

File metadata

  • Download URL: molcraft-0.3.5.tar.gz
  • Upload date:
  • Size: 53.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for molcraft-0.3.5.tar.gz
Algorithm Hash digest
SHA256 f36a5e61fa91024ab50575ccc9d9374d2711b19942985e9e46b0414594d94856
MD5 e2bbd27c9213ad5c6ca88ffb9d936e1f
BLAKE2b-256 729b0e64788bf335836746eb5e9646670d6ea2389840b07251b74278d1292908

See more details on using hashes here.

File details

Details for the file molcraft-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: molcraft-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 53.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for molcraft-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 02030e897b2a7dd28f1731693fc8a6675e483a73e93fed2903f6f66611b9e603
MD5 7a3b635cea9959be784afe081205fb2d
BLAKE2b-256 8d43aff0f4b3eb77f1a66f1bb8ad2009b22b0f5de42983050a50f6df0b992a6f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page