partridgejiang / Kekule.js

A Javascript cheminformatics toolkit.
http://partridgejiang.github.io/Kekule.js
MIT License
250 stars 61 forks source link

Stereoisomer matching with multiple stereo bonds #81

Closed MrDarkHorse closed 5 years ago

MrDarkHorse commented 6 years ago

Found some weird scenarios, best shown with pictures. In my case, I'm comparing structures with 2 stereo bonds, and I'm really testing for isometry here.

First case, same structure, it correctly matches them: screen shot 2018-09-26 at 1 51 14 pm

Second case, I drop the Oxygen to the other side, and again, it correctly knows that these structures no longer match: screen shot 2018-09-26 at 1 51 28 pm

Then it starts getting weird.

Third case, I drop the Nitrogen below the left bond, but it still thinks they match screen shot 2018-09-26 at 1 53 54 pm

There are other variants as well.

Fourth case, I pull the Chlorine above the bond on the right, and it still thinks they match: screen shot 2018-09-26 at 1 52 50 pm

Another version of the previous: screen shot 2018-09-26 at 1 52 41 pm

Seems like something in the parity calculation breaks down. Right?

MrDarkHorse commented 6 years ago

Any thoughts on this?

partridgejiang commented 6 years ago

@MrDarkHorse Thanks a lot for the feedback. In current bond stereo detection code, it is assumed that the end atoms/groups should be on different side of the bond line. The error may occur when two end groups are moved to the same side of bond as shown in your figures. This issue should be fixed soon.

MrDarkHorse commented 6 years ago

Thanks! I know these are kind of ambiguous scenarios. In our situation, since we use this for teaching purposes, we would prefer that ambiguous defaults to non-matching. Thank you so much for your help!

partridgejiang commented 6 years ago

@MrDarkHorse The issue has been fixed, please check the latest release files, :).

MrDarkHorse commented 5 years ago

Thank you for fixing this!

One more ambiguous case I'm trying to deal with is if we are trying to match an ambiguous linear bond. Again, this is weird, but right now this is matching, and I was hoping that in this case it would default to not matching. I had a solution for this using the old method, but I haven't been able to sort it out using the new method.

These 2 structures should not match because the first is ambiguously drawn.

screen shot 2018-10-23 at 1 04 15 pm screen shot 2018-10-23 at 1 04 02 pm

MrDarkHorse commented 5 years ago

Wait! I actually have a solution for this. I could make a pull request for it if it's something others are interested in having.

MrDarkHorse commented 5 years ago

adding the following lines in _calcParityOfCoordPairs:

`var axisAngle = Math.atan2(axisVector.y, axisVector.x);

    var rightAxisVector = CU.substract(c1, a1);  // vector of bond
    var leftAxisVector = CU.substract(b1, c2);  // vector of bond
    var rightAxisAngle = Math.atan2(rightAxisVector.y, rightAxisVector.x);
    var leftAxisAngle = Math.atan2(leftAxisVector.y, leftAxisVector.x);

    var threshold = 1e-2;
    var leftDiff = Math.abs(axisAngle - leftAxisAngle);
    var rightDiff = Math.abs(axisAngle - rightAxisAngle);

    // if there is an linearly drawn bond angle, the stereo parity is UNKNOWN
    if (leftDiff < threshold || rightDiff < threshold) {
        return SP.UNKNOWN;
    }`
partridgejiang commented 5 years ago

@MrDarkHorse The liner bond issue is now fixed. Please check the latest release files.