001    package org.jaga.individualRepresentation.proteinLocation;
002    
003    import java.util.ArrayList;
004    
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 PolypeptidePattern {
035    
036            /*
037            Structure (in order of preference):
038            - exact amino acid to match: e.g. ...-M-T-P-...
039            - some amino posessing a given property to match: ...-<Small>-<Hydrophobic>-<Polar>-<Positive>-<Negative>-<Tiny>-<Aliphatic>-<Aromatic>-...
040            - some amino acid out of a given list to match: e.g. ...-[AG]-[DE]-[KR]-...
041            - any amino acid matches: ?
042            */
043    
044            private ArrayList items = new ArrayList();
045    
046            public PolypeptidePattern() {}
047    
048            public void clearAllItems() {
049                    items.clear();
050            }
051    
052            public int getLength() {
053                    return items.size();
054            }
055    
056            public PolypeptidePatternItem getItem(int index) {
057                    return (PolypeptidePatternItem) items.get(index);
058            }
059    
060            public void removeItem(int index) {
061                    items.remove(index);
062            }
063    
064            public void replaceItem(int index, PolypeptidePatternItem item) {
065                    if (null == item)
066                            throw new NullPointerException("Debug!");
067                    if (item instanceof AminoAcidGroup && ((AminoAcidGroup) item).size() == 0)
068                            throw new RuntimeException("Debug!");
069                    items.set(index, item);
070            }
071    
072            public void insertItem(int index, PolypeptidePatternItem item) {
073                    if (null == item)
074                            throw new NullPointerException("Debug!");
075                    if (item instanceof AminoAcidGroup && ((AminoAcidGroup) item).size() == 0)
076                            throw new RuntimeException("Debug!");
077                    items.add(index, item);
078            }
079    
080            public void insertItem(PolypeptidePatternItem item) {
081                    if (null == item)
082                            throw new NullPointerException("Debug!");
083                    if (item instanceof AminoAcidGroup && ((AminoAcidGroup) item).size() == 0)
084                            throw new RuntimeException("Debug!");
085                    items.add(item);
086            }
087    
088            public boolean matches(Protein prot, int fromLocation) {
089                    return false;
090                    /* BRING UP DO DATE WITH THE FAST VERSION BEFORE UNCOMMENTING!!
091                    int protLen = prot.getLength();
092                    int patternLen = items.size();
093    
094                    if (protLen - patternLen < fromLocation)
095                            return false;
096    
097                    int location = fromLocation;
098                    PolypeptidePatternItem patItem;
099                    AminoAcid protResidue;
100                    for (int i = 0; i < patternLen; i++, location++) {
101                            patItem = (PolypeptidePatternItem) items.get(i);
102                            protResidue = prot.getResidue(location);
103                            if (patItem.doesntMatch(protResidue))
104                                    return false;
105                    }
106                    return true;
107                    */
108            }
109    
110            public boolean matchesPerformanceHack(AminoAcid [] sequence, int fromLocation) {
111    
112                    final int patternLen = items.size();
113                    final int protLen = sequence.length;
114                    PolypeptidePatternItem patItem;
115                    int location = 0;
116                    int i = (fromLocation < 0 ? -fromLocation : 0);
117                    while (i < patternLen && location < protLen) {
118                            patItem = (PolypeptidePatternItem) items.get(i++);
119                            if (patItem.doesntMatch(sequence[location++]))
120                                    return false;
121                    }
122                    return true;
123            }
124    
125            public String toString() {
126                    StringBuffer s = new StringBuffer("{");
127                    for (int i = 0; i < items.size(); i++) {
128                            if (i > 0)
129                                    s.append("-");
130                            s.append(items.get(i).toString());
131                    }
132                    s.append("}");
133                    return s.toString();
134            }
135    
136    }