2525
2626import org .openscience .cdk .interfaces .IAtom ;
2727import org .openscience .cdk .interfaces .IAtomContainer ;
28- import org .openscience .cdk .isomorphism .matchers .Expr ;
29- import org .openscience .cdk .isomorphism .matchers .IQueryAtom ;
30- import org .openscience .cdk .isomorphism .matchers .IQueryAtomContainer ;
31- import org .openscience .cdk .isomorphism .matchers .QueryAtomContainer ;
28+ import org .openscience .cdk .interfaces .IBond ;
29+ import org .openscience .cdk .isomorphism .matchers .*;
3230
3331import java .util .Collections ;
3432
8179 */
8280public class DfPattern extends Pattern {
8381
84- private final IQueryAtomContainer query ;
85- private final DfState state ;
82+ private final IAtomContainer src ;
83+ private final IAtomContainer query ;
84+ private final DfState state ;
8685
87- private DfPattern (IQueryAtomContainer query ) {
86+ private DfPattern (IAtomContainer src , IAtomContainer query ) {
87+ this .src = src ;
8888 this .query = query ;
8989 determineFilters (query );
9090 state = new DfState (query );
@@ -94,9 +94,9 @@ private static void checkCompatibleAPI(IAtom atom) {
9494 if (atom .getContainer () == null ) {
9595 throw new IllegalArgumentException (
9696 "This API can only be used with the option " +
97- "CdkUseLegacyAtomContainer=false (default). The atoms in " +
98- "the molecule provided do not know about their parent " +
99- "molecule"
97+ "CdkUseLegacyAtomContainer=false (default). The atoms in " +
98+ "the molecule provided do not know about their parent " +
99+ "molecule"
100100 );
101101 }
102102 }
@@ -123,12 +123,12 @@ public boolean matches(IAtomContainer target) {
123123 @ Override
124124 public Mappings matchAll (IAtomContainer mol ) {
125125 if (mol .getAtomCount () < query .getAtomCount ())
126- return new Mappings (query , mol , Collections .<int []>emptySet ());
126+ return new Mappings (src , mol , Collections .<int []>emptySet ());
127127 if (mol .getAtomCount () > 0 )
128128 checkCompatibleAPI (mol .getAtom (0 ));
129129 DfState local = new DfState (state );
130130 local .setMol (mol );
131- return filter (new Mappings (query , mol , local ), query , mol );
131+ return filter (new Mappings (src , mol , local ), query , mol );
132132 }
133133
134134 /**
@@ -171,15 +171,29 @@ public boolean matchesRoot(IAtom root) {
171171 * @see QueryAtomContainer#create(IAtomContainer, Expr.Type...)
172172 */
173173 public static DfPattern findSubstructure (IAtomContainer query ) {
174- if (query instanceof IQueryAtomContainer )
175- return new DfPattern ((IQueryAtomContainer ) query );
176- else
177- return new DfPattern (QueryAtomContainer .create (query ,
174+ // if one or more atoms/bonds is not a query atom/bond we need to convert
175+ // out input to a query molecule using some sensible defaults. Note any existing
176+ // query atom/bonds will be copied
177+ if (!isCompleteQuery (query )) {
178+ return new DfPattern (query ,
179+ QueryAtomContainer .create (query ,
178180 ALIPHATIC_ELEMENT ,
179181 AROMATIC_ELEMENT ,
180182 SINGLE_OR_AROMATIC ,
181183 ALIPHATIC_ORDER ,
182184 STEREOCHEMISTRY ));
185+ } else {
186+ return new DfPattern (query , query );
187+ }
183188 }
184189
190+ private static boolean isCompleteQuery (IAtomContainer query ) {
191+ for (IAtom atom : query .atoms ())
192+ if (!(atom instanceof IQueryAtom ))
193+ return false ;
194+ for (IBond bond : query .bonds ())
195+ if (!(bond instanceof IQueryBond ))
196+ return false ;
197+ return true ;
198+ }
185199}
0 commit comments