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 */
021package org.biojava.nbio.aaproperties.xml;
022
023import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
024import org.biojava.nbio.core.sequence.template.CompoundSet;
025import org.biojava.nbio.core.sequence.template.Sequence;
026
027import java.util.*;
028
029public class ModifiedAminoAcidCompoundSet implements CompoundSet<AminoAcidCompound> {
030
031        private final Map<String, AminoAcidCompound> aminoAcidCompoundCache = new HashMap<String, AminoAcidCompound>();
032
033        public ModifiedAminoAcidCompoundSet(List<AminoAcidComposition> aaList, Map<Character, Double> aaSymbol2MolecularWeight) {
034                this.aminoAcidCompoundCache.put("-", new AminoAcidCompound(null, "-", "", "", 0.0f));
035                for (AminoAcidComposition aa : aaList) {
036                        this.aminoAcidCompoundCache.put(aa.getSymbol(),
037                                        new AminoAcidCompound(null, aa.getSymbol(), aa.getShorName(), aa.getName(),
038                                                        aaSymbol2MolecularWeight.get(aa.getSymbol().charAt(0)).floatValue()));
039                }
040        }
041
042        @Override
043        public int getMaxSingleCompoundStringLength() {
044                return 1;
045        }
046
047        @Override
048        public boolean isCompoundStringLengthEqual() {
049                return true;
050        }
051
052        @Override
053        public AminoAcidCompound getCompoundForString(String string) {
054                if (string.length() == 0) {
055                        return null;
056                }
057                if (string.length() > this.getMaxSingleCompoundStringLength()) {
058                        throw new IllegalArgumentException("String supplied (" + string + ") is too long. Max is " + getMaxSingleCompoundStringLength());
059                }
060                return this.aminoAcidCompoundCache.get(string);
061        }
062
063        @Override
064        public String getStringForCompound(AminoAcidCompound compound) {
065                return compound.toString();
066        }
067
068        @Override
069        public boolean compoundsEquivalent(AminoAcidCompound compoundOne, AminoAcidCompound compoundTwo) {
070                // TODO Auto-generated method stub
071                return false;
072        }
073
074        @Override
075        public Set<AminoAcidCompound> getEquivalentCompounds(AminoAcidCompound compound) {
076                // TODO Auto-generated method stub
077                return null;
078        }
079
080        @Override
081        public boolean hasCompound(AminoAcidCompound compound) {
082                return aminoAcidCompoundCache.containsValue(compound);
083        }
084
085        @Override
086        public List<AminoAcidCompound> getAllCompounds() {
087                return new ArrayList<AminoAcidCompound>(aminoAcidCompoundCache.values());
088        }
089
090        @Override
091        public boolean isComplementable() {
092                return false;
093        }
094
095        @Override
096        public boolean isValidSequence(Sequence<AminoAcidCompound> sequence) {
097                for (AminoAcidCompound c : sequence) {
098                        if (!hasCompound(c)) {
099                                return false;
100                        }
101                }
102                return true;
103        }
104
105}