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