001 package org.jaga.individualRepresentation.proteinLocation;
002
003
004 /**
005 * TODO: Complete these comments.
006 *
007 * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
008 *
009 * <p><u>Company:</u> University College London and JAGA.Org
010 * (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
011 * </p>
012 *
013 * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
014 * This program is free software; you can redistribute it and/or modify
015 * it under the terms of the GNU General Public License as published by
016 * the Free Software Foundation, ONLY if you include a note of the original
017 * author(s) in any redistributed/modified copy.<br/>
018 * This program is distributed in the hope that it will be useful,
019 * but WITHOUT ANY WARRANTY; without even the implied warranty of
020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021 * GNU General Public License for more details.<br/>
022 * You should have received a copy of the GNU General Public License
023 * along with this program; if not, write to the Free Software
024 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025 * or see http://www.gnu.org/licenses/gpl.html</p>
026 *
027 * @author Greg Paperin (greg@jaga.org)
028 *
029 * @version JAGA public release 1.0 beta
030 */
031
032 public class Protein {
033
034 private String name = null;
035 private AminoAcid [] sequence = null;
036
037 public Protein() {
038 this.name = null;
039 this.sequence = new AminoAcid[0];
040 }
041
042 public Protein(String name, String sequence) {
043 this.name = name;
044 sequence = sequence.trim();
045 this.sequence = new AminoAcid[sequence.length()];
046 for (int i = 0; i < sequence.length(); i++) {
047 String code = sequence.substring(i, i + 1);
048 AminoAcid aminoAcid = AminoAcidFactory.getResidueByCode(code);
049 if (null == aminoAcid)
050 throw new IllegalArgumentException("Could not construct an amino acid with the code '"
051 + code + "'.");
052 this.sequence[i] = aminoAcid;
053 }
054 }
055
056 public Protein(String name, AminoAcid [] sequence) {
057 this.name = name;
058 this.sequence = new AminoAcid[sequence.length];
059 System.arraycopy(sequence, 0, this.sequence, 0, sequence.length);
060 }
061
062 public String getName() {
063 return this.name;
064 }
065
066 public String getSequenceString() {
067 StringBuffer s = new StringBuffer(sequence.length);
068 for (int i = 0; i < sequence.length; i++)
069 s.append(sequence[i].toString());
070 return s.toString();
071 }
072
073 public AminoAcid [] getSequence() {
074 AminoAcid [] seq = new AminoAcid[sequence.length];
075 System.arraycopy(this.sequence, 0, seq, 0, this.sequence.length);
076 return seq;
077 }
078
079 public AminoAcid [] getSequenceReferencePerformanceHack() {
080 return sequence;
081 }
082
083 public AminoAcid getResidue(int index) {
084 return this.sequence[index];
085 }
086
087 public int getLength() {
088 return sequence.length;
089 }
090
091 /*
092 public Protein.Iterator getIterator() {
093 return new Protein.Iterator(this, 0);
094 }
095
096 public Protein.Iterator getIterator(int startIndex) {
097 return new Protein.Iterator(this, startIndex);
098 }
099 */
100
101 public String toString() {
102 StringBuffer s;
103 if (null != name) {
104 s = new StringBuffer(name);
105 s.append(" : [");
106 } else {
107 s = new StringBuffer("[");
108 }
109 s.append(getSequenceString());
110 s.append("]");
111 return s.toString();
112 }
113
114 /*
115 public class Iterator implements java.util.Iterator {
116
117 private Protein prot = null;
118 private int index = 0;
119
120 private Iterator() {
121 throw new UnsupportedOperationException("Use Iterator(Protein prot).");
122 }
123
124 private Iterator(Protein prot, int startIndex) {
125 this.prot = prot;
126 this.index = startIndex;
127 }
128
129 public boolean hasNext() {
130 return index < prot.getLength();
131 }
132
133 public Object next() {
134 return prot.getResidue(index++);
135 }
136
137 public AminoAcid nextResidue() {
138 return prot.getResidue(index++);
139 }
140
141 public void remove() {
142 throw new UnsupportedOperationException("Protein.Iterator cannot remove residues.");
143 }
144 }
145 */
146 }