UNPKG

smiles-drawer

Version:

A SMILES drawer and parser. Generate molecular structure depictions in pure JavaScript.

620 lines (515 loc) 42.6 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>SSSR.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ArrayHelper.html">ArrayHelper</a><ul class='methods'><li data-type='method'><a href="ArrayHelper.html#.clone">clone</a></li><li data-type='method'><a href="ArrayHelper.html#.contains">contains</a></li><li data-type='method'><a href="ArrayHelper.html#.containsAll">containsAll</a></li><li data-type='method'><a href="ArrayHelper.html#.count">count</a></li><li data-type='method'><a href="ArrayHelper.html#.deepCopy">deepCopy</a></li><li data-type='method'><a href="ArrayHelper.html#.each">each</a></li><li data-type='method'><a href="ArrayHelper.html#.equals">equals</a></li><li data-type='method'><a href="ArrayHelper.html#.get">get</a></li><li data-type='method'><a href="ArrayHelper.html#.intersection">intersection</a></li><li data-type='method'><a href="ArrayHelper.html#.merge">merge</a></li><li data-type='method'><a href="ArrayHelper.html#.print">print</a></li><li data-type='method'><a href="ArrayHelper.html#.remove">remove</a></li><li data-type='method'><a href="ArrayHelper.html#.removeAll">removeAll</a></li><li data-type='method'><a href="ArrayHelper.html#.removeUnique">removeUnique</a></li><li data-type='method'><a href="ArrayHelper.html#.sortByAtomicNumberDesc">sortByAtomicNumberDesc</a></li><li data-type='method'><a href="ArrayHelper.html#.toggle">toggle</a></li><li data-type='method'><a href="ArrayHelper.html#.unique">unique</a></li></ul></li><li><a href="Atom.html">Atom</a><ul class='methods'><li data-type='method'><a href="Atom.html#addAnchoredRing">addAnchoredRing</a></li><li data-type='method'><a href="Atom.html#addNeighbouringElement">addNeighbouringElement</a></li><li data-type='method'><a href="Atom.html#attachPseudoElement">attachPseudoElement</a></li><li data-type='method'><a href="Atom.html#backupRings">backupRings</a></li><li data-type='method'><a href="Atom.html#getAtomicNumber">getAtomicNumber</a></li><li data-type='method'><a href="Atom.html#getAttachedPseudoElements">getAttachedPseudoElements</a></li><li data-type='method'><a href="Atom.html#getAttachedPseudoElementsCount">getAttachedPseudoElementsCount</a></li><li data-type='method'><a href="Atom.html#getMaxBonds">getMaxBonds</a></li><li data-type='method'><a href="Atom.html#getRingbondCount">getRingbondCount</a></li><li data-type='method'><a href="Atom.html#haveCommonRingbond">haveCommonRingbond</a></li><li data-type='method'><a href="Atom.html#isHeteroAtom">isHeteroAtom</a></li><li data-type='method'><a href="Atom.html#neighbouringElementsEqual">neighbouringElementsEqual</a></li><li data-type='method'><a href="Atom.html#restoreRings">restoreRings</a></li></ul></li><li><a href="CanvasWrapper.html">CanvasWrapper</a><ul class='methods'><li data-type='method'><a href="CanvasWrapper.html#clear">clear</a></li><li data-type='method'><a href="CanvasWrapper.html#drawAromaticityRing">drawAromaticityRing</a></li><li data-type='method'><a href="CanvasWrapper.html#drawBall">drawBall</a></li><li data-type='method'><a href="CanvasWrapper.html#drawCircle">drawCircle</a></li><li data-type='method'><a href="CanvasWrapper.html#drawDashedWedge">drawDashedWedge</a></li><li data-type='method'><a href="CanvasWrapper.html#drawDebugPoint">drawDebugPoint</a></li><li data-type='method'><a href="CanvasWrapper.html#drawDebugText">drawDebugText</a></li><li data-type='method'><a href="CanvasWrapper.html#drawLine">drawLine</a></li><li data-type='method'><a href="CanvasWrapper.html#drawPoint">drawPoint</a></li><li data-type='method'><a href="CanvasWrapper.html#drawText">drawText</a></li><li data-type='method'><a href="CanvasWrapper.html#drawWedge">drawWedge</a></li><li data-type='method'><a href="CanvasWrapper.html#getChargeText">getChargeText</a></li><li data-type='method'><a href="CanvasWrapper.html#getColor">getColor</a></li><li data-type='method'><a href="CanvasWrapper.html#reset">reset</a></li><li data-type='method'><a href="CanvasWrapper.html#scale">scale</a></li><li data-type='method'><a href="CanvasWrapper.html#setTheme">setTheme</a></li><li data-type='method'><a href="CanvasWrapper.html#updateSize">updateSize</a></li></ul></li><li><a href="Drawer.html">Drawer</a><ul class='methods'><li data-type='method'><a href="Drawer.html#addRing">addRing</a></li><li data-type='method'><a href="Drawer.html#addRingConnection">addRingConnection</a></li><li data-type='method'><a href="Drawer.html#annotateStereochemistry">annotateStereochemistry</a></li><li data-type='method'><a href="Drawer.html#areVerticesInSameRing">areVerticesInSameRing</a></li><li data-type='method'><a href="Drawer.html#backupRingInformation">backupRingInformation</a></li><li data-type='method'><a href="Drawer.html#chooseSide">chooseSide</a></li><li data-type='method'><a href="Drawer.html#createBridgedRing">createBridgedRing</a></li><li data-type='method'><a href="Drawer.html#createNextBond">createNextBond</a></li><li data-type='method'><a href="Drawer.html#createRing">createRing</a></li><li data-type='method'><a href="Drawer.html#draw">draw</a></li><li data-type='method'><a href="Drawer.html#drawEdge">drawEdge</a></li><li data-type='method'><a href="Drawer.html#drawEdges">drawEdges</a></li><li data-type='method'><a href="Drawer.html#drawVertices">drawVertices</a></li><li data-type='method'><a href="Drawer.html#edgeRingCount">edgeRingCount</a></li><li data-type='method'><a href="Drawer.html#extend">extend</a></li><li data-type='method'><a href="Drawer.html#getBridgedRingRings">getBridgedRingRings</a></li><li data-type='method'><a href="Drawer.html#getBridgedRings">getBridgedRings</a></li><li data-type='method'><a href="Drawer.html#getClosestVertex">getClosestVertex</a></li><li data-type='method'><a href="Drawer.html#getCommonRingbondNeighbour">getCommonRingbondNeighbour</a></li><li data-type='method'><a href="Drawer.html#getCommonRings">getCommonRings</a></li><li data-type='method'><a href="Drawer.html#getCurrentCenterOfMass">getCurrentCenterOfMass</a></li><li data-type='method'><a href="Drawer.html#getCurrentCenterOfMassInNeigbourhood">getCurrentCenterOfMassInNeigbourhood</a></li><li data-type='method'><a href="Drawer.html#getEdgeNormals">getEdgeNormals</a></li><li data-type='method'><a href="Drawer.html#getFusedRings">getFusedRings</a></li><li data-type='method'><a href="Drawer.html#getHeavyAtomCount">getHeavyAtomCount</a></li><li data-type='method'><a href="Drawer.html#getLargestOrAromaticCommonRing">getLargestOrAromaticCommonRing</a></li><li data-type='method'><a href="Drawer.html#getLastVertexWithAngle">getLastVertexWithAngle</a></li><li data-type='method'><a href="Drawer.html#getMolecularFormula">getMolecularFormula</a></li><li data-type='method'><a href="Drawer.html#getNonRingNeighbours">getNonRingNeighbours</a></li><li data-type='method'><a href="Drawer.html#getOverlapScore">getOverlapScore</a></li><li data-type='method'><a href="Drawer.html#getRing">getRing</a></li><li data-type='method'><a href="Drawer.html#getRingbondType">getRingbondType</a></li><li data-type='method'><a href="Drawer.html#getRingConnection">getRingConnection</a></li><li data-type='method'><a href="Drawer.html#getRingConnections">getRingConnections</a></li><li data-type='method'><a href="Drawer.html#getRingCount">getRingCount</a></li><li data-type='method'><a href="Drawer.html#getSpiros">getSpiros</a></li><li data-type='method'><a href="Drawer.html#getSubringCenter">getSubringCenter</a></li><li data-type='method'><a href="Drawer.html#getSubtreeOverlapScore">getSubtreeOverlapScore</a></li><li data-type='method'><a href="Drawer.html#getTotalOverlapScore">getTotalOverlapScore</a></li><li data-type='method'><a href="Drawer.html#getVerticesAt">getVerticesAt</a></li><li data-type='method'><a href="Drawer.html#hasBridgedRing">hasBridgedRing</a></li><li data-type='method'><a href="Drawer.html#initPseudoElements">initPseudoElements</a></li><li data-type='method'><a href="Drawer.html#initRings">initRings</a></li><li data-type='method'><a href="Drawer.html#isEdgeInRing">isEdgeInRing</a></li><li data-type='method'><a href="Drawer.html#isEdgeRotatable">isEdgeRotatable</a></li><li data-type='method'><a href="Drawer.html#isPartOfBridgedRing">isPartOfBridgedRing</a></li><li data-type='method'><a href="Drawer.html#isPointInRing">isPointInRing</a></li><li data-type='method'><a href="Drawer.html#isRingAromatic">isRingAromatic</a></li><li data-type='method'><a href="Drawer.html#position">position</a></li><li data-type='method'><a href="Drawer.html#printRingInfo">printRingInfo</a></li><li data-type='method'><a href="Drawer.html#removeRing">removeRing</a></li><li data-type='method'><a href="Drawer.html#removeRingConnection">removeRingConnection</a></li><li data-type='method'><a href="Drawer.html#removeRingConnectionsBetween">removeRingConnectionsBetween</a></li><li data-type='method'><a href="Drawer.html#resolvePrimaryOverlaps">resolvePrimaryOverlaps</a></li><li data-type='method'><a href="Drawer.html#resolveSecondaryOverlaps">resolveSecondaryOverlaps</a></li><li data-type='method'><a href="Drawer.html#restoreRingInformation">restoreRingInformation</a></li><li data-type='method'><a href="Drawer.html#rotateDrawing">rotateDrawing</a></li><li data-type='method'><a href="Drawer.html#rotateSubtree">rotateSubtree</a></li><li data-type='method'><a href="Drawer.html#setRingCenter">setRingCenter</a></li><li data-type='method'><a href="Drawer.html#visitStereochemistry">visitStereochemistry</a></li></ul></li><li><a href="Edge.html">Edge</a><ul class='methods'><li data-type='method'><a href="Edge.html#setBondType">setBondType</a></li></ul></li><li><a href="Graph.html">Graph</a><ul class='methods'><li data-type='method'><a href="Graph.html#._ccCountDfs">_ccCountDfs</a></li><li data-type='method'><a href="Graph.html#._ccGetDfs">_ccGetDfs</a></li><li data-type='method'><a href="Graph.html#.getConnectedComponentCount">getConnectedComponentCount</a></li><li data-type='method'><a href="Graph.html#.getConnectedComponents">getConnectedComponents</a></li><li data-type='method'><a href="Graph.html#_bridgeDfs">_bridgeDfs</a></li><li data-type='method'><a href="Graph.html#_init">_init</a></li><li data-type='method'><a href="Graph.html#addEdge">addEdge</a></li><li data-type='method'><a href="Graph.html#addVertex">addVertex</a></li><li data-type='method'><a href="Graph.html#clear">clear</a></li><li data-type='method'><a href="Graph.html#getAdjacencyList">getAdjacencyList</a></li><li data-type='method'><a href="Graph.html#getAdjacencyMatrix">getAdjacencyMatrix</a></li><li data-type='method'><a href="Graph.html#getBridges">getBridges</a></li><li data-type='method'><a href="Graph.html#getComponentsAdjacencyMatrix">getComponentsAdjacencyMatrix</a></li><li data-type='method'><a href="Graph.html#getDistanceMatrix">getDistanceMatrix</a></li><li data-type='method'><a href="Graph.html#getEdge">getEdge</a></li><li data-type='method'><a href="Graph.html#getEdgeList">getEdgeList</a></li><li data-type='method'><a href="Graph.html#getEdges">getEdges</a></li><li data-type='method'><a href="Graph.html#getSubgraphAdjacencyList">getSubgraphAdjacencyList</a></li><li data-type='method'><a href="Graph.html#getSubgraphAdjacencyMatrix">getSubgraphAdjacencyMatrix</a></li><li data-type='method'><a href="Graph.html#getSubgraphDistanceMatrix">getSubgraphDistanceMatrix</a></li><li data-type='method'><a href="Graph.html#getTreeDepth">getTreeDepth</a></li><li data-type='method'><a href="Graph.html#getVertexList">getVertexList</a></li><li data-type='method'><a href="Graph.html#hasEdge">hasEdge</a></li><li data-type='method'><a href="Graph.html#kkLayout">kkLayout</a></li><li data-type='method'><a href="Graph.html#traverseBF">traverseBF</a></li><li data-type='method'><a href="Graph.html#traverseTree">traverseTree</a></li></ul></li><li><a href="Line.html">Line</a><ul class='methods'><li data-type='method'><a href="Line.html#clone">clone</a></li><li data-type='method'><a href="Line.html#getAngle">getAngle</a></li><li data-type='method'><a href="Line.html#getLeftChiral">getLeftChiral</a></li><li data-type='method'><a href="Line.html#getLeftElement">getLeftElement</a></li><li data-type='method'><a href="Line.html#getLeftVector">getLeftVector</a></li><li data-type='method'><a href="Line.html#getLength">getLength</a></li><li data-type='method'><a href="Line.html#getRightChiral">getRightChiral</a></li><li data-type='method'><a href="Line.html#getRightElement">getRightElement</a></li><li data-type='method'><a href="Line.html#getRightVector">getRightVector</a></li><li data-type='method'><a href="Line.html#rotate">rotate</a></li><li data-type='method'><a href="Line.html#rotateToXAxis">rotateToXAxis</a></li><li data-type='method'><a href="Line.html#setLeftVector">setLeftVector</a></li><li data-type='method'><a href="Line.html#setRightVector">setRightVector</a></li><li data-type='method'><a href="Line.html#shorten">shorten</a></li><li data-type='method'><a href="Line.html#shortenFrom">shortenFrom</a></li><li data-type='method'><a href="Line.html#shortenLeft">shortenLeft</a></li><li data-type='method'><a href="Line.html#shortenRight">shortenRight</a></li><li data-type='method'><a href="Line.html#shortenTo">shortenTo</a></li></ul></li><li><a href="MathHelper.html">MathHelper</a><ul class='methods'><li data-type='method'><a href="MathHelper.html#.apothem">apothem</a></li><li data-type='method'><a href="MathHelper.html#.centralAngle">centralAngle</a></li><li data-type='method'><a href="MathHelper.html#.innerAngle">innerAngle</a></li><li data-type='method'><a href="MathHelper.html#.meanAngle">meanAngle</a></li><li data-type='method'><a href="MathHelper.html#.parityOfPermutation">parityOfPermutation</a></li><li data-type='method'><a href="MathHelper.html#.polyCircumradius">polyCircumradius</a></li><li data-type='method'><a href="MathHelper.html#.round">round</a></li><li data-type='method'><a href="MathHelper.html#.toDeg">toDeg</a></li><li data-type='method'><a href="MathHelper.html#.toRad">toRad</a></li></ul></li><li><a href="Ring.html">Ring</a><ul class='methods'><li data-type='method'><a href="Ring.html#clone">clone</a></li><li data-type='method'><a href="Ring.html#contains">contains</a></li><li data-type='method'><a href="Ring.html#eachMember">eachMember</a></li><li data-type='method'><a href="Ring.html#getAngle">getAngle</a></li><li data-type='method'><a href="Ring.html#getDoubleBondCount">getDoubleBondCount</a></li><li data-type='method'><a href="Ring.html#getOrderedNeighbours">getOrderedNeighbours</a></li><li data-type='method'><a href="Ring.html#getPolygon">getPolygon</a></li><li data-type='method'><a href="Ring.html#getSize">getSize</a></li><li data-type='method'><a href="Ring.html#isBenzeneLike">isBenzeneLike</a></li></ul></li><li><a href="RingConnection.html">RingConnection</a><ul class='methods'><li data-type='method'><a href="RingConnection.html#.getNeighbours">getNeighbours</a></li><li data-type='method'><a href="RingConnection.html#.getVertices">getVertices</a></li><li data-type='method'><a href="RingConnection.html#.isBridge">isBridge</a></li><li data-type='method'><a href="RingConnection.html#addVertex">addVertex</a></li><li data-type='method'><a href="RingConnection.html#containsRing">containsRing</a></li><li data-type='method'><a href="RingConnection.html#isBridge">isBridge</a></li><li data-type='method'><a href="RingConnection.html#updateOther">updateOther</a></li></ul></li><li><a href="SSSR.html">SSSR</a><ul class='methods'><li data-type='method'><a href="SSSR.html#.areSetsEqual">areSetsEqual</a></li><li data-type='method'><a href="SSSR.html#.bondsToAtoms">bondsToAtoms</a></li><li data-type='method'><a href="SSSR.html#.getBondCount">getBondCount</a></li><li data-type='method'><a href="SSSR.html#.getEdgeCount">getEdgeCount</a></li><li data-type='method'><a href="SSSR.html#.getEdgeList">getEdgeList</a></li><li data-type='method'><a href="SSSR.html#.getPathIncludedDistanceMatrices">getPathIncludedDistanceMatrices</a></li><li data-type='method'><a href="SSSR.html#.getRingCandidates">getRingCandidates</a></li><li data-type='method'><a href="SSSR.html#.getRings">getRings</a></li><li data-type='method'><a href="SSSR.html#.getSSSR">getSSSR</a></li><li data-type='method'><a href="SSSR.html#.isSupersetOf">isSupersetOf</a></li><li data-type='method'><a href="SSSR.html#.matrixToString">matrixToString</a></li><li data-type='method'><a href="SSSR.html#.pathSetsContain">pathSetsContain</a></li></ul></li><li><a href="Vector2.html">Vector2</a><ul class='methods'><li data-type='method'><a href="Vector2.html#add">add</a></li><li data-type='method'><a href="Vector2.html#angle">angle</a></li><li data-type='method'><a href="Vector2.html#clockwise">clockwise</a></li><li data-type='method'><a href="Vector2.html#clone">clone</a></li><li data-type='method'><a href="Vector2.html#distance">distance</a></li><li data-type='method'><a href="Vector2.html#distanceSq">distanceSq</a></li><li data-type='method'><a href="Vector2.html#divide">divide</a></li><li data-type='method'><a href="Vector2.html#getRotateAwayFromAngle">getRotateAwayFromAngle</a></li><li data-type='method'><a href="Vector2.html#getRotateToAngle">getRotateToAngle</a></li><li data-type='method'><a href="Vector2.html#getRotateTowardsAngle">getRotateTowardsAngle</a></li><li data-type='method'><a href="Vector2.html#invert">invert</a></li><li data-type='method'><a href="Vector2.html#isInPolygon">isInPolygon</a></li><li data-type='method'><a href="Vector2.html#length">length</a></li><li data-type='method'><a href="Vector2.html#lengthSq">lengthSq</a></li><li data-type='method'><a href="Vector2.html#multiply">multiply</a></li><li data-type='method'><a href="Vector2.html#multiplyScalar">multiplyScalar</a></li><li data-type='method'><a href="Vector2.html#normalize">normalize</a></li><li data-type='method'><a href="Vector2.html#normalized">normalized</a></li><li data-type='method'><a href="Vector2.html#relativeClockwise">relativeClockwise</a></li><li data-type='method'><a href="Vector2.html#rotate">rotate</a></li><li data-type='method'><a href="Vector2.html#rotateAround">rotateAround</a></li><li data-type='method'><a href="Vector2.html#rotateAwayFrom">rotateAwayFrom</a></li><li data-type='method'><a href="Vector2.html#rotateTo">rotateTo</a></li><li data-type='method'><a href="Vector2.html#sameSideAs">sameSideAs</a></li><li data-type='method'><a href="Vector2.html#subtract">subtract</a></li><li data-type='method'><a href="Vector2.html#toString">toString</a></li><li data-type='method'><a href="Vector2.html#whichSide">whichSide</a></li><li data-type='method'><a href="Vector2.html#.add">add</a></li><li data-type='method'><a href="Vector2.html#.angle">angle</a></li><li data-type='method'><a href="Vector2.html#.averageDirection">averageDirection</a></li><li data-type='method'><a href="Vector2.html#.divide">divide</a></li><li data-type='method'><a href="Vector2.html#.divideScalar">divideScalar</a></li><li data-type='method'><a href="Vector2.html#.dot">dot</a></li><li data-type='method'><a href="Vector2.html#.midpoint">midpoint</a></li><li data-type='method'><a href="Vector2.html#.multiply">multiply</a></li><li data-type='method'><a href="Vector2.html#.multiplyScalar">multiplyScalar</a></li><li data-type='method'><a href="Vector2.html#.normals">normals</a></li><li data-type='method'><a href="Vector2.html#.scalarProjection">scalarProjection</a></li><li data-type='method'><a href="Vector2.html#.subtract">subtract</a></li><li data-type='method'><a href="Vector2.html#.threePointangle">threePointangle</a></li><li data-type='method'><a href="Vector2.html#.units">units</a></li></ul></li><li><a href="Vertex.html">Vertex</a><ul class='methods'><li data-type='method'><a href="Vertex.html#addChild">addChild</a></li><li data-type='method'><a href="Vertex.html#addRingbondChild">addRingbondChild</a></li><li data-type='method'><a href="Vertex.html#clone">clone</a></li><li data-type='method'><a href="Vertex.html#equals">equals</a></li><li data-type='method'><a href="Vertex.html#getAngle">getAngle</a></li><li data-type='method'><a href="Vertex.html#getDrawnNeighbours">getDrawnNeighbours</a></li><li data-type='method'><a href="Vertex.html#getNeighbourCount">getNeighbourCount</a></li><li data-type='method'><a href="Vertex.html#getNeighbours">getNeighbours</a></li><li data-type='method'><a href="Vertex.html#getNextInRing">getNextInRing</a></li><li data-type='method'><a href="Vertex.html#getSpanningTreeNeighbours">getSpanningTreeNeighbours</a></li><li data-type='method'><a href="Vertex.html#getTextDirection">getTextDirection</a></li><li data-type='method'><a href="Vertex.html#isTerminal">isTerminal</a></li><li data-type='method'><a href="Vertex.html#setParentVertexId">setParentVertexId</a></li><li data-type='method'><a href="Vertex.html#setPosition">setPosition</a></li><li data-type='method'><a href="Vertex.html#setPositionFromVector">setPositionFromVector</a></li></ul></li></ul> </nav> <div id="main"> <h1 class="page-title">SSSR.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>//@ts-check const Graph = require('./Graph') /** A class encapsulating the functionality to find the smallest set of smallest rings in a graph. */ class SSSR { /** * Returns an array containing arrays, each representing a ring from the smallest set of smallest rings in the graph. * * @param {Graph} graph A Graph object. * @returns {Array[]} An array containing arrays, each representing a ring from the smallest set of smallest rings in the group. */ static getRings(graph) { let adjacencyMatrix = graph.getComponentsAdjacencyMatrix(); if (adjacencyMatrix.length === 0) { return null; } let connectedComponents = Graph.getConnectedComponents(adjacencyMatrix); let rings = Array(); for (var i = 0; i &lt; connectedComponents.length; i++) { let connectedComponent = connectedComponents[i]; let ccAdjacencyMatrix = graph.getSubgraphAdjacencyMatrix([...connectedComponent]); let arrBondCount = new Uint16Array(ccAdjacencyMatrix.length); let arrRingCount = new Uint16Array(ccAdjacencyMatrix.length); for (var j = 0; j &lt; ccAdjacencyMatrix.length; j++) { arrRingCount[j] = 0; arrBondCount[j] = 0; for (var k = 0; k &lt; ccAdjacencyMatrix[j].length; k++) { arrBondCount[j] += ccAdjacencyMatrix[j][k]; } } // Get the edge number and the theoretical number of rings in SSSR let nEdges = 0; for (var j = 0; j &lt; ccAdjacencyMatrix.length; j++) { for (var k = j + 1; k &lt; ccAdjacencyMatrix.length; k++) { nEdges += ccAdjacencyMatrix[j][k]; } } let nSssr = nEdges - ccAdjacencyMatrix.length + 1; // console.log(nEdges, ccAdjacencyMatrix.length, nSssr); // console.log(SSSR.getEdgeList(ccAdjacencyMatrix)); // console.log(ccAdjacencyMatrix); // If all vertices have 3 incident edges, calculate with different formula (see Euler) let allThree = true; for (var j = 0; j &lt; arrBondCount.length; j++) { if (arrBondCount[j] !== 3) { allThree = false; } } if (allThree) { nSssr = 2.0 + nEdges - ccAdjacencyMatrix.length; } // All vertices are part of one ring if theres only one ring. if (nSssr === 1) { rings.push([...connectedComponent]); continue; } let { d, pe, pe_prime } = SSSR.getPathIncludedDistanceMatrices(ccAdjacencyMatrix); let c = SSSR.getRingCandidates(d, pe, pe_prime); let sssr = SSSR.getSSSR(c, d, ccAdjacencyMatrix, pe, pe_prime, arrBondCount, arrRingCount, nSssr); for (var j = 0; j &lt; sssr.length; j++) { let ring = Array(sssr[j].size); let index = 0; for (let val of sssr[j]) { // Get the original id of the vertex back ring[index++] = connectedComponent[val]; } rings.push(ring); } } return rings; } /** * Creates a printable string from a matrix (2D array). * * @param {Array[]} matrix A 2D array. * @returns {String} A string representing the matrix. */ static matrixToString(matrix) { let str = ''; for (var i = 0; i &lt; matrix.length; i++) { for (var j = 0; j &lt; matrix[i].length; j++) { str += matrix[i][j] + ' '; } str += '\n'; } return str; } /** * Returnes the two path-included distance matrices used to find the sssr. * * @param {Array[]} adjacencyMatrix An adjacency matrix. * @returns {Object} The path-included distance matrices. { p1, p2 } */ static getPathIncludedDistanceMatrices(adjacencyMatrix) { let length = adjacencyMatrix.length; let d = Array(length); let pe = Array(length); let pe_prime = Array(length); var l = 0; var m = 0; var n = 0; var i = length; while (i--) { d[i] = Array(length); pe[i] = Array(length); pe_prime[i] = Array(length); var j = length; while (j--) { d[i][j] = (i === j || adjacencyMatrix[i][j] === 1) ? adjacencyMatrix[i][j] : Number.POSITIVE_INFINITY; if (d[i][j] === 1) { pe[i][j] = [[[i, j]]]; } else { pe[i][j] = Array(); } pe_prime[i][j] = Array(); } } var k = length; var j; while (k--) { i = length; while (i--) { j = length; while (j--) { const previousPathLength = d[i][j]; const newPathLength = d[i][k] + d[k][j]; if (previousPathLength > newPathLength) { var l, m, n; if (previousPathLength === newPathLength + 1) { pe_prime[i][j] = [pe[i][j].length]; l = pe[i][j].length while (l--) { pe_prime[i][j][l] = [pe[i][j][l].length]; m = pe[i][j][l].length while (m--) { pe_prime[i][j][l][m] = [pe[i][j][l][m].length]; n = pe[i][j][l][m].length; while (n--) { pe_prime[i][j][l][m][n] = [pe[i][j][l][m][0], pe[i][j][l][m][1]]; } } } } else { pe_prime[i][j] = Array(); } d[i][j] = newPathLength; pe[i][j] = [[]]; l = pe[i][k][0].length; while (l--) { pe[i][j][0].push(pe[i][k][0][l]); } l = pe[k][j][0].length; while (l--) { pe[i][j][0].push(pe[k][j][0][l]); } } else if (previousPathLength === newPathLength) { if (pe[i][k].length &amp;&amp; pe[k][j].length) { var l; if (pe[i][j].length) { let tmp = Array(); l = pe[i][k][0].length; while (l--) { tmp.push(pe[i][k][0][l]); } l = pe[k][j][0].length; while (l--) { tmp.push(pe[k][j][0][l]); } pe[i][j].push(tmp); } else { let tmp = Array(); l = pe[i][k][0].length; while (l--) { tmp.push(pe[i][k][0][l]); } l = pe[k][j][0].length; while (l--) { tmp.push(pe[k][j][0][l]); } pe[i][j][0] = tmp } } } else if (previousPathLength === newPathLength - 1) { var l; if (pe_prime[i][j].length) { let tmp = Array(); l = pe[i][k][0].length; while (l--) { tmp.push(pe[i][k][0][l]); } l = pe[k][j][0].length; while (l--) { tmp.push(pe[k][j][0][l]); } pe_prime[i][j].push(tmp); } else { let tmp = Array(); l = pe[i][k][0].length; while (l--) { tmp.push(pe[i][k][0][l]); } l = pe[k][j][0].length; while (l--) { tmp.push(pe[k][j][0][l]); } pe_prime[i][j][0] = tmp; } } } } } return { d: d, pe: pe, pe_prime: pe_prime }; } /** * Get the ring candidates from the path-included distance matrices. * * @param {Array[]} d The distance matrix. * @param {Array[]} pe A matrix containing the shortest paths. * @param {Array[]} pe_prime A matrix containing the shortest paths + one vertex. * @returns {Array[]} The ring candidates. */ static getRingCandidates(d, pe, pe_prime) { let length = d.length; let candidates = Array(); let c = 0; for (let i = 0; i &lt; length; i++) { for (let j = 0; j &lt; length; j++) { if (d[i][j] === 0 || (pe[i][j].length === 1 &amp;&amp; pe_prime[i][j] === 0)) { continue; } else { // c is the number of vertices in the cycle. if (pe_prime[i][j].length !== 0) { c = 2 * (d[i][j] + 0.5); } else { c = 2 * d[i][j]; } if (c !== Infinity) { candidates.push([c, pe[i][j], pe_prime[i][j]]); } } } } // Candidates have to be sorted by c candidates.sort(function (a, b) { return a[0] - b[0]; }); return candidates; } /** * Searches the candidates for the smallest set of smallest rings. * * @param {Array[]} c The candidates. * @param {Array[]} d The distance matrix. * @param {Array[]} adjacencyMatrix An adjacency matrix. * @param {Array[]} pe A matrix containing the shortest paths. * @param {Array[]} pe_prime A matrix containing the shortest paths + one vertex. * @param {Uint16Array} arrBondCount A matrix containing the bond count of each vertex. * @param {Uint16Array} arrRingCount A matrix containing the number of rings associated with each vertex. * @param {Number} nsssr The theoretical number of rings in the graph. * @returns {Set[]} The smallest set of smallest rings. */ static getSSSR(c, d, adjacencyMatrix, pe, pe_prime, arrBondCount, arrRingCount, nsssr) { let cSssr = Array(); let allBonds = Array(); for (let i = 0; i &lt; c.length; i++) { if (c[i][0] % 2 !== 0) { for (let j = 0; j &lt; c[i][2].length; j++) { let bonds = c[i][1][0].concat(c[i][2][j]); // Some bonds are added twice, resulting in [[u, v], [u, v]] instead of [u, v]. // TODO: This is a workaround, fix later. Probably should be a set rather than an array, however the computational overhead // is probably bigger compared to leaving it like this. for (var k = 0; k &lt; bonds.length; k++) { if (bonds[k][0].constructor === Array) bonds[k] = bonds[k][0]; } let atoms = SSSR.bondsToAtoms(bonds); if (SSSR.getBondCount(atoms, adjacencyMatrix) === atoms.size &amp;&amp; !SSSR.pathSetsContain(cSssr, atoms, bonds, allBonds, arrBondCount, arrRingCount)) { cSssr.push(atoms); allBonds = allBonds.concat(bonds); } if (cSssr.length > nsssr) { return cSssr; } } } else { for (let j = 0; j &lt; c[i][1].length - 1; j++) { let bonds = c[i][1][j].concat(c[i][1][j + 1]); // Some bonds are added twice, resulting in [[u, v], [u, v]] instead of [u, v]. // TODO: This is a workaround, fix later. Probably should be a set rather than an array, however the computational overhead // is probably bigger compared to leaving it like this. for (var k = 0; k &lt; bonds.length; k++) { if (bonds[k][0].constructor === Array) bonds[k] = bonds[k][0]; } let atoms = SSSR.bondsToAtoms(bonds); if (SSSR.getBondCount(atoms, adjacencyMatrix) === atoms.size &amp;&amp; !SSSR.pathSetsContain(cSssr, atoms, bonds, allBonds, arrBondCount, arrRingCount)) { cSssr.push(atoms); allBonds = allBonds.concat(bonds); } if (cSssr.length > nsssr) { return cSssr; } } } } return cSssr; } /** * Returns the number of edges in a graph defined by an adjacency matrix. * * @param {Array[]} adjacencyMatrix An adjacency matrix. * @returns {Number} The number of edges in the graph defined by the adjacency matrix. */ static getEdgeCount(adjacencyMatrix) { let edgeCount = 0; let length = adjacencyMatrix.length; var i = length - 1; while (i--) { var j = length; while (j--) { if (adjacencyMatrix[i][j] === 1) { edgeCount++; } } } return edgeCount; } /** * Returns an edge list constructed form an adjacency matrix. * * @param {Array[]} adjacencyMatrix An adjacency matrix. * @returns {Array[]} An edge list. E.g. [ [ 0, 1 ], ..., [ 16, 2 ] ] */ static getEdgeList(adjacencyMatrix) { let length = adjacencyMatrix.length; let edgeList = Array(); var i = length - 1; while (i--) { var j = length; while (j--) { if (adjacencyMatrix[i][j] === 1) { edgeList.push([i, j]); } } } return edgeList; } /** * Return a set of vertex indices contained in an array of bonds. * * @param {Array} bonds An array of bonds. A bond is defined as [ sourceVertexId, targetVertexId ]. * @returns {Set&lt;Number>} An array of vertices. */ static bondsToAtoms(bonds) { let atoms = new Set(); var i = bonds.length; while (i--) { atoms.add(bonds[i][0]); atoms.add(bonds[i][1]); } return atoms; } /** * Returns the number of bonds within a set of atoms. * * @param {Set&lt;Number>} atoms An array of atom ids. * @param {Array[]} adjacencyMatrix An adjacency matrix. * @returns {Number} The number of bonds in a set of atoms. */ static getBondCount(atoms, adjacencyMatrix) { let count = 0; for (let u of atoms) { for (let v of atoms) { if (u === v) { continue; } count += adjacencyMatrix[u][v] } } return count / 2; } /** * Checks whether or not a given path already exists in an array of paths. * * @param {Set[]} pathSets An array of sets each representing a path. * @param {Set&lt;Number>} pathSet A set representing a path. * @param {Array[]} bonds The bonds associated with the current path. * @param {Array[]} allBonds All bonds currently associated with rings in the SSSR set. * @param {Uint16Array} arrBondCount A matrix containing the bond count of each vertex. * @param {Uint16Array} arrRingCount A matrix containing the number of rings associated with each vertex. * @returns {Boolean} A boolean indicating whether or not a give path is contained within a set. */ static pathSetsContain(pathSets, pathSet, bonds, allBonds, arrBondCount, arrRingCount) { var i = pathSets.length; while (i--) { if (SSSR.isSupersetOf(pathSet, pathSets[i])) { return true; } if (pathSets[i].size !== pathSet.size) { continue; } if (SSSR.areSetsEqual(pathSets[i], pathSet)) { return true; } } // Check if the edges from the candidate are already all contained within the paths of the set of paths. // TODO: For some reason, this does not replace the isSupersetOf method above -> why? let count = 0; let allContained = false; i = bonds.length; while (i--) { var j = allBonds.length; while (j--) { if (bonds[i][0] === allBonds[j][0] &amp;&amp; bonds[i][1] === allBonds[j][1] || bonds[i][1] === allBonds[j][0] &amp;&amp; bonds[i][0] === allBonds[j][1]) { count++; } if (count === bonds.length) { allContained = true; } } } // If all the bonds and thus vertices are already contained within other rings // check if there's one vertex with ringCount &lt; bondCount let specialCase = false; if (allContained) { for (let element of pathSet) { if (arrRingCount[element] &lt; arrBondCount[element]) { specialCase = true; break; } } } if (allContained &amp;&amp; !specialCase) { return true; } // Update the ring counts for the vertices for (let element of pathSet) { arrRingCount[element]++; } return false; } /** * Checks whether or not two sets are equal (contain the same elements). * * @param {Set&lt;Number>} setA A set. * @param {Set&lt;Number>} setB A set. * @returns {Boolean} A boolean indicating whether or not the two sets are equal. */ static areSetsEqual(setA, setB) { if (setA.size !== setB.size) { return false; } for (let element of setA) { if (!setB.has(element)) { return false; } } return true; } /** * Checks whether or not a set (setA) is a superset of another set (setB). * * @param {Set&lt;Number>} setA A set. * @param {Set&lt;Number>} setB A set. * @returns {Boolean} A boolean indicating whether or not setB is a superset of setA. */ static isSupersetOf(setA, setB) { for (var element of setB) { if (!setA.has(element)) { return false; } } return true; } } module.exports = SSSR;</code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jun 26 2018 08:07:40 GMT+0200 (W. Europe Summer Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/linenumber.js"></script> </body> </html>