d8a54626f36d1680afff9d4b93c3a0322321eba9
[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    */
401   public default SequenceI getRootDatasetSequence()
402   {
403     var sequence = this;
404     while (sequence.getDatasetSequence() != null)
405     {
406       sequence = sequence.getDatasetSequence();
407     }
408     return sequence;
409   }
410
411   /**
412    * Returns a new array containing this sequence's annotations, or null.
413    */
414   public AlignmentAnnotation[] getAnnotation();
415
416   /**
417    * Returns true if this sequence has the given annotation (by object
418    * identity).
419    */
420   public boolean hasAnnotation(AlignmentAnnotation ann);
421
422   /**
423    * Add the given annotation, if not already added, and set its sequence ref to
424    * be this sequence. Does nothing if this sequence's annotations already
425    * include this annotation (by identical object reference).
426    */
427   public void addAlignmentAnnotation(AlignmentAnnotation annotation);
428
429   public void removeAlignmentAnnotation(AlignmentAnnotation annotation);
430
431   /**
432    * Derive a sequence (using this one's dataset or as the dataset)
433    * 
434    * @return duplicate sequence with valid dataset sequence
435    */
436   public SequenceI deriveSequence();
437
438   /**
439    * set the array of associated AlignmentAnnotation for this sequenceI
440    * 
441    * @param revealed
442    */
443   public void setAlignmentAnnotation(AlignmentAnnotation[] annotation);
444
445   /**
446    * Get one or more alignment annotations with a particular label.
447    * 
448    * @param label
449    *          string which each returned annotation must have as a label.
450    * @return null or array of annotations.
451    */
452   public AlignmentAnnotation[] getAnnotation(String label);
453
454   /**
455    * Returns a (possibly empty) list of any annotations that match on given
456    * calcId (source) and label (type). Null values do not match.
457    * 
458    * @param calcId
459    * @param label
460    */
461   public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
462           String label);
463
464   /**
465    * Returns a (possibly empty) list of any annotations that match on given
466    * calcId (source), label (type) and description (observation instance).
467    * Null values do not match.
468    * 
469    * @param calcId
470    * @param label
471    * @param description
472    */
473   public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
474           String label, String description);
475   /**
476    * create a new dataset sequence (if necessary) for this sequence and sets
477    * this sequence to refer to it. This call will move any features or
478    * references on the sequence onto the dataset. It will also make a duplicate
479    * of existing annotation rows for the dataset sequence, rather than relocate
480    * them in order to preserve external references (since 2.8.2).
481    * 
482    * @return dataset sequence for this sequence
483    */
484   public SequenceI createDatasetSequence();
485
486   /**
487    * Transfer any database references or annotation from entry under a sequence
488    * mapping. <br/>
489    * <strong>Note: DOES NOT transfer sequence associated alignment annotation
490    * </strong><br/>
491    * 
492    * @param entry
493    * @param mp
494    *          null or mapping from entry's numbering to local start/end
495    */
496   public void transferAnnotation(SequenceI entry, Mapping mp);
497
498   /**
499    * @return The RNA of the sequence in the alignment
500    */
501
502   public RNA getRNA();
503
504   /**
505    * @param rna
506    *          The RNA.
507    */
508   public void setRNA(RNA rna);
509
510   /**
511    * 
512    * @return list of insertions (gap characters) in sequence
513    */
514   public List<int[]> getInsertions();
515
516   /**
517    * Given a pdbId String, return the equivalent PDBEntry if available in the
518    * given sequence
519    * 
520    * @param pdbId
521    * @return
522    */
523   public PDBEntry getPDBEntry(String pdbId);
524
525   /**
526    * Get all primary database/accessions for this sequence's data. These
527    * DBRefEntry are expected to resolve to a valid record in the associated
528    * external database, either directly or via a provided 1:1 Mapping.
529    * 
530    * @return just the primary references (if any) for this sequence, or an empty
531    *         list
532    */
533   public List<DBRefEntry> getPrimaryDBRefs();
534   /**
535    * Answers true if the sequence has annotation for Hidden Markov Model
536    * information content, else false
537    */
538   boolean hasHMMAnnotation();
539
540   /**
541    * Returns a (possibly empty) list of sequence features that overlap the given
542    * alignment column range, optionally restricted to one or more specified
543    * feature types. If the range is all gaps, then features which enclose it are
544    * included (but not contact features).
545    * 
546    * @param fromCol
547    *          start column of range inclusive (1..)
548    * @param toCol
549    *          end column of range inclusive (1..)
550    * @param types
551    *          optional feature types to restrict results to
552    * @return
553    */
554   List<SequenceFeature> findFeatures(int fromCol, int toCol, String... types);
555
556   /**
557    * Method to call to indicate that the sequence (characters or alignment/gaps)
558    * has been modified. Provided to allow any cursors on residue/column
559    * positions to be invalidated.
560    */
561   void sequenceChanged();
562   
563   /**
564    * 
565    * @return BitSet corresponding to index [0,length) where Comparison.isGap()
566    *         returns true.
567    */
568   BitSet getInsertionsAsBits();
569
570   /**
571    * Replaces every occurrence of c1 in the sequence with c2 and returns the
572    * number of characters changed
573    * 
574    * @param c1
575    * @param c2
576    */
577   public int replace(char c1, char c2);
578
579   /**
580    * Answers the GeneLociI, or null if not known
581    * 
582    * @return
583    */
584   GeneLociI getGeneLoci();
585
586   /**
587    * Sets the mapping to gene loci for the sequence
588    * 
589    * @param speciesId
590    * @param assemblyId
591    * @param chromosomeId
592    * @param map
593    */
594   void setGeneLoci(String speciesId, String assemblyId,
595           String chromosomeId, MapList map);
596
597
598   /**
599    * Returns the sequence string constructed from the substrings of a sequence
600    * defined by the int[] ranges provided by an iterator. E.g. the iterator
601    * could iterate over all visible regions of the alignment
602    * 
603    * @param it
604    *          the iterator to use
605    * @return a String corresponding to the sequence
606    */
607   public String getSequenceStringFromIterator(Iterator<int[]> it);
608
609   /**
610    * Locate the first position in this sequence which is not contained in an
611    * iterator region. If no such position exists, return 0
612    * 
613    * @param it
614    *          iterator over regions
615    * @return first residue not contained in regions
616    */
617   public int firstResidueOutsideIterator(Iterator<int[]> it);
618
619
620   /**
621    * Answers true if this sequence has an associated Hidden Markov Model
622    * 
623    * @return
624    */
625   boolean hasHMMProfile();
626 }
627