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