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);
349 public void deleteAllGroups()
351 groups.removeAllElements();
355 public void deleteGroup(SequenceGroup g)
357 if (groups.contains(g))
359 if (annotations!=null && annotations.length>0)
361 if (g.hasAnnotationRows())
362 { // remove any annotation references.
363 deleteAnnotation(g.getConsensus());// todo - create=false flag so we don't create another object unnecessarily
364 deleteAnnotation(g.getConservationRow());
367 groups.removeElement(g);
372 public SequenceI findName(String name)
374 return findName(name, false);
380 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
382 public SequenceI findName(String token, boolean b)
384 return findName(null, token, b);
390 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
393 public SequenceI findName(SequenceI startAfter, String token, boolean b)
398 String sqname = null;
399 if (startAfter != null)
401 // try to find the sequence in the alignment
402 boolean matched = false;
403 while (i < sequences.size())
405 if (getSequenceAt(i++) == startAfter)
416 while (i < sequences.size())
418 sq = getSequenceAt(i);
419 sqname = sq.getName();
420 if (sqname.equals(token) // exact match
421 || (b && // allow imperfect matches - case varies
422 (sqname.equalsIgnoreCase(token))))
424 return getSequenceAt(i);
433 public SequenceI[] findSequenceMatch(String name)
435 Vector matches = new Vector();
438 while (i < sequences.size())
440 if (getSequenceAt(i).getName().equals(name))
442 matches.addElement(getSequenceAt(i));
447 SequenceI[] result = new SequenceI[matches.size()];
448 for (i = 0; i < result.length; i++)
450 result[i] = (SequenceI) matches.elementAt(i);
460 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
462 public int findIndex(SequenceI s)
466 while (i < sequences.size())
468 if (s == getSequenceAt(i))
482 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
484 public int findIndex(SearchResults results)
488 while (i < sequences.size())
490 if (results.involvesSequence(getSequenceAt(i)))
502 * @return DOCUMENT ME!
504 public int getHeight()
506 return sequences.size();
512 * @return DOCUMENT ME!
514 public int getWidth()
518 for (int i = 0; i < sequences.size(); i++)
520 if (getSequenceAt(i).getLength() > maxLength)
522 maxLength = getSequenceAt(i).getLength();
535 public void setGapCharacter(char gc)
539 for (int i = 0; i < sequences.size(); i++)
541 Sequence seq = (Sequence) sequences.elementAt(i);
542 seq.setSequence(seq.getSequenceAsString().replace('.', gc).replace(
543 '-', gc).replace(' ', gc));
550 * @return DOCUMENT ME!
552 public char getGapCharacter()
560 * @return DOCUMENT ME!
562 public boolean isAligned()
564 int width = getWidth();
566 for (int i = 0; i < sequences.size(); i++)
568 if (getSequenceAt(i).getLength() != width)
580 * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
582 public boolean deleteAnnotation(AlignmentAnnotation aa)
586 if (annotations != null)
588 aSize = annotations.length;
596 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
598 boolean swap = false;
601 for (int i = 0; i < aSize; i++)
603 if (annotations[i] == aa)
608 if (tIndex < temp.length)
609 temp[tIndex++] = annotations[i];
615 if (aa.sequenceRef != null)
616 aa.sequenceRef.removeAlignmentAnnotation(aa);
622 * @see jalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.AlignmentAnnotation)
624 public void addAnnotation(AlignmentAnnotation aa)
626 addAnnotation(aa, -1);
630 * @see jalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.AlignmentAnnotation, int)
632 public void addAnnotation(AlignmentAnnotation aa, int pos)
635 if (annotations != null)
637 aSize = annotations.length + 1;
640 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
642 if (pos==-1 || pos>=aSize) {
643 temp[aSize - 1] = aa;
650 for (i = 0; i < (aSize-1); i++,p++)
657 temp[p] = annotations[i];
665 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
667 if (aa == null || annotations == null || annotations.length - 1 < index)
672 int aSize = annotations.length;
673 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
677 for (int i = 0; i < aSize; i++)
686 temp[i] = annotations[i];
690 temp[i] = annotations[i - 1];
700 * @return DOCUMENT ME!
702 public AlignmentAnnotation[] getAlignmentAnnotation()
707 public void setNucleotide(boolean b)
719 public boolean isNucleotide()
721 if (type == NUCLEOTIDE)
731 public void setDataset(Alignment data)
733 if (dataset == null && data == null)
735 // Create a new dataset for this alignment.
736 // Can only be done once, if dataset is not null
737 // This will not be performed
738 SequenceI[] seqs = new SequenceI[getHeight()];
739 SequenceI currentSeq;
740 for (int i = 0; i < getHeight(); i++)
742 currentSeq = getSequenceAt(i);
743 if (currentSeq.getDatasetSequence() != null)
745 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
749 seqs[i] = currentSeq.createDatasetSequence();
753 dataset = new Alignment(seqs);
755 else if (dataset == null && data != null)
759 dataset.addAlignmentRef();
763 * reference count for number of alignments referencing this one.
765 int alignmentRefs = 0;
768 * increase reference count to this alignment.
770 private void addAlignmentRef()
775 public Alignment getDataset()
780 public boolean padGaps()
782 boolean modified = false;
784 // Remove excess gaps from the end of alignment
788 for (int i = 0; i < sequences.size(); i++)
790 current = getSequenceAt(i);
791 for (int j = current.getLength(); j > maxLength; j--)
794 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
805 for (int i = 0; i < sequences.size(); i++)
807 current = getSequenceAt(i);
808 cLength = current.getLength();
810 if (cLength < maxLength)
812 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
815 else if (current.getLength() > maxLength)
817 current.deleteChars(maxLength, current.getLength());
823 * Justify the sequences to the left or right by deleting and inserting gaps before the initial residue or after the terminal residue
824 * @param right true if alignment padded to right, false to justify to left
825 * @return true if alignment was changed
827 public boolean justify(boolean right)
829 boolean modified = false;
831 // Remove excess gaps from the end of alignment
833 int ends[] = new int[sequences.size()*2];
835 for (int i = 0; i < sequences.size(); i++)
837 current = getSequenceAt(i);
838 // This should really be a sequence method
839 ends[i*2] = current.findIndex(current.getStart());
840 ends[i*2+1] = current.findIndex(current.getStart()+current.getLength());
841 boolean hitres=false;
842 for (int j = 0,rs=0,ssiz=current.getLength(); j<ssiz; j++)
844 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
852 if (j-ends[i*2]>maxLength)
854 maxLength = j-ends[i*2];
862 // now edit the flanking gaps to justify to either left or right
863 int cLength,extent,diff;
864 for (int i = 0; i < sequences.size(); i++)
866 current = getSequenceAt(i);
868 cLength = 1+ends[i*2+1]-ends[i*2];
869 diff = maxLength-cLength; // number of gaps to indent
870 extent = current.getLength();
874 if (extent>ends[i*2+1])
876 current.deleteChars(ends[i*2+1]+1, extent);
881 current.deleteChars(0, ends[i*2]-diff);
886 current.insertCharAt(0, diff-ends[i*2],gapCharacter);
894 current.deleteChars(0, ends[i*2]);
896 ends[i*2+1]-=ends[i*2];
899 if (extent>maxLength)
901 current.deleteChars(maxLength+1, extent);
904 if (extent<maxLength)
906 current.insertCharAt(extent, maxLength-extent,gapCharacter);
915 public HiddenSequences getHiddenSequences()
917 return hiddenSequences;
920 public CigarArray getCompactAlignment()
922 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
923 for (int i = 0; i < sequences.size(); i++)
925 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
927 CigarArray cal = new CigarArray(alseqs);
928 cal.addOperation(CigarArray.M, getWidth());
932 public void setProperty(Object key, Object value)
934 if (alignmentProperties == null)
935 alignmentProperties = new Hashtable();
937 alignmentProperties.put(key, value);
940 public Object getProperty(Object key)
942 if (alignmentProperties != null)
943 return alignmentProperties.get(key);
948 public Hashtable getProperties()
950 return alignmentProperties;
953 AlignedCodonFrame[] codonFrameList = null;
958 * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
960 public void addCodonFrame(AlignedCodonFrame codons)
964 if (codonFrameList == null)
966 codonFrameList = new AlignedCodonFrame[]
970 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
971 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
972 t[codonFrameList.length] = codons;
979 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
981 public AlignedCodonFrame getCodonFrame(int index)
983 return codonFrameList[index];
989 * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
991 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
993 if (seq == null || codonFrameList == null)
995 Vector cframes = new Vector();
996 for (int f = 0; f < codonFrameList.length; f++)
998 if (codonFrameList[f].involvesSequence(seq))
999 cframes.addElement(codonFrameList[f]);
1001 if (cframes.size() == 0)
1003 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
1004 cframes.copyInto(cfr);
1011 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1013 public AlignedCodonFrame[] getCodonFrames()
1015 return codonFrameList;
1021 * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
1023 public boolean removeCodonFrame(AlignedCodonFrame codons)
1025 if (codons == null || codonFrameList == null)
1027 boolean removed = false;
1028 int i = 0, iSize = codonFrameList.length;
1031 if (codonFrameList[i] == codons)
1036 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize
1049 public void append(AlignmentI toappend)
1051 // TODO test this method for a future 2.5 release
1052 // currently tested for use in jalview.gui.SequenceFetcher
1053 boolean samegap = toappend.getGapCharacter()==getGapCharacter();
1054 char oldc = toappend.getGapCharacter();
1055 boolean hashidden = toappend.getHiddenSequences()!=null && toappend.getHiddenSequences().hiddenSequences!=null;
1056 // get all sequences including any hidden ones
1057 Vector sqs = (hashidden) ? toappend.getHiddenSequences().getFullAlignment().getSequences() : toappend.getSequences();
1060 Enumeration sq = sqs.elements();
1061 while (sq.hasMoreElements())
1063 SequenceI addedsq=(SequenceI) sq.nextElement();
1066 char[] oldseq = addedsq.getSequence();
1067 for (int c=0;c<oldseq.length;c++)
1069 if (oldseq[c]==oldc)
1071 oldseq[c] = gapCharacter;
1075 addSequence(addedsq);
1078 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1079 for (int a = 0; alan != null && a < alan.length; a++)
1081 addAnnotation(alan[a]);
1083 AlignedCodonFrame[] acod = toappend.getCodonFrames();
1084 for (int a = 0; acod != null && a < acod.length; a++)
1086 this.addCodonFrame(acod[a]);
1088 Vector sg = toappend.getGroups();
1091 Enumeration el = sg.elements();
1092 while (el.hasMoreElements())
1094 addGroup((SequenceGroup) el.nextElement());
1097 if (toappend.getHiddenSequences()!=null)
1099 HiddenSequences hs = toappend.getHiddenSequences();
1100 if (hiddenSequences==null)
1102 hiddenSequences = new HiddenSequences(this);
1104 if (hs.hiddenSequences!=null)
1106 for (int s=0;s<hs.hiddenSequences.length; s++)
1108 // hide the newly appended sequence in the alignment
1109 if (hs.hiddenSequences[s]!=null)
1111 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1116 if (toappend.getProperties()!=null)
1118 // we really can't do very much here - just try to concatenate strings where property collisions occur.
1119 Enumeration key = toappend.getProperties().keys();
1120 while (key.hasMoreElements())
1122 Object k = key.nextElement();
1123 Object ourval = this.getProperty(k);
1124 Object toapprop = toappend.getProperty(k);
1127 if (ourval.getClass().equals(toapprop.getClass()) && !ourval.equals(toapprop))
1129 if (ourval instanceof String)
1132 this.setProperty(k, ((String) ourval)+"; "+((String) toapprop));
1134 if (ourval instanceof Vector)
1137 Enumeration theirv = ((Vector) toapprop).elements();
1138 while (theirv.hasMoreElements())
1140 ((Vector)ourval).addElement(theirv);
1146 // just add new property directly
1147 setProperty(k, toapprop);