-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a workflow to generate the header file from the SMILES templates …
…file (#6)
- Loading branch information
Showing
12 changed files
with
334 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
name: Update the header file and image gallery | ||
on: | ||
workflow_dispatch: ~ | ||
workflow_call: ~ | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- templates.smi | ||
|
||
permissions: | ||
contents: write | ||
|
||
env: | ||
tpl_hdr: 'template_smiles.h' | ||
gallery_file: 'gallery.md' | ||
img_dir: 'img' | ||
|
||
jobs: | ||
Check_new_templates: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v3 | ||
- name: Set up Python & RDKit | ||
uses: mamba-org/setup-micromamba@v1 | ||
with: | ||
micromamba-version: 'latest' | ||
environment-file: environment.yml | ||
generate-run-shell: true | ||
cache-environment: true | ||
post-cleanup: 'none' | ||
- name: Run basic checks | ||
if: success() | ||
run: pytest -n=auto checks/test_requisites.py | ||
shell: micromamba-shell {0} | ||
- name: Run check for duplicates | ||
if: success() | ||
run: pytest -sv checks/test_duplicates.py | ||
shell: micromamba-shell {0} | ||
- name: Generate the header file | ||
id: header-gen | ||
if: success() | ||
run: python3 src/header_generation.py | ||
shell: micromamba-shell {0} | ||
- name: Test the header file | ||
if: ${{ steps.header-gen.outputs.header_changed == 'true' && success() }} | ||
run: g++ -std=c++17 -I. checks/test_program.cpp -o test && ./test | ||
shell: micromamba-shell {0} | ||
- name: Update the gallery | ||
if: ${{ steps.header-gen.outputs.header_changed == 'true' && success() }} | ||
run: python3 src/update_gallery.py | ||
shell: micromamba-shell {0} | ||
|
||
- name: Commit and push the new header file | ||
if: ${{ steps.header-gen.outputs.header_changed == 'true' && success() }} | ||
run: | | ||
if ! git diff --quiet --exit-code ${{ env.tpl_hdr }}; then | ||
git config user.name "github-actions[bot]" | ||
git config user.email "github-actions[bot]@noreply.github.com" | ||
git checkout main | ||
git pull | ||
git add ${{ env.tpl_hdr }} ${{ env.gallery_file }} ${{ env.img_dir }} | ||
coauthor=$(git log -1 --pretty=format:'%an <%ae>' -- ${{ env.tpl_hdr }}) | ||
> commitmsg | ||
echo -e "[bot] Update molecular templates header\n\n" >> commitmsg | ||
echo -e "Co-authored-by: ${coauthor}\n" >> commitmsg | ||
git commit -F commitmsg | ||
git push | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <iostream> | ||
|
||
#include "template_smiles.h" | ||
|
||
int main() | ||
{ | ||
size_t i = 0; | ||
for (const auto& cxsmiles : TEMPLATE_SMILES) { | ||
std::cout << "Template #" << ++i << ": " << cxsmiles << std::endl; | ||
} | ||
|
||
std::cout << "All templates in header listed, check passed." << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ channels: | |
- conda-forge | ||
dependencies: | ||
- gitpython | ||
- gxx | ||
- python=3.11 | ||
- pytest | ||
- pytest-xdist | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import filecmp | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
from rdkit import Chem | ||
|
||
HEADER_FILE = 'template_smiles.h' | ||
TEMPLATE_FILE = 'templates.smi' | ||
|
||
HEADER_TEXT = """// | ||
// Copyright (C) 2023 Schrödinger, LLC | ||
// | ||
// @@ All Rights Reserved @@ | ||
// This file is part of the RDKit. | ||
// The contents are covered by the terms of the BSD license | ||
// which is included in the file license.txt, found at the root | ||
// of the RDKit source tree. | ||
// | ||
// THIS FILE IS AUTOMATICALLY GENERATED. It contains templates used | ||
// in 2D coordinate generation. If you want to contribute to these | ||
// templates, please refer to instructions in: | ||
// https://github.com/rdkit/molecular_templates/blob/main/README.md | ||
// | ||
#include <string> | ||
#include <vector> | ||
// clang-format off | ||
const std::vector<std::string> TEMPLATE_SMILES = { | ||
""" | ||
|
||
|
||
def clean_smiles(template_smiles): | ||
""" | ||
Translate all atoms into dummy atoms so that templates are not atom-specific. | ||
""" | ||
template = Chem.MolFromSmiles(template_smiles) | ||
for atom in template.GetAtoms(): | ||
atom.SetAtomicNum(0) | ||
|
||
# TO_DO: replace bonds with query bonds | ||
|
||
return Chem.MolToCXSmiles(template) | ||
|
||
|
||
def generate_header(generated_header_path): | ||
with open(generated_header_path, 'w') as f_out: | ||
f_out.write(HEADER_TEXT) | ||
with open(TEMPLATE_FILE, 'r') as f_in: | ||
for line in f_in: | ||
if not (cxsmiles := line.strip()): | ||
continue | ||
|
||
# TO_DO: Clean smiles to make them atom-type and bond-type agnostic | ||
# cxsmiles = clean_smiles(cxsmiles) | ||
|
||
f_out.write(f' "{cxsmiles}",\n') | ||
f_out.write('};\n// clang-format on\n') | ||
print(f"Successfully generated {generated_header_path}") | ||
|
||
|
||
def check_header_changed(header_file_path): | ||
if header_changed := not filecmp.cmp(header_file_path, HEADER_FILE): | ||
if gh_output := os.environ.get('GITHUB_OUTPUT', ''): | ||
with open(gh_output, 'a') as f: | ||
f.write(f'header_changed={str(header_changed).lower()}') | ||
print(f'Header file has {"" if header_changed else "not "}changed') | ||
return header_changed | ||
|
||
|
||
def main(): | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
generated_header_path = os.path.join(tmpdir, HEADER_FILE) | ||
generate_header(generated_header_path) | ||
|
||
if check_header_changed(generated_header_path): | ||
shutil.copy(generated_header_path, HEADER_FILE) | ||
print(f'Updated {HEADER_FILE} with the generated header') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.