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