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 *
021 */
022
023package org.biojava.nbio.structure.io.mmcif;
024
025import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
026
027import java.util.HashMap;
028import java.util.Map;
029
030/** A representation of the Chemical Component Dictionary.
031 *
032 * @author Andreas Prlic
033 * @since 1.7
034 * @see <a href="http://mmcif.rcsb.org/dictionaries/">link into mmCIF dictionary</a>
035 *
036 */
037public class ChemicalComponentDictionary {
038
039        private Map<String, ChemComp> dictionary;
040        private Map<String,String> replaces;
041        private Map<String,String> isreplacedby;
042
043        public ChemicalComponentDictionary(){
044                dictionary = new HashMap<String, ChemComp>();
045                replaces   = new HashMap<String, String>();
046                isreplacedby = new HashMap<String, String>();
047        }
048
049        public boolean isReplaced(ChemComp c){
050                return isReplaced(c.getId());
051
052        }
053        public boolean isReplaced(String id){
054                if ( isreplacedby.containsKey(id))
055                        return true;
056                return false;
057        }
058        public boolean isReplacer(ChemComp c){
059                return isReplacer(c.getId());
060        }
061        public boolean isReplacer(String id){
062                if ( replaces.containsKey(id) )
063                        return true;
064                return false;
065        }
066
067        /** if ChemComp is replaced by another one, get the newer version
068         * otherwise return the same ChemComp again.
069         * @param c
070         * @return get the component that replaced ChemComp.
071         */
072        public ChemComp getReplacer(ChemComp c){
073                return getReplacer(c.getId());
074        }
075        public ChemComp getReplacer(String id){
076                if (isReplaced(id)){
077                        return dictionary.get(isreplacedby.get(id));
078                }
079                return dictionary.get(id);
080        }
081
082        /** if ChemComp is replacing another one, get the old version
083         * otherwise return the same ChemComp again.
084         * @param  c the ChemComp for which older versions should be looked up.
085         */
086
087        public ChemComp getReplaced(ChemComp c){
088                return getReplaced(c.getId());
089        }
090        public ChemComp getReplaced(String id){
091                if (isReplacer(id)){
092                        return dictionary.get(replaces.get(id));
093                }
094                return dictionary.get(id);
095        }
096
097        /** Get the parent of a component. If component has no parent, return null
098         *
099         * @param c
100         * @return get the parent component or null if ChemComp has no parent.
101         */
102        public ChemComp getParent(ChemComp c){
103
104                if (c.hasParent()){
105                        return dictionary.get(c.getMon_nstd_parent_comp_id());
106                }
107                return null;
108        }
109
110
111
112        /** add a new component to the dictionary
113         *
114         * @param comp
115         */
116        public void addChemComp(ChemComp comp){
117
118                dictionary.put(comp.getId(),comp);
119                String rep = comp.getPdbx_replaces();
120                if ( (rep != null) && ( ! rep.equals("?"))){
121                        replaces.put(comp.getId(),rep);
122                }
123
124                String isrep = comp.getPdbx_replaced_by();
125                if ( (isrep != null) && ( ! isrep.equals("?"))){
126                        isreplacedby.put(comp.getId(),isrep);
127                }
128        }
129
130        public ChemComp getChemComp(String id){
131                return dictionary.get(id);
132        }
133}