3853af9891fcd5efd390bdc95438ffe97d9d3f8f
[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 package jalview.datamodel;\r
20 \r
21 import MCview.*;\r
22 \r
23 import jalview.analysis.*;\r
24 \r
25 import java.awt.*;\r
26 \r
27 import java.util.*;\r
28 \r
29 \r
30 public class Sequence implements SequenceI {\r
31     protected String name;\r
32     protected String sequence;\r
33     protected String description;\r
34     protected int start;\r
35     protected int end;\r
36     protected String displayId;\r
37     protected Color color = Color.white;\r
38     String pdbId;\r
39     public Vector sequenceFeatures = new Vector();\r
40 \r
41     public Sequence(String name, String sequence, int start, int end) {\r
42         this.name = name;\r
43         this.sequence = sequence;\r
44         this.start = start;\r
45         this.end = end;\r
46 \r
47         setDisplayId();\r
48     }\r
49 \r
50     public Sequence(String name, String sequence) {\r
51         this(name, sequence, 1, sequence.length());\r
52     }\r
53 \r
54     public Sequence(SequenceI seq) {\r
55         this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());\r
56     }\r
57 \r
58     public void setSequenceFeatures(Vector v) {\r
59         sequenceFeatures = v;\r
60     }\r
61 \r
62     public Vector getSequenceFeatures() {\r
63         return sequenceFeatures;\r
64     }\r
65 \r
66     public void setPDBId(String id) {\r
67         pdbId = id;\r
68     }\r
69 \r
70     public String getPDBId() {\r
71         return pdbId;\r
72     }\r
73 \r
74     public String getDisplayId() {\r
75         return displayId;\r
76     }\r
77 \r
78     public void setDisplayId() {\r
79         displayId = name + "/" + start + "-" + end;\r
80     }\r
81 \r
82     public void setName(String name) {\r
83         this.name = name;\r
84         setDisplayId();\r
85     }\r
86 \r
87     public String getName() {\r
88         return this.name;\r
89     }\r
90 \r
91     public void setStart(int start) {\r
92         this.start = start;\r
93         setDisplayId();\r
94     }\r
95 \r
96     public int getStart() {\r
97         return this.start;\r
98     }\r
99 \r
100     public void setEnd(int end) {\r
101         this.end = end;\r
102         setDisplayId();\r
103     }\r
104 \r
105     public int getEnd() {\r
106         return this.end;\r
107     }\r
108 \r
109     public int getLength() {\r
110         return this.sequence.length();\r
111     }\r
112 \r
113     public void setSequence(String seq) {\r
114         this.sequence = seq;\r
115     }\r
116 \r
117     public String getSequence() {\r
118         return this.sequence;\r
119     }\r
120 \r
121     public String getSequence(int start, int end) {\r
122         // JBPNote - left to user to pad the result here (TODO:Decide on this policy)\r
123         if (start >= sequence.length()) {\r
124             return "";\r
125         }\r
126 \r
127         if (end >= sequence.length()) {\r
128             end = sequence.length();\r
129         }\r
130 \r
131         return this.sequence.substring(start, end);\r
132     }\r
133 \r
134     public char getCharAt(int i) {\r
135         if (i < sequence.length()) {\r
136             return sequence.charAt(i);\r
137         } else {\r
138             return ' ';\r
139         }\r
140     }\r
141 \r
142     public void setDescription(String desc) {\r
143         this.description = desc;\r
144     }\r
145 \r
146     public String getDescription() {\r
147         return this.description;\r
148     }\r
149 \r
150     public int findIndex(int pos) {\r
151         // returns the alignment position for a residue\r
152         int j = start;\r
153         int i = 0;\r
154 \r
155         while ((i < sequence.length()) && (j <= end) && (j <= pos)) {\r
156             char c = sequence.charAt(i);\r
157 \r
158             if (!jalview.util.Comparison.isGap((c))) {\r
159                 j++;\r
160             }\r
161 \r
162             i++;\r
163         }\r
164 \r
165         if ((j == end) && (j < pos)) {\r
166             return end + 1;\r
167         } else {\r
168             return i;\r
169         }\r
170     }\r
171 \r
172     public int findPosition(int i) {\r
173         // Returns the sequence position for an alignment position\r
174         int j = 0;\r
175         int pos = start;\r
176 \r
177         while ((j < i) && (j < sequence.length())) {\r
178             char c = sequence.charAt(j);\r
179 \r
180             if (!jalview.util.Comparison.isGap((c))) {\r
181                 pos++;\r
182             }\r
183 \r
184             j++;\r
185         }\r
186 \r
187         return pos;\r
188     }\r
189 \r
190     public int[] gapMap() {\r
191         // Returns an int array giving the position of each residue in the sequence in the alignment\r
192         String seq = jalview.analysis.AlignSeq.extractGaps("-. ", sequence);\r
193         int[] map = new int[seq.length()];\r
194         int j = 0;\r
195         int p = 0;\r
196 \r
197         while (j < sequence.length()) {\r
198             if (!jalview.util.Comparison.isGap(sequence.charAt(j))) {\r
199                 map[p++] = j;\r
200             }\r
201 \r
202             j++;\r
203         }\r
204 \r
205         return map;\r
206     }\r
207 \r
208     public void deleteCharAt(int i) {\r
209         if (i >= sequence.length()) {\r
210             return;\r
211         }\r
212 \r
213         sequence = sequence.substring(0, i) + sequence.substring(i + 1);\r
214     }\r
215 \r
216     public void deleteChars(int i, int j) {\r
217         if (i >= sequence.length()) {\r
218             return;\r
219         }\r
220 \r
221         if (j >= sequence.length()) {\r
222             sequence = sequence.substring(0, i);\r
223         } else {\r
224             sequence = sequence.substring(0, i) + sequence.substring(j);\r
225         }\r
226     }\r
227 \r
228     public void insertCharAt(int i, char c) {\r
229         insertCharAt(i, c, true);\r
230     }\r
231 \r
232     public void insertCharAt(int i, char c, boolean chop) {\r
233         String tmp = new String(sequence);\r
234 \r
235         if (i < sequence.length()) {\r
236             sequence = tmp.substring(0, i) + String.valueOf(c) +\r
237                 tmp.substring(i);\r
238         } else {\r
239             // JBPNote : padding char at end of sequence. We'll not get away with this when we insert residues, I bet!\r
240             char[] ch = new char[(1 + i) - sequence.length()];\r
241 \r
242             for (int j = 0, k = ch.length; j < k; j++)\r
243                 ch[j] = c;\r
244 \r
245             sequence = tmp + String.valueOf(ch);\r
246         }\r
247     }\r
248 \r
249     public void setColor(Color c) {\r
250         this.color = c;\r
251     }\r
252 \r
253     public Color getColor() {\r
254         return color;\r
255     }\r
256 }\r