d7f2b9f1e3a264117b9b9bbb070f19cc6d35e9b0
[jalview.git] / src / jalview / datamodel / Sequence.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 \r
20 package jalview.datamodel;\r
21 \r
22 import jalview.analysis.*;\r
23 import java.awt.*;\r
24 import java.util.*;\r
25 import MCview.*;\r
26 \r
27 \r
28 public class Sequence implements SequenceI\r
29 {\r
30   protected String name;\r
31   protected String sequence;\r
32   protected String description;\r
33   protected int start;\r
34   protected int end;\r
35   protected String displayId;\r
36   protected Color color = Color.white;\r
37   String pdbId;\r
38 \r
39    public Vector sequenceFeatures = new Vector();\r
40    public void setSequenceFeatures(Vector v)\r
41    {\r
42      sequenceFeatures = v;\r
43    }\r
44 \r
45    public Vector getSequenceFeatures()\r
46    {return sequenceFeatures; }\r
47 \r
48    public void setPDBId(String id)\r
49    {\r
50      pdbId = id;\r
51    }\r
52    public String getPDBId()\r
53    {\r
54      return pdbId;\r
55    }\r
56 \r
57 \r
58   public Sequence(String name, String sequence, int start, int end)\r
59   {\r
60 \r
61     this.name     = name;\r
62     this.sequence = sequence;\r
63     this.start    = start;\r
64     this.end      = end;\r
65 \r
66     setDisplayId();\r
67 \r
68   }\r
69 \r
70   public Sequence(String name,String sequence) {\r
71     this(name,sequence,1,sequence.length());\r
72   }\r
73   public Sequence(SequenceI seq) {\r
74     this(seq.getName(),seq.getSequence(),seq.getStart(),seq.getEnd());\r
75   }\r
76   public String getDisplayId() {\r
77     return displayId;\r
78   }\r
79   public void setDisplayId() {\r
80     displayId = name + "/" + start + "-" + end;\r
81   }\r
82   public void setName(String name) {\r
83     this.name = name;\r
84     setDisplayId();\r
85   }\r
86   public String getName() {\r
87     return this.name;\r
88   }\r
89   public void setStart(int start) {\r
90     this.start = start;\r
91     setDisplayId();\r
92   }\r
93   public int getStart() {\r
94     return this.start;\r
95   }\r
96   public void setEnd(int end) {\r
97     this.end = end;\r
98     setDisplayId();\r
99   }\r
100   public int getEnd() {\r
101     return this.end;\r
102   }\r
103   public int getLength() {\r
104     return this.sequence.length();\r
105   }\r
106   public void setSequence(String seq) {\r
107     this.sequence = seq;\r
108   }\r
109   public String getSequence() {\r
110     return this.sequence;\r
111   }\r
112   public String getSequence(int start,int end) {\r
113     // JBPNote - left to user to pad the result here (TODO:Decide on this policy)\r
114     if(start>=sequence.length())\r
115       return "";\r
116 \r
117     if(end>=sequence.length())\r
118       end = sequence.length();\r
119 \r
120     return this.sequence.substring(start,end);\r
121   }\r
122 \r
123   public char getCharAt(int i) {\r
124     if (i < sequence.length()) {\r
125       return sequence.charAt(i);\r
126     } else {\r
127       return ' ';\r
128     }\r
129   }\r
130   public void setDescription(String desc) {\r
131     this.description = desc;\r
132   }\r
133   public String getDescription() {\r
134     return this.description;\r
135   }\r
136 \r
137   public int findIndex(int pos) {\r
138     // returns the alignment position for a residue\r
139     int j = start;\r
140     int i = 0;\r
141 \r
142     while (i< sequence.length() && j <= end && j <= pos) {\r
143 \r
144       char c = sequence.charAt(i);\r
145 \r
146       if (!jalview.util.Comparison.isGap((c)))\r
147         j++;\r
148 \r
149       i++;\r
150     }\r
151     if (j == end && j < pos)\r
152       return end+1;\r
153     else\r
154       return i;\r
155 \r
156   }\r
157 \r
158   public int findPosition(int i) {\r
159     // Returns the sequence position for an alignment position\r
160     int j   = 0;\r
161     int pos = start;\r
162 \r
163     while (j < i && j<sequence.length())\r
164     {\r
165       char c = sequence.charAt(j);\r
166       if (!jalview.util.Comparison.isGap((c)))\r
167         pos++;\r
168 \r
169       j++;\r
170     }\r
171     return pos;\r
172   }\r
173 \r
174   public int[] gapMap() {\r
175     // Returns an int array giving the position of each residue in the sequence in the alignment\r
176     String seq = jalview.analysis.AlignSeq.extractGaps("-. ",sequence);\r
177     int[] map = new int[seq.length()];\r
178     int j=0;\r
179     int p=0;\r
180     while (j<sequence.length()) {\r
181       if (!jalview.util.Comparison.isGap(sequence.charAt(j))) {\r
182         map[p++]=j;\r
183       }\r
184       j++;\r
185     }\r
186     return map;\r
187   }\r
188 \r
189   public void deleteCharAt(int i)\r
190   {\r
191     if (i>=sequence.length())\r
192       return;\r
193     sequence = sequence.substring(0,i) + sequence.substring(i+1);\r
194   }\r
195 \r
196   public void deleteChars(int i, int j)\r
197   {\r
198     if (i>=sequence.length())\r
199       return;\r
200     if (j>=sequence.length())\r
201       sequence = sequence.substring(0,i);\r
202     else\r
203       sequence = sequence.substring(0,i) + sequence.substring(j);\r
204   }\r
205 \r
206   public void insertCharAt(int i, char c)\r
207   {\r
208     insertCharAt(i,c,true);\r
209   }\r
210 \r
211   public void insertCharAt(int i,char c,boolean chop) {\r
212 \r
213     String tmp = new String(sequence);\r
214 \r
215     if (i < sequence.length()) {\r
216       sequence = tmp.substring(0,i) + String.valueOf(c) + tmp.substring(i);\r
217     } else {\r
218       // JBPNote : padding char at end of sequence. We'll not get away with this when we insert residues, I bet!\r
219       char[] ch = new char[1+i-sequence.length()];\r
220       for (int j=0, k=ch.length; j<k; j++)\r
221         ch[j] = c;\r
222       sequence = tmp + String.valueOf(ch);\r
223     }\r
224 \r
225   }\r
226 \r
227   public void        setColor(Color c) {\r
228     this.color = c;\r
229   }\r
230 \r
231   public Color       getColor() {\r
232     return color;\r
233   }\r
234 \r
235 }\r