Skip to content

Commit c8b249d

Browse files
committed
improvements to make sure it's REALLY an atom list
1 parent eb3a48b commit c8b249d

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

storage/ctab/src/main/java/org/openscience/cdk/io/MDLV2000Writer.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.openscience.cdk.sgroup.Sgroup;
5353
import org.openscience.cdk.sgroup.SgroupBracket;
5454
import org.openscience.cdk.sgroup.SgroupKey;
55-
import org.openscience.cdk.sgroup.SgroupType;
5655
import org.openscience.cdk.tools.ILoggingTool;
5756
import org.openscience.cdk.tools.LoggingToolFactory;
5857
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
@@ -486,15 +485,17 @@ public void writeMolecule(IAtomContainer container) throws Exception {
486485
}
487486
}
488487

489-
}else if(container.getAtom(f) instanceof IQueryAtom){
488+
} else if(container.getAtom(f) instanceof IQueryAtom) {
490489
QueryAtom queryAtom = (QueryAtom) AtomRef.deref(container.getAtom(f));
491490
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));
495493
atomLists.add(container.getAtom(f));
494+
}else{
495+
line.append(formatMDLString(container.getAtom(f).getSymbol(), 3));
496496
}
497497
} else {
498+
498499
line.append(formatMDLString(container.getAtom(f).getSymbol(), 3));
499500
}
500501

@@ -795,16 +796,28 @@ else if (e.equals(new Expr(Expr.Type.ALIPHATIC_ORDER, 2).or(new Expr(Expr.Type.I
795796
writer.write('\n');
796797
writer.flush();
797798
}
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;
804806
}else{
805-
return "A";
807+
//not a list
808+
return false;
806809
}
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());
807819
}
820+
808821
private static List<String> getAtomList(Expr exp){
809822
List<Expr> elist = new ArrayList<>();
810823
getLeafNodes(exp, elist);

0 commit comments

Comments
 (0)