Skip to content
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

OpenChemLib in Jmol SwingJS - and InChI #83

Open
BobHanson opened this issue Jan 16, 2023 · 10 comments
Open

OpenChemLib in Jmol SwingJS - and InChI #83

BobHanson opened this issue Jan 16, 2023 · 10 comments

Comments

@BobHanson
Copy link

Hello from Jmol! And Happy New Year 2023

More of a comment than an issue. In integrating JME into Jmol and Jmol-SwingJS yesterday, I also brought in OpenChemLib. It's the first I have looked at it, but it seems to be excellent. I have it running smoothly in Eclipse in Java and simultaneously in a browser using Java2Script/SwingJS. (Though the only features that I am using there so far are SMILES->2D and MOL->SVG; Jmol has all the other parts I needed for all the file conversions). Happy to report that there was only one issue there with how SwingJS was handling deep unknown size array declarations, but that was easy enough to fix. Other than that, transpilation to JavaScript appears to have been simple.

Wondering if you are interested in lifting the Java/JavaScript JNIInChI implementation from Jmol for OpenChemLib? It includes full InChI atom/bond model access (so allowing InChI -> SMILES) and full support for InChI flags (e.g., fixedH), as well as JavaScript WASM support (which currently doesn't expose the InChI atom/bond model, however).

Also wondering (along the lines of Luc's recent post relative to long type) how much the high end of long is used? JSmol in its legacy form (since 2013) doesn't handle long any differently from int. This is not a big deal; just wondering. SwingJS does handle long appropriately in JavaScript, so there's no problem there with Jmol-SwingJS.

Also wondering why the JavaFX dependency? This looks well isolated in the GUI, jfx, and chem/dnd packages. Is there something specific JavaFX gets you that Swing does not? Or was it just a convenience for some programmer along the way? Or is the GUI not really something I would be interested in? Also not seeing how the GUI classes connect to a Java application so that I can see what that is about. Is there a main class?

Thanks -- Bob Hanson

@thsa
Copy link
Contributor

thsa commented Jan 25, 2023

Hello Bob, Happy New Year, and sorry for the delayed answer,

I personally have no experience in JavaScript, but realize that there is a strong interest of at least parts of OpenChemLib being converted to JavaScript. Among other solutions, SwingJS seems like a perfect solution for it. One thing to try it on would be the new editor, which was refactored last year to completely separate logic from a thin layer of UI implementations. Currently, OpenChemLib contains a Swing based and an FX based implementation of the editor. Conceptually, the idea was to build a JS editor by converting the Java logic with GWT or J2CL and by providing the JS UI implementations manually. To my knowledge the OpenChemLib-JS project is working on this. Could we use SwingJS to directly translate from the Sing editor (com.actelion.research.gui.editor.SwingEditorArea or Dialog)?

Our Inchi support is currently based on the JNAInchi project. A native Java implementation would be very interesting, of course. I hope that the Inchi-2.0 project comes up with C and Java code from the beginning...

Concering JavaFX dependency: OpenChemlib evolved historically from core cheminformatics functionality developed at Actelion to support internal tools. For some time it looked, whether FX could replace Swing. At that time we also developed some FX applications with common functionality put into our toolbox, the predecessor of OpenChemLib. The most important FX-component today is probably the FX based structure editor.

An example of how to use the FX editor comes at the end.

By the way, I am still using your Graphics3D engine from early JMol for the cartesian 3D view in DataWarrior, because it is much more efficient than any other Java 3D solution I know. For 3Dmolecules, however, we switched from JMol to our own FX-Molviewer.

Best wishes,

Thomas

package com.example.fxtest;

import com.actelion.research.chem.SmilesParser;
import com.actelion.research.chem.StereoMolecule;
import com.actelion.research.gui.editor.FXEditorDialog;
import javafx.application.Application;
import javafx.stage.Stage;

public class FXEditor extends Application {
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Molecule.setDefaultAverageBondLength(HiDPIHelper.scale(12));
StereoMolecule mol = new SmilesParser().parseMolecule("Nc1cc(OCCO)cc(N)c1");
mol.setFragment(true);

	// test FXEditorDialog
	new FXEditorDialog(null, mol).show();

	/* test FXEditorPane
	FXEditorPane editorPane =  new FXEditorPane(mol);

// String css = getClass().getResource("/resources/fxeditor.css").toExternalForm();
Scene scene = new Scene(editorPane, 1024, 768, true, SceneAntialiasing.BALANCED);
// scene.getStylesheets().add(css);
// editorPane.getScene3D().widthProperty().bind(scene.widthProperty());
// editorPane.getScene3D().heightProperty().bind(scene.heightProperty());
primaryStage.setTitle("Molecule Editor");
primaryStage.setScene(scene);
primaryStage.show();
*/
}
}

@BobHanson
Copy link
Author

...To my knowledge the OpenChemLib-JS project is working on this.

Who's the best contact for that?

Could we use SwingJS to directly translate from the Sing editor (com.actelion.research.gui.editor.SwingEditorArea or Dialog)?

Absolutely. Modal dialogs require a bit of reconfiguring to make them both Java and JavaScript compliant -- SwingJS in JavaScript can't do Thread.wait(). But I have done several projects, and they all presented fairly simple solutions to that. Mostly using Java Runnable callbacks to allow for asynchronous action.

Our Inchi support is currently based on the JNAInchi project. A native Java implementation would be very interesting, of course. I hope that the Inchi-2.0 project comes up with C and Java code from the beginning...

JNAInchi, JNI-InChI, not sure of the difference. I like the JNI-InChI Java very much, as it allows access to the canonical model that InChI creates. And it has been translated into JavaScript WASM code, so I get the same results and options in both.

FX is not supported in SwingJS, but all of Swing and Java 8 is supported with almost certainly no issues. I can give it a crack and see if I can set up a demo for you.

Bob

@thsa
Copy link
Contributor

thsa commented Jan 25, 2023

...To my knowledge the OpenChemLib-JS project is working on this.

Who's the best contact for that?

That would be Michael Zasso and Luc Patiny

JNAInchi, JNI-InChI, not sure of the difference. I like the JNI-InChI Java very much, as it allows access to the
canonical model that InChI creates. And it has been translated into JavaScript WASM code, so I get the
same results and options in both.

JNA is a more recent technology to JNI to wrap processor native code for Java. JNA-Inchi uses that wrapping mechanism and supports Inchi version 1.06, while the older JNI-Inchi does Inchi version 1.03. OpenChemLib does not include Inchi-creation because that would add a dependency and till now we have avoided any dependency except for the JRE.

Swing and Java 8 is supported with almost certainly no issues. I can give it a crack and see if I can set up a demo for you.

It would be fantastic, if there would be an easy solution to build the JS editor and to keep it in sync with the Java Swing one. Thanks in advance,

Thomas

@BobHanson
Copy link
Author

Sorry this took so long -- I had a zoom meeting.
image

A couple of issues, but nothing to bad.

@BobHanson
Copy link
Author

Thomas,

I've put up at https://chemapps.stolaf.edu/temp/ two zip files.

ocl_swingjs_site1.zip 2023-01-25 13:26 1.2M
site.zip 2023-01-25 13:26 14M

The 14 MB one, site.zip, is the entire Java library in JavaScript along with the entire OCL library and a full set of what looks to me like example tests from various main() methods. I ran a few -- hose, for example -- and they seemed to write OK to System.out on the test pages, but I don't know to what extent they worked or not. These are created automatically in real time (every time Java updates the bin/ directory in Eclipse) for classes with public static void main(String[]) methods.

The smaller (1.2 MB) file is a compressed selection of about 425 files that were covered when I started the applet and played with it a bit. That's more representative of the footprint of the application. If you run the "core" html file in that one, you might see some file-not-found issues if you need files that I didn't get into the compression.

Making this run in JavaScript took only about 30 minutes. There were a few issues:

  1. I forgot to include the image resources the first time I tried to run it. (duh!)
  2. SwingJS does not implement glyph fonts. I adjusted the code in SwingDrawContext and Depictor2D to just use regular FontMetrics to get a string width, and that took care of that. But there are probably other places in the code where this needs changing. I didn't notice any difference, I have a 4K laptop, so the pixels are too small for me to see if that glyph business made any difference. I doubt it.
  3. I had (for some unknown reason) commented out java.awt.Toolkit.getBestCursorSize(), and I had to uncomment that and update Toolkit.js.

After that, I think it all worked for me. It's a nice little GUI.

Pretty sure you are going to want to see this in action yourself. If that's the case, I can get you set up. Maybe what I should do is fork is repo so that you can have a working project. There are a few files I had to add to the Eclipse project to make it all work.

Bob

@BobHanson
Copy link
Author

I put up a minimal configuration (not at all full coverage, I suspect) at

https://chemapps.stolaf.edu/temp/site1/ocl_core.html

This site1/ is what you get when you unzip that 1.2 MB zip file.

@BobHanson
Copy link
Author

ps -- for example, I immediately hit a FileNotFound exception playing with bonding. So just use that link as a basic idea of how it would work, and if you really want to exercise it, unzip the 14 MB zip file. It has everything. It's just that -- obviously-- one doesn't want to force people to download all that just to run a little applet.

Bob

@thsa
Copy link
Contributor

thsa commented Feb 1, 2023 via email

@BobHanson
Copy link
Author

Thanks; I did figure that out. Did you try out the SwingJS version? I pointed to the directory, but maybe you did not see that the app was there as well.

https://chemapps.stolaf.edu/temp/site1
https://chemapps.stolaf.edu/temp/site1/ocl_core.html

I was trying to impress you that it only took me an hour or two to create that, with just a few minor tweaks in OpenChemLib that have no effect on Java. :)

@BobHanson
Copy link
Author

BobHanson commented Feb 1, 2023

Ah, I just realized that I only put up the quick "core" compressed version that was missing three files. The whole system is up there now. I see that there are plenty of modal dialogs -- these would have to be tweaked to allow both Java and JavaScript, since JavaScript doesn't allow modal dialogs. It's not a difficult task, but I'll pass on it for now. Anyway, interested in your thoughts on that.

Bob

ps - OpenChemLib is working fabulously in JME-SwingJS Java and JavaScript. It was great to not have to work out small rings and aromaticity myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants