001package org.biojava.nbio.structure.chem;
002
003import java.util.HashMap;
004import java.util.Map;
005
006/**
007 * A representation of the Chemical Component Dictionary.
008 *
009 * @author Andreas Prlic
010 * @since 1.7
011 * @see <a href="http://mmcif.rcsb.org/dictionaries/">link into mmCIF dictionary</a>
012 *
013 */
014public class ChemicalComponentDictionary {
015    private final Map<String, ChemComp> dictionary;
016    private final Map<String,String> replaces;
017    private final Map<String,String> isReplacedBy;
018
019    public ChemicalComponentDictionary(){
020        dictionary = new HashMap<>();
021        replaces  = new HashMap<>();
022        isReplacedBy = new HashMap<>();
023    }
024
025    public boolean isReplaced(ChemComp c) {
026        return isReplaced(c.getId());
027    }
028
029    public boolean isReplaced(String id) {
030        return isReplacedBy.containsKey(id);
031    }
032
033    public boolean isReplacer(ChemComp c) {
034        return isReplacer(c.getId());
035    }
036
037    public boolean isReplacer(String id) {
038        return replaces.containsKey(id);
039    }
040
041    /**
042     * If ChemComp is replaced by another one, get the newer version otherwise return the same ChemComp again.
043     * @param c
044     * @return get the component that replaced ChemComp.
045     */
046    public ChemComp getReplacer(ChemComp c) {
047        return getReplacer(c.getId());
048    }
049
050    public ChemComp getReplacer(String id) {
051        if (isReplaced(id)) {
052            return dictionary.get(isReplacedBy.get(id));
053        }
054        return dictionary.get(id);
055    }
056
057    /**
058     * If ChemComp is replacing another one, get the old version otherwise return the same ChemComp again.
059     * @param  c the ChemComp for which older versions should be looked up.
060     */
061    public ChemComp getReplaced(ChemComp c) {
062        return getReplaced(c.getId());
063    }
064
065    public ChemComp getReplaced(String id){
066        if (isReplacer(id)) {
067            return dictionary.get(replaces.get(id));
068        }
069        return dictionary.get(id);
070    }
071
072    /**
073     * Get the parent of a component. If component has no parent, return null
074     * @param c
075     * @return get the parent component or null if ChemComp has no parent.
076     */
077    public ChemComp getParent(ChemComp c) {
078        if (c.hasParent()) {
079            return dictionary.get(c.getMonNstdParentCompId());
080        }
081        return null;
082    }
083
084    /**
085     * Add a new component to the dictionary
086     * @param comp
087     */
088    public void addChemComp(ChemComp comp) {
089        dictionary.put(comp.getId(),comp);
090        String rep = comp.getPdbxReplaces();
091        if (rep != null && !rep.equals("?")) {
092            replaces.put(comp.getId(),rep);
093        }
094
095        String isrep = comp.getPdbxReplacedBy();
096        if (isrep != null && !isrep.equals("?")) {
097            isReplacedBy.put(comp.getId(), isrep);
098        }
099    }
100
101    /**
102     * Returns the number of ChemComps in this dictionary
103     * @return nr. of ChemComps
104     */
105    public int size() {
106        return dictionary.size();
107    }
108
109    public ChemComp getChemComp(String id) {
110        return dictionary.get(id);
111    }
112}