2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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
28 public class Alignment implements AlignmentI
30 protected Alignment dataset;
32 protected Vector sequences;
34 protected Vector groups = new Vector();
36 protected char gapCharacter = '-';
38 protected int type = NUCLEOTIDE;
40 public static final int PROTEIN = 0;
42 public static final int NUCLEOTIDE = 1;
45 public AlignmentAnnotation[] annotations;
47 HiddenSequences hiddenSequences = new HiddenSequences(this);
49 public Hashtable alignmentProperties;
51 private void initAlignment(SequenceI[] seqs)
55 if (jalview.util.Comparison.isNucleotide(seqs))
64 sequences = new Vector();
66 for (i = 0; i < seqs.length; i++)
68 sequences.addElement(seqs[i]);
74 * Make an alignment from an array of Sequences.
78 public Alignment(SequenceI[] seqs)
84 * Make a new alignment from an array of SeqCigars
89 public Alignment(SeqCigar[] alseqs)
91 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
92 gapCharacter, new ColumnSelection(), null);
97 * Make a new alignment from an CigarArray JBPNote - can only do this when
98 * compactAlignment does not contain hidden regions. JBPNote - must also check
99 * that compactAlignment resolves to a set of SeqCigars - or construct them
102 * @param compactAlignment
105 public static AlignmentI createAlignment(CigarArray compactAlignment)
107 throw new Error("Alignment(CigarArray) not yet implemented");
108 // this(compactAlignment.refCigars);
114 * @return DOCUMENT ME!
116 public Vector getSequences()
121 public SequenceI[] getSequencesArray()
123 if (sequences == null)
125 SequenceI[] reply = new SequenceI[sequences.size()];
126 for (int i = 0; i < sequences.size(); i++)
128 reply[i] = (SequenceI) sequences.elementAt(i);
139 * @return DOCUMENT ME!
141 public SequenceI getSequenceAt(int i)
143 if (i < sequences.size())
145 return (SequenceI) sequences.elementAt(i);
152 * Adds a sequence to the alignment. Recalculates maxLength and size.
156 public void addSequence(SequenceI snew)
160 // maintain dataset integrity
161 if (snew.getDatasetSequence() != null)
163 getDataset().addSequence(snew.getDatasetSequence());
167 // derive new sequence
168 SequenceI adding = snew.deriveSequence();
169 getDataset().addSequence(adding.getDatasetSequence());
173 if (sequences == null)
175 initAlignment(new SequenceI[]
180 sequences.addElement(snew);
182 if (hiddenSequences != null)
183 hiddenSequences.adjustHeightSequenceAdded();
187 * Adds a sequence to the alignment. Recalculates maxLength and size.
191 public void setSequenceAt(int i, SequenceI snew)
193 SequenceI oldseq = getSequenceAt(i);
194 deleteSequence(oldseq);
196 sequences.setElementAt(snew, i);
202 * @return DOCUMENT ME!
204 public Vector getGroups()
209 public void finalize()
211 if (getDataset() != null)
212 getDataset().removeAlignmentRef();
218 hiddenSequences = null;
222 * decrement the alignmentRefs counter by one and call finalize if it goes to
225 private void removeAlignmentRef()
227 if (--alignmentRefs == 0)
239 public void deleteSequence(SequenceI s)
241 deleteSequence(findIndex(s));
250 public void deleteSequence(int i)
252 if (i > -1 && i < getHeight())
254 sequences.removeElementAt(i);
255 hiddenSequences.adjustHeightSequenceDeleted(i);
260 public SequenceGroup findGroup(SequenceI s)
262 for (int i = 0; i < this.groups.size(); i++)
264 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
266 if (sg.getSequences(null).contains(s))
281 * @return DOCUMENT ME!
283 public SequenceGroup[] findAllGroups(SequenceI s)
285 Vector temp = new Vector();
287 int gSize = groups.size();
288 for (int i = 0; i < gSize; i++)
290 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
291 if (sg == null || sg.getSequences(null) == null)
293 this.deleteGroup(sg);
298 if (sg.getSequences(null).contains(s))
304 SequenceGroup[] ret = new SequenceGroup[temp.size()];
306 for (int i = 0; i < temp.size(); i++)
308 ret[i] = (SequenceGroup) temp.elementAt(i);
315 public void addGroup(SequenceGroup sg)
317 if (!groups.contains(sg))
319 if (hiddenSequences.getSize() > 0)
321 int i, iSize = sg.getSize();
322 for (i = 0; i < iSize; i++)
324 if (!sequences.contains(sg.getSequenceAt(i)))
326 sg.deleteSequence(sg.getSequenceAt(i), false);
332 if (sg.getSize() < 1)
338 groups.addElement(sg);
345 public void deleteAllGroups()
347 groups.removeAllElements();
351 public void deleteGroup(SequenceGroup g)
353 if (groups.contains(g))
355 groups.removeElement(g);
360 public SequenceI findName(String name)
362 return findName(name, false);
368 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
370 public SequenceI findName(String token, boolean b)
372 return findName(null, token, b);
378 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
381 public SequenceI findName(SequenceI startAfter, String token, boolean b)
386 String sqname = null;
387 if (startAfter != null)
389 // try to find the sequence in the alignment
390 boolean matched = false;
391 while (i < sequences.size())
393 if (getSequenceAt(i++) == startAfter)
404 while (i < sequences.size())
406 sq = getSequenceAt(i);
407 sqname = sq.getName();
408 if (sqname.equals(token) // exact match
409 || (b && // allow imperfect matches - case varies
410 (sqname.equalsIgnoreCase(token))))
412 return getSequenceAt(i);
421 public SequenceI[] findSequenceMatch(String name)
423 Vector matches = new Vector();
426 while (i < sequences.size())
428 if (getSequenceAt(i).getName().equals(name))
430 matches.addElement(getSequenceAt(i));
435 SequenceI[] result = new SequenceI[matches.size()];
436 for (i = 0; i < result.length; i++)
438 result[i] = (SequenceI) matches.elementAt(i);
448 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
450 public int findIndex(SequenceI s)
454 while (i < sequences.size())
456 if (s == getSequenceAt(i))
470 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
472 public int findIndex(SearchResults results)
476 while (i < sequences.size())
478 if (results.involvesSequence(getSequenceAt(i)))
490 * @return DOCUMENT ME!
492 public int getHeight()
494 return sequences.size();
500 * @return DOCUMENT ME!
502 public int getWidth()
506 for (int i = 0; i < sequences.size(); i++)
508 if (getSequenceAt(i).getLength() > maxLength)
510 maxLength = getSequenceAt(i).getLength();
523 public void setGapCharacter(char gc)
527 for (int i = 0; i < sequences.size(); i++)
529 Sequence seq = (Sequence) sequences.elementAt(i);
530 seq.setSequence(seq.getSequenceAsString().replace('.', gc).replace(
531 '-', gc).replace(' ', gc));
538 * @return DOCUMENT ME!
540 public char getGapCharacter()
548 * @return DOCUMENT ME!
550 public boolean isAligned()
552 int width = getWidth();
554 for (int i = 0; i < sequences.size(); i++)
556 if (getSequenceAt(i).getLength() != width)
568 * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
570 public boolean deleteAnnotation(AlignmentAnnotation aa)
574 if (annotations != null)
576 aSize = annotations.length;
584 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
586 boolean swap = false;
589 for (int i = 0; i < aSize; i++)
591 if (annotations[i] == aa)
596 if (tIndex < temp.length)
597 temp[tIndex++] = annotations[i];
603 if (aa.sequenceRef != null)
604 aa.sequenceRef.removeAlignmentAnnotation(aa);
615 public void addAnnotation(AlignmentAnnotation aa)
618 if (annotations != null)
620 aSize = annotations.length + 1;
623 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
625 temp[aSize - 1] = aa;
631 for (i = 0; i < (aSize - 1); i++)
633 temp[i] = annotations[i];
640 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
642 if (aa == null || annotations == null || annotations.length - 1 < index)
647 int aSize = annotations.length;
648 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
652 for (int i = 0; i < aSize; i++)
661 temp[i] = annotations[i];
665 temp[i] = annotations[i - 1];
675 * @return DOCUMENT ME!
677 public AlignmentAnnotation[] getAlignmentAnnotation()
682 public void setNucleotide(boolean b)
694 public boolean isNucleotide()
696 if (type == NUCLEOTIDE)
706 public void setDataset(Alignment data)
708 if (dataset == null && data == null)
710 // Create a new dataset for this alignment.
711 // Can only be done once, if dataset is not null
712 // This will not be performed
713 SequenceI[] seqs = new SequenceI[getHeight()];
714 SequenceI currentSeq;
715 for (int i = 0; i < getHeight(); i++)
717 currentSeq = getSequenceAt(i);
718 if (currentSeq.getDatasetSequence() != null)
720 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
724 seqs[i] = currentSeq.createDatasetSequence();
728 dataset = new Alignment(seqs);
730 else if (dataset == null && data != null)
734 dataset.addAlignmentRef();
738 * reference count for number of alignments referencing this one.
740 int alignmentRefs = 0;
743 * increase reference count to this alignment.
745 private void addAlignmentRef()
750 public Alignment getDataset()
755 public boolean padGaps()
757 boolean modified = false;
759 // Remove excess gaps from the end of alignment
763 for (int i = 0; i < sequences.size(); i++)
765 current = getSequenceAt(i);
766 for (int j = current.getLength(); j > maxLength; j--)
769 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
780 for (int i = 0; i < sequences.size(); i++)
782 current = getSequenceAt(i);
783 cLength = current.getLength();
785 if (cLength < maxLength)
787 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
790 else if (current.getLength() > maxLength)
792 current.deleteChars(maxLength, current.getLength());
798 * Justify the sequences to the left or right by deleting and inserting gaps before the initial residue or after the terminal residue
799 * @param right true if alignment padded to right, false to justify to left
800 * @return true if alignment was changed
802 public boolean justify(boolean right)
804 boolean modified = false;
806 // Remove excess gaps from the end of alignment
808 int ends[] = new int[sequences.size()*2];
810 for (int i = 0; i < sequences.size(); i++)
812 current = getSequenceAt(i);
813 // This should really be a sequence method
814 ends[i*2] = current.findIndex(current.getStart());
815 ends[i*2+1] = current.findIndex(current.getStart()+current.getLength());
816 boolean hitres=false;
817 for (int j = 0,rs=0,ssiz=current.getLength(); j<ssiz; j++)
819 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
827 if (j-ends[i*2]>maxLength)
829 maxLength = j-ends[i*2];
837 // now edit the flanking gaps to justify to either left or right
838 int cLength,extent,diff;
839 for (int i = 0; i < sequences.size(); i++)
841 current = getSequenceAt(i);
843 cLength = 1+ends[i*2+1]-ends[i*2];
844 diff = maxLength-cLength; // number of gaps to indent
845 extent = current.getLength();
849 if (extent>ends[i*2+1])
851 current.deleteChars(ends[i*2+1]+1, extent);
856 current.deleteChars(0, ends[i*2]-diff);
861 current.insertCharAt(0, diff-ends[i*2],gapCharacter);
869 current.deleteChars(0, ends[i*2]);
871 ends[i*2+1]-=ends[i*2];
874 if (extent>maxLength)
876 current.deleteChars(maxLength+1, extent);
879 if (extent<maxLength)
881 current.insertCharAt(extent, maxLength-extent,gapCharacter);
890 public HiddenSequences getHiddenSequences()
892 return hiddenSequences;
895 public CigarArray getCompactAlignment()
897 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
898 for (int i = 0; i < sequences.size(); i++)
900 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
902 CigarArray cal = new CigarArray(alseqs);
903 cal.addOperation(CigarArray.M, getWidth());
907 public void setProperty(Object key, Object value)
909 if (alignmentProperties == null)
910 alignmentProperties = new Hashtable();
912 alignmentProperties.put(key, value);
915 public Object getProperty(Object key)
917 if (alignmentProperties != null)
918 return alignmentProperties.get(key);
923 public Hashtable getProperties()
925 return alignmentProperties;
928 AlignedCodonFrame[] codonFrameList = null;
933 * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
935 public void addCodonFrame(AlignedCodonFrame codons)
939 if (codonFrameList == null)
941 codonFrameList = new AlignedCodonFrame[]
945 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
946 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
947 t[codonFrameList.length] = codons;
954 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
956 public AlignedCodonFrame getCodonFrame(int index)
958 return codonFrameList[index];
964 * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
966 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
968 if (seq == null || codonFrameList == null)
970 Vector cframes = new Vector();
971 for (int f = 0; f < codonFrameList.length; f++)
973 if (codonFrameList[f].involvesSequence(seq))
974 cframes.addElement(codonFrameList[f]);
976 if (cframes.size() == 0)
978 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
979 cframes.copyInto(cfr);
986 * @see jalview.datamodel.AlignmentI#getCodonFrames()
988 public AlignedCodonFrame[] getCodonFrames()
990 return codonFrameList;
996 * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
998 public boolean removeCodonFrame(AlignedCodonFrame codons)
1000 if (codons == null || codonFrameList == null)
1002 boolean removed = false;
1003 int i = 0, iSize = codonFrameList.length;
1006 if (codonFrameList[i] == codons)
1011 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize
1024 public void append(AlignmentI toappend)
1026 // TODO test this method for a future 2.5 release
1027 // currently tested for use in jalview.gui.SequenceFetcher
1028 boolean samegap = toappend.getGapCharacter()==getGapCharacter();
1029 char oldc = toappend.getGapCharacter();
1030 boolean hashidden = toappend.getHiddenSequences()!=null && toappend.getHiddenSequences().hiddenSequences!=null;
1031 // get all sequences including any hidden ones
1032 Vector sqs = (hashidden) ? toappend.getHiddenSequences().getFullAlignment().getSequences() : toappend.getSequences();
1035 Enumeration sq = sqs.elements();
1036 while (sq.hasMoreElements())
1038 SequenceI addedsq=(SequenceI) sq.nextElement();
1041 char[] oldseq = addedsq.getSequence();
1042 for (int c=0;c<oldseq.length;c++)
1044 if (oldseq[c]==oldc)
1046 oldseq[c] = gapCharacter;
1050 addSequence(addedsq);
1053 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1054 for (int a = 0; alan != null && a < alan.length; a++)
1056 addAnnotation(alan[a]);
1058 AlignedCodonFrame[] acod = toappend.getCodonFrames();
1059 for (int a = 0; acod != null && a < acod.length; a++)
1061 this.addCodonFrame(acod[a]);
1063 Vector sg = toappend.getGroups();
1066 Enumeration el = sg.elements();
1067 while (el.hasMoreElements())
1069 addGroup((SequenceGroup) el.nextElement());
1072 if (toappend.getHiddenSequences()!=null)
1074 HiddenSequences hs = toappend.getHiddenSequences();
1075 if (hiddenSequences==null)
1077 hiddenSequences = new HiddenSequences(this);
1079 if (hs.hiddenSequences!=null)
1081 for (int s=0;s<hs.hiddenSequences.length; s++)
1083 // hide the newly appended sequence in the alignment
1084 if (hs.hiddenSequences[s]!=null)
1086 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1091 if (toappend.getProperties()!=null)
1093 // we really can't do very much here - just try to concatenate strings where property collisions occur.
1094 Enumeration key = toappend.getProperties().keys();
1095 while (key.hasMoreElements())
1097 Object k = key.nextElement();
1098 Object ourval = this.getProperty(k);
1099 Object toapprop = toappend.getProperty(k);
1102 if (ourval.getClass().equals(toapprop.getClass()) && !ourval.equals(toapprop))
1104 if (ourval instanceof String)
1107 this.setProperty(k, ((String) ourval)+"; "+((String) toapprop));
1109 if (ourval instanceof Vector)
1112 Enumeration theirv = ((Vector) toapprop).elements();
1113 while (theirv.hasMoreElements())
1115 ((Vector)ourval).addElement(theirv);
1121 // just add new property directly
1122 setProperty(k, toapprop);