001    package org.jaga.individualRepresentation.proteinLocation;
002    
003    import org.jaga.definitions.*;
004    
005    /**
006     * TODO: Complete these comments.
007     *
008     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
009     *
010     * <p><u>Company:</u> University College London and JAGA.Org
011     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
012     * </p>
013     *
014     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
015     *    This program is free software; you can redistribute it and/or modify
016     *    it under the terms of the GNU General Public License as published by
017     *    the Free Software Foundation, ONLY if you include a note of the original
018     *    author(s) in any redistributed/modified copy.<br/>
019     *    This program is distributed in the hope that it will be useful,
020     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
021     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022     *    GNU General Public License for more details.<br/>
023     *    You should have received a copy of the GNU General Public License
024     *    along with this program; if not, write to the Free Software
025     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026     *    or see http://www.gnu.org/licenses/gpl.html</p>
027     *
028     * @author Greg Paperin (greg@jaga.org)
029     *
030     * @version JAGA public release 1.0 beta
031     */
032    
033    public abstract class SubstitutionScoringMatrix {
034    
035            private static final AminoAcid [] allAcids = new AminoAcid[] {
036                            AminoAcidFactory.getResidueByCode("I"), AminoAcidFactory.getResidueByCode("L"),
037                            AminoAcidFactory.getResidueByCode("M"), AminoAcidFactory.getResidueByCode("F"),
038                            AminoAcidFactory.getResidueByCode("A"), AminoAcidFactory.getResidueByCode("C"),
039                            AminoAcidFactory.getResidueByCode("V"), AminoAcidFactory.getResidueByCode("T"),
040                            AminoAcidFactory.getResidueByCode("W"), AminoAcidFactory.getResidueByCode("Y"),
041                            AminoAcidFactory.getResidueByCode("H"), AminoAcidFactory.getResidueByCode("K"),
042                            AminoAcidFactory.getResidueByCode("R"), AminoAcidFactory.getResidueByCode("E"),
043                            AminoAcidFactory.getResidueByCode("D"), AminoAcidFactory.getResidueByCode("N"),
044                            AminoAcidFactory.getResidueByCode("G"), AminoAcidFactory.getResidueByCode("S"),
045                            AminoAcidFactory.getResidueByCode("Q"), AminoAcidFactory.getResidueByCode("P"),
046                            null};
047    
048            public SubstitutionScoringMatrix() {}
049    
050            public abstract int getLogScore(AminoAcid aminoAcid1, AminoAcid aminoAcid2);
051            public abstract double getActualScore(AminoAcid aminoAcid1, AminoAcid aminoAcid2);
052    
053            public int getLogScore(String aminoAcid1, String aminoAcid2) {
054                    AminoAcid a1 = (null == aminoAcid1 ? null : AminoAcidFactory.getResidueByCode(aminoAcid1));
055                    AminoAcid a2 = (null == aminoAcid2 ? null : AminoAcidFactory.getResidueByCode(aminoAcid2));
056                    return getLogScore(a1, a2);
057            }
058    
059            public double getActualScore(String aminoAcid1, String aminoAcid2) {
060                    AminoAcid a1 = (null == aminoAcid1 ? null : AminoAcidFactory.getResidueByCode(aminoAcid1));
061                    AminoAcid a2 = (null == aminoAcid2 ? null : AminoAcidFactory.getResidueByCode(aminoAcid2));
062                    return getActualScore(a1, a2);
063            }
064    
065            public AminoAcid chooseProbabalisticMutation(AminoAcid aminoAcid, GAParameterSet params) {
066                    double [] probs = new double[21];
067                    double sum = 0.;
068                    for (int i = 0; i < 21; i++) {
069                            probs[i] = getActualScore(aminoAcid, allAcids[i]);
070                            sum += probs[i];
071                    }
072    
073                    double dice = params.getRandomGenerator().nextDouble(0, sum);
074                    double lower = 0.;
075                    double upper = 0.;
076                    for (int i = 0; i < 21; i++) {
077                            upper += probs[i];
078                            if (lower <= dice && dice < upper) {
079                                    return allAcids[i];
080                            }
081                            lower = upper;
082                    }
083    
084                    throw new Error("This line should never be executed. Hafe fun debugging!");
085            }
086    
087            public AminoAcid chooseProbabalisticMutation(String aminoAcid, GAParameterSet params) {
088                    return chooseProbabalisticMutation(AminoAcidFactory.getResidueByCode(aminoAcid), params);
089            }
090    
091    }