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