Skip to content

Commit

Permalink
Merge pull request #185 from hjkgrp/importlib_resources
Browse files Browse the repository at this point in the history
Importlib resources
  • Loading branch information
ralf-meyer authored Oct 20, 2023
2 parents 2788416 + 891afd6 commit 84f1741
Show file tree
Hide file tree
Showing 465 changed files with 523 additions and 552 deletions.
2 changes: 2 additions & 0 deletions devtools/conda-envs/mols.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ dependencies:
- pandas
- scipy
- libffi
- importlib_resources
- pip
- pip:
- pytest
- pytest-resource-path

# Optional
# GUI / plotting
Expand Down
2 changes: 2 additions & 0 deletions devtools/conda-envs/mols_minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependencies:
- pandas
- scipy
- libffi
- importlib_resources
- pip
- pip:
- pytest
- pytest-resource-path
41 changes: 14 additions & 27 deletions molSimplify/Classes/mGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import glob
import tempfile
import re
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files
import xml.etree.ElementTree as ET

try:
Expand Down Expand Up @@ -142,8 +142,7 @@ def initGUI(self, app):
helpAction.triggered.connect(self.qshowhelp)
menu2.addAction(helpAction)
# ## place title top ###
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/logo.png")
f = resource_files("molSimplify").joinpath("icons/logo.png")
clogo = mQPixmap(f)
self.grid.addWidget(clogo, 2, 8, 1, 10)
self.txtdev = mQLabel('Developed by Kulik group @ MIT', '', 'c', 16)
Expand Down Expand Up @@ -363,8 +362,7 @@ def initGUI(self, app):
if globs.custom_path:
f = globs.custom_path + "/Ligands/ligands.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/ligands.dict")
f = str(resource_files("molSimplify").joinpath("Ligands/ligands.dict"))
qcav0 = getligroups(readdict(f))
qcav = [_f for _f in qcav0.split(' ') if _f]
self.etliggrp = mQComboBox(qcav, ctip, 12)
Expand Down Expand Up @@ -613,8 +611,7 @@ def initGUI(self, app):
self.qctWindow.setPalette(p) # set background color
self.qctWindow.setLayout(self.qctgrid) # set layout

f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/petachem.png")
f = resource_files("molSimplify").joinpath("icons/petachem.png")
c0 = mQPixmap(f)
self.qctgrid.addWidget(c0, 0, 2, 1, 1)
# top text
Expand Down Expand Up @@ -876,10 +873,8 @@ def initGUI(self, app):
self.sgrid.addWidget(self.jWindow) # add to stacked grid
self.jWindow.setPalette(p) # set background color
self.jWindow.setLayout(self.jgrid) # set layout
f1 = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/sge.png")
f2 = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/slurm.png")
f1 = resource_files("molSimplify").joinpath("icons/sge.png")
f2 = resource_files("molSimplify").joinpath("icons/slurm.png")
c1 = mQPixmap(f1)
c2 = mQPixmap(f2)
self.jgrid.addWidget(c1, 1, 2, 1, 1)
Expand Down Expand Up @@ -1181,8 +1176,7 @@ def initGUI(self, app):
self.etcDBmw1 = mQLineEdit('', ctip, 'l', 14)
self.cDBgrid.addWidget(self.etcDBmw1, 11, 8, 1, 1)
# aspirin icon
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/chemdb.png")
f = resource_files("molSimplify").joinpath("icons/chemdb.png")
c = mQPixmap(f)
relresize(c, c, 0.4)
self.cDBgrid.addWidget(c, 7, 0, 4, 2)
Expand Down Expand Up @@ -1271,8 +1265,7 @@ def initGUI(self, app):
self.butpret.clicked.connect(self.qretmain)
self.pgrid.addWidget(self.butpret, 7, 3, 1, 2)
# c1p = mPic(self.pWindow,globs.installdir+'/icons/wft1.png',0.04,0.7,0.2)
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/wft3.png")
f = resource_files("molSimplify").joinpath("icons/wft3.png")
c3p = mQPixmap(f)
self.pgrid.addWidget(c3p, 3, 0, 4, 1)
# c2p = mPic(self.pWindow,globs.installdir+'/icons/wft2.png',0.04,0.035,0.2)
Expand Down Expand Up @@ -1746,8 +1739,7 @@ def qaddg(self):
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = str(resource_files("molSimplify").joinpath("Data/coordinations.dict"))
with open(f, 'r') as fl:
s = fl.read().splitlines()
if gname.lower() in s:
Expand Down Expand Up @@ -1790,8 +1782,7 @@ def qaddg(self):
fl.write(xyzl)
# write png file
if glob.glob(cfile):
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/geoms/")
f = str(resource_files("molSimplify").joinpath("icons/geoms/"))
shutil.copy2(cfile, f+gshort+'.png')
choice = QMessageBox.information(
self.geWindow, 'Add', 'Successfully added to the database!')
Expand Down Expand Up @@ -1827,8 +1818,7 @@ def qdelg(self):
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = str(resource_files("molSimplify").joinpath("Data/coordinations.dict"))
with open(f, 'r') as fl:
s = fl.read()
if gname.lower() not in s and gshort.lower() not in s:
Expand All @@ -1847,8 +1837,7 @@ def qdelg(self):
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = str(resource_files("molSimplify").joinpath("Data/coordinations.dict"))
with open(f, 'w') as fl:
fl.write(snew)
# remove file
Expand Down Expand Up @@ -2076,8 +2065,7 @@ def drawligs_svg(self):
def viewgeom(self):
# get geometry
geom = self.dcoordg.currentText()
gfname = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/icons/geoms/" + geom + ".png")
gfname = str(resource_files("molSimplify").joinpath(f"icons/geoms/{geom}.png"))
if glob.glob(gfname):
rows = self.lgrid.rowCount()
# Clear existing widgets in layout
Expand Down Expand Up @@ -2268,8 +2256,7 @@ def matchgeomcoord(self):
# add to box
for i, t in enumerate(qc):
# File f is never actually used? RM 2022/02/17
f = resource_filename(Requirement.parse( # noqa F841
"molSimplify"), "molSimplify/icons/geoms/" + t + ".png")
f = str(resource_files("molSimplify").joinpath(f"icons/geoms/{t}.png")) # noqa F841
self.dcoordg.addItem(QIcon(t), t)
self.dcoordg.setIconSize(QSize(60, 60))
# set default geometry
Expand Down
5 changes: 2 additions & 3 deletions molSimplify/Classes/mol3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,7 @@ def returnxyz(self):
ss += "%s \t%f\t%f\t%f\n" % (atom.sym, xyz[0], xyz[1], xyz[2])
return (ss)

