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