001 package org.jaga.individualRepresentation.proteinLocation;
002
003 import java.util.ArrayList;
004 import org.jaga.definitions.RandomGenerator;
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 ProteinGroup {
035
036 private ArrayList proteins = new ArrayList();
037 private ArrayList groups = new ArrayList();
038 private String name = "<Untitled>";
039
040 public ProteinGroup() {}
041
042 public ProteinGroup(String name) {
043 setName(name);
044 }
045
046 public ProteinGroup(String name, ProteinFileParser parser, String fileName) {
047 setName(name);
048 load(parser, fileName);
049 }
050
051 public void add(Protein prot) {
052 this.proteins.add(prot);
053 }
054
055 public void add(ProteinGroup group) {
056 this.groups.add(group);
057 }
058
059 public Protein getProtein(int index) {
060 return (Protein) proteins.get(index);
061 }
062
063 public int size() {
064 int s = 0;
065 for (int i = 0; i < groups.size(); i++)
066 s += ((ProteinGroup) groups.get(i)).size();
067 s += proteins.size();
068 return s;
069 }
070
071 public void flatten() {
072 while (!groups.isEmpty()) {
073 final int i = groups.size() - 1;
074 ProteinGroup g = (ProteinGroup) groups.remove(i);
075 g.flatten();
076 for (int j = 0; j < g.size(); j++)
077 proteins.add(g.getProtein(j));
078 }
079 }
080
081 public void randomise(RandomGenerator rnd) {
082
083 ArrayList newGroups = new ArrayList(groups.size());
084 while(!groups.isEmpty()) {
085 int i = rnd.nextInt(0, groups.size());
086 ProteinGroup g = (ProteinGroup) groups.remove(i);
087 newGroups.add(g);
088 g.randomise(rnd);
089 }
090 groups = newGroups;
091
092 ArrayList newProteins = new ArrayList(proteins.size());
093 while(!proteins.isEmpty()) {
094 int i = rnd.nextInt(0, proteins.size());
095 Protein p = (Protein) proteins.remove(i);
096 newProteins.add(p);
097 }
098 proteins = newProteins;
099 }
100
101 public int align(final PolypeptidePattern pattern, final int minOverlap) {
102 int aligned = 0;
103 aligned += alignInSubgroups(pattern, minOverlap);
104 aligned += alignInProteins(pattern, minOverlap);
105 return aligned;
106 }
107
108 private int alignInProteins(final PolypeptidePattern pattern, final int minOverlap) {
109 int aligned = 0;
110 for (int i = 0; i < proteins.size(); i++) {
111 if (alignProtein((Protein) proteins.get(i), pattern, minOverlap))
112 aligned++;
113 }
114 return aligned;
115 }
116
117 private boolean alignProtein(final Protein protein, final PolypeptidePattern pattern, final int minOverlap) {
118 /*
119 int pStop = protein.getLength() - pattern.getLength();
120 for (int p = 0; p <= pStop; p++) {
121 if (pattern.matches(protein, p))
122 return true;
123 }
124 return false;
125 */
126 // Use this speed hack:
127 final int patLen = pattern.getLength();
128 int pStart = -(patLen - minOverlap);
129 if (pStart > 0)
130 pStart = 0;
131 final int pStop = protein.getLength() - minOverlap;
132 for (int p = pStart; p <= pStop; p++) {
133 if (pattern.matchesPerformanceHack(protein.getSequenceReferencePerformanceHack(), p))
134 return true;
135 }
136 return false;
137 }
138
139 private int alignInSubgroups(final PolypeptidePattern pattern, final int minOverlap) {
140 int aligned = 0;
141 for (int i = 0; i < groups.size(); i++)
142 aligned += ((ProteinGroup) groups.get(i)).align(pattern, minOverlap);
143 return aligned;
144 }
145
146 public void load(ProteinFileParser parser, String fileName) {
147 try {
148 parser.readFromFile(fileName, this);
149 } catch (java.io.IOException e) {
150 throw new RuntimeException(e.getMessage());
151 }
152 }
153
154 public String getName() {
155 return name;
156 }
157
158 public void setName(String name) {
159 if (null == name)
160 this.name = "<Untitled>";
161 else
162 this.name = name;
163 }
164
165 }