2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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.*;
25 /** Data structure to hold and manipulate a multiple sequence alignment
27 public class Alignment
30 protected Alignment dataset;
31 protected Vector sequences;
32 protected Vector groups = new Vector();
33 protected char gapCharacter = '-';
34 protected int type = NUCLEOTIDE;
35 public static final int PROTEIN = 0;
36 public static final int NUCLEOTIDE = 1;
39 public AlignmentAnnotation[] annotations;
41 HiddenSequences hiddenSequences = new HiddenSequences(this);
43 public Hashtable alignmentProperties;
45 private void initAlignment(SequenceI[] seqs)
49 if (jalview.util.Comparison.isNucleotide(seqs))
58 sequences = new Vector();
60 for (i = 0; i < seqs.length; i++)
62 sequences.addElement(seqs[i]);
67 /** Make an alignment from an array of Sequences.
71 public Alignment(SequenceI[] seqs)
77 * Make a new alignment from an array of SeqCigars
78 * @param seqs SeqCigar[]
80 public Alignment(SeqCigar[] alseqs)
82 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs, gapCharacter,
83 new ColumnSelection(), null);
88 * Make a new alignment from an CigarArray
89 * JBPNote - can only do this when compactAlignment does not contain hidden regions.
90 * JBPNote - must also check that compactAlignment resolves to a set of SeqCigars - or construct them appropriately.
91 * @param compactAlignment CigarArray
93 public static AlignmentI createAlignment(CigarArray compactAlignment)
95 throw new Error("Alignment(CigarArray) not yet implemented");
96 // this(compactAlignment.refCigars);
102 * @return DOCUMENT ME!
104 public Vector getSequences()
109 public SequenceI[] getSequencesArray()
113 SequenceI[] reply = new SequenceI[sequences.size()];
114 for (int i = 0; i < sequences.size(); i++)
116 reply[i] = (SequenceI) sequences.elementAt(i);
124 * @param i DOCUMENT ME!
126 * @return DOCUMENT ME!
128 public SequenceI getSequenceAt(int i)
130 if (i < sequences.size())
132 return (SequenceI) sequences.elementAt(i);
138 /** Adds a sequence to the alignment. Recalculates maxLength and size.
142 public void addSequence(SequenceI snew)
146 // maintain dataset integrity
147 if (snew.getDatasetSequence() != null)
149 getDataset().addSequence(snew.getDatasetSequence());
153 // derive new sequence
154 SequenceI adding = snew.deriveSequence();
155 getDataset().addSequence(adding.getDatasetSequence());
159 if (sequences==null) {
160 initAlignment(new SequenceI[] { snew });
162 sequences.addElement(snew);
164 if (hiddenSequences!=null)
165 hiddenSequences.adjustHeightSequenceAdded();
168 /** Adds a sequence to the alignment. Recalculates maxLength and size.
172 public void setSequenceAt(int i, SequenceI snew)
174 SequenceI oldseq = getSequenceAt(i);
175 deleteSequence(oldseq);
177 sequences.setElementAt(snew, i);
183 * @return DOCUMENT ME!
185 public Vector getGroups()
190 public void finalize()
192 if(getDataset()!=null)
193 getDataset().removeAlignmentRef();
199 hiddenSequences = null;
203 * decrement the alignmentRefs counter by one and call finalize if it goes to zero.
205 private void removeAlignmentRef()
207 if (--alignmentRefs==0)
216 * @param s DOCUMENT ME!
218 public void deleteSequence(SequenceI s)
220 deleteSequence(findIndex(s));
226 * @param i DOCUMENT ME!
228 public void deleteSequence(int i)
230 if (i > -1 && i < getHeight())
232 sequences.removeElementAt(i);
233 hiddenSequences.adjustHeightSequenceDeleted(i);
238 public SequenceGroup findGroup(SequenceI s)
240 for (int i = 0; i < this.groups.size(); i++)
242 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
244 if (sg.getSequences(null).contains(s))
256 * @param s DOCUMENT ME!
258 * @return DOCUMENT ME!
260 public SequenceGroup[] findAllGroups(SequenceI s)
262 Vector temp = new Vector();
264 int gSize = groups.size();
265 for (int i = 0; i < gSize; i++)
267 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
268 if (sg == null || sg.getSequences(null) == null)
270 this.deleteGroup(sg);
275 if (sg.getSequences(null).contains(s))
281 SequenceGroup[] ret = new SequenceGroup[temp.size()];
283 for (int i = 0; i < temp.size(); i++)
285 ret[i] = (SequenceGroup) temp.elementAt(i);
292 public void addGroup(SequenceGroup sg)
294 if (!groups.contains(sg))
296 if (hiddenSequences.getSize() > 0)
298 int i, iSize = sg.getSize();
299 for (i = 0; i < iSize; i++)
301 if (!sequences.contains(sg.getSequenceAt(i)))
303 sg.deleteSequence(sg.getSequenceAt(i), false);
309 if (sg.getSize() < 1)
315 groups.addElement(sg);
322 public void deleteAllGroups()
324 groups.removeAllElements();
328 public void deleteGroup(SequenceGroup g)
330 if (groups.contains(g))
332 groups.removeElement(g);
337 public SequenceI findName(String name)
341 while (i < sequences.size())
343 if (getSequenceAt(i).getName().equals(name))
345 return getSequenceAt(i);
354 public SequenceI[] findSequenceMatch(String name)
356 Vector matches = new Vector();
359 while (i < sequences.size())
361 if (getSequenceAt(i).getName().equals(name))
363 matches.addElement(getSequenceAt(i));
368 SequenceI[] result = new SequenceI[matches.size()];
369 for (i = 0; i < result.length; i++)
371 result[i] = (SequenceI) matches.elementAt(i);
379 public int findIndex(SequenceI s)
383 while (i < sequences.size())
385 if (s == getSequenceAt(i))
399 * @return DOCUMENT ME!
401 public int getHeight()
403 return sequences.size();
409 * @return DOCUMENT ME!
411 public int getWidth()
415 for (int i = 0; i < sequences.size(); i++)
417 if (getSequenceAt(i).getLength() > maxLength)
419 maxLength = getSequenceAt(i).getLength();
429 * @param gc DOCUMENT ME!
431 public void setGapCharacter(char gc)
435 for (int i = 0; i < sequences.size(); i++)
437 Sequence seq = (Sequence) sequences.elementAt(i);
438 seq.setSequence(seq.getSequenceAsString()
449 * @return DOCUMENT ME!
451 public char getGapCharacter()
459 * @return DOCUMENT ME!
461 public boolean isAligned()
463 int width = getWidth();
465 for (int i = 0; i < sequences.size(); i++)
467 if (getSequenceAt(i).getLength() != width)
476 * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
478 public boolean deleteAnnotation(AlignmentAnnotation aa)
482 if (annotations != null)
484 aSize = annotations.length;
492 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
497 for (int i = 0; i < aSize; i++)
499 if (annotations[i] == aa)
504 if (tIndex<temp.length)
505 temp[tIndex++] = annotations[i];
511 if(aa.sequenceRef!=null)
512 aa.sequenceRef.removeAlignmentAnnotation(aa);
520 * @param aa DOCUMENT ME!
522 public void addAnnotation(AlignmentAnnotation aa)
525 if (annotations != null)
527 aSize = annotations.length + 1;
530 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
532 temp[aSize - 1] = aa;
538 for (i = 0; i < (aSize - 1); i++)
540 temp[i] = annotations[i];
547 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
549 if (aa == null || annotations == null || annotations.length - 1 < index)
554 int aSize = annotations.length;
555 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
559 for (int i = 0; i < aSize; i++)
568 temp[i] = annotations[i];
572 temp[i] = annotations[i - 1];
582 * @return DOCUMENT ME!
584 public AlignmentAnnotation[] getAlignmentAnnotation()
589 public void setNucleotide(boolean b)
601 public boolean isNucleotide()
603 if (type == NUCLEOTIDE)
613 public void setDataset(Alignment data)
615 if (dataset == null && data == null)
617 // Create a new dataset for this alignment.
618 // Can only be done once, if dataset is not null
619 // This will not be performed
620 SequenceI[] seqs = new SequenceI[getHeight()];
621 SequenceI currentSeq;
622 for (int i = 0; i < getHeight(); i++)
624 currentSeq = getSequenceAt(i);
625 if (currentSeq.getDatasetSequence() != null)
627 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
631 seqs[i] = currentSeq.createDatasetSequence();
635 dataset = new Alignment(seqs);
637 else if (dataset == null && data != null)
641 dataset.addAlignmentRef();
644 * reference count for number of alignments referencing this one.
648 * increase reference count to this alignment.
650 private void addAlignmentRef()
655 public Alignment getDataset()
660 public boolean padGaps()
662 boolean modified = false;
664 //Remove excess gaps from the end of alignment
668 for (int i = 0; i < sequences.size(); i++)
670 current = getSequenceAt(i);
671 for (int j = current.getLength(); j > maxLength; j--)
673 if (j > maxLength && !jalview.util.Comparison.isGap(
674 current.getCharAt(j)))
685 for (int i = 0; i < sequences.size();
688 current = getSequenceAt(i);
689 cLength = current.getLength();
691 if (cLength < maxLength)
693 current.insertCharAt(cLength,
694 maxLength - cLength, gapCharacter);
697 else if (current.getLength() > maxLength)
699 current.deleteChars(maxLength, current.getLength());
705 public HiddenSequences getHiddenSequences()
707 return hiddenSequences;
710 public CigarArray getCompactAlignment()
712 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
713 for (int i = 0; i < sequences.size(); i++)
715 alseqs[i] = new SeqCigar( (SequenceI) sequences.elementAt(i));
717 CigarArray cal = new CigarArray(alseqs);
718 cal.addOperation(CigarArray.M, getWidth());
722 public void setProperty(Object key, Object value)
724 if(alignmentProperties==null)
725 alignmentProperties = new Hashtable();
727 alignmentProperties.put(key,value);
730 public Object getProperty(Object key)
732 if(alignmentProperties!=null)
733 return alignmentProperties.get(key);
738 public Hashtable getProperties()
740 return alignmentProperties;
742 AlignedCodonFrame[] codonFrameList=null;
744 * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
746 public void addCodonFrame(AlignedCodonFrame codons)
750 if (codonFrameList==null)
752 codonFrameList = new AlignedCodonFrame[] { codons };
755 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length+1];
756 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
757 t[codonFrameList.length] = codons;
762 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
764 public AlignedCodonFrame getCodonFrame(int index)
766 return codonFrameList[index];
770 * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
772 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
774 if (seq==null || codonFrameList==null)
776 Vector cframes=new Vector();
777 for (int f=0;f<codonFrameList.length; f++)
779 if (codonFrameList[f].involvesSequence(seq))
780 cframes.addElement(codonFrameList[f]);
782 if (cframes.size()==0)
784 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
785 cframes.copyInto(cfr);
790 * @see jalview.datamodel.AlignmentI#getCodonFrames()
792 public AlignedCodonFrame[] getCodonFrames()
794 return codonFrameList;
798 * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
800 public boolean removeCodonFrame(AlignedCodonFrame codons)
802 if (codons==null || codonFrameList==null)
804 boolean removed=false;
805 int i=0,iSize=codonFrameList.length;
808 if (codonFrameList[i]==codons)
813 System.arraycopy(codonFrameList,i+1,codonFrameList, i, iSize-i-1);