001    package org.jaga.individualRepresentation.proteinLocation;
002    
003    import java.util.ArrayList;
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 class AminoAcidGroup extends PolypeptidePatternItem {
034    
035            private ArrayList acids = new ArrayList();
036            private int countSmall = 0;
037            private int countHydrophobic = 0;
038            private int countPolar = 0;
039            private int countPositive = 0;
040            private int countNegative = 0;
041            private int countTiny = 0;
042            private int countAliphatic = 0;
043            private int countAromatic = 0;
044    
045            public AminoAcidGroup() {}
046    
047            public boolean doesntMatch(AminoAcid aminoAcid) {
048                    return !acids.contains(aminoAcid);
049            }
050    
051            public AminoAcid getResidue(int index) {
052                    return (AminoAcid) this.acids.get(index);
053            }
054    
055            public void swapResidue(int index, AminoAcid newAminoAcid) {
056                    if (acids.contains(newAminoAcid))
057                            return;
058                    removeResidue(index);
059                    addResidue(newAminoAcid);
060            }
061    
062            public AminoAcid removeResidue(int index) {
063                    AminoAcid aa = (AminoAcid) this.acids.remove(index);
064                    removeAcidFromStats(aa);
065                    return aa;
066            }
067    
068            public void addResidue(AminoAcid aminoAcid) {
069                    if (acids.contains(aminoAcid))
070                            return;
071                    acids.add(aminoAcid);
072                    addAcidToStats(aminoAcid);
073            }
074    
075            public int size() {
076                    return this.acids.size();
077            }
078    
079            private void addAcidToStats(AminoAcid aminoAcid) {
080                    if (aminoAcid.isSmall())        countSmall++;
081                    if (aminoAcid.isHydrophobic())  countHydrophobic++;
082                    if (aminoAcid.isPolar())        countPolar++;
083                    if (aminoAcid.isPositive())     countPositive++;
084                    if (aminoAcid.isNegative())     countNegative++;
085                    if (aminoAcid.isTiny())         countTiny++;
086                    if (aminoAcid.isAliphatic())    countAliphatic++;
087                    if (aminoAcid.isAromatic())     countAromatic++;
088            }
089    
090            private void removeAcidFromStats(AminoAcid aminoAcid) {
091                    if (aminoAcid.isSmall())        countSmall--;
092                    if (aminoAcid.isHydrophobic())  countHydrophobic--;
093                    if (aminoAcid.isPolar())        countPolar--;
094                    if (aminoAcid.isPositive())     countPositive--;
095                    if (aminoAcid.isNegative())     countNegative--;
096                    if (aminoAcid.isTiny())         countTiny--;
097                    if (aminoAcid.isAliphatic())    countAliphatic--;
098                    if (aminoAcid.isAromatic())     countAromatic--;
099            }
100    
101            public double getPropertyProportion(int property) {
102                    int count = 0;
103                    if (0 != (property & AminoAcidProperty.Small))
104                            count += countSmall;
105                    if (0 != (property & AminoAcidProperty.Hydrophobic))
106                            count += countHydrophobic;
107                    if (0 != (property & AminoAcidProperty.Polar))
108                            count += countPolar;
109                    if (0 != (property & AminoAcidProperty.Positive))
110                            count += countPositive;
111                    if (0 != (property & AminoAcidProperty.Negative))
112                            count += countNegative;
113                    if (0 != (property & AminoAcidProperty.Tiny))
114                            count += countTiny;
115                    if (0 != (property & AminoAcidProperty.Aliphatic))
116                            count += countAliphatic;
117                    if (0 != (property & AminoAcidProperty.Aromatic))
118                            count += countAromatic;
119                    return ((double) count) / ((double) acids.size());
120            }
121    
122            public int getMaxRepresentedProperty() {
123                    double percent = 0;
124                    int property = 0;
125                    if (getPropertyProportion(AminoAcidProperty.Small) > percent) {
126                            percent = getPropertyProportion(AminoAcidProperty.Small);
127                            property = AminoAcidProperty.Small;
128                    }
129                    if (getPropertyProportion(AminoAcidProperty.Hydrophobic) > percent) {
130                            percent = getPropertyProportion(AminoAcidProperty.Hydrophobic);
131                            property = AminoAcidProperty.Hydrophobic;
132                    }
133                    if (getPropertyProportion(AminoAcidProperty.Polar) > percent) {
134                            percent = getPropertyProportion(AminoAcidProperty.Polar);
135                            property = AminoAcidProperty.Polar;
136                    }
137                    if (getPropertyProportion(AminoAcidProperty.Positive) > percent) {
138                            percent = getPropertyProportion(AminoAcidProperty.Positive);
139                            property = AminoAcidProperty.Positive;
140                    }
141                    if (getPropertyProportion(AminoAcidProperty.Negative) > percent) {
142                            percent = getPropertyProportion(AminoAcidProperty.Negative);
143                            property = AminoAcidProperty.Negative;
144                    }
145                    if (getPropertyProportion(AminoAcidProperty.Tiny) > percent) {
146                            percent = getPropertyProportion(AminoAcidProperty.Tiny);
147                            property = AminoAcidProperty.Tiny;
148                    }
149                    if (getPropertyProportion(AminoAcidProperty.Aliphatic) > percent) {
150                            percent = getPropertyProportion(AminoAcidProperty.Aliphatic);
151                            property = AminoAcidProperty.Aliphatic;
152                    }
153                    if (getPropertyProportion(AminoAcidProperty.Aromatic) > percent) {
154                            percent = getPropertyProportion(AminoAcidProperty.Aromatic);
155                            property = AminoAcidProperty.Aromatic;
156                    }
157                    return property;
158            }
159    
160            public String toString() {
161                    StringBuffer s = new StringBuffer("[");
162                    for (int i = 0; i < acids.size(); i++)
163                            s.append(acids.get(i).toString());
164                    s.append("]");
165                    return s.toString();
166            }
167    
168    }