Skip to content

Commit

Permalink
Merge commit '021b0014ea1e1ca3024ffdaa7bd17492d3810d35'
Browse files Browse the repository at this point in the history
  • Loading branch information
mojaie committed Jan 26, 2025
2 parents ec09248 + 021b001 commit 11bf72c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ JULIA ?= julia
DLEXT := $(shell $(JULIA) --startup-file=no -e 'using Libdl; print(Libdl.dlext)')

TARGET="../compiled"
DEST="/usr/local/moleculargraphjl"
DEST="/opt/moleculargraphjl"

MYLIB_INCLUDES = $(TARGET)/include/julia_init.h $(TARGET)/include/libmoleculargraph.h
MYLIB_PATH := $(TARGET)/lib/libmoleculargraph.$(DLEXT)
Expand Down
10 changes: 10 additions & 0 deletions build/generate_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function run()
# write(f, img)
# close(f)
@debug "length(drawpng(io, demomol, 1000, 1000))" length(img)
@debug "length(molblock(demomol))" length(unsafe_string(molblock(unsafe_convert(Cstring, demomol))))
@debug "length(sdfmolblock(demomol))" length(unsafe_string(sdfmolblock(unsafe_convert(Cstring, demomol))))

nullmol_sdf = read(open(joinpath(dirname(@__FILE__), "../assets/test/null.mol")), String)
nullmol = unsafe_string(sdftomol(
Expand All @@ -60,8 +62,16 @@ function run()

notamide = unsafe_string(smartstomol(
unsafe_convert(Cstring, raw"[NX3;H2,H1;!$(NC=O)]")))
notamide2 = unsafe_string(smartstomol(
unsafe_convert(Cstring, raw"[NX3;H2,H1]")))
rotatable = unsafe_string(smartstomol(
unsafe_convert(Cstring, raw"[!$(*#*)&!D1]-!@[!$(*#*)&!D1]")))
@debug "notamide rotatable" has_exact_match(
unsafe_convert(Cstring, notamide), unsafe_convert(Cstring, rotatable),
unsafe_convert(Cstring, op))
@debug "notamide notamide2" has_substruct_match(
unsafe_convert(Cstring, notamide), unsafe_convert(Cstring, notamide2),
unsafe_convert(Cstring, op))

@debug "nullsmiles nullmol" has_exact_match(
unsafe_convert(Cstring, nullsmiles), unsafe_convert(Cstring, nullmol),
Expand Down
2 changes: 2 additions & 0 deletions build/libmoleculargraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ int vertex_count(char*);
int edge_count(char*);
char* inchikey(char*);
double standard_weight(char*);
char* molblock(char*);
char* sdfmolblock(char*);
char* drawsvg(char*);
char* drawpng(char*, unsigned int, unsigned int);
int has_exact_match(char*, char*, char*);
Expand Down
24 changes: 21 additions & 3 deletions scripts/python_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Python >= 3.10

import base64
from ctypes import CDLL, RTLD_GLOBAL, POINTER, cast, c_char_p, c_ubyte, c_double, c_int, c_uint
import json
Expand All @@ -7,7 +9,7 @@

def jl_init():
dlext = "dylib" if platform.system() == "Darwin" else "so"
libdir = Path("/usr/local/moleculargraphjl/lib")
libdir = Path("/opt/moleculargraphjl/lib")
jl = CDLL(libdir / f"libmoleculargraph.{dlext}", RTLD_GLOBAL)
jl.jl_init_with_image(bytes(libdir), f"libmoleculargraph.{dlext}".encode())
return jl
Expand All @@ -18,7 +20,7 @@ def jl_exit(jl):


def smiles_to_mol(jl, smiles: str) -> bytes:
jl.smilestomol.argtypes = [c_char_p]
jl.smilestomol.argtypes = [c_char_p, c_char_p]
jl.smilestomol.restype = c_char_p
return jl.smilestomol(smiles.encode(), r"{}".encode())

Expand All @@ -31,7 +33,7 @@ def smarts_to_mol(jl, smarts: str) -> bytes:

def sdf_to_mol(jl, sdf: str) -> bytes:
# sdf: SDFile string (open as f -> f.read())
jl.sdftomol.argtypes = [c_char_p]
jl.sdftomol.argtypes = [c_char_p, c_char_p]
jl.sdftomol.restype = c_char_p
return jl.sdftomol(sdf.encode(), r"{}".encode())

Expand Down Expand Up @@ -99,6 +101,20 @@ def smiles_to_png(jl, smiles: str, width: int, height: int) -> bytes:
return mol_to_png(jl, mol, width, height)


def mol_to_molblock(jl, mol: bytes) -> str:
# mol: json.dumps(mol_dict).encode()
jl.molblock.argtypes = [c_char_p]
jl.molblock.restype = c_char_p
return jl.molblock(mol).decode("utf-8")


def mol_to_sdfblock(jl, mol: bytes) -> str:
# mol: json.dumps(mol_dict).encode()
jl.sdfmolblock.argtypes = [c_char_p]
jl.sdfmolblock.restype = c_char_p
return jl.sdfmolblock(mol).decode("utf-8")


def has_exact_match(jl, mol1: bytes, mol2: bytes, options: bytes) -> bool:
# mol1, mol2, options: json.dumps(dict).encode()
jl.has_exact_match.argtypes = [c_char_p, c_char_p, c_char_p]
Expand Down Expand Up @@ -137,4 +153,6 @@ def tdmces_size(jl, mol1: bytes, mol2: bytes, options: bytes) -> int:
print(tdmces_size(jl, mol1, mol2, json.dumps({}).encode()))
print(len(mol_to_svg(jl, mol1)))
print(len(mol_to_png(jl, mol1, 100, 100)))
print(len(mol_to_molblock(jl, mol1)))
print(len(mol_to_sdfblock(jl, mol1)))
jl_exit(jl)
8 changes: 5 additions & 3 deletions src/draw/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ svgtransform(tf::Array{Float64,2}
) = @sprintf "%.2f %.2f %.2f %.2f %.2f %.2f" tf[1] tf[2] tf[4] tf[5] tf[7] tf[8]


function tosvg(canvas::SvgCanvas; width="100%", height="100%", kwargs...)
function tosvg(canvas::SvgCanvas; width="100%", height="100%", viewbox=true, kwargs...)
vbWf = @sprintf "%.2f" canvas.viewboxW
vbHf = @sprintf "%.2f" canvas.viewboxH
vb = viewbox ? " viewBox=\"0 0 $(vbWf) $(vbHf)\"" : ""
w = viewbox ? width : vbWf
h = viewbox ? height : vbHf
bgc = svgcolor(canvas.bgcolor)
header = """<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
Expand All @@ -83,8 +86,7 @@ function tosvg(canvas::SvgCanvas; width="100%", height="100%", kwargs...)
preserveAspectRatio="xMidYMid meet"
font-weight="$(canvas.fontweight)"
font-family="$(canvas.fontfamily)"
width="$(width)" height="$(height)"
viewBox="0 0 $(vbWf) $(vbHf)">
width="$(w)" height="$(h)"$(vb)>
"""
bg = """<rect x="0" y="0" width="$(vbWf)" height="$(vbHf)"
fill="$(bgc)" opacity="$(canvas.bgopacity)"/>
Expand Down
25 changes: 25 additions & 0 deletions src/libmoleculargraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Base64

export
vertex_count, edge_count,
molblock, sdfmolblock,
tdmcis_size, tdmces_size, tdmcis_gls, tdmces_gls


Expand Down Expand Up @@ -97,6 +98,30 @@ Base.@ccallable function standard_weight(mol::Cstring)::Cdouble
end


Base.@ccallable function molblock(mol::Cstring)::Cstring
return try
molobj = MolGraph(JSON.parse(unsafe_string(mol)))
return unsafe_convert(Cstring, printv2mol(molobj))
catch
Base.invokelatest(Base.display_error, Base.catch_stack())
end
end


Base.@ccallable function sdfmolblock(mol::Cstring)::Cstring
return try
molobj = MolGraph(JSON.parse(unsafe_string(mol)))
buf = IOBuffer(write=true)
printv2sdf(buf, molobj)
res = String(take!(buf))
close(buf)
return unsafe_convert(Cstring, res)
catch
Base.invokelatest(Base.display_error, Base.catch_stack())
end
end


Base.@ccallable function drawsvg(mol::Cstring)::Cstring
return try
molobj = MolGraph(JSON.parse(unsafe_string(mol)))
Expand Down

0 comments on commit 11bf72c

Please sign in to comment.