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);
446 public int findIndex(SequenceI s)
450 while (i < sequences.size())
452 if (s == getSequenceAt(i))
466 * @return DOCUMENT ME!
468 public int getHeight()
470 return sequences.size();
476 * @return DOCUMENT ME!
478 public int getWidth()
482 for (int i = 0; i < sequences.size(); i++)
484 if (getSequenceAt(i).getLength() > maxLength)
486 maxLength = getSequenceAt(i).getLength();
499 public void setGapCharacter(char gc)
503 for (int i = 0; i < sequences.size(); i++)
505 Sequence seq = (Sequence) sequences.elementAt(i);
506 seq.setSequence(seq.getSequenceAsString().replace('.', gc).replace(
507 '-', gc).replace(' ', gc));
514 * @return DOCUMENT ME!
516 public char getGapCharacter()
524 * @return DOCUMENT ME!
526 public boolean isAligned()
528 int width = getWidth();
530 for (int i = 0; i < sequences.size(); i++)
532 if (getSequenceAt(i).getLength() != width)
544 * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
546 public boolean deleteAnnotation(AlignmentAnnotation aa)
550 if (annotations != null)
552 aSize = annotations.length;
560 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
562 boolean swap = false;
565 for (int i = 0; i < aSize; i++)
567 if (annotations[i] == aa)
572 if (tIndex < temp.length)
573 temp[tIndex++] = annotations[i];
579 if (aa.sequenceRef != null)
580 aa.sequenceRef.removeAlignmentAnnotation(aa);
591 public void addAnnotation(AlignmentAnnotation aa)
594 if (annotations != null)
596 aSize = annotations.length + 1;
599 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
601 temp[aSize - 1] = aa;
607 for (i = 0; i < (aSize - 1); i++)
609 temp[i] = annotations[i];
616 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
618 if (aa == null || annotations == null || annotations.length - 1 < index)
623 int aSize = annotations.length;
624 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
628 for (int i = 0; i < aSize; i++)
637 temp[i] = annotations[i];
641 temp[i] = annotations[i - 1];
651 * @return DOCUMENT ME!
653 public AlignmentAnnotation[] getAlignmentAnnotation()
658 public void setNucleotide(boolean b)
670 public boolean isNucleotide()
672 if (type == NUCLEOTIDE)
682 public void setDataset(Alignment data)
684 if (dataset == null && data == null)
686 // Create a new dataset for this alignment.
687 // Can only be done once, if dataset is not null
688 // This will not be performed
689 SequenceI[] seqs = new SequenceI[getHeight()];
690 SequenceI currentSeq;
691 for (int i = 0; i < getHeight(); i++)
693 currentSeq = getSequenceAt(i);
694 if (currentSeq.getDatasetSequence() != null)
696 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
700 seqs[i] = currentSeq.createDatasetSequence();
704 dataset = new Alignment(seqs);
706 else if (dataset == null && data != null)
710 dataset.addAlignmentRef();
714 * reference count for number of alignments referencing this one.
716 int alignmentRefs = 0;
719 * increase reference count to this alignment.
721 private void addAlignmentRef()
726 public Alignment getDataset()
731 public boolean padGaps()
733 boolean modified = false;
735 // Remove excess gaps from the end of alignment
739 for (int i = 0; i < sequences.size(); i++)
741 current = getSequenceAt(i);
742 for (int j = current.getLength(); j > maxLength; j--)
745 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
756 for (int i = 0; i < sequences.size(); i++)
758 current = getSequenceAt(i);
759 cLength = current.getLength();
761 if (cLength < maxLength)
763 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
766 else if (current.getLength() > maxLength)
768 current.deleteChars(maxLength, current.getLength());
774 public HiddenSequences getHiddenSequences()
776 return hiddenSequences;
779 public CigarArray getCompactAlignment()
781 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
782 for (int i = 0; i < sequences.size(); i++)
784 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
786 CigarArray cal = new CigarArray(alseqs);
787 cal.addOperation(CigarArray.M, getWidth());
791 public void setProperty(Object key, Object value)
793 if (alignmentProperties == null)
794 alignmentProperties = new Hashtable();
796 alignmentProperties.put(key, value);
799 public Object getProperty(Object key)
801 if (alignmentProperties != null)
802 return alignmentProperties.get(key);
807 public Hashtable getProperties()
809 return alignmentProperties;
812 AlignedCodonFrame[] codonFrameList = null;
817 * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
819 public void addCodonFrame(AlignedCodonFrame codons)
823 if (codonFrameList == null)
825 codonFrameList = new AlignedCodonFrame[]
829 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
830 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
831 t[codonFrameList.length] = codons;
838 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
840 public AlignedCodonFrame getCodonFrame(int index)
842 return codonFrameList[index];
848 * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
850 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
852 if (seq == null || codonFrameList == null)
854 Vector cframes = new Vector();
855 for (int f = 0; f < codonFrameList.length; f++)
857 if (codonFrameList[f].involvesSequence(seq))
858 cframes.addElement(codonFrameList[f]);
860 if (cframes.size() == 0)
862 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
863 cframes.copyInto(cfr);
870 * @see jalview.datamodel.AlignmentI#getCodonFrames()
872 public AlignedCodonFrame[] getCodonFrames()
874 return codonFrameList;
880 * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
882 public boolean removeCodonFrame(AlignedCodonFrame codons)
884 if (codons == null || codonFrameList == null)
886 boolean removed = false;
887 int i = 0, iSize = codonFrameList.length;
890 if (codonFrameList[i] == codons)
895 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize