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)
339 return findName(name,false);
343 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
345 public SequenceI findName(String token, boolean b)
351 while (i < sequences.size())
353 sq = getSequenceAt(i);
354 sqname = sq.getName();
355 if (sqname.equals(token) // exact match
356 || (b && // allow imperfect matches - case varies
357 (sqname.equalsIgnoreCase(token))))
359 return getSequenceAt(i);
367 public SequenceI[] findSequenceMatch(String name)
369 Vector matches = new Vector();
372 while (i < sequences.size())
374 if (getSequenceAt(i).getName().equals(name))
376 matches.addElement(getSequenceAt(i));
381 SequenceI[] result = new SequenceI[matches.size()];
382 for (i = 0; i < result.length; i++)
384 result[i] = (SequenceI) matches.elementAt(i);
392 public int findIndex(SequenceI s)
396 while (i < sequences.size())
398 if (s == getSequenceAt(i))
412 * @return DOCUMENT ME!
414 public int getHeight()
416 return sequences.size();
422 * @return DOCUMENT ME!
424 public int getWidth()
428 for (int i = 0; i < sequences.size(); i++)
430 if (getSequenceAt(i).getLength() > maxLength)
432 maxLength = getSequenceAt(i).getLength();
442 * @param gc DOCUMENT ME!
444 public void setGapCharacter(char gc)
448 for (int i = 0; i < sequences.size(); i++)
450 Sequence seq = (Sequence) sequences.elementAt(i);
451 seq.setSequence(seq.getSequenceAsString()
462 * @return DOCUMENT ME!
464 public char getGapCharacter()
472 * @return DOCUMENT ME!
474 public boolean isAligned()
476 int width = getWidth();
478 for (int i = 0; i < sequences.size(); i++)
480 if (getSequenceAt(i).getLength() != width)
489 * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
491 public boolean deleteAnnotation(AlignmentAnnotation aa)
495 if (annotations != null)
497 aSize = annotations.length;
505 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
510 for (int i = 0; i < aSize; i++)
512 if (annotations[i] == aa)
517 if (tIndex<temp.length)
518 temp[tIndex++] = annotations[i];
524 if(aa.sequenceRef!=null)
525 aa.sequenceRef.removeAlignmentAnnotation(aa);
533 * @param aa DOCUMENT ME!
535 public void addAnnotation(AlignmentAnnotation aa)
538 if (annotations != null)
540 aSize = annotations.length + 1;
543 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
545 temp[aSize - 1] = aa;
551 for (i = 0; i < (aSize - 1); i++)
553 temp[i] = annotations[i];
560 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
562 if (aa == null || annotations == null || annotations.length - 1 < index)
567 int aSize = annotations.length;
568 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
572 for (int i = 0; i < aSize; i++)
581 temp[i] = annotations[i];
585 temp[i] = annotations[i - 1];
595 * @return DOCUMENT ME!
597 public AlignmentAnnotation[] getAlignmentAnnotation()
602 public void setNucleotide(boolean b)
614 public boolean isNucleotide()
616 if (type == NUCLEOTIDE)
626 public void setDataset(Alignment data)
628 if (dataset == null && data == null)
630 // Create a new dataset for this alignment.
631 // Can only be done once, if dataset is not null
632 // This will not be performed
633 SequenceI[] seqs = new SequenceI[getHeight()];
634 SequenceI currentSeq;
635 for (int i = 0; i < getHeight(); i++)
637 currentSeq = getSequenceAt(i);
638 if (currentSeq.getDatasetSequence() != null)
640 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
644 seqs[i] = currentSeq.createDatasetSequence();
648 dataset = new Alignment(seqs);
650 else if (dataset == null && data != null)
654 dataset.addAlignmentRef();
657 * reference count for number of alignments referencing this one.
661 * increase reference count to this alignment.
663 private void addAlignmentRef()
668 public Alignment getDataset()
673 public boolean padGaps()
675 boolean modified = false;
677 //Remove excess gaps from the end of alignment
681 for (int i = 0; i < sequences.size(); i++)
683 current = getSequenceAt(i);
684 for (int j = current.getLength(); j > maxLength; j--)
686 if (j > maxLength && !jalview.util.Comparison.isGap(
687 current.getCharAt(j)))
698 for (int i = 0; i < sequences.size();
701 current = getSequenceAt(i);
702 cLength = current.getLength();
704 if (cLength < maxLength)
706 current.insertCharAt(cLength,
707 maxLength - cLength, gapCharacter);
710 else if (current.getLength() > maxLength)
712 current.deleteChars(maxLength, current.getLength());
718 public HiddenSequences getHiddenSequences()
720 return hiddenSequences;
723 public CigarArray getCompactAlignment()
725 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
726 for (int i = 0; i < sequences.size(); i++)
728 alseqs[i] = new SeqCigar( (SequenceI) sequences.elementAt(i));
730 CigarArray cal = new CigarArray(alseqs);
731 cal.addOperation(CigarArray.M, getWidth());
735 public void setProperty(Object key, Object value)
737 if(alignmentProperties==null)
738 alignmentProperties = new Hashtable();
740 alignmentProperties.put(key,value);
743 public Object getProperty(Object key)
745 if(alignmentProperties!=null)
746 return alignmentProperties.get(key);
751 public Hashtable getProperties()
753 return alignmentProperties;
755 AlignedCodonFrame[] codonFrameList=null;
757 * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
759 public void addCodonFrame(AlignedCodonFrame codons)
763 if (codonFrameList==null)
765 codonFrameList = new AlignedCodonFrame[] { codons };
768 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length+1];
769 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
770 t[codonFrameList.length] = codons;
775 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
777 public AlignedCodonFrame getCodonFrame(int index)
779 return codonFrameList[index];
783 * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
785 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
787 if (seq==null || codonFrameList==null)
789 Vector cframes=new Vector();
790 for (int f=0;f<codonFrameList.length; f++)
792 if (codonFrameList[f].involvesSequence(seq))
793 cframes.addElement(codonFrameList[f]);
795 if (cframes.size()==0)
797 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
798 cframes.copyInto(cfr);
803 * @see jalview.datamodel.AlignmentI#getCodonFrames()
805 public AlignedCodonFrame[] getCodonFrames()
807 return codonFrameList;
811 * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
813 public boolean removeCodonFrame(AlignedCodonFrame codons)
815 if (codons==null || codonFrameList==null)
817 boolean removed=false;
818 int i=0,iSize=codonFrameList.length;
821 if (codonFrameList[i]==codons)
826 System.arraycopy(codonFrameList,i+1,codonFrameList, i, iSize-i-1);