Actelion / openchemlib

Open source Java-based chemistry library
Other
76 stars 27 forks source link

OpenChemLib in Jmol SwingJS - and InChI #83

Open BobHanson opened 1 year ago

BobHanson commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

...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 commented 1 year ago

...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 commented 1 year ago

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

A couple of issues, but nothing to bad.

BobHanson commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Implicit H-atoms are always shown on hetero atoms unless the molecule is not a molecule, but a query sub-structure. Within those, of course, implicit hydrogens don't exist and, therefore, cannot be displayed. If the StereoMolecule returns true on isFragment(), the molecule is actually a query structure.


Von: Bob Hanson @.> Gesendet: Freitag, 27. Januar 2023 00:31 An: Actelion/openchemlib @.> Cc: Thomas Sander @.>; Comment @.> Betreff: Re: [Actelion/openchemlib] OpenChemLib in Jmol SwingJS - and InChI (Issue #83)

Q: How do I get H atoms to display on heteroatoms? Is there a simple flag for this?

— Reply to this email directly, view it on GitHubhttps://che01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FActelion%2Fopenchemlib%2Fissues%2F83%23issuecomment-1405812275&data=05%7C01%7Cthomas.sander%40idorsia.com%7Ce12198fa33c2466ac06d08dafff583b3%7Cbb9214bf0cb941fdbd55d0c1c3eda110%7C0%7C0%7C638103727180265492%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Yw19zrLcw5%2Fgauu9xHZ%2Bpjq9X%2BNhTRJHLe%2BfkmGHmps%3D&reserved=0, or unsubscribehttps://che01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACNFEBVPNOUJ7I6LDOO2HVDWUMCOXANCNFSM6AAAAAAT4XWCOI&data=05%7C01%7Cthomas.sander%40idorsia.com%7Ce12198fa33c2466ac06d08dafff583b3%7Cbb9214bf0cb941fdbd55d0c1c3eda110%7C0%7C0%7C638103727180421715%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=LZKsybSJZ%2Fifmj8aXZ3%2FxOGOUEa3QCWQLNk4uzRCcGc%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>


The information of this email and in any file transmitted with it is strictly confidential and may be legally privileged. It is intended solely for the addressee. If you are not the intended recipient, any copying, distribution or any other use of this email is prohibited and may be unlawful. In such case, you should please notify the sender immediately and destroy this email. The content of this email is not legally binding unless confirmed by letter. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of the sender's company.

BobHanson commented 1 year ago

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 commented 1 year ago

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.