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);
445 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
447 public int findIndex(SequenceI s)
451 while (i < sequences.size())
453 if (s == getSequenceAt(i))
464 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
466 public int findIndex(SearchResults results)
470 while (i<sequences.size())
472 if (results.involvesSequence(getSequenceAt(i)))
484 * @return DOCUMENT ME!
486 public int getHeight()
488 return sequences.size();
494 * @return DOCUMENT ME!
496 public int getWidth()
500 for (int i = 0; i < sequences.size(); i++)
502 if (getSequenceAt(i).getLength() > maxLength)
504 maxLength = getSequenceAt(i).getLength();
517 public void setGapCharacter(char gc)
521 for (int i = 0; i < sequences.size(); i++)
523 Sequence seq = (Sequence) sequences.elementAt(i);
524 seq.setSequence(seq.getSequenceAsString().replace('.', gc).replace(
525 '-', gc).replace(' ', gc));
532 * @return DOCUMENT ME!
534 public char getGapCharacter()
542 * @return DOCUMENT ME!
544 public boolean isAligned()
546 int width = getWidth();
548 for (int i = 0; i < sequences.size(); i++)
550 if (getSequenceAt(i).getLength() != width)
562 * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
564 public boolean deleteAnnotation(AlignmentAnnotation aa)
568 if (annotations != null)
570 aSize = annotations.length;
578 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
580 boolean swap = false;
583 for (int i = 0; i < aSize; i++)
585 if (annotations[i] == aa)
590 if (tIndex < temp.length)
591 temp[tIndex++] = annotations[i];
597 if (aa.sequenceRef != null)
598 aa.sequenceRef.removeAlignmentAnnotation(aa);
609 public void addAnnotation(AlignmentAnnotation aa)
612 if (annotations != null)
614 aSize = annotations.length + 1;
617 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
619 temp[aSize - 1] = aa;
625 for (i = 0; i < (aSize - 1); i++)
627 temp[i] = annotations[i];
634 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
636 if (aa == null || annotations == null || annotations.length - 1 < index)
641 int aSize = annotations.length;
642 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
646 for (int i = 0; i < aSize; i++)
655 temp[i] = annotations[i];
659 temp[i] = annotations[i - 1];
669 * @return DOCUMENT ME!
671 public AlignmentAnnotation[] getAlignmentAnnotation()
676 public void setNucleotide(boolean b)
688 public boolean isNucleotide()
690 if (type == NUCLEOTIDE)
700 public void setDataset(Alignment data)
702 if (dataset == null && data == null)
704 // Create a new dataset for this alignment.
705 // Can only be done once, if dataset is not null
706 // This will not be performed
707 SequenceI[] seqs = new SequenceI[getHeight()];
708 SequenceI currentSeq;
709 for (int i = 0; i < getHeight(); i++)
711 currentSeq = getSequenceAt(i);
712 if (currentSeq.getDatasetSequence() != null)
714 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
718 seqs[i] = currentSeq.createDatasetSequence();
722 dataset = new Alignment(seqs);
724 else if (dataset == null && data != null)
728 dataset.addAlignmentRef();
732 * reference count for number of alignments referencing this one.
734 int alignmentRefs = 0;
737 * increase reference count to this alignment.
739 private void addAlignmentRef()
744 public Alignment getDataset()
749 public boolean padGaps()
751 boolean modified = false;
753 // Remove excess gaps from the end of alignment
757 for (int i = 0; i < sequences.size(); i++)
759 current = getSequenceAt(i);
760 for (int j = current.getLength(); j > maxLength; j--)
763 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
774 for (int i = 0; i < sequences.size(); i++)
776 current = getSequenceAt(i);
777 cLength = current.getLength();
779 if (cLength < maxLength)
781 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
784 else if (current.getLength() > maxLength)
786 current.deleteChars(maxLength, current.getLength());
792 public HiddenSequences getHiddenSequences()
794 return hiddenSequences;
797 public CigarArray getCompactAlignment()
799 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
800 for (int i = 0; i < sequences.size(); i++)
802 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
804 CigarArray cal = new CigarArray(alseqs);
805 cal.addOperation(CigarArray.M, getWidth());
809 public void setProperty(Object key, Object value)
811 if (alignmentProperties == null)
812 alignmentProperties = new Hashtable();
814 alignmentProperties.put(key, value);
817 public Object getProperty(Object key)
819 if (alignmentProperties != null)
820 return alignmentProperties.get(key);
825 public Hashtable getProperties()
827 return alignmentProperties;
830 AlignedCodonFrame[] codonFrameList = null;
835 * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
837 public void addCodonFrame(AlignedCodonFrame codons)
841 if (codonFrameList == null)
843 codonFrameList = new AlignedCodonFrame[]
847 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
848 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
849 t[codonFrameList.length] = codons;
856 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
858 public AlignedCodonFrame getCodonFrame(int index)
860 return codonFrameList[index];
866 * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
868 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
870 if (seq == null || codonFrameList == null)
872 Vector cframes = new Vector();
873 for (int f = 0; f < codonFrameList.length; f++)
875 if (codonFrameList[f].involvesSequence(seq))
876 cframes.addElement(codonFrameList[f]);
878 if (cframes.size() == 0)
880 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
881 cframes.copyInto(cfr);
888 * @see jalview.datamodel.AlignmentI#getCodonFrames()
890 public AlignedCodonFrame[] getCodonFrames()
892 return codonFrameList;
898 * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
900 public boolean removeCodonFrame(AlignedCodonFrame codons)
902 if (codons == null || codonFrameList == null)
904 boolean removed = false;
905 int i = 0, iSize = codonFrameList.length;
908 if (codonFrameList[i] == codons)
913 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize