JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / datamodel / AlignmentI.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 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 License for more details.
16  * 
17  * You should have received a copy of the GNU General 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 java.util.Hashtable;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 /**
29  * Data structure to hold and manipulate a multiple sequence alignment
30  */
31 public interface AlignmentI extends AnnotatedCollectionI
32 {
33   /**
34    * Calculates the number of sequences in an alignment
35    * 
36    * @return Number of sequences in alignment
37    */
38   int getHeight();
39
40   /**
41    * 
42    * Calculates the maximum width of the alignment, including gaps.
43    * 
44    * @return Greatest sequence length within alignment, or -1 if no sequences
45    *         present
46    */
47   @Override
48   int getWidth();
49
50   /**
51    * Calculates if this set of sequences (visible and invisible) are all the
52    * same length
53    * 
54    * @return true if all sequences in alignment are the same length
55    */
56   boolean isAligned();
57
58   /**
59    * Calculates if this set of sequences is all the same length
60    * 
61    * @param includeHidden
62    *          optionally exclude hidden sequences from test
63    * @return true if all (or just visible) sequences are the same length
64    */
65   boolean isAligned(boolean includeHidden);
66
67   /**
68    * Gets sequences as a Synchronized collection
69    * 
70    * @return All sequences in alignment.
71    */
72   @Override
73   List<SequenceI> getSequences();
74
75   /**
76    * Gets sequences as a SequenceI[]
77    * 
78    * @return All sequences in alignment.
79    */
80   SequenceI[] getSequencesArray();
81
82   /**
83    * Find a specific sequence in this alignment.
84    * 
85    * @param i
86    *          Index of required sequence.
87    * 
88    * @return SequenceI at given index.
89    */
90   SequenceI getSequenceAt(int i);
91
92   /**
93    * Returns a map of lists of sequences keyed by sequence name.
94    * 
95    * @return
96    */
97   Map<String, List<SequenceI>> getSequencesByName();
98
99   /**
100    * Add a new sequence to this alignment.
101    * 
102    * @param seq
103    *          New sequence will be added at end of alignment.
104    */
105   void addSequence(SequenceI seq);
106
107   /**
108    * Used to set a particular index of the alignment with the given sequence.
109    * 
110    * @param i
111    *          Index of sequence to be updated. if i>length, sequence will be
112    *          added to end, with no intervening positions.
113    * @param seq
114    *          New sequence to be inserted. The existing sequence at position i
115    *          will be replaced.
116    * @return existing sequence (or null if i>current length)
117    */
118   SequenceI replaceSequenceAt(int i, SequenceI seq);
119
120   /**
121    * Deletes a sequence from the alignment
122    * 
123    * @param s
124    *          Sequence to be deleted.
125    */
126   void deleteSequence(SequenceI s);
127
128   /**
129    * Deletes a sequence from the alignment.
130    * 
131    * @param i
132    *          Index of sequence to be deleted.
133    */
134   void deleteSequence(int i);
135
136   /**
137    * Finds sequence in alignment using sequence name as query.
138    * 
139    * @param name
140    *          Id of sequence to search for.
141    * 
142    * @return Sequence matching query, if found. If not found returns null.
143    */
144   SequenceI findName(String name);
145
146   SequenceI[] findSequenceMatch(String name);
147
148   /**
149    * Finds index of a given sequence in the alignment.
150    * 
151    * @param s
152    *          Sequence to look for.
153    * 
154    * @return Index of sequence within the alignment or -1 if not found
155    */
156   int findIndex(SequenceI s);
157
158   /**
159    * Finds group that given sequence is part of.
160    * 
161    * @param s
162    *          Sequence in alignment.
163    * 
164    * @return First group found for sequence. WARNING : Sequences may be members
165    *         of several groups. This method is incomplete.
166    */
167   SequenceGroup findGroup(SequenceI s);
168
169   /**
170    * Finds all groups that a given sequence is part of.
171    * 
172    * @param s
173    *          Sequence in alignment.
174    * 
175    * @return All groups containing given sequence.
176    */
177   SequenceGroup[] findAllGroups(SequenceI s);
178
179   /**
180    * Adds a new SequenceGroup to this alignment.
181    * 
182    * @param sg
183    *          New group to be added.
184    */
185   void addGroup(SequenceGroup sg);
186
187   /**
188    * Deletes a specific SequenceGroup
189    * 
190    * @param g
191    *          Group will be deleted from alignment.
192    */
193   void deleteGroup(SequenceGroup g);
194
195   /**
196    * Get all the groups associated with this alignment.
197    * 
198    * @return All groups as a list.
199    */
200   List<SequenceGroup> getGroups();
201
202   /**
203    * Deletes all groups from this alignment.
204    */
205   void deleteAllGroups();
206
207   /**
208    * Adds a new AlignmentAnnotation to this alignment
209    * 
210    * @note Care should be taken to ensure that annotation is at least as wide as
211    *       the longest sequence in the alignment for rendering purposes.
212    */
213   void addAnnotation(AlignmentAnnotation aa);
214
215   /**
216    * moves annotation to a specified index in alignment annotation display stack
217    * 
218    * @param aa
219    *          the annotation object to be moved
220    * @param index
221    *          the destination position
222    */
223   void setAnnotationIndex(AlignmentAnnotation aa, int index);
224
225   /**
226    * Delete all annotations, including auto-calculated if the flag is set true.
227    * Returns true if at least one annotation was deleted, else false.
228    * 
229    * @param includingAutoCalculated
230    * @return
231    */
232   boolean deleteAllAnnotations(boolean includingAutoCalculated);
233
234   /**
235    * Deletes a specific AlignmentAnnotation from the alignment, and removes its
236    * reference from any SequenceI or SequenceGroup object's annotation if and
237    * only if aa is contained within the alignment's annotation vector.
238    * Otherwise, it will do nothing.
239    * 
240    * @param aa
241    *          the annotation to delete
242    * @return true if annotation was deleted from this alignment.
243    */
244   boolean deleteAnnotation(AlignmentAnnotation aa);
245
246   /**
247    * Deletes a specific AlignmentAnnotation from the alignment, and optionally
248    * removes any reference from any SequenceI or SequenceGroup object's
249    * annotation if and only if aa is contained within the alignment's annotation
250    * vector. Otherwise, it will do nothing.
251    * 
252    * @param aa
253    *          the annotation to delete
254    * @param unhook
255    *          flag indicating if any references should be removed from
256    *          annotation - use this if you intend to add the annotation back
257    *          into the alignment
258    * @return true if annotation was deleted from this alignment.
259    */
260   boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook);
261
262   /**
263    * Get the annotation associated with this alignment (this can be null if no
264    * annotation has ever been created on the alignment)
265    * 
266    * @return array of AlignmentAnnotation objects
267    */
268   @Override
269   AlignmentAnnotation[] getAlignmentAnnotation();
270
271   /**
272    * Change the gap character used in this alignment to 'gc'
273    * 
274    * @param gc
275    *          the new gap character.
276    */
277   void setGapCharacter(char gc);
278
279   /**
280    * Get the gap character used in this alignment
281    * 
282    * @return gap character
283    */
284   char getGapCharacter();
285
286   /**
287    * Test for all nucleotide alignment
288    * 
289    * @return true if alignment is nucleotide sequence
290    */
291   boolean isNucleotide();
292
293   /**
294    * Test if alignment contains RNA structure
295    * 
296    * @return true if RNA structure AligmnentAnnotation was added to alignment
297    */
298   boolean hasRNAStructure();
299
300   /**
301    * Set alignment to be a nucleotide sequence
302    * 
303    */
304   void setNucleotide(boolean b);
305
306   /**
307    * Get the associated dataset for the alignment.
308    * 
309    * @return Alignment containing dataset sequences or null of this is a
310    *         dataset.
311    */
312   AlignmentI getDataset();
313
314   /**
315    * Set the associated dataset for the alignment, or create one.
316    * 
317    * @param dataset
318    *          The dataset alignment or null to construct one.
319    */
320   void setDataset(AlignmentI dataset);
321
322   /**
323    * pads sequences with gaps (to ensure the set looks like an alignment)
324    * 
325    * @return boolean true if alignment was modified
326    */
327   boolean padGaps();
328
329   HiddenSequences getHiddenSequences();
330
331   /**
332    * Compact representation of alignment
333    * 
334    * @return CigarArray
335    */
336   CigarArray getCompactAlignment();
337
338   /**
339    * Set an arbitrary key value pair for an alignment. Note: both key and value
340    * objects should return a meaningful, human readable response to .toString()
341    * 
342    * @param key
343    * @param value
344    */
345   void setProperty(Object key, Object value);
346
347   /**
348    * Get a named property from the alignment.
349    * 
350    * @param key
351    * @return value of property
352    */
353   Object getProperty(Object key);
354
355   /**
356    * Get the property hashtable.
357    * 
358    * @return hashtable of alignment properties (or null if none are defined)
359    */
360   Hashtable getProperties();
361
362   /**
363    * add a reference to a frame of aligned codons for this alignment
364    * 
365    * @param codons
366    */
367   void addCodonFrame(AlignedCodonFrame codons);
368
369   /**
370    * remove a particular codon frame reference from this alignment
371    * 
372    * @param codons
373    * @return true if codon frame was removed.
374    */
375   boolean removeCodonFrame(AlignedCodonFrame codons);
376
377   /**
378    * get all codon frames associated with this alignment
379    * 
380    * @return
381    */
382   List<AlignedCodonFrame> getCodonFrames();
383
384   /**
385    * Set the codon frame mappings (replacing any existing list).
386    */
387   void setCodonFrames(List<AlignedCodonFrame> acfs);
388
389   /**
390    * get codon frames involving sequenceI
391    */
392   List<AlignedCodonFrame> getCodonFrame(SequenceI seq);
393
394   /**
395    * find sequence with given name in alignment
396    * 
397    * @param token
398    *          name to find
399    * @param b
400    *          true implies that case insensitive matching will <em>also</em> be
401    *          tried
402    * @return matched sequence or null
403    */
404   SequenceI findName(String token, boolean b);
405
406   /**
407    * find next sequence with given name in alignment starting after a given
408    * sequence
409    * 
410    * @param startAfter
411    *          the sequence after which the search will be started (usually the
412    *          result of the last call to findName)
413    * @param token
414    *          name to find
415    * @param b
416    *          true implies that case insensitive matching will <em>also</em> be
417    *          tried
418    * @return matched sequence or null
419    */
420   SequenceI findName(SequenceI startAfter, String token, boolean b);
421
422   /**
423    * find first sequence in alignment which is involved in the given search
424    * result object
425    * 
426    * @param results
427    * @return -1 or index of sequence in alignment
428    */
429   int findIndex(SearchResults results);
430
431   /**
432    * append sequences and annotation from another alignment object to this one.
433    * Note: this is a straight transfer of object references, and may result in
434    * toappend's dependent data being transformed to fit the alignment (changing
435    * gap characters, etc...). If you are uncertain, use the copy Alignment copy
436    * constructor to create a new version which can be appended without side
437    * effect.
438    * 
439    * @param toappend
440    *          - the alignment to be appended.
441    */
442   void append(AlignmentI toappend);
443
444   /**
445    * Justify the sequences to the left or right by deleting and inserting gaps
446    * before the initial residue or after the terminal residue
447    * 
448    * @param right
449    *          true if alignment padded to right, false to justify to left
450    * @return true if alignment was changed TODO: return undo object
451    */
452   boolean justify(boolean right);
453
454   /**
455    * add given annotation row at given position (0 is start, -1 is end)
456    * 
457    * @param consensus
458    * @param i
459    */
460   void addAnnotation(AlignmentAnnotation consensus, int i);
461
462   /**
463    * search for or create a specific annotation row on the alignment
464    * 
465    * @param name
466    *          name for annotation (must match)
467    * @param calcId
468    *          calcId for the annotation (null or must match)
469    * @param autoCalc
470    *          - value of autocalc flag for the annotation
471    * @param seqRef
472    *          - null or specific sequence reference
473    * @param groupRef
474    *          - null or specific group reference
475    * @param method
476    *          - CalcId for the annotation (must match)
477    * 
478    * @return existing annotation matching the given attributes
479    */
480   AlignmentAnnotation findOrCreateAnnotation(String name, String calcId,
481           boolean autoCalc, SequenceI seqRef, SequenceGroup groupRef);
482
483   /**
484    * move the given group up or down in the alignment by the given number of
485    * rows. Implementor assumes given group is already present on alignment - no
486    * recalculations are triggered.
487    * 
488    * @param sg
489    * @param map
490    * @param up
491    * @param i
492    */
493   void moveSelectedSequencesByOne(SequenceGroup sg,
494           Map<SequenceI, SequenceCollectionI> map, boolean up);
495
496   /**
497    * validate annotation after an edit and update any alignment state flags
498    * accordingly
499    * 
500    * @param alignmentAnnotation
501    */
502   void validateAnnotation(AlignmentAnnotation alignmentAnnotation);
503
504   /**
505    * Align this alignment the same as the given one. If both of the same type
506    * (nucleotide/protein) then align both identically. If this is nucleotide and
507    * the other is protein, make 3 gaps for each gap in the protein sequences. If
508    * this is protein and the other is nucleotide, insert a gap for each 3 gaps
509    * (or part thereof) between nucleotide bases. Returns the number of mapped
510    * sequences that were realigned .
511    * 
512    * @param al
513    * @return
514    */
515   int alignAs(AlignmentI al);
516
517   /**
518    * Returns the set of distinct sequence names in the alignment.
519    * 
520    * @return
521    */
522   Set<String> getSequenceNames();
523
524   /**
525    * Checks if the alignment has at least one sequence with one non-gaped
526    * residue
527    * 
528    * @return
529    */
530   public boolean hasValidSequence();
531
532   /**
533    * Update any mappings to 'virtual' sequences to compatible real ones, if
534    * present in the added sequences. Returns a count of mappings updated.
535    * 
536    * @param seqs
537    * @return
538    */
539   int realiseMappings(List<SequenceI> seqs);
540
541   /**
542    * Returns the first AlignedCodonFrame that has a mapping between the given
543    * dataset sequences
544    * 
545    * @param mapFrom
546    * @param mapTo
547    * @return
548    */
549   AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo);
550
551   /**
552    * Calculate the visible start and end index of an alignment. The result is
553    * returned an int array where: int[0] = startIndex, and int[1] = endIndex.
554    * 
555    * @param hiddenCols
556    * @return
557    */
558   public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols);
559 }