001    package org.jaga.individualRepresentation.proteinLocation;
002    
003    import java.util.StringTokenizer;
004    import org.jaga.definitions.*;
005    
006    /**
007     * TODO: Complete these comments.
008     *
009     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
010     *
011     * <p><u>Company:</u> University College London and JAGA.Org
012     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
013     * </p>
014     *
015     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
016     *    This program is free software; you can redistribute it and/or modify
017     *    it under the terms of the GNU General Public License as published by
018     *    the Free Software Foundation, ONLY if you include a note of the original
019     *    author(s) in any redistributed/modified copy.<br/>
020     *    This program is distributed in the hope that it will be useful,
021     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
022     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023     *    GNU General Public License for more details.<br/>
024     *    You should have received a copy of the GNU General Public License
025     *    along with this program; if not, write to the Free Software
026     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
027     *    or see http://www.gnu.org/licenses/gpl.html</p>
028     *
029     * @author Greg Paperin (greg@jaga.org)
030     *
031     * @version JAGA public release 1.0 beta
032     */
033    
034    public class ProteinLocationClassifierFactory implements IndividualsFactory {
035    
036            private int maxPatternLength = 100;
037            private int aminoAcidChance = 2;
038            private int groupChance = 15;
039            private int propertyChance = 15;
040            private int singleGapChance = 2;
041            private int maxGroupLength = 20;
042            private String classifierName = "<untitled>";
043    
044            public ProteinLocationClassifierFactory() {}
045    
046            public ProteinLocationClassifierFactory(int maxPatternLength, int aminoAcidChance,
047                                                                                            int groupChance, int propertyChance,
048                                                                                            int singleGapChance, int maxGroupLength,
049                                                                                            String classifierName) {
050                    this.maxPatternLength = maxPatternLength;
051                    this.aminoAcidChance = aminoAcidChance;
052                    this.groupChance = groupChance;
053                    this.propertyChance = propertyChance;
054                    this.singleGapChance = singleGapChance;
055                    this.maxGroupLength = maxGroupLength;
056                    this.classifierName = classifierName;
057            }
058    
059            public PolypeptidePattern createRandomPattern(GAParameterSet params) {
060                    int len = params.getRandomGenerator().nextInt(1, maxPatternLength + 1);
061                    return createRandomPattern(len, params);
062            }
063    
064            public PolypeptidePattern createRandomPattern(int length, GAParameterSet params) {
065                    PolypeptidePattern pattern = new PolypeptidePattern();
066                    for (int i = 0; i < length; i++)
067                            pattern.insertItem(i, createRandomPatternItem(params));
068                    return pattern;
069            }
070    
071            public PolypeptidePatternItem createRandomPatternItem(GAParameterSet params) {
072                    RandomGenerator rnd = params.getRandomGenerator();
073                    int dice = rnd.nextInt(0, aminoAcidChance + groupChance + propertyChance + singleGapChance);
074    
075                    // amino acid:
076                    if (dice < aminoAcidChance) {
077                            return AminoAcidFactory.getRandomResidue(params);
078                    }
079    
080                    // group:
081                    if (dice < aminoAcidChance + groupChance) {
082                            AminoAcidGroup group = new AminoAcidGroup();
083                            int gLen = rnd.nextInt(1, maxGroupLength + 1);
084                            for (int i = 0; i < gLen; i++)
085                                    group.addResidue(AminoAcidFactory.getRandomResidue(params));
086                            return group;
087                    }
088    
089                    // property:
090                    if (dice < aminoAcidChance + groupChance + propertyChance) {
091                            return AminoAcidPropertyFactory.getRandomProperty(params);
092                    }
093    
094                    // single gap:
095                    if (dice < aminoAcidChance + groupChance + propertyChance + singleGapChance) {
096                            return SingleGap.getInstance();
097                    }
098    
099                    throw new Error("Should never come to execute this line. Have fun debugging!");
100            }
101    
102            public Individual createDefaultIndividual(GAParameterSet params) {
103                    PolypeptidePattern p = new PolypeptidePattern();
104                    p.insertItem(SingleGap.getInstance());
105                    return createSpecificIndividual(p, params);
106            }
107    
108            public Individual createRandomIndividual(GAParameterSet params) {
109                    PolypeptidePattern p = createRandomPattern(params);
110                    p.insertItem(SingleGap.getInstance());
111                    return createSpecificIndividual(p, params);
112            }
113    
114            public Individual createSpecificIndividual(Object init, GAParameterSet params) {
115                    if (null == init)
116                            throw new NullPointerException("Cannot have an null initialisation object.");
117    
118                    if (init instanceof ProteinLocationClassifier)
119                            return createSpecificIndividual((ProteinLocationClassifier) init, params);
120    
121                    if (init instanceof PolypeptidePattern)
122                            return createSpecificIndividual((PolypeptidePattern) init, params);
123    
124                    if (init instanceof String)
125                            return createSpecificIndividual((String) init, params);
126    
127                    throw new ClassCastException("Cannot have an initialisation object of class"
128                                                                                       + init.getClass().getName());
129            }
130    
131            public Individual createSpecificIndividual(ProteinLocationClassifier init, GAParameterSet params) {
132                    return createSpecificIndividual(init.getPattern(), params);
133            }
134    
135            public Individual createSpecificIndividual(PolypeptidePattern init, GAParameterSet params) {
136                    PolypeptidePattern p = new PolypeptidePattern();
137                    for (int i = 0; i < init.getLength(); i++) {
138                            PolypeptidePatternItem oIt = init.getItem(i);
139                            PolypeptidePatternItem nIt;
140                            if (oIt instanceof AminoAcidGroup) {
141                                    nIt = new AminoAcidGroup();
142                                    for (int j = 0; j < ((AminoAcidGroup) oIt).size(); j++)
143                                            ((AminoAcidGroup) nIt).addResidue(((AminoAcidGroup) oIt).getResidue(j));
144                            } else
145                                    nIt = oIt;
146                            p.insertItem(nIt);
147                    }
148                    return new ProteinLocationClassifier(p, classifierName);
149            }
150    
151            public Individual createSpecificIndividual(String init, GAParameterSet params) {
152                    PolypeptidePattern pattern = new PolypeptidePattern();
153                    StringTokenizer tok = new StringTokenizer(init, "-");
154                    while (tok.hasMoreTokens()) {
155                            String str = tok.nextToken();
156    
157                            if (str.charAt(0) == '?') {
158                                    pattern.insertItem(SingleGap.getInstance());
159                                    //System.out.println("Parsed: " + pattern.getItem(pattern.getLength()-1));
160                                    continue;
161                            }
162    
163                            if (str.charAt(0) == '<') {
164                                    String s = str.substring(1, str.length() - 1);
165                                    pattern.insertItem(AminoAcidPropertyFactory.getPropertyByName(s));
166                                    //System.out.println("Parsed: " + pattern.getItem(pattern.getLength()-1));
167                                    continue;
168                            }
169    
170                            if (Character.isLetter(str.charAt(0))) {
171                                    pattern.insertItem(AminoAcidFactory.getResidueByCode(str.substring(0, 1)));
172                                    //System.out.println("Parsed: " + pattern.getItem(pattern.getLength()-1));
173                                    continue;
174                            }
175    
176                            if (str.charAt(0) == '[') {
177                                    AminoAcidGroup g = new AminoAcidGroup();
178                                    for (int p = 1; str.charAt(p) != ']'; p++)
179                                            g.addResidue(AminoAcidFactory.getResidueByCode(str.substring(p, p+1)));
180                                    pattern.insertItem(g);
181                                    //System.out.println("Parsed: " + pattern.getItem(pattern.getLength()-1));
182                                    continue;
183                            }
184    
185                            throw new RuntimeException("Parse error!");
186                    }
187                    return createSpecificIndividual(pattern, params);
188            }
189    
190            public int getAminoAcidChance() {
191                    return aminoAcidChance;
192            }
193    
194            public void setAminoAcidChance(int aminoAcidChance) {
195                    this.aminoAcidChance = aminoAcidChance;
196            }
197    
198            public int getGroupChance() {
199                    return groupChance;
200            }
201    
202            public void setGroupChance(int groupChance) {
203                    this.groupChance = groupChance;
204            }
205    
206            public int getMaxGroupLength() {
207                    return maxGroupLength;
208            }
209    
210            public void setMaxGroupLength(int maxGroupLength) {
211                    this.maxGroupLength = maxGroupLength;
212            }
213    
214            public int getMaxPatternLength() {
215                    return maxPatternLength;
216            }
217    
218            public void setMaxPatternLength(int maxPatternLength) {
219                    this.maxPatternLength = maxPatternLength;
220            }
221    
222            public int getPropertyChance() {
223                    return propertyChance;
224            }
225    
226            public void setPropertyChance(int propertyChance) {
227                    this.propertyChance = propertyChance;
228            }
229    
230            public int getSingleGapChance() {
231                    return singleGapChance;
232            }
233    
234            public void setSingleGapChance(int singleGapChance) {
235                    this.singleGapChance = singleGapChance;
236            }
237    
238            public String getClassifierName() {
239                    return classifierName;
240            }
241    
242            public void setClassifierName(String classifierName) {
243                    this.classifierName = classifierName;
244            }
245    
246    }