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