JAL-2291 SequenceI.getInsertionsAsBits for convenient logical operations
[jalview.git] / src / jalview / datamodel / SequenceI.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import java.util.BitSet;
24 import java.util.List;
25 import java.util.Vector;
26
27 import fr.orsay.lri.varna.models.rna.RNA;
28
29 /**
30  * Methods for manipulating a sequence, its metadata and related annotation in
31  * an alignment or dataset.
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public interface SequenceI extends ASequenceI
37 {
38   /**
39    * Set the display name for the sequence
40    * 
41    * @param name
42    */
43   public void setName(String name);
44
45   /**
46    * Get the display name
47    */
48   public String getName();
49
50   /**
51    * Set start position of first non-gapped symbol in sequence
52    * 
53    * @param start
54    *          new start position
55    */
56   public void setStart(int start);
57
58   /**
59    * get start position of first non-gapped residue in sequence
60    * 
61    * @return
62    */
63   public int getStart();
64
65   /**
66    * get the displayed id of the sequence
67    * 
68    * @return true means the id will be returned in the form
69    *         DisplayName/Start-End
70    */
71   public String getDisplayId(boolean jvsuffix);
72
73   /**
74    * set end position for last residue in sequence
75    * 
76    * @param end
77    */
78   public void setEnd(int end);
79
80   /**
81    * get end position for last residue in sequence getEnd()>getStart() unless
82    * sequence only consists of gap characters
83    * 
84    * @return
85    */
86   public int getEnd();
87
88   /**
89    * @return length of sequence including gaps
90    * 
91    */
92   public int getLength();
93
94   /**
95    * Replace the sequence with the given string
96    * 
97    * @param sequence
98    *          new sequence string
99    */
100   public void setSequence(String sequence);
101
102   /**
103    * @return sequence as string
104    */
105   public String getSequenceAsString();
106
107   /**
108    * get a range on the sequence as a string
109    * 
110    * @param start
111    *          position relative to start of sequence including gaps (from 0)
112    * @param end
113    *          position relative to start of sequence including gaps (from 0)
114    * 
115    * @return String containing all gap and symbols in specified range
116    */
117   public String getSequenceAsString(int start, int end);
118
119   /**
120    * Get the sequence as a character array
121    * 
122    * @return seqeunce and any gaps
123    */
124   public char[] getSequence();
125
126   /**
127    * get stretch of sequence characters in an array
128    * 
129    * @param start
130    *          absolute index into getSequence()
131    * @param end
132    *          exclusive index of last position in segment to be returned.
133    * 
134    * @return char[max(0,end-start)];
135    */
136   public char[] getSequence(int start, int end);
137
138   /**
139    * create a new sequence object with a subsequence of this one but sharing the
140    * same dataset sequence
141    * 
142    * @param start
143    *          int index for start position (base 0, inclusive)
144    * @param end
145    *          int index for end position (base 0, exclusive)
146    * 
147    * @return SequenceI
148    * @note implementations may use getSequence to get the sequence data
149    */
150   public SequenceI getSubSequence(int start, int end);
151
152   /**
153    * get the i'th character in this sequence's local reference frame (ie from
154    * 0-number of characters lying from start-end)
155    * 
156    * @param i
157    *          index
158    * @return character or ' '
159    */
160   public char getCharAt(int i);
161
162   /**
163    * DOCUMENT ME!
164    * 
165    * @param desc
166    *          DOCUMENT ME!
167    */
168   public void setDescription(String desc);
169
170   /**
171    * DOCUMENT ME!
172    * 
173    * @return DOCUMENT ME!
174    */
175   public String getDescription();
176
177   /**
178    * Return the alignment column for a sequence position
179    * 
180    * @param pos
181    *          lying from start to end
182    * 
183    * @return aligned column for residue (0 if residue is upstream from
184    *         alignment, -1 if residue is downstream from alignment) note.
185    *         Sequence object returns sequence.getEnd() for positions upstream
186    *         currently. TODO: change sequence for
187    *         assert(findIndex(seq.getEnd()+1)==-1) and fix incremental bugs
188    * 
189    */
190   public int findIndex(int pos);
191
192   /**
193    * Returns the sequence position for an alignment position.
194    * 
195    * @param i
196    *          column index in alignment (from 0..<length)
197    * 
198    * @return TODO: JAL-2562 - residue number for residue (left of and) nearest
199    *         ith column
200    */
201   public int findPosition(int i);
202
203   /**
204    * Returns an int array where indices correspond to each residue in the
205    * sequence and the element value gives its position in the alignment
206    * 
207    * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
208    *         residues in SequenceI object
209    */
210   public int[] gapMap();
211
212   /**
213    * Returns an int array where indices correspond to each position in sequence
214    * char array and the element value gives the result of findPosition for that
215    * index in the sequence.
216    * 
217    * @return int[SequenceI.getLength()]
218    */
219   public int[] findPositionMap();
220
221   /**
222    * Answers true if the sequence is composed of amino acid characters. Note
223    * that implementations may use heuristic methods which are not guaranteed to
224    * give the biologically 'right' answer.
225    * 
226    * @return
227    */
228   public boolean isProtein();
229
230   /**
231    * Delete a range of aligned sequence columns, creating a new dataset sequence
232    * if necessary and adjusting start and end positions accordingly.
233    * 
234    * @param i
235    *          first column in range to delete (inclusive)
236    * @param j
237    *          last column in range to delete (exclusive)
238    */
239   public void deleteChars(int i, int j);
240
241   /**
242    * DOCUMENT ME!
243    * 
244    * @param i
245    *          alignment column number
246    * @param c
247    *          character to insert
248    */
249   public void insertCharAt(int i, char c);
250
251   /**
252    * insert given character at alignment column position
253    * 
254    * @param position
255    *          alignment column number
256    * @param count
257    *          length of insert
258    * @param ch
259    *          character to insert
260    */
261   public void insertCharAt(int position, int count, char ch);
262
263   /**
264    * Gets array holding sequence features associated with this sequence. The
265    * array may be held by the sequence's dataset sequence if that is defined.
266    * 
267    * @return hard reference to array
268    */
269   public SequenceFeature[] getSequenceFeatures();
270
271   /**
272    * Replaces the array of sequence features associated with this sequence with
273    * a new array reference. If this sequence has a dataset sequence, then this
274    * method will update the dataset sequence's feature array
275    * 
276    * @param features
277    *          New array of sequence features
278    */
279   public void setSequenceFeatures(SequenceFeature[] features);
280
281   /**
282    * DOCUMENT ME!
283    * 
284    * @param id
285    *          DOCUMENT ME!
286    */
287   public void setPDBId(Vector<PDBEntry> ids);
288
289   /**
290    * Returns a list
291    * 
292    * @return DOCUMENT ME!
293    */
294   public Vector<PDBEntry> getAllPDBEntries();
295
296   /**
297    * Adds the entry to the *normalised* list of PDBIds.
298    * 
299    * If a PDBEntry is passed with the same entry.getID() string as one already
300    * in the list, or one is added that appears to be the same but has a chain ID
301    * appended, then the existing PDBEntry will be updated with the new
302    * attributes instead, unless the entries have distinct chain codes or
303    * associated structure files.
304    * 
305    * @param entry
306    * @return true if the entry was added, false if updated
307    */
308   public boolean addPDBId(PDBEntry entry);
309
310   /**
311    * update the list of PDBEntrys to include any DBRefEntrys citing structural
312    * databases
313    * 
314    * @return true if PDBEntry list was modified
315    */
316   public boolean updatePDBIds();
317
318   public String getVamsasId();
319
320   public void setVamsasId(String id);
321
322   /**
323    * set the array of Database references for the sequence.
324    * 
325    * @param dbs
326    * @deprecated - use is discouraged since side-effects may occur if DBRefEntry
327    *             set are not normalised.
328    */
329   @Deprecated
330   public void setDBRefs(DBRefEntry[] dbs);
331
332   public DBRefEntry[] getDBRefs();
333
334   /**
335    * add the given entry to the list of DBRefs for this sequence, or replace a
336    * similar one if entry contains a map object and the existing one doesnt.
337    * 
338    * @param entry
339    */
340   public void addDBRef(DBRefEntry entry);
341
342   /**
343    * Adds the given sequence feature and returns true, or returns false if it is
344    * already present on the sequence
345    * 
346    * @param sf
347    * @return
348    */
349   public boolean addSequenceFeature(SequenceFeature sf);
350
351   public void deleteFeature(SequenceFeature sf);
352
353   public void setDatasetSequence(SequenceI seq);
354
355   public SequenceI getDatasetSequence();
356
357   /**
358    * Returns a new array containing this sequence's annotations, or null.
359    */
360   public AlignmentAnnotation[] getAnnotation();
361
362   /**
363    * Returns true if this sequence has the given annotation (by object
364    * identity).
365    */
366   public boolean hasAnnotation(AlignmentAnnotation ann);
367
368   /**
369    * Add the given annotation, if not already added, and set its sequence ref to
370    * be this sequence. Does nothing if this sequence's annotations already
371    * include this annotation (by identical object reference).
372    */
373   public void addAlignmentAnnotation(AlignmentAnnotation annotation);
374
375   public void removeAlignmentAnnotation(AlignmentAnnotation annotation);
376
377   /**
378    * Derive a sequence (using this one's dataset or as the dataset)
379    * 
380    * @return duplicate sequence with valid dataset sequence
381    */
382   public SequenceI deriveSequence();
383
384   /**
385    * set the array of associated AlignmentAnnotation for this sequenceI
386    * 
387    * @param revealed
388    */
389   public void setAlignmentAnnotation(AlignmentAnnotation[] annotation);
390
391   /**
392    * Get one or more alignment annotations with a particular label.
393    * 
394    * @param label
395    *          string which each returned annotation must have as a label.
396    * @return null or array of annotations.
397    */
398   public AlignmentAnnotation[] getAnnotation(String label);
399
400   /**
401    * Returns a (possibly empty) list of any annotations that match on given
402    * calcId (source) and label (type). Null values do not match.
403    * 
404    * @param calcId
405    * @param label
406    */
407   public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
408           String label);
409
410   /**
411    * create a new dataset sequence (if necessary) for this sequence and sets
412    * this sequence to refer to it. This call will move any features or
413    * references on the sequence onto the dataset. It will also make a duplicate
414    * of existing annotation rows for the dataset sequence, rather than relocate
415    * them in order to preserve external references (since 2.8.2).
416    * 
417    * @return dataset sequence for this sequence
418    */
419   public SequenceI createDatasetSequence();
420
421   /**
422    * Transfer any database references or annotation from entry under a sequence
423    * mapping. <br/>
424    * <strong>Note: DOES NOT transfer sequence associated alignment annotation
425    * </strong><br/>
426    * 
427    * @param entry
428    * @param mp
429    *          null or mapping from entry's numbering to local start/end
430    */
431   public void transferAnnotation(SequenceI entry, Mapping mp);
432
433   /**
434    * @param index
435    *          The sequence index in the MSA
436    */
437   public void setIndex(int index);
438
439   /**
440    * @return The index of the sequence in the alignment
441    */
442   public int getIndex();
443
444   /**
445    * @return The RNA of the sequence in the alignment
446    */
447
448   public RNA getRNA();
449
450   /**
451    * @param rna
452    *          The RNA.
453    */
454   public void setRNA(RNA rna);
455
456   /**
457    * 
458    * @return list of insertions (gap characters) in sequence
459    */
460   public List<int[]> getInsertions();
461
462   /**
463    * Given a pdbId String, return the equivalent PDBEntry if available in the
464    * given sequence
465    * 
466    * @param pdbId
467    * @return
468    */
469   public PDBEntry getPDBEntry(String pdbId);
470
471   /**
472    * Get all primary database/accessions for this sequence's data. These
473    * DBRefEntry are expected to resolve to a valid record in the associated
474    * external database, either directly or via a provided 1:1 Mapping.
475    * 
476    * @return just the primary references (if any) for this sequence, or an empty
477    *         list
478    */
479   public List<DBRefEntry> getPrimaryDBRefs();
480
481   /**
482    * 
483    * @return BitSet corresponding to index [0,length) where Comparison.isGap()
484    *         returns true.
485    */
486   BitSet getInsertionsAsBits();
487 }