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