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.

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[]

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.

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]

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.html
Force Field X documentation: 	https://ffx.biochem.uiowa.edu

LoadJavaClass["ffx.Main", StaticsVisible->True];
script = ffxScript[{"Energy",filename}];
forceFieldEnergy = script@forceFieldEnergy;
forceFieldEnergy@toString[]

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.

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[]