-
-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Python charge scripts. First example xtb GFN2 charges #998
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
set(scriptcharges_srcs | ||
scriptchargemodel.cpp | ||
scriptcharges.cpp | ||
) | ||
|
||
avogadro_plugin(ScriptCharges | ||
"Scriptable electrostatics models" | ||
ExtensionPlugin | ||
scriptcharges.h | ||
ScriptCharges | ||
"${scriptcharges_srcs}" | ||
"" | ||
) | ||
|
||
target_link_libraries(ScriptCharges PRIVATE AvogadroCalc ) | ||
|
||
# Bundled format scripts: | ||
set(charge_scripts | ||
chargeScripts/xtb.py | ||
) | ||
|
||
install(PROGRAMS ${charge_scripts} | ||
DESTINATION "${INSTALL_LIBRARY_DIR}/avogadro2/scripts/charges/") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# This source file is part of the Avogadro project. | ||
# This source code is released under the 3-Clause BSD License, (see "LICENSE"). | ||
|
||
import argparse | ||
import json | ||
import sys | ||
import os | ||
from shutil import which | ||
import tempfile | ||
import subprocess | ||
Check notice Code scanning Consider possible security implications associated with subprocess module.
Consider possible security implications associated with subprocess module.
|
||
|
||
|
||
def getMetaData(): | ||
# before we return metadata, make sure xtb is in the path | ||
if which("xtb") is None: | ||
return {} # Avogadro will ignore us now | ||
|
||
metaData = {} | ||
metaData["inputFormat"] = "xyz" # could be other formats, but this is fine | ||
metaData["identifier"] = "GFN2" | ||
metaData["name"] = "GFN2" | ||
metaData["description"] = "Calculate atomic partial charges using GFN2 and xtb" | ||
metaData["charges"] = True | ||
metaData["potential"] = False | ||
metaData["elements"] = "1-86" # up to Radon | ||
return metaData | ||
|
||
|
||
def charges(): | ||
# Avogadro will send us the sdf file as stdin | ||
# we need to write it to a temporary file | ||
|
||
# get the whole sdf file | ||
xyz = sys.stdin.read() | ||
|
||
fd, name = tempfile.mkstemp(".xyz") | ||
os.write(fd, xyz.encode()) | ||
os.close(fd) | ||
|
||
# run xtb | ||
xtb = which("xtb") | ||
if xtb is None: # we check again | ||
return "" | ||
|
||
# for now, ignore the output itself | ||
tempdir = tempfile.mkdtemp() | ||
output = subprocess.run( | ||
Check notice Code scanning subprocess call - check for execution of untrusted input.
subprocess call - check for execution of untrusted input.
Check notice Code scanning Unused variable 'output' (unused-variable)
Unused variable 'output' (unused-variable)
|
||
[xtb, name], stdout=subprocess.PIPE, cwd=tempdir, check=True | ||
) | ||
# instead we read the "charges" file | ||
result = "" | ||
with open(tempdir + "/" + "charges", "r", encoding="utf-8") as f: | ||
result = f.read() | ||
|
||
# try to cleanup the temporary files | ||
os.remove(name) | ||
for filename in os.listdir(tempdir): | ||
try: | ||
os.remove(tempdir + "/" + filename) | ||
except: | ||
Check notice Code scanning Try, Except, Continue detected.
Try, Except, Continue detected.
Check notice Code scanning No exception type(s) specified (bare-except)
No exception type(s) specified (bare-except)
|
||
continue | ||
# and try to cleanup the directory | ||
try: | ||
os.rmdir(tempdir) | ||
except: | ||
Check notice Code scanning Try, Except, Pass detected.
Try, Except, Pass detected.
Check notice Code scanning No exception type(s) specified (bare-except)
No exception type(s) specified (bare-except)
|
||
pass | ||
|
||
# write the charges to stdout | ||
return result | ||
|
||
|
||
def potential(): | ||
# at the moment, xtb doesn't have a good way to do this | ||
# and the method shouldn't be called anyway | ||
|
||
# if your plugin has a potential, you can return it here | ||
# .. you'll get JSON with the file and the set of points | ||
# e.g. { "xyz" : "xyz file contents", "points" : [ x,y,z, x,y,z, ... ] } | ||
# or { "sdf" : "sdf file contents", "points" : [ x,y,z, x,y,z, ... ] } | ||
# .. and you print the list of potentials to stdout | ||
return "" | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser("GFN2 partial charges") | ||
parser.add_argument("--display-name", action="store_true") | ||
parser.add_argument("--metadata", action="store_true") | ||
parser.add_argument("--charges", action="store_true") | ||
parser.add_argument("--potential", action="store_true") | ||
parser.add_argument("--lang", nargs="?", default="en") | ||
args = vars(parser.parse_args()) | ||
|
||
if args["metadata"]: | ||
print(json.dumps(getMetaData())) | ||
elif args["display_name"]: | ||
print(getMetaData()["name"]) | ||
elif args["charges"]: | ||
print(charges()) | ||
elif args["potential"]: | ||
print(potential()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blacklist: Consider possible security implications associated with the subprocess module.
Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.
When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.
Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]