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