def readfromxyz(self, filename, ligand_unique_id=False, read_final_optim_step=False):
def readfromxyz(self, filename: str, ligand_unique_id=False, read_final_optim_step=False):
"""
Read XYZ into a mol3D class instance.
Expand All @@ -2693,8 +2693,7 @@ def readfromxyz(self, filename, ligand_unique_id=False, read_final_optim_step=Fa
amassdict = globs.amass()
self.graph = []
self.xyzfile = filename
fname = filename.split('.xyz')[0]
with open(fname + '.xyz', 'r') as f:
with open(filename, 'r') as f:
s = f.read().splitlines()
try:
atom_count = int(s[0])
Expand Down
5 changes: 2 additions & 3 deletions molSimplify/Informatics/MOF/MOF_functionalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
XYZ_connected,
write_cif,
)
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files
import numpy as np
import scipy
import networkx as nx
Expand Down Expand Up @@ -1257,8 +1257,7 @@ def functionalize_MOF_at_indices_mol3D_merge(cif_file, path2write, functional_gr

# Load in the mol3D from the folder molSimplify folder monofunctionalized_BDC.
functional_group_template = mol3D()
func_group_xyz_path = resource_filename(Requirement.parse(
"molSimplify"), f"molSimplify/Informatics/MOF/monofunctionalized_BDC/{functional_group}.xyz")
func_group_xyz_path = str(resource_files("molSimplify").joinpath(f"Informatics/MOF/monofunctionalized_BDC/{functional_group}.xyz"))
functional_group_template.readfromxyz(func_group_xyz_path) # This is a whole BDC linker with the requested functional group on it.

# Read information about the important indices of the functional_group_template.
Expand Down
4 changes: 2 additions & 2 deletions molSimplify/Informatics/geometrics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import numpy as np
import json
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files
from molSimplify.Classes.mol3D import mol3D


Expand All @@ -17,7 +17,7 @@ def get_percentile_csd_geometrics(geometrics_csd, geodict, geotype, maxdent,
metrics: list, a list of geometric considered.
path2metric: str, the molSimplify path to load geometrics_csd if geometrics_csd is not specified
'''
jsonpath = resource_filename(Requirement.parse("molSimplify"), path2metric)
jsonpath = resource_files("molSimplify").joinpath(path2metric)
if not isinstance(geometrics_csd, dict):
print("loading csd geometrics...")
with open(jsonpath, "r") as f:
Expand Down
63 changes: 21 additions & 42 deletions molSimplify/Scripts/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
except ImportError:
import openbabel # fallback to version 2
from typing import Any, List, Dict, Tuple, Union, Optional
from pkg_resources import resource_filename, Requirement
from importlib_resources import files as resource_files

from molSimplify.Classes.globalvars import (globalvars,
romans)
Expand All @@ -34,8 +34,7 @@ def printgeoms():
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = resource_files("molSimplify").joinpath("Data/coordinations.dict")
with open(f, 'r') as f:
s = f.read().splitlines()
s = [_f for _f in s if _f]
Expand Down Expand Up @@ -64,8 +63,7 @@ def getgeoms():
if globs.custom_path:
f = globs.custom_path + "/Data/coordinations.dict"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/coordinations.dict")
f = resource_files("molSimplify").joinpath("Data/coordinations.dict")
with open(f, 'r') as f:
s = f.read().splitlines()
s = [_f for _f in s if _f]
Expand Down Expand Up @@ -164,8 +162,7 @@ def getlicores(flip: bool = True) -> Dict[str, Any]:
if globs.custom_path: # test if a custom path is used:
licores_path = str(globs.custom_path).rstrip('/') + "/Ligands/ligands.dict"
else:
licores_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/ligands.dict")
licores_path = resource_files("molSimplify").joinpath("Ligands/ligands.dict")
licores = readdict(licores_path)
if flip:
for ligand in list(licores.keys()):
Expand Down Expand Up @@ -195,8 +192,7 @@ def getslicores() -> Dict[str, Any]:
slicores_path = str(globs.custom_path).rstrip(
'/') + "/Ligands/simple_ligands.dict"
else:
slicores_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/simple_ligands.dict")
slicores_path = resource_files("molSimplify").joinpath("Ligands/simple_ligands.dict")
slicores = readdict(slicores_path)
return slicores

Expand Down Expand Up @@ -245,8 +241,7 @@ def getbcores() -> dict:
if globs.custom_path: # test if a custom path is used:
bcores_path = str(globs.custom_path).rstrip('/') + "/Bind/bind.dict"
else:
bcores_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Bind/bind.dict")
bcores_path = resource_files("molSimplify").joinpath("Bind/bind.dict")
bcores = readdict(bcores_path)
return bcores

Expand Down Expand Up @@ -275,8 +270,7 @@ def getmcores():
if globs.custom_path: # test if a custom path is used:
mcores = str(globs.custom_path).rstrip('/') + "/Cores/cores.dict"
else:
mcores = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Cores/cores.dict")
mcores = resource_files("molSimplify").joinpath("Cores/cores.dict")
mcores = readdict(mcores)
return mcores

Expand Down Expand Up @@ -306,8 +300,7 @@ def getsubcores():
subcores = str(globs.custom_path).rstrip(
'/') + "/Substrates/substrates.dict"
else:
subcores = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Substrates/substrates.dict")
subcores = resource_files("molSimplify").joinpath("Substrates/substrates.dict")
subcores = readdict_sub(subcores)
return subcores

Expand All @@ -323,8 +316,7 @@ def loaddata(path: str) -> dict:
if globs.custom_path: # test if a custom path is used:
fname = str(globs.custom_path).rstrip('/') + path
else:
fname = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify"+path)
fname = resource_files("molSimplify").joinpath(path.strip('/'))
d = dict()

with open(fname) as f:
Expand All @@ -348,8 +340,7 @@ def loaddata_ts(path: str) -> dict:
if globs.custom_path: # test if a custom path is used:
fname = str(globs.custom_path).rstrip('/') + path
else:
fname = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify"+path)
fname = resource_files("molSimplify").joinpath(path.strip('/'))
d = dict()

with open(fname) as f:
Expand Down Expand Up @@ -452,8 +443,7 @@ def loadcoord(coord: str) -> List[List[float]]:
if globs.custom_path:
f = globs.custom_path + "/Data/" + coord + ".dat"
else:
f = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Data/" + coord + ".dat")
f = resource_files("molSimplify").joinpath(f"Data/{coord}.dat")
with open(f) as f:
txt = [_f for _f in f.read().splitlines() if _f]
b = []
Expand Down Expand Up @@ -485,8 +475,7 @@ def core_load(usercore: str, mcores: Optional[dict] = None) -> Tuple[Union[mol3D
if globs.custom_path:
fcore = globs.custom_path + "/Cores/" + dbentry[0]
else:
fcore = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Cores/" + dbentry[0])
fcore = str(resource_files("molSimplify").joinpath(f"Cores/{dbentry[0]}"))
# check if core xyz/mol file exists
if not glob.glob(fcore):
emsg = "We can't find the core structure file %s right now! Something is amiss. Exiting..\n" % fcore
Expand Down Expand Up @@ -582,8 +571,7 @@ def substr_load(usersubstrate: str,
if globs.custom_path:
fsubst = globs.custom_path + "/Substrates/" + var_list_sub_i[0]
else:
fsubst = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Substrates/" + var_list_sub_i[0])
fsubst = str(resource_files("molSimplify").joinpath(f"Substrates/{var_list_sub_i[0]}"))
# check if substrate xyz/mol file exists
if not glob.glob(fsubst):
emsg = "We can't find the substrate structure file %s right now! Something is amiss. Exiting..\n" % fsubst
Expand Down Expand Up @@ -706,8 +694,7 @@ def lig_load(userligand: str, licores: Optional[dict] = None) -> Tuple[Any, str]
if globs.custom_path:
flig = globs.custom_path + "/Ligands/" + dbentry[0]
else:
flig = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands/" + dbentry[0])
flig = str(resource_files("molSimplify").joinpath(f"Ligands/{dbentry[0]}"))
# check if ligand xyz/mol file exists
print(('looking for '+flig))
if not os.path.isfile(flig):
Expand Down Expand Up @@ -753,7 +740,6 @@ def lig_load(userligand: str, licores: Optional[dict] = None) -> Tuple[Any, str]
lig.ffopt = dbentry[4][0]
# load from file
elif ('.mol' in userligand or '.xyz' in userligand or '.smi' in userligand or '.sdf' in userligand):
# flig = resource_filename(Requirement.parse("molSimplify"),"molSimplify/" +userligand)
if glob.glob(userligand):
ftype = userligand.split('.')[-1]
# try and catch error if conversion doesn't work
Expand Down Expand Up @@ -818,8 +804,7 @@ def bind_load(userbind: str, bindcores: dict) -> Tuple[Union[mol3D, None], bool,
if globs.custom_path:
fbind = globs.custom_path + "/Bind/" + bindcores[userbind][0]
else:
fbind = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Bind/" + bindcores[userbind][0])
fbind = str(resource_files("molSimplify").joinpath(f"Bind/{bindcores[userbind][0]}"))
# check if bind xyz/mol file exists
if not glob.glob(fbind):
emsg = "We can't find the binding species structure file %s right now! Something is amiss. Exiting..\n" % fbind
Expand Down Expand Up @@ -903,8 +888,7 @@ def getinputargs(args, fname: str):


def plugin_defs() -> str:
plugin_path = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/plugindefines_reference.txt")
plugin_path = str(resource_files("molSimplify").joinpath("plugindefines_reference.txt"))
return plugin_path

# def get_name(args,rootdir,core,ligname,bind = False,bsmi = False):
Expand Down Expand Up @@ -1144,16 +1128,11 @@ def copy_to_custom_path():
if not os.path.exists(globs.custom_path):
os.makedirs(globs.custom_path)
# copytree cannot overwrite, need to enusre directory does not exist already
core_dir = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Cores")
li_dir = resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Ligands")
bind_dir = (resource_filename(
Requirement.parse("molSimplify"), "molSimplify/Bind"))
data_dir = (resource_filename(
Requirement.parse("molSimplify"), "molSimplify/Data"))
subs_dir = (resource_filename(Requirement.parse(
"molSimplify"), "molSimplify/Substrates"))
core_dir = resource_files("molSimplify").joinpath("Cores")
li_dir = resource_files("molSimplify").joinpath("Ligands")
bind_dir = resource_files("molSimplify").joinpath("Bind")
data_dir = resource_files("molSimplify").joinpath("Data")
subs_dir = resource_files("molSimplify").joinpath("Substrates")
if os.path.exists(str(globs.custom_path).rstrip("/")+"/Cores"):
print('Note: removing old molSimplify data')
shutil.rmtree(str(globs.custom_path).rstrip("/")+"/Cores")
Expand Down
Loading

0 comments on commit 84f1741

Please sign in to comment.