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