3535import static java .util .Collections .unmodifiableList ;
3636import java .util .Comparator ;
3737import java .util .GregorianCalendar ;
38+ import java .util .HashMap ;
3839import java .util .HashSet ;
3940import java .util .LinkedList ;
4041import java .util .List ;
@@ -181,6 +182,7 @@ public void UpdateMatrix(Holder mh, boolean removeHydrogen) throws InterruptedEx
181182 } catch (InterruptedException e ) {
182183 LOGGER .error ("Error in matching molecules, check Graph Matcher module! " , e .getMessage ());
183184 }
185+ Map <ReactionContainer .Key , MCSSolution > indexedSolutions = indexSolutions (mcsSolutions );
184186 for (int substrateIndex = 0 ; substrateIndex < reactionStructureInformation .getEductCount (); substrateIndex ++) {
185187 for (int productIndex = 0 ; productIndex < reactionStructureInformation .getProductCount (); productIndex ++) {
186188 try {
@@ -202,7 +204,7 @@ public void UpdateMatrix(Holder mh, boolean removeHydrogen) throws InterruptedEx
202204 || mh .getGraphSimilarityMatrix ().getValue (substrateIndex , productIndex ) == -1 ) {
203205 if (reactionStructureInformation .isEductModified (substrateIndex )
204206 || reactionStructureInformation .isProductModified (productIndex )) {
205- refillMatrixWithNewData (mh , substrateIndex , productIndex , mcsSolutions );
207+ refillMatrixWithNewData (mh , substrateIndex , productIndex , indexedSolutions );
206208 } else {
207209 refillMatrixWithOldData (mh , substrateIndex , productIndex );
208210 }
@@ -235,6 +237,7 @@ public void UpdateMatrix(Collection<MCSSolution> mcsSolutions, Holder mh, boolea
235237 try {
236238 LOGGER .debug ("**********Updated Matrix And Calculate Similarity**************" );
237239 ReactionContainer reactionStructureInformation = mh .getReactionContainer ();
240+ Map <ReactionContainer .Key , MCSSolution > indexedSolutions = indexSolutions (mcsSolutions );
238241 for (int substrateIndex = 0 ; substrateIndex < reactionStructureInformation .getEductCount (); substrateIndex ++) {
239242 for (int productIndex = 0 ; productIndex < reactionStructureInformation .getProductCount (); productIndex ++) {
240243 IAtomContainer educt = reactionStructureInformation .getEduct (substrateIndex );
@@ -245,7 +248,7 @@ public void UpdateMatrix(Collection<MCSSolution> mcsSolutions, Holder mh, boolea
245248 || mh .getGraphSimilarityMatrix ().getValue (substrateIndex , productIndex ) == -1 ) {
246249 if (reactionStructureInformation .isEductModified (substrateIndex )
247250 || reactionStructureInformation .isProductModified (productIndex )) {
248- refillMatrixWithNewData (mh , substrateIndex , productIndex , mcsSolutions );
251+ refillMatrixWithNewData (mh , substrateIndex , productIndex , indexedSolutions );
249252 } else {
250253 refillMatrixWithOldData (mh , substrateIndex , productIndex );
251254 }
@@ -268,7 +271,7 @@ public void UpdateMatrix(Collection<MCSSolution> mcsSolutions, Holder mh, boolea
268271
269272 private void refillMatrixWithNewData (
270273 Holder holder , int substrateIndex , int productIndex ,
271- Collection < MCSSolution > mcsSolutions ) {
274+ Map < ReactionContainer . Key , MCSSolution > solutionIndex ) {
272275 LOGGER .debug ("**********Generate NEW MCS And Calculate Similarity**************" );
273276 try {
274277 ReactionContainer reactionContainer = holder .getReactionContainer ();
@@ -286,7 +289,7 @@ private void refillMatrixWithNewData(
286289 MCSSolution atomatomMapping = getMappings (
287290 holder .getReactionID (),
288291 holder .getTheory () == null ? "UNKNOWN" : holder .getTheory ().name (),
289- substrateIndex , productIndex , educt , product , mcsSolutions );
292+ substrateIndex , productIndex , educt , product , solutionIndex );
290293 if (atomatomMapping == null ) {
291294 clearScores (holder , substrateIndex , productIndex );
292295 return ;
@@ -339,33 +342,47 @@ private MCSSolution getMappings(
339342 String reactionId , String algorithmName ,
340343 int queryPosition , int targetPosition ,
341344 IAtomContainer educt , IAtomContainer product ,
342- Collection < MCSSolution > mcsSolutions ) throws CDKException {
343- if (mcsSolutions .isEmpty ()) {
345+ Map < ReactionContainer . Key , MCSSolution > solutionIndex ) throws CDKException {
346+ if (solutionIndex == null || solutionIndex .isEmpty ()) {
344347 return quickMapping (reactionId , algorithmName , educt , product , queryPosition , targetPosition );
345348 }
346- for (MCSSolution solution : mcsSolutions ) {
347- if (solution .getQueryPosition () == queryPosition
348- && solution .getTargetPosition () == targetPosition ) {
349- if (solution .getAtomAtomMapping ().isEmpty ()) {
350- Set <String > atomMaps = new HashSet <>();
351- for (IAtom a : educt .atoms ()) {
352- atomMaps .add (a .getSymbol ());
353- }
354- boolean mappingPossible = false ;
355- for (IAtom a : product .atoms ()) {
356- if (atomMaps .contains (a .getSymbol ())) {
357- mappingPossible = true ;
358- }
359- }
360- atomMaps .clear ();
361- if (mappingPossible ) {
362- return quickMapping (reactionId , algorithmName , educt , product , queryPosition , targetPosition );
363- }
349+ MCSSolution solution = solutionIndex .get (new ReactionContainer .Key (queryPosition , targetPosition ));
350+ if (solution == null ) {
351+ return null ;
352+ }
353+ if (solution .getAtomAtomMapping ().isEmpty ()) {
354+ Set <String > atomMaps = new HashSet <>();
355+ for (IAtom a : educt .atoms ()) {
356+ atomMaps .add (a .getSymbol ());
357+ }
358+ boolean mappingPossible = false ;
359+ for (IAtom a : product .atoms ()) {
360+ if (atomMaps .contains (a .getSymbol ())) {
361+ mappingPossible = true ;
364362 }
365- return solution ;
363+ }
364+ atomMaps .clear ();
365+ if (mappingPossible ) {
366+ return quickMapping (reactionId , algorithmName , educt , product , queryPosition , targetPosition );
366367 }
367368 }
368- return null ;
369+ return solution ;
370+ }
371+
372+ private Map <ReactionContainer .Key , MCSSolution > indexSolutions (Collection <MCSSolution > mcsSolutions ) {
373+ Map <ReactionContainer .Key , MCSSolution > indexedSolutions = new HashMap <>();
374+ if (mcsSolutions == null ) {
375+ return indexedSolutions ;
376+ }
377+ for (MCSSolution solution : mcsSolutions ) {
378+ if (solution == null ) {
379+ continue ;
380+ }
381+ indexedSolutions .put (
382+ new ReactionContainer .Key (solution .getQueryPosition (), solution .getTargetPosition ()),
383+ solution );
384+ }
385+ return indexedSolutions ;
369386 }
370387
371388 private MCSSolution quickMapping (String reactionId , String algorithmName ,
0 commit comments