Views
No views yet
1git clone https://github.com/pnnl/FragNet.git
2cd FragNet
3# make sure a python virtual environment is activated
4pip install --upgrade pip
5pip install -r requirements.txt
6pip install torch-scatter -f https://data.pyg.org/whl/torch-2.4.0+cpu.html
7pip install .1import torch
2from huggingface_hub import hf_hub_download
3from fragnet.model.gat.gat2 import FragNetFineTune
4from huggingface.fragnet_config import FragNetConfig
5
6config_path = hf_hub_download(repo_id="gihan12/FragNet", filename="config.json")
7model_path = hf_hub_download(repo_id="gihan12/FragNet", filename="pytorch_model.bin")
8
9config = FragNetConfig.from_json_file(config_path)
10model = FragNetFineTune(**config.get_model_kwargs())
11model.load_state_dict(torch.load(model_path, map_location='cpu'), strict=False)
12model.eval()1import pandas as pd
2import pickle
3from fragnet.dataset.data import CreateData
4from fragnet.dataset.fragments import get_3Dcoords2
5
6# A function to process SMILES
7def smiles_to_fragnet_data(smiles, data_type="exp1s", frag_type="murcko"):
8 """Convert SMILES to FragNet data format."""
9 create_data = CreateData(
10 data_type=data_type,
11 create_bond_graph_data=True,
12 add_dhangles=True,
13 )
14
15 # Get 3D coordinates
16 res = get_3Dcoords2(smiles, maxiters=500)
17 if res is None:
18 return None
19
20 mol, conf_res = res
21
22 # get_3Dcoords2 returns (mol, list of (conf_id, energy))
23 # We need to get the conformer with the lowest energy
24 if not conf_res:
25 return None
26
27 # Sort by energy and get the best conformer
28 conf_res_sorted = sorted(conf_res, key=lambda x: x[1])
29 best_conf_id = conf_res_sorted[0][0]
30 best_conf = mol.GetConformer(best_conf_id)
31
32 # create_data_point expects: (smiles, y, mol, conf, frag_type)
33 # For inference, use a dummy y value (0.0) - it will be replaced by prediction
34 args = (smiles, 0.0, mol, best_conf, frag_type)
35 data = create_data.create_data_point(args)
36
37 # Fix y to be 1D tensor for proper batching
38 data.y = data.y.reshape(-1)
39
40 return data1# Test with Aspirin
2smiles = "CC(=O)OC1=CC=CC=C1C(=O)O"
3data = smiles_to_fragnet_data(smiles)
4
5if data is not None:
6 print("✓ Data created successfully")
7 print(f" Atoms: {data.x_atoms.shape}")
8 print(f" Fragments: {data.x_frags.shape}")
9else:
10 print("✗ Failed to create data")1from fragnet.dataset.data import collate_fn
2
3if data is not None:
4 # Create batch using the proper collate function
5 batch = collate_fn([data])
6
7 # Predict
8 with torch.no_grad():
9 prediction = model(batch)
10 print(f"\nPrediction for {smiles}")
11 print(f" Value: {prediction.item():.4f}")
12