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