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