~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/templates/AminoAcids.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Revision: 8130 $ $Author: egonw $ $Date: 2007-03-16 15:15:22 +0100 (Fri, 16 Mar 2007) $
 
2
 * 
 
3
 * Copyright (C) 2005-2007  Martin Eklund <martin.eklund@farmbio.uu.se>
 
4
 * 
 
5
 * Contact: cdk-devel@lists.sourceforge.net
 
6
 * 
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public License
 
9
 * as published by the Free Software Foundation; either version 2.1
 
10
 * of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU Lesser General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 
 
20
 */
 
21
package org.openscience.cdk.templates;
 
22
 
 
23
import java.util.Enumeration;
 
24
import java.util.HashMap;
 
25
import java.util.Iterator;
 
26
import java.util.List;
 
27
 
 
28
import org.openscience.cdk.AminoAcid;
 
29
import org.openscience.cdk.dict.DictRef;
 
30
import org.openscience.cdk.interfaces.IAtom;
 
31
import org.openscience.cdk.interfaces.IAtomContainer;
 
32
import org.openscience.cdk.interfaces.IBond;
 
33
import org.openscience.cdk.interfaces.IChemFile;
 
34
import org.openscience.cdk.io.CMLReader;
 
35
import org.openscience.cdk.tools.LoggingTool;
 
36
import org.openscience.cdk.tools.manipulator.AminoAcidManipulator;
 
37
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
 
38
 
 
39
/**
 
40
 * Tool that provides templates for the (natural) amino acids.
 
41
 *
 
42
 * @author      Martin Eklund <martin.eklund@farmbio.uu.se>
 
43
 * @cdk.module  pdb
 
44
 * @cdk.keyword templates
 
45
 * @cdk.keyword amino acids, stuctures
 
46
 * @cdk.created 2005-02-08
 
47
 */
 
48
public class AminoAcids {
 
49
 
 
50
        private static final LoggingTool logger = new LoggingTool(AminoAcids.class);
 
51
        
 
52
    /**
 
53
     * Creates matrix with info about the bonds in the amino acids.
 
54
     * 0 = bond id, 1 = atom1 in bond, 2 = atom2 in bond, 3 = bond order.
 
55
     * @return info
 
56
     */
 
57
    public static int[][] aaBondInfo()  {
 
58
 
 
59
        if (aminoAcids == null) {
 
60
            createAAs();
 
61
        }
 
62
        
 
63
        int[][] info = new int[153][4];
 
64
        
 
65
        int counter = 0;
 
66
        int total = 0;
 
67
        for (int aa=0; aa<aminoAcids.length; aa++) {
 
68
                AminoAcid acid = aminoAcids[aa];
 
69
 
 
70
                logger.debug("#bonds for ", acid.getProperty(RESIDUE_NAME).toString(), " = " + acid.getBondCount());
 
71
                total += acid.getBondCount();
 
72
                logger.debug("total #bonds: ", total);
 
73
 
 
74
            Iterator bonds = acid.bonds();
 
75
            while (bonds.hasNext()) {
 
76
                IBond bond = (IBond) bonds.next();
 
77
                        info[counter][0] = counter;
 
78
                        info[counter][1] = acid.getAtomNumber(bond.getAtom(0));
 
79
                        info[counter][2] = acid.getAtomNumber(bond.getAtom(1));
 
80
                        info[counter][3] = (int)bond.getOrder();
 
81
                        counter++;
 
82
                }
 
83
        }
 
84
        
 
85
        if (counter>153) {
 
86
                logger.error("Error while creating AA info! Bond count is too large: ", counter);
 
87
                return null;
 
88
        }
 
89
        
 
90
        return info;
 
91
    }
 
92
    
 
93
    private static AminoAcid[] aminoAcids = null;
 
94
    
 
95
    public final static String RESIDUE_NAME = "residueName";
 
96
    public final static String RESIDUE_NAME_SHORT = "residueNameShort";
 
97
    public final static String NO_ATOMS = "noOfAtoms";
 
98
    public final static String NO_BONDS = "noOfBonds";
 
99
    public final static String ID = "id";
 
100
    
 
101
    /**
 
102
     * Creates amino acid AminoAcid objects.
 
103
     * 
 
104
     * @return aminoAcids, a HashMap containing the amino acids as AminoAcids.
 
105
     */
 
106
    public static AminoAcid[] createAAs() {
 
107
        if (aminoAcids != null) {
 
108
            return aminoAcids;
 
109
        }
 
110
        
 
111
        // Create set of AtomContainers
 
112
        aminoAcids = new AminoAcid[20];
 
113
 
 
114
        IChemFile list = new org.openscience.cdk.ChemFile();
 
115
        CMLReader reader = new CMLReader(
 
116
                AminoAcids.class.getClassLoader().getResourceAsStream(
 
117
                                "data/templates/list_aminoacids.cml"
 
118
                )
 
119
        );
 
120
        try {
 
121
                list = (IChemFile)reader.read(list);
 
122
                List containersList = ChemFileManipulator.getAllAtomContainers(list);
 
123
                Iterator iterator = containersList.iterator();
 
124
                int counter = 0;
 
125
                while(iterator.hasNext())
 
126
                {
 
127
                        IAtomContainer ac = (IAtomContainer)iterator.next();
 
128
                        logger.debug("Adding AA: ", ac);
 
129
                        // convert into an AminoAcid
 
130
                        AminoAcid aminoAcid = new AminoAcid();
 
131
                        java.util.Iterator atoms = ac.atoms();
 
132
                        Enumeration props = ac.getProperties().keys();
 
133
                        while (props.hasMoreElements()) {
 
134
                                Object next = props.nextElement();
 
135
                                logger.debug("Prop class: " + next.getClass().getName());
 
136
                                logger.debug("Prop: " + next.toString());
 
137
                                if (next instanceof DictRef) {
 
138
                                        DictRef dictRef = (DictRef)next;
 
139
                                        // logger.debug("DictRef type: " + dictRef.getType());
 
140
                                        if (dictRef.getType().equals("pdb:residueName")) {
 
141
                                                aminoAcid.setProperty(RESIDUE_NAME, ac.getProperty(dictRef).toString().toUpperCase());
 
142
                                                aminoAcid.setMonomerName(ac.getProperty(dictRef).toString());
 
143
                                        } else if (dictRef.getType().equals("pdb:oneLetterCode")) {
 
144
                                                aminoAcid.setProperty(RESIDUE_NAME_SHORT, ac.getProperty(dictRef));
 
145
                                        } else if (dictRef.getType().equals("pdb:id")) {
 
146
                                                aminoAcid.setProperty(ID, ac.getProperty(dictRef));
 
147
                                                logger.debug("Set AA ID to: ", ac.getProperty(dictRef));
 
148
                                        } else {
 
149
                                                logger.error("Cannot deal with dictRef!");
 
150
                                        }
 
151
                                }
 
152
                        }
 
153
                        while (atoms.hasNext()) {
 
154
                                org.openscience.cdk.interfaces.IAtom atom = (IAtom)atoms.next();
 
155
                                String dictRef = (String)atom.getProperty("org.openscience.cdk.dict");
 
156
                                if (dictRef != null && dictRef.equals("pdb:nTerminus")) {
 
157
                                        aminoAcid.addNTerminus(atom);
 
158
                                } else if (dictRef != null && dictRef.equals("pdb:cTerminus")) {
 
159
                                        aminoAcid.addCTerminus(atom);
 
160
                                } else {
 
161
                                        aminoAcid.addAtom(atom);
 
162
                                }
 
163
                        }
 
164
//                      org.openscience.cdk.interfaces.IBond[] bonds = ac.getBonds();
 
165
                Iterator bonds = ac.bonds();
 
166
                while (bonds.hasNext()) {
 
167
                    IBond bond = (IBond) bonds.next();
 
168
                                aminoAcid.addBond(bond);
 
169
                        }
 
170
                        AminoAcidManipulator.removeAcidicOxygen(aminoAcid);
 
171
                aminoAcid.setProperty(NO_ATOMS, "" + aminoAcid.getAtomCount());
 
172
                aminoAcid.setProperty(NO_BONDS, "" + aminoAcid.getBondCount());
 
173
                if (counter < aminoAcids.length) {
 
174
                        aminoAcids[counter] = aminoAcid;
 
175
                } else {
 
176
                        logger.error("Could not store AminoAcid! Array too short!");
 
177
                }
 
178
                counter++;
 
179
                }
 
180
        } catch (Exception exception) {
 
181
                logger.error("Failed reading file: ", exception.getMessage());
 
182
                logger.debug(exception);
 
183
        }
 
184
        
 
185
        return aminoAcids;
 
186
    }
 
187
 
 
188
    /**
 
189
     * Returns a HashMap where the key is one of G, A, V, L, I, S, T, C, M, D,
 
190
     * N, E, Q, R, K, H, F, Y, W and P.
 
191
     */
 
192
    public static HashMap getHashMapBySingleCharCode() {
 
193
        AminoAcid[] monomers = createAAs();
 
194
        HashMap map = new HashMap();
 
195
        for (int i=0; i<monomers.length; i++) {
 
196
            map.put(monomers[i].getProperty(RESIDUE_NAME_SHORT), monomers[i]);
 
197
        }
 
198
        return map;
 
199
    }
 
200
    
 
201
    /**
 
202
     * Returns a HashMap where the key is one of GLY, ALA, VAL, LEU, ILE, SER,
 
203
     * THR, CYS, MET, ASP, ASN, GLU, GLN, ARG, LYS, HIS, PHE, TYR, TRP AND PRO.
 
204
     */
 
205
    public static HashMap getHashMapByThreeLetterCode() {
 
206
        AminoAcid[] monomers = createAAs();
 
207
        HashMap map = new HashMap();
 
208
        for (int i=0; i<monomers.length; i++) {
 
209
            map.put(monomers[i].getProperty(RESIDUE_NAME), monomers[i]);
 
210
        }
 
211
        return map;
 
212
    }
 
213
    
 
214
    /**
 
215
     * Returns the one letter code of an amino acid given a three letter code.
 
216
     * For example, it will return "V" when "Val" was passed.
 
217
     */
 
218
    public static String convertThreeLetterCodeToOneLetterCode(String threeLetterCode) {
 
219
        AminoAcid[] monomers = createAAs();
 
220
        for (int i=0; i<monomers.length; i++) {
 
221
                if (monomers[i].getProperty(RESIDUE_NAME).equals(threeLetterCode)) {
 
222
                        return (String)monomers[i].getProperty(RESIDUE_NAME_SHORT);
 
223
                }
 
224
        }
 
225
        return null;
 
226
    }
 
227
 
 
228
    /**
 
229
     * Returns the three letter code of an amino acid given a one letter code.
 
230
     * For example, it will return "Val" when "V" was passed.
 
231
     */
 
232
    public static String convertOneLetterCodeToThreeLetterCode(String oneLetterCode) {
 
233
        AminoAcid[] monomers = createAAs();
 
234
        for (int i=0; i<monomers.length; i++) {
 
235
                if (monomers[i].getProperty(RESIDUE_NAME_SHORT).equals(oneLetterCode)) {
 
236
                        return (String)monomers[i].getProperty(RESIDUE_NAME);
 
237
                }
 
238
        }
 
239
        return null;
 
240
    }
 
241
 
 
242
}