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