2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
23 import jalview.analysis.*;
26 * Data structure to hold and manipulate a multiple sequence alignment
32 public class Alignment implements AlignmentI
34 protected Alignment dataset;
36 protected Vector sequences;
38 protected Vector groups = new Vector();
40 protected char gapCharacter = '-';
42 protected int type = NUCLEOTIDE;
44 public static final int PROTEIN = 0;
46 public static final int NUCLEOTIDE = 1;
49 public AlignmentAnnotation[] annotations;
51 HiddenSequences hiddenSequences = new HiddenSequences(this);
53 public Hashtable alignmentProperties;
55 private void initAlignment(SequenceI[] seqs)
59 if (jalview.util.Comparison.isNucleotide(seqs))
68 sequences = new Vector();
70 for (i = 0; i < seqs.length; i++)
72 sequences.addElement(seqs[i]);
78 * Make an alignment from an array of Sequences.
82 public Alignment(SequenceI[] seqs)
88 * Make a new alignment from an array of SeqCigars
93 public Alignment(SeqCigar[] alseqs)
95 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
96 gapCharacter, new ColumnSelection(), null);
101 * Make a new alignment from an CigarArray JBPNote - can only do this when
102 * compactAlignment does not contain hidden regions. JBPNote - must also check
103 * that compactAlignment resolves to a set of SeqCigars - or construct them
106 * @param compactAlignment
109 public static AlignmentI createAlignment(CigarArray compactAlignment)
111 throw new Error("Alignment(CigarArray) not yet implemented");
112 // this(compactAlignment.refCigars);
118 * @return DOCUMENT ME!
120 public Vector getSequences()
125 public SequenceI[] getSequencesArray()
127 if (sequences == null)
129 SequenceI[] reply = new SequenceI[sequences.size()];
130 for (int i = 0; i < sequences.size(); i++)
132 reply[i] = (SequenceI) sequences.elementAt(i);
143 * @return DOCUMENT ME!
145 public SequenceI getSequenceAt(int i)
147 if (i < sequences.size())
149 return (SequenceI) sequences.elementAt(i);
156 * Adds a sequence to the alignment. Recalculates maxLength and size.
160 public void addSequence(SequenceI snew)
164 // maintain dataset integrity
165 if (snew.getDatasetSequence() != null)
167 getDataset().addSequence(snew.getDatasetSequence());
171 // derive new sequence
172 SequenceI adding = snew.deriveSequence();
173 getDataset().addSequence(adding.getDatasetSequence());
177 if (sequences == null)
179 initAlignment(new SequenceI[]
184 sequences.addElement(snew);
186 if (hiddenSequences != null)
187 hiddenSequences.adjustHeightSequenceAdded();
191 * Adds a sequence to the alignment. Recalculates maxLength and size.
195 public void setSequenceAt(int i, SequenceI snew)
197 SequenceI oldseq = getSequenceAt(i);
198 deleteSequence(oldseq);
200 sequences.setElementAt(snew, i);
206 * @return DOCUMENT ME!
208 public Vector getGroups()
213 public void finalize()
215 if (getDataset() != null)
216 getDataset().removeAlignmentRef();
222 hiddenSequences = null;
226 * decrement the alignmentRefs counter by one and call finalize if it goes to
229 private void removeAlignmentRef()
231 if (--alignmentRefs == 0)
243 public void deleteSequence(SequenceI s)
245 deleteSequence(findIndex(s));
254 public void deleteSequence(int i)
256 if (i > -1 && i < getHeight())
258 sequences.removeElementAt(i);
259 hiddenSequences.adjustHeightSequenceDeleted(i);
264 public SequenceGroup findGroup(SequenceI s)
266 for (int i = 0; i < this.groups.size(); i++)
268 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
270 if (sg.getSequences(null).contains(s))
285 * @return DOCUMENT ME!
287 public SequenceGroup[] findAllGroups(SequenceI s)
289 Vector temp = new Vector();
291 int gSize = groups.size();
292 for (int i = 0; i < gSize; i++)
294 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
295 if (sg == null || sg.getSequences(null) == null)
297 this.deleteGroup(sg);
302 if (sg.getSequences(null).contains(s))
308 SequenceGroup[] ret = new SequenceGroup[temp.size()];
310 for (int i = 0; i < temp.size(); i++)
312 ret[i] = (SequenceGroup) temp.elementAt(i);
319 public void addGroup(SequenceGroup sg)
321 if (!groups.contains(sg))
323 if (hiddenSequences.getSize() > 0)
325 int i, iSize = sg.getSize();
326 for (i = 0; i < iSize; i++)
328 if (!sequences.contains(sg.getSequenceAt(i)))
330 sg.deleteSequence(sg.getSequenceAt(i), false);
336 if (sg.getSize() < 1)
342 groups.addElement(sg);
347 * remove any annotation that references gp
348 * @param gp (if null, removes all group associated annotation)
350 private void removeAnnotationForGroup(SequenceGroup gp)
352 if (annotations==null || annotations.length==0)
356 // remove annotation very quickly
357 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
361 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
363 if (annotations[i].groupRef != null)
365 todelete[p++] = annotations[i];
369 tokeep[k++] = annotations[i];
375 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
377 if (annotations[i].groupRef == gp)
379 todelete[p++] = annotations[i];
383 tokeep[k++] = annotations[i];
389 // clear out the group associated annotation.
390 for (i = 0; i < p; i++)
392 unhookAnnotation(todelete[i]);
395 t = new AlignmentAnnotation[k];
396 for (i = 0; i < k; i++)
404 public void deleteAllGroups()
406 if (annotations != null)
408 removeAnnotationForGroup(null);
410 groups.removeAllElements();
414 public void deleteGroup(SequenceGroup g)
416 if (groups.contains(g))
418 removeAnnotationForGroup(g);
419 groups.removeElement(g);
424 public SequenceI findName(String name)
426 return findName(name, false);
432 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
434 public SequenceI findName(String token, boolean b)
436 return findName(null, token, b);
442 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
445 public SequenceI findName(SequenceI startAfter, String token, boolean b)
450 String sqname = null;
451 if (startAfter != null)
453 // try to find the sequence in the alignment
454 boolean matched = false;
455 while (i < sequences.size())
457 if (getSequenceAt(i++) == startAfter)
468 while (i < sequences.size())
470 sq = getSequenceAt(i);
471 sqname = sq.getName();
472 if (sqname.equals(token) // exact match
473 || (b && // allow imperfect matches - case varies
474 (sqname.equalsIgnoreCase(token))))
476 return getSequenceAt(i);
485 public SequenceI[] findSequenceMatch(String name)
487 Vector matches = new Vector();
490 while (i < sequences.size())
492 if (getSequenceAt(i).getName().equals(name))
494 matches.addElement(getSequenceAt(i));
499 SequenceI[] result = new SequenceI[matches.size()];
500 for (i = 0; i < result.length; i++)
502 result[i] = (SequenceI) matches.elementAt(i);
512 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
514 public int findIndex(SequenceI s)
518 while (i < sequences.size())
520 if (s == getSequenceAt(i))
535 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
537 public int findIndex(SearchResults results)
541 while (i < sequences.size())
543 if (results.involvesSequence(getSequenceAt(i)))
555 * @return DOCUMENT ME!
557 public int getHeight()
559 return sequences.size();
565 * @return DOCUMENT ME!
567 public int getWidth()
571 for (int i = 0; i < sequences.size(); i++)
573 if (getSequenceAt(i).getLength() > maxLength)
575 maxLength = getSequenceAt(i).getLength();
588 public void setGapCharacter(char gc)
592 for (int i = 0; i < sequences.size(); i++)
594 Sequence seq = (Sequence) sequences.elementAt(i);
595 seq.setSequence(seq.getSequenceAsString().replace('.', gc).replace(
596 '-', gc).replace(' ', gc));
603 * @return DOCUMENT ME!
605 public char getGapCharacter()
613 * @return DOCUMENT ME!
615 public boolean isAligned()
617 int width = getWidth();
619 for (int i = 0; i < sequences.size(); i++)
621 if (getSequenceAt(i).getLength() != width)
633 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
634 * AlignmentAnnotation)
636 public boolean deleteAnnotation(AlignmentAnnotation aa)
640 if (annotations != null)
642 aSize = annotations.length;
650 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
652 boolean swap = false;
655 for (int i = 0; i < aSize; i++)
657 if (annotations[i] == aa)
662 if (tIndex < temp.length)
663 temp[tIndex++] = annotations[i];
669 unhookAnnotation(aa);
675 * remove any object references associated with this annotation
679 private void unhookAnnotation(AlignmentAnnotation aa)
681 if (aa.sequenceRef != null)
683 aa.sequenceRef.removeAlignmentAnnotation(aa);
685 if (aa.groupRef != null)
687 // probably need to do more here in the future (post 2.5.0)
695 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
696 * AlignmentAnnotation)
698 public void addAnnotation(AlignmentAnnotation aa)
700 addAnnotation(aa, -1);
706 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
707 * AlignmentAnnotation, int)
709 public void addAnnotation(AlignmentAnnotation aa, int pos)
712 if (annotations != null)
714 aSize = annotations.length + 1;
717 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
719 if (pos == -1 || pos >= aSize)
721 temp[aSize - 1] = aa;
730 for (i = 0; i < (aSize - 1); i++, p++)
738 temp[p] = annotations[i];
746 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
748 if (aa == null || annotations == null || annotations.length - 1 < index)
753 int aSize = annotations.length;
754 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
758 for (int i = 0; i < aSize; i++)
767 temp[i] = annotations[i];
771 temp[i] = annotations[i - 1];
781 * @return DOCUMENT ME!
783 public AlignmentAnnotation[] getAlignmentAnnotation()
788 public void setNucleotide(boolean b)
800 public boolean isNucleotide()
802 if (type == NUCLEOTIDE)
812 public void setDataset(Alignment data)
814 if (dataset == null && data == null)
816 // Create a new dataset for this alignment.
817 // Can only be done once, if dataset is not null
818 // This will not be performed
819 SequenceI[] seqs = new SequenceI[getHeight()];
820 SequenceI currentSeq;
821 for (int i = 0; i < getHeight(); i++)
823 currentSeq = getSequenceAt(i);
824 if (currentSeq.getDatasetSequence() != null)
826 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
830 seqs[i] = currentSeq.createDatasetSequence();
834 dataset = new Alignment(seqs);
836 else if (dataset == null && data != null)
840 dataset.addAlignmentRef();
844 * reference count for number of alignments referencing this one.
846 int alignmentRefs = 0;
849 * increase reference count to this alignment.
851 private void addAlignmentRef()
856 public Alignment getDataset()
861 public boolean padGaps()
863 boolean modified = false;
865 // Remove excess gaps from the end of alignment
869 for (int i = 0; i < sequences.size(); i++)
871 current = getSequenceAt(i);
872 for (int j = current.getLength(); j > maxLength; j--)
875 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
886 for (int i = 0; i < sequences.size(); i++)
888 current = getSequenceAt(i);
889 cLength = current.getLength();
891 if (cLength < maxLength)
893 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
896 else if (current.getLength() > maxLength)
898 current.deleteChars(maxLength, current.getLength());
905 * Justify the sequences to the left or right by deleting and inserting gaps
906 * before the initial residue or after the terminal residue
909 * true if alignment padded to right, false to justify to left
910 * @return true if alignment was changed
912 public boolean justify(boolean right)
914 boolean modified = false;
916 // Remove excess gaps from the end of alignment
918 int ends[] = new int[sequences.size() * 2];
920 for (int i = 0; i < sequences.size(); i++)
922 current = getSequenceAt(i);
923 // This should really be a sequence method
924 ends[i * 2] = current.findIndex(current.getStart());
925 ends[i * 2 + 1] = current.findIndex(current.getStart()
926 + current.getLength());
927 boolean hitres = false;
928 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
930 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
940 if (j - ends[i * 2] > maxLength)
942 maxLength = j - ends[i * 2];
950 // now edit the flanking gaps to justify to either left or right
951 int cLength, extent, diff;
952 for (int i = 0; i < sequences.size(); i++)
954 current = getSequenceAt(i);
956 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
957 diff = maxLength - cLength; // number of gaps to indent
958 extent = current.getLength();
962 if (extent > ends[i * 2 + 1])
964 current.deleteChars(ends[i * 2 + 1] + 1, extent);
967 if (ends[i * 2] > diff)
969 current.deleteChars(0, ends[i * 2] - diff);
974 if (ends[i * 2] < diff)
976 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
986 current.deleteChars(0, ends[i * 2]);
988 ends[i * 2 + 1] -= ends[i * 2];
989 extent -= ends[i * 2];
991 if (extent > maxLength)
993 current.deleteChars(maxLength + 1, extent);
998 if (extent < maxLength)
1000 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1009 public HiddenSequences getHiddenSequences()
1011 return hiddenSequences;
1014 public CigarArray getCompactAlignment()
1016 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1017 for (int i = 0; i < sequences.size(); i++)
1019 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
1021 CigarArray cal = new CigarArray(alseqs);
1022 cal.addOperation(CigarArray.M, getWidth());
1026 public void setProperty(Object key, Object value)
1028 if (alignmentProperties == null)
1029 alignmentProperties = new Hashtable();
1031 alignmentProperties.put(key, value);
1034 public Object getProperty(Object key)
1036 if (alignmentProperties != null)
1037 return alignmentProperties.get(key);
1042 public Hashtable getProperties()
1044 return alignmentProperties;
1047 AlignedCodonFrame[] codonFrameList = null;
1053 * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1056 public void addCodonFrame(AlignedCodonFrame codons)
1060 if (codonFrameList == null)
1062 codonFrameList = new AlignedCodonFrame[]
1066 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
1067 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
1068 t[codonFrameList.length] = codons;
1075 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
1077 public AlignedCodonFrame getCodonFrame(int index)
1079 return codonFrameList[index];
1086 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1088 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
1090 if (seq == null || codonFrameList == null)
1092 Vector cframes = new Vector();
1093 for (int f = 0; f < codonFrameList.length; f++)
1095 if (codonFrameList[f].involvesSequence(seq))
1096 cframes.addElement(codonFrameList[f]);
1098 if (cframes.size() == 0)
1100 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
1101 cframes.copyInto(cfr);
1108 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1110 public AlignedCodonFrame[] getCodonFrames()
1112 return codonFrameList;
1118 * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
1119 * AlignedCodonFrame)
1121 public boolean removeCodonFrame(AlignedCodonFrame codons)
1123 if (codons == null || codonFrameList == null)
1125 boolean removed = false;
1126 int i = 0, iSize = codonFrameList.length;
1129 if (codonFrameList[i] == codons)
1134 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize
1147 public void append(AlignmentI toappend)
1149 // TODO test this method for a future 2.5 release
1150 // currently tested for use in jalview.gui.SequenceFetcher
1151 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1152 char oldc = toappend.getGapCharacter();
1153 boolean hashidden = toappend.getHiddenSequences() != null
1154 && toappend.getHiddenSequences().hiddenSequences != null;
1155 // get all sequences including any hidden ones
1156 Vector sqs = (hashidden) ? toappend.getHiddenSequences()
1157 .getFullAlignment().getSequences() : toappend.getSequences();
1160 Enumeration sq = sqs.elements();
1161 while (sq.hasMoreElements())
1163 SequenceI addedsq = (SequenceI) sq.nextElement();
1166 char[] oldseq = addedsq.getSequence();
1167 for (int c = 0; c < oldseq.length; c++)
1169 if (oldseq[c] == oldc)
1171 oldseq[c] = gapCharacter;
1175 addSequence(addedsq);
1178 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1179 for (int a = 0; alan != null && a < alan.length; a++)
1181 addAnnotation(alan[a]);
1183 AlignedCodonFrame[] acod = toappend.getCodonFrames();
1184 for (int a = 0; acod != null && a < acod.length; a++)
1186 this.addCodonFrame(acod[a]);
1188 Vector sg = toappend.getGroups();
1191 Enumeration el = sg.elements();
1192 while (el.hasMoreElements())
1194 addGroup((SequenceGroup) el.nextElement());
1197 if (toappend.getHiddenSequences() != null)
1199 HiddenSequences hs = toappend.getHiddenSequences();
1200 if (hiddenSequences == null)
1202 hiddenSequences = new HiddenSequences(this);
1204 if (hs.hiddenSequences != null)
1206 for (int s = 0; s < hs.hiddenSequences.length; s++)
1208 // hide the newly appended sequence in the alignment
1209 if (hs.hiddenSequences[s] != null)
1211 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1216 if (toappend.getProperties() != null)
1218 // we really can't do very much here - just try to concatenate strings
1219 // where property collisions occur.
1220 Enumeration key = toappend.getProperties().keys();
1221 while (key.hasMoreElements())
1223 Object k = key.nextElement();
1224 Object ourval = this.getProperty(k);
1225 Object toapprop = toappend.getProperty(k);
1228 if (ourval.getClass().equals(toapprop.getClass())
1229 && !ourval.equals(toapprop))
1231 if (ourval instanceof String)
1234 this.setProperty(k, ((String) ourval) + "; "
1235 + ((String) toapprop));
1239 if (ourval instanceof Vector)
1242 Enumeration theirv = ((Vector) toapprop).elements();
1243 while (theirv.hasMoreElements())
1245 ((Vector) ourval).addElement(theirv);
1253 // just add new property directly
1254 setProperty(k, toapprop);