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().finalize();
199 hiddenSequences = null;
206 * @param s DOCUMENT ME!
208 public void deleteSequence(SequenceI s)
210 deleteSequence(findIndex(s));
216 * @param i DOCUMENT ME!
218 public void deleteSequence(int i)
220 if (i > -1 && i < getHeight())
222 sequences.removeElementAt(i);
223 hiddenSequences.adjustHeightSequenceDeleted(i);
228 public SequenceGroup findGroup(SequenceI s)
230 for (int i = 0; i < this.groups.size(); i++)
232 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
234 if (sg.getSequences(null).contains(s))
246 * @param s DOCUMENT ME!
248 * @return DOCUMENT ME!
250 public SequenceGroup[] findAllGroups(SequenceI s)
252 Vector temp = new Vector();
254 int gSize = groups.size();
255 for (int i = 0; i < gSize; i++)
257 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
258 if (sg == null || sg.getSequences(null) == null)
260 this.deleteGroup(sg);
265 if (sg.getSequences(null).contains(s))
271 SequenceGroup[] ret = new SequenceGroup[temp.size()];
273 for (int i = 0; i < temp.size(); i++)
275 ret[i] = (SequenceGroup) temp.elementAt(i);
282 public void addGroup(SequenceGroup sg)
284 if (!groups.contains(sg))
286 if (hiddenSequences.getSize() > 0)
288 int i, iSize = sg.getSize();
289 for (i = 0; i < iSize; i++)
291 if (!sequences.contains(sg.getSequenceAt(i)))
293 sg.deleteSequence(sg.getSequenceAt(i), false);
299 if (sg.getSize() < 1)
305 groups.addElement(sg);
312 public void deleteAllGroups()
314 groups.removeAllElements();
318 public void deleteGroup(SequenceGroup g)
320 if (groups.contains(g))
322 groups.removeElement(g);
327 public SequenceI findName(String name)
331 while (i < sequences.size())
333 if (getSequenceAt(i).getName().equals(name))
335 return getSequenceAt(i);
344 public SequenceI[] findSequenceMatch(String name)
346 Vector matches = new Vector();
349 while (i < sequences.size())
351 if (getSequenceAt(i).getName().equals(name))
353 matches.addElement(getSequenceAt(i));
358 SequenceI[] result = new SequenceI[matches.size()];
359 for (i = 0; i < result.length; i++)
361 result[i] = (SequenceI) matches.elementAt(i);
369 public int findIndex(SequenceI s)
373 while (i < sequences.size())
375 if (s == getSequenceAt(i))
389 * @return DOCUMENT ME!
391 public int getHeight()
393 return sequences.size();
399 * @return DOCUMENT ME!
401 public int getWidth()
405 for (int i = 0; i < sequences.size(); i++)
407 if (getSequenceAt(i).getLength() > maxLength)
409 maxLength = getSequenceAt(i).getLength();
419 * @param gc DOCUMENT ME!
421 public void setGapCharacter(char gc)
425 for (int i = 0; i < sequences.size(); i++)
427 Sequence seq = (Sequence) sequences.elementAt(i);
428 seq.setSequence(seq.getSequenceAsString()
439 * @return DOCUMENT ME!
441 public char getGapCharacter()
449 * @return DOCUMENT ME!
451 public boolean isAligned()
453 int width = getWidth();
455 for (int i = 0; i < sequences.size(); i++)
457 if (getSequenceAt(i).getLength() != width)
466 * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
468 public boolean deleteAnnotation(AlignmentAnnotation aa)
472 if (annotations != null)
474 aSize = annotations.length;
482 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
487 for (int i = 0; i < aSize; i++)
489 if (annotations[i] == aa)
494 if (tIndex<temp.length)
495 temp[tIndex++] = annotations[i];
501 if(aa.sequenceRef!=null)
502 aa.sequenceRef.removeAlignmentAnnotation(aa);
510 * @param aa DOCUMENT ME!
512 public void addAnnotation(AlignmentAnnotation aa)
515 if (annotations != null)
517 aSize = annotations.length + 1;
520 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
522 temp[aSize - 1] = aa;
528 for (i = 0; i < (aSize - 1); i++)
530 temp[i] = annotations[i];
537 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
539 if (aa == null || annotations == null || annotations.length - 1 < index)
544 int aSize = annotations.length;
545 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
549 for (int i = 0; i < aSize; i++)
558 temp[i] = annotations[i];
562 temp[i] = annotations[i - 1];
572 * @return DOCUMENT ME!
574 public AlignmentAnnotation[] getAlignmentAnnotation()
579 public void setNucleotide(boolean b)
591 public boolean isNucleotide()
593 if (type == NUCLEOTIDE)
603 public void setDataset(Alignment data)
605 if (dataset == null && data == null)
607 // Create a new dataset for this alignment.
608 // Can only be done once, if dataset is not null
609 // This will not be performed
610 Sequence[] seqs = new Sequence[getHeight()];
611 SequenceI currentSeq;
612 for (int i = 0; i < getHeight(); i++)
614 currentSeq = getSequenceAt(i);
615 if (currentSeq.getDatasetSequence() != null)
617 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
621 seqs[i] = new Sequence(currentSeq.getName(),
622 AlignSeq.extractGaps(
623 jalview.util.Comparison.GapChars,
624 currentSeq.getSequenceAsString()
626 currentSeq.getStart(),
627 currentSeq.getEnd());
628 seqs[i].sequenceFeatures = currentSeq.getSequenceFeatures();
629 seqs[i].setDescription(currentSeq.getDescription());
630 getSequenceAt(i).setSequenceFeatures(null);
631 getSequenceAt(i).setDatasetSequence(seqs[i]);
635 dataset = new Alignment(seqs);
637 else if (dataset == null && data != null)
643 public Alignment getDataset()
648 public boolean padGaps()
650 boolean modified = false;
652 //Remove excess gaps from the end of alignment
656 for (int i = 0; i < sequences.size(); i++)
658 current = getSequenceAt(i);
659 for (int j = current.getLength(); j > maxLength; j--)
661 if (j > maxLength && !jalview.util.Comparison.isGap(
662 current.getCharAt(j)))
673 for (int i = 0; i < sequences.size();
676 current = getSequenceAt(i);
677 cLength = current.getLength();
679 if (cLength < maxLength)
681 current.insertCharAt(cLength,
682 maxLength - cLength, gapCharacter);
685 else if (current.getLength() > maxLength)
687 current.deleteChars(maxLength, current.getLength());
693 public HiddenSequences getHiddenSequences()
695 return hiddenSequences;
698 public CigarArray getCompactAlignment()
700 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
701 for (int i = 0; i < sequences.size(); i++)
703 alseqs[i] = new SeqCigar( (SequenceI) sequences.elementAt(i));
705 CigarArray cal = new CigarArray(alseqs);
706 cal.addOperation(CigarArray.M, getWidth());
710 public void setProperty(Object key, Object value)
712 if(alignmentProperties==null)
713 alignmentProperties = new Hashtable();
715 alignmentProperties.put(key,value);
718 public Object getProperty(Object key)
720 if(alignmentProperties!=null)
721 return alignmentProperties.get(key);
726 public Hashtable getProperties()
728 return alignmentProperties;
730 AlignedCodonFrame[] codonFrameList=null;
732 * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
734 public void addCodonFrame(AlignedCodonFrame codons)
738 if (codonFrameList==null)
740 codonFrameList = new AlignedCodonFrame[] { codons };
743 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length+1];
744 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
745 t[codonFrameList.length] = codons;
750 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
752 public AlignedCodonFrame getCodonFrame(int index)
754 return codonFrameList[index];
758 * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
760 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
762 if (seq==null || codonFrameList==null)
764 Vector cframes=new Vector();
765 for (int f=0;f<codonFrameList.length; f++)
767 if (codonFrameList[f].involvesSequence(seq))
768 cframes.addElement(codonFrameList[f]);
770 if (cframes.size()==0)
772 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
773 cframes.copyInto(cfr);
778 * @see jalview.datamodel.AlignmentI#getCodonFrames()
780 public AlignedCodonFrame[] getCodonFrames()
782 return codonFrameList;
786 * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
788 public boolean removeCodonFrame(AlignedCodonFrame codons)
790 if (codons==null || codonFrameList==null)
792 boolean removed=false;
793 int i=0,iSize=codonFrameList.length;
796 if (codonFrameList[i]==codons)
801 System.arraycopy(codonFrameList,i+1,codonFrameList, i, iSize-i-1);