a0b3ff1a0696fdac5c26d9f107e725b03642de9d
[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    * Returns the first group (in the order in which groups were added) that
160    * includes the given sequence and aligned position (base 0), or null if none
161    * found
162    * 
163    * @param seq
164    * @param position
165    * 
166    * @return
167    */
168   SequenceGroup findGroup(SequenceI seq, int position);
169
170   /**
171    * Finds all groups that a given sequence is part of.
172    * 
173    * @param s
174    *          Sequence in alignment.
175    * 
176    * @return All groups containing given sequence.
177    */
178   SequenceGroup[] findAllGroups(SequenceI s);
179
180   /**
181    * Adds a new SequenceGroup to this alignment.
182    * 
183    * @param sg
184    *          New group to be added.
185    */
186   void addGroup(SequenceGroup sg);
187
188   /**
189    * Deletes a specific SequenceGroup
190    * 
191    * @param g
192    *          Group will be deleted from alignment.
193    */
194   void deleteGroup(SequenceGroup g);
195
196   /**
197    * Get all the groups associated with this alignment.
198    * 
199    * @return All groups as a list.
200    */
201   List<SequenceGroup> getGroups();
202
203   /**
204    * Deletes all groups from this alignment.
205    */
206   void deleteAllGroups();
207
208   /**
209    * Adds a new AlignmentAnnotation to this alignment
210    * 
211    * @note Care should be taken to ensure that annotation is at least as wide as
212    *       the longest sequence in the alignment for rendering purposes.
213    */
214   void addAnnotation(AlignmentAnnotation aa);
215
216   /**
217    * moves annotation to a specified index in alignment annotation display stack
218    * 
219    * @param aa
220    *          the annotation object to be moved
221    * @param index
222    *          the destination position
223    */
224   void setAnnotationIndex(AlignmentAnnotation aa, int index);
225
226   /**
227    * Delete all annotations, including auto-calculated if the flag is set true.
228    * Returns true if at least one annotation was deleted, else false.
229    * 
230    * @param includingAutoCalculated
231    * @return
232    */
233   boolean deleteAllAnnotations(boolean includingAutoCalculated);
234
235   /**
236    * Deletes a specific AlignmentAnnotation from the alignment, and removes its
237    * reference from any SequenceI or SequenceGroup object's annotation if and
238    * only if aa is contained within the alignment's annotation vector.
239    * Otherwise, it will do nothing.
240    * 
241    * @param aa
242    *          the annotation to delete
243    * @return true if annotation was deleted from this alignment.
244    */
245   boolean deleteAnnotation(AlignmentAnnotation aa);
246
247   /**
248    * Deletes a specific AlignmentAnnotation from the alignment, and optionally
249    * removes any reference from any SequenceI or SequenceGroup object's
250    * annotation if and only if aa is contained within the alignment's annotation
251    * vector. Otherwise, it will do nothing.
252    * 
253    * @param aa
254    *          the annotation to delete
255    * @param unhook
256    *          flag indicating if any references should be removed from
257    *          annotation - use this if you intend to add the annotation back
258    *          into the alignment
259    * @return true if annotation was deleted from this alignment.
260    */
261   boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook);
262
263   /**
264    * Get the annotation associated with this alignment (this can be null if no
265    * annotation has ever been created on the alignment)
266    * 
267    * @return array of AlignmentAnnotation objects
268    */
269   @Override
270   AlignmentAnnotation[] getAlignmentAnnotation();
271
272   /**
273    * Change the gap character used in this alignment to 'gc'
274    * 
275    * @param gc
276    *          the new gap character.
277    */
278   void setGapCharacter(char gc);
279
280   /**
281    * Get the gap character used in this alignment
282    * 
283    * @return gap character
284    */
285   char getGapCharacter();
286
287   /**
288    * Test for all nucleotide alignment
289    * 
290    * @return true if alignment is nucleotide sequence
291    */
292   boolean isNucleotide();
293
294   /**
295    * Test if alignment contains RNA structure
296    * 
297    * @return true if RNA structure AligmnentAnnotation was added to alignment
298    */
299   boolean hasRNAStructure();
300
301   /**
302    * Get the associated dataset for the alignment.
303    * 
304    * @return Alignment containing dataset sequences or null of this is a
305    *         dataset.
306    */
307   AlignmentI getDataset();
308
309   /**
310    * Set the associated dataset for the alignment, or create one.
311    * 
312    * @param dataset
313    *          The dataset alignment or null to construct one.
314    */
315   void setDataset(AlignmentI dataset);
316
317   /**
318    * pads sequences with gaps (to ensure the set looks like an alignment)
319    * 
320    * @return boolean true if alignment was modified
321    */
322   boolean padGaps();
323
324   HiddenSequences getHiddenSequences();
325
326   /**
327    * Compact representation of alignment
328    * 
329    * @return CigarArray
330    */
331   CigarArray getCompactAlignment();
332
333   /**
334    * Set an arbitrary key value pair for an alignment. Note: both key and value
335    * objects should return a meaningful, human readable response to .toString()
336    * 
337    * @param key
338    * @param value
339    */
340   void setProperty(Object key, Object value);
341
342   /**
343    * Get a named property from the alignment.
344    * 
345    * @param key
346    * @return value of property
347    */
348   Object getProperty(Object key);
349
350   /**
351    * Get the property hashtable.
352    * 
353    * @return hashtable of alignment properties (or null if none are defined)
354    */
355   Hashtable getProperties();
356
357   /**
358    * add a reference to a frame of aligned codons for this alignment
359    * 
360    * @param codons
361    */
362   void addCodonFrame(AlignedCodonFrame codons);
363
364   /**
365    * remove a particular codon frame reference from this alignment
366    * 
367    * @param codons
368    * @return true if codon frame was removed.
369    */
370   boolean removeCodonFrame(AlignedCodonFrame codons);
371
372   /**
373    * get all codon frames associated with this alignment
374    * 
375    * @return
376    */
377   List<AlignedCodonFrame> getCodonFrames();
378
379   /**
380    * Set the codon frame mappings (replacing any existing list).
381    */
382   void setCodonFrames(List<AlignedCodonFrame> acfs);
383
384   /**
385    * get codon frames involving sequenceI
386    */
387   List<AlignedCodonFrame> getCodonFrame(SequenceI seq);
388
389   /**
390    * find sequence with given name in alignment
391    * 
392    * @param token
393    *          name to find
394    * @param b
395    *          true implies that case insensitive matching will <em>also</em> be
396    *          tried
397    * @return matched sequence or null
398    */
399   SequenceI findName(String token, boolean b);
400
401   /**
402    * find next sequence with given name in alignment starting after a given
403    * sequence
404    * 
405    * @param startAfter
406    *          the sequence after which the search will be started (usually the
407    *          result of the last call to findName)
408    * @param token
409    *          name to find
410    * @param b
411    *          true implies that case insensitive matching will <em>also</em> be
412    *          tried
413    * @return matched sequence or null
414    */
415   SequenceI findName(SequenceI startAfter, String token, boolean b);
416
417   /**
418    * find first sequence in alignment which is involved in the given search
419    * result object
420    * 
421    * @param results
422    * @return -1 or index of sequence in alignment
423    */
424   int findIndex(SearchResultsI results);
425
426   /**
427    * append sequences and annotation from another alignment object to this one.
428    * Note: this is a straight transfer of object references, and may result in
429    * toappend's dependent data being transformed to fit the alignment (changing
430    * gap characters, etc...). If you are uncertain, use the copy Alignment copy
431    * constructor to create a new version which can be appended without side
432    * effect.
433    * 
434    * @param toappend
435    *          - the alignment to be appended.
436    */
437   void append(AlignmentI toappend);
438
439   /**
440    * Justify the sequences to the left or right by deleting and inserting gaps
441    * before the initial residue or after the terminal residue
442    * 
443    * @param right
444    *          true if alignment padded to right, false to justify to left
445    * @return true if alignment was changed TODO: return undo object
446    */
447   boolean justify(boolean right);
448
449   /**
450    * add given annotation row at given position (0 is start, -1 is end)
451    * 
452    * @param consensus
453    * @param i
454    */
455   void addAnnotation(AlignmentAnnotation consensus, int i);
456
457   /**
458    * search for or create a specific annotation row on the alignment
459    * 
460    * @param name
461    *          name for annotation (must match)
462    * @param calcId
463    *          calcId for the annotation (null or must match)
464    * @param autoCalc
465    *          - value of autocalc flag for the annotation
466    * @param seqRef
467    *          - null or specific sequence reference
468    * @param groupRef
469    *          - null or specific group reference
470    * @param method
471    *          - CalcId for the annotation (must match)
472    * 
473    * @return existing annotation matching the given attributes
474    */
475   AlignmentAnnotation findOrCreateAnnotation(String name, String calcId,
476           boolean autoCalc, SequenceI seqRef, SequenceGroup groupRef);
477
478   /**
479    * move the given group up or down in the alignment by the given number of
480    * rows. Implementor assumes given group is already present on alignment - no
481    * recalculations are triggered.
482    * 
483    * @param sg
484    * @param map
485    * @param up
486    * @param i
487    */
488   void moveSelectedSequencesByOne(SequenceGroup sg,
489           Map<SequenceI, SequenceCollectionI> map, boolean up);
490
491   /**
492    * validate annotation after an edit and update any alignment state flags
493    * accordingly
494    * 
495    * @param alignmentAnnotation
496    */
497   void validateAnnotation(AlignmentAnnotation alignmentAnnotation);
498
499   /**
500    * Align this alignment the same as the given one. If both of the same type
501    * (nucleotide/protein) then align both identically. If this is nucleotide and
502    * the other is protein, make 3 gaps for each gap in the protein sequences. If
503    * this is protein and the other is nucleotide, insert a gap for each 3 gaps
504    * (or part thereof) between nucleotide bases. Returns the number of mapped
505    * sequences that were realigned .
506    * 
507    * @param al
508    * @return
509    */
510   int alignAs(AlignmentI al);
511
512   /**
513    * Returns the set of distinct sequence names in the alignment.
514    * 
515    * @return
516    */
517   Set<String> getSequenceNames();
518
519   /**
520    * Checks if the alignment has at least one sequence with one non-gaped
521    * residue
522    * 
523    * @return
524    */
525   public boolean hasValidSequence();
526
527   /**
528    * Update any mappings to 'virtual' sequences to compatible real ones, if
529    * present in the added sequences. Returns a count of mappings updated.
530    * 
531    * @param seqs
532    * @return
533    */
534   int realiseMappings(List<SequenceI> seqs);
535
536   /**
537    * Returns the first AlignedCodonFrame that has a mapping between the given
538    * dataset sequences
539    * 
540    * @param mapFrom
541    * @param mapTo
542    * @return
543    */
544   AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo);
545
546   /**
547    * Calculate the visible start and end index of an alignment. The result is
548    * returned an int array where: int[0] = startIndex, and int[1] = endIndex.
549    * 
550    * @param hiddenCols
551    * @return
552    */
553   public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols);
554 }