1 /* Copyright (C) 2004-2008  Rajarshi Guha <rajarshi.guha@gmail.com>
2  *
3  *  Contact: cdk-devel@lists.sourceforge.net
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 package org.openscience.cdk.pharmacophore;
20 
21 import org.openscience.cdk.Atom;
22 import org.openscience.cdk.interfaces.IAtom;
23 import org.openscience.cdk.isomorphism.matchers.IQueryAtom;
24 import org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer;
25 import org.openscience.cdk.isomorphism.matchers.QueryAtomContainer;
26 import org.openscience.cdk.smarts.Smarts;
27 import org.openscience.cdk.smarts.SmartsPattern;
28 
29 /**
30  * Represents a query pharmacophore group.
31  *
32  * This class is meant to be used to construct pharmacophore queries in conjunction
33  * with {@link org.openscience.cdk.pharmacophore.PharmacophoreQueryBond} and an
34  * {@link org.openscience.cdk.isomorphism.matchers.QueryAtomContainer}.
35  *
36  * @author Rajarshi Guha
37  * @cdk.module pcore
38  * @cdk.githash
39  * @cdk.keyword pharmacophore
40  * @cdk.keyword 3D isomorphism
41  * @see org.openscience.cdk.pharmacophore.PharmacophoreQueryBond
42  * @see org.openscience.cdk.isomorphism.matchers.QueryAtomContainer
43  * @see org.openscience.cdk.pharmacophore.PharmacophoreMatcher
44  */
45 public class PharmacophoreQueryAtom extends Atom implements IQueryAtom {
46 
47     private String smarts;
48     private SmartsPattern[] compiledSmarts;
49     private String symbol;
50 
51     /**
52      * Creat a new query pharmacophore group
53      *
54      * @param symbol The symbol for the group
55      * @param smarts The SMARTS pattern to be used for matching
56      */
PharmacophoreQueryAtom(String symbol, String smarts)57     public PharmacophoreQueryAtom(String symbol, String smarts) {
58         this.symbol = symbol;
59         this.smarts = smarts;
60         // Note that we allow a special form of SMARTS where the | operator
61         // represents logical or of multi-atom groups (as opposed to ','
62         // which is for single atom matches)
63         String[] subSmarts = smarts.split("\\|");
64         this.compiledSmarts = new SmartsPattern[subSmarts.length];
65         for (int i = 0; i < compiledSmarts.length; i++) {
66             compiledSmarts[i] = SmartsPattern.create(subSmarts[i])
67                                              .setPrepare(false);
68         }
69     }
70 
71     /**
72      * {@inheritDoc}
73      */
74     @Override
getSymbol()75     public String getSymbol() {
76         return this.symbol;
77     }
78 
79     /**
80      * {@inheritDoc}
81      */
82     @Override
setSymbol(String symbol)83     public void setSymbol(String symbol) {
84         this.symbol = symbol;
85     }
86 
87     /**
88      * Get the SMARTS pattern for this pharmacophore group.
89      *
90      * @return The SMARTS pattern
91      */
getSmarts()92     public String getSmarts() {
93         return smarts;
94     }
95 
96     /**
97      * Accessed the compiled SMARTS for this pcore query atom.
98      * @return compiled SMARTS patterns
99      */
getCompiledSmarts()100     SmartsPattern[] getCompiledSmarts() {
101         return compiledSmarts;
102     }
103 
104     /**
105      * Checks whether this query atom matches a target atom.
106      *
107      * Currently a query pharmacophore atom will match a target pharmacophore group if the
108      * symbols of the two groups match. This is based on the assumption that
109      * pharmacophore groups with the same symbol will have the same SMARTS
110      * pattern.
111      *
112      * @param atom A target pharmacophore group
113      * @return true if the current query group has the same symbol as the target group
114      */
115     @Override
matches(IAtom atom)116     public boolean matches(IAtom atom) {
117         PharmacophoreAtom patom = PharmacophoreAtom.get(atom);
118         return patom.getSymbol().equals(getSymbol());
119     }
120 
121     /**
122      * String representation of this pharmacophore group.
123      *
124      * @return String representation of this pharmacophore group
125      */
126     @Override
toString()127     public String toString() {
128         StringBuffer s = new StringBuffer();
129         s.append(getSymbol()).append(" [").append(getSmarts()).append(']');
130         return s.toString();
131     }
132 }
133