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