Force Field X with Mathematica

1. Prerequisites: Install Java and Add FFX to the Classpath

This file is available in the Force Field X distribution “mathematica” directory and on GitHub at:
https://github.com/SchniedersLab/forcefieldx/blob/master/mathematica/ForceFieldX.nb
​
Force Field X depends on Java version 21. If your version of Mathematica is using an earlier version, please either
1) Set the “JAVA_HOME” environment variable to point to version 21, and then start Mathematica from the command line
​or
2) Edit the javacommand string below from
javacommand = “/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home/bin/java “<>flags;
to
javacommand = “/your/path/to/jdk/bin/java “<>flags;
​
The first time this notebook is executed, FFX will be downloaded to the Mathematica User Base DIrectory for reuse.
To update FFX, please delete this file and it will be re-downloaded.
In[]:=
Quit;​​Needs["JLink`"];​​ffxVersion="1.0.0-beta";​​flags="-Dj3d.rend=noop -Djava.awt.headless=true";​​javacommand="/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home/bin/java "<>flags;​​If[FailureQ[Environment["JAVA_HOME"]],,javacommand=Environment["JAVA_HOME"]<>"/bin/java "<>flags];​​ReinstallJava[CommandLinejavacommand,ClassPathNone];​​LoadJavaClass["java.lang.Runtime",StaticsVisibleTrue];​​"Java version: "<>version[]@toString[]​​SetDirectory[$UserBaseDirectory];​​ffxFile="ffx-"<>ffxVersion<>"-bin.tar.gz";​​If[FileExistsQ[ffxFile],"Using existing Force Field X download: "<>AbsoluteFileName[ffxFile],ExtractArchive[URLDownload["https://ffx.biochem.uiowa.edu/"<>ffxFile,File[ffxFile]]]]​​ffxLibDir="ffx-"<>ffxVersion<>"/lib";​​AddToClassPath[AbsoluteFileName[ffxLibDir],"Prepend"True];​​"Current Directory: "<>ResetDirectory[]
Out[]=
Java version: 21+35-LTS-2513
Out[]=
Using existing Force Field X download: /Users/mjschnie/Library/Mathematica/ffx-1.0.0-beta-bin.tar.gz
Out[]=
Current Directory: /Users/mjschnie

2. Load a Protein Databank File from the RCSB.

Download a file from the PDB and render it.
a.) Define a string with a 4-character PDB ID. This example uses the SARS-CoV-2 main protease.
b.) Import the file from the Protein Databank, and save it locally.
c.) Display an 2D image of the structure.
In[]:=
pdbID="1BNA";​​filename=pdbID<>".pdb"​​URLDownload["http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId="<>pdbID,filename]​​Import[filename,ImageSizeMedium]
Out[]=
1BNA.pdb
Out[]=
File
1BNA.pdb

Out[]=

3. Use Force Field X Command Line Syntax

Force Field X command line syntax consists of naming a script (e.g. “Energy” below) and passing an array of String arguments (e.g. filename below).​JLink documentation: https://reference.wolfram.com/language/JLink/tutorial/Overview.htmlForce Field X documentation: https://ffx.biochem.uiowa.edu
In[]:=
LoadJavaClass["ffx.Main",StaticsVisibleTrue];​​script=ffxScript[{"Energy",filename}];​​forceFieldEnergy=script@forceFieldEnergy;​​forceFieldEnergy@toString[]
Out[]=
Bond Stretching 104.71188695 976 0.000 ( 0.01669) Angle Bending 330.00061577 1556 0.002 ( 3.35761) Stretch-Bend 1.82412339 1246 0.002 Urey-Bradley -0.12349295 80 0.000 Out-of-Plane Bend 0.34878178 432 0.000 Torsional Angle -28.96931600 2156 0.001 Pi-Orbital Torsion 0.84096434 140 0.000 Stretch-Torsion 0.06914191 68 0.000 Angle-Torsion -6.16994404 112 0.000 Van der Waals 1360.46674832 224997 0.028 Atomic Multipoles -3501.51456144 52803 Polarization -404.31559758 52803 0.588 Total Potential -2142.83064954 (Kcal/mole) 0.623 (sec) Unit Cell -8571.32259817 Replicates Cell -17142.64519634

4. Use JLink Syntax to Use the Force Field X

Alternatively, more precise control is possible using JLink Syntax to use the entire Force Field X API.

a.) An ArrayList (list) is created and loaded with String arguments.
b.) A Groovy Binding is created, and the "args" property is set to the list.
c.) The Energy script is created, loaded with the Groovy Binding, and its run method called.
d.) Finally, the toString method of the Energy script field "forceFieldEnergy" is called.
In[]:=
list=JavaNew["java.util.ArrayList"];​​list@add[JavaNew["java.lang.String",filename]];​​binding=JavaNew["groovy.lang.Binding"];​​binding@setProperty["args",list];​​script=JavaNew["ffx.potential.groovy.Energy"];​​script@setBinding[binding];​​script@run[];​​forceFieldEnergy=script@forceFieldEnergy;​​forceFieldEnergy@toString[]
Out[]=
Bond Stretching 104.71188695 976 0.000 ( 0.01669) Angle Bending 330.00061577 1556 0.001 ( 3.35761) Stretch-Bend 1.82412339 1246 0.001 Urey-Bradley -0.12349295 80 0.000 Out-of-Plane Bend 0.34878178 432 0.000 Torsional Angle -28.96931600 2156 0.001 Pi-Orbital Torsion 0.84096434 140 0.000 Stretch-Torsion 0.06914191 68 0.000 Angle-Torsion -6.16994404 112 0.000 Van der Waals 1360.46674832 224997 0.034 Atomic Multipoles -3501.51456144 52803 Polarization -404.31559758 52803 0.288 Total Potential -2142.83064954 (Kcal/mole) 0.327 (sec) Unit Cell -8571.32259817 Replicates Cell -17142.64519634