|
52 | 52 | import org.openscience.cdk.sgroup.Sgroup; |
53 | 53 | import org.openscience.cdk.sgroup.SgroupBracket; |
54 | 54 | import org.openscience.cdk.sgroup.SgroupKey; |
55 | | -import org.openscience.cdk.sgroup.SgroupType; |
56 | 55 | import org.openscience.cdk.tools.ILoggingTool; |
57 | 56 | import org.openscience.cdk.tools.LoggingToolFactory; |
58 | 57 | import org.openscience.cdk.tools.manipulator.AtomContainerManipulator; |
@@ -486,15 +485,17 @@ public void writeMolecule(IAtomContainer container) throws Exception { |
486 | 485 | } |
487 | 486 | } |
488 | 487 |
|
489 | | - }else if(container.getAtom(f) instanceof IQueryAtom){ |
| 488 | + } else if(container.getAtom(f) instanceof IQueryAtom) { |
490 | 489 | QueryAtom queryAtom = (QueryAtom) AtomRef.deref(container.getAtom(f)); |
491 | 490 | Expr expr = queryAtom.getExpression(); |
492 | | - String symbol = getSymbolForAtomExpression(expr); |
493 | | - line.append(formatMDLString(symbol, 3)); |
494 | | - if("L".equals(symbol)){ |
| 491 | + if(isValidAtomListExpression(expr)){ |
| 492 | + line.append(formatMDLString("L", 3)); |
495 | 493 | atomLists.add(container.getAtom(f)); |
| 494 | + }else{ |
| 495 | + line.append(formatMDLString(container.getAtom(f).getSymbol(), 3)); |
496 | 496 | } |
497 | 497 | } else { |
| 498 | + |
498 | 499 | line.append(formatMDLString(container.getAtom(f).getSymbol(), 3)); |
499 | 500 | } |
500 | 501 |
|
@@ -795,16 +796,28 @@ else if (e.equals(new Expr(Expr.Type.ALIPHATIC_ORDER, 2).or(new Expr(Expr.Type.I |
795 | 796 | writer.write('\n'); |
796 | 797 | writer.flush(); |
797 | 798 | } |
798 | | - private static String getSymbolForAtomExpression(Expr exp){ |
799 | | - List<Expr> elist = new ArrayList<>(); |
800 | | - getLeafNodes(exp, elist); |
801 | | - if(!elist.isEmpty() && elist.stream() |
802 | | - .allMatch(ex->ex.type().equals(Expr.Type.ELEMENT))){ |
803 | | - return "L"; |
| 799 | + private static boolean isValidAtomListExpression(Expr exp){ |
| 800 | + |
| 801 | + Expr rootToCheck; |
| 802 | + if(Expr.Type.NOT==exp.type()){ |
| 803 | + rootToCheck = exp.left(); |
| 804 | + }else if(Expr.Type.OR==exp.type()){ |
| 805 | + rootToCheck = exp; |
804 | 806 | }else{ |
805 | | - return "A"; |
| 807 | + //not a list |
| 808 | + return false; |
806 | 809 | } |
| 810 | + Set<Expr.Type> allowedTypes = EnumSet.of(Expr.Type.ELEMENT, Expr.Type.ALIPHATIC_ELEMENT, Expr.Type.AROMATIC_ELEMENT); |
| 811 | + |
| 812 | + return allOrsOfAllowedTypes(rootToCheck, allowedTypes); |
| 813 | + } |
| 814 | + private static boolean allOrsOfAllowedTypes(Expr expr, Set<Expr.Type> allowedTypes){ |
| 815 | + if(expr.type() == Expr.Type.OR){ |
| 816 | + return allOrsOfAllowedTypes(expr.left(), allowedTypes) && allOrsOfAllowedTypes(expr.right(), allowedTypes); |
| 817 | + } |
| 818 | + return allowedTypes.contains(expr.type()); |
807 | 819 | } |
| 820 | + |
808 | 821 | private static List<String> getAtomList(Expr exp){ |
809 | 822 | List<Expr> elist = new ArrayList<>(); |
810 | 823 | getLeafNodes(exp, elist); |
|
0 commit comments