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/**
034 * A class that provides a set of standard amino acids.
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        }
053
054        /**
055         * <p>
056         * Initialize the static StandardAminoAcid resource.
057         * </p>
058         *
059         * <p>
060         * This parses the resource
061         * <code>{@value #STANDARD_AMINOS_FILE}</code>
062         * and builds a basic set of amino acids.
063         *</p>
064         * @author Tamas Horvath provided the standard amino acids
065         */
066        static {
067                aminoAcids = new HashMap<String,AminoAcid>();
068
069
070                InputStream fileStream = StandardAminoAcid.class.getClassLoader().getResourceAsStream(STANDARD_AMINOS_FILE);
071                if (fileStream == null) {
072                        throw new RuntimeException("Could not find resource "+STANDARD_AMINOS_FILE+".  This probably means that your biojava.jar file is corrupt or incorrectly built.");
073                }
074
075
076
077                try {
078                        GZIPInputStream gzipIS = new GZIPInputStream(fileStream);
079                        PDBFileParser parser = new PDBFileParser();
080                        Structure s = parser.parsePDBFile(gzipIS);
081
082
083                        GroupIterator iter = new GroupIterator(s);
084                        while (iter.hasNext()){
085                                Group g = iter.next();
086
087                                if ( g instanceof AminoAcid){
088                                        AminoAcid aa = (AminoAcid)g;
089
090                                        aminoAcids.put(aa.getPDBName(),aa);
091                                        aminoAcids.put(aa.getAminoType().toString(),aa);
092
093                                }
094                        }
095
096                } catch (Exception t) {
097                        throw new RuntimeException( "Unable to initialize standard aminoacids", t);
098                }
099        }
100
101        /** get a standard amino acid.
102         *
103         * @param name the 3- or 1-letter representation of the amino acid.
104         * @return the amino acids, or null if the name can not be matched
105         */
106        public static AminoAcid getAminoAcid(String name){
107
108                return aminoAcids.get(name);
109        }
110
111}