001/*
002 *                  BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Dec 21, 2005
021 *
022 */
023package org.biojava.nbio.structure;
024
025import org.biojava.nbio.structure.io.PDBFileParser;
026
027import java.io.InputStream;
028import java.util.HashMap;
029import java.util.Map;
030import java.util.zip.GZIPInputStream;
031
032
033/** A class that provides a set of standard amino acids.
034 *
035 *
036 *
037 * @author Andreas Prlic
038 * @author Tamas Horvath provided the standard amino acids.
039 *
040 *
041 */
042public final class StandardAminoAcid {
043
044        private static final String STANDARD_AMINOS_FILE = "org/biojava/nbio/structure/standardaminos.pdb.gz";
045
046        static private Map<String,AminoAcid> aminoAcids;
047
048        /**
049         * Cannot be instantiated.
050         */
051        private StandardAminoAcid() {
052                super();
053        }
054
055        /**
056         * <p>
057         * Initialize the static StandardAminoAcid resource.
058         * </p>
059         *
060         * <p>
061         * This parses the resource
062         * <code>{@value #STANDARD_AMINOS_FILE}</code>
063         * and builds a basic set of amino acids.
064         *</p>
065         * @author Tamas Horvath provided the standard amino acids
066         */
067        static {
068                aminoAcids = new HashMap<String,AminoAcid>();
069
070
071                InputStream fileStream = StandardAminoAcid.class.getClassLoader().getResourceAsStream(STANDARD_AMINOS_FILE);
072                if (fileStream == null) {
073                        throw new RuntimeException("Could not find resource "+STANDARD_AMINOS_FILE+".  This probably means that your biojava.jar file is corrupt or incorrectly built.");
074                }
075
076
077
078                try {
079                        GZIPInputStream gzipIS = new GZIPInputStream(fileStream);
080                        PDBFileParser parser = new PDBFileParser();
081                        Structure s = parser.parsePDBFile(gzipIS);
082
083
084                        GroupIterator iter = new GroupIterator(s);
085                        while (iter.hasNext()){
086                                Group g = iter.next();
087
088                                if ( g instanceof AminoAcid){
089                                        AminoAcid aa = (AminoAcid)g;
090
091                                        aminoAcids.put(aa.getPDBName(),aa);
092                                        aminoAcids.put(aa.getAminoType().toString(),aa);
093
094                                }
095                        }
096
097                } catch (Exception t) {
098                        throw new RuntimeException( "Unable to initialize standard aminoacids", t);
099                }
100        }
101
102        /** get a standard amino acid.
103         *
104         * @param name the 3- or 1-letter representation of the amino acid.
105         * @return the amino acids, or null if the name can not be matched
106         */
107        public static AminoAcid getAminoAcid(String name){
108
109                return aminoAcids.get(name);
110        }
111
112}