@@ -408,7 +408,8 @@ public void writeMolecule(IAtomContainer container) throws Exception {
408408 writer .write (line .toString ());
409409 writer .write ('\n' );
410410
411- Set <IAtom > atomLists = new LinkedHashSet <>();
411+ //map of ALS atoms to write and their indexes since some atom getIndex() can return -1
412+ Map <IAtom , Integer > atomLists = new LinkedHashMap <>();
412413 // write Atom block
413414 for (int f = 0 ; f < container .getAtomCount (); f ++) {
414415 IAtom atom = container .getAtom (f );
@@ -490,7 +491,7 @@ public void writeMolecule(IAtomContainer container) throws Exception {
490491 Expr expr = queryAtom .getExpression ();
491492 if (isValidAtomListExpression (expr )){
492493 line .append (formatMDLString ("L" , 3 ));
493- atomLists .add (container .getAtom (f ));
494+ atomLists .put (container .getAtom (f ), f );
494495 }else {
495496 line .append (formatMDLString (container .getAtom (f ).getSymbol (), 3 ));
496497 }
@@ -767,15 +768,15 @@ else if (e.equals(new Expr(Expr.Type.ALIPHATIC_ORDER, 2).or(new Expr(Expr.Type.I
767768 }
768769 }
769770 //write atom lists
770- for (IAtom a : atomLists ){
771- QueryAtom qa = (QueryAtom ) AtomRef .deref (a );
771+ for (Map . Entry < IAtom , Integer > entry : atomLists . entrySet () ){
772+ QueryAtom qa = (QueryAtom ) AtomRef .deref (entry . getKey () );
772773 //atom lists are limited to just a list of ELEMENTS OR'ed together
773774 //with the whole expression possibly negated
774775
775776 Expr expression = qa .getExpression ();
776777 List <String > elements =getAtomList (expression );
777778 writer .write ("M ALS " );
778- writer .write (formatMDLInt (a . getIndex ()+1 , 3 ));
779+ writer .write (formatMDLInt (entry . getValue ()+1 , 3 ));
779780 writer .write (formatMDLInt (elements .size (), 3 ));
780781 //root expression type is either OR or NOT
781782 if (expression .type () == Expr .Type .NOT ){
0 commit comments