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 private void initAlignment(SequenceI[] seqs)
47 if (jalview.util.Comparison.isNucleotide(seqs))
56 sequences = new Vector();
58 for (i = 0; i < seqs.length; i++)
60 sequences.addElement(seqs[i]);
65 /** Make an alignment from an array of Sequences.
69 public Alignment(SequenceI[] seqs)
75 * Make a new alignment from an array of SeqCigars
76 * @param seqs SeqCigar[]
78 public Alignment(SeqCigar[] alseqs)
80 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs, gapCharacter,
81 new ColumnSelection(), null);
86 * Make a new alignment from an CigarArray
87 * JBPNote - can only do this when compactAlignment does not contain hidden regions.
88 * JBPNote - must also check that compactAlignment resolves to a set of SeqCigars - or construct them appropriately.
89 * @param compactAlignment CigarArray
91 public static AlignmentI createAlignment(CigarArray compactAlignment)
93 throw new Error("Alignment(CigarArray) not yet implemented");
94 // this(compactAlignment.refCigars);
100 * @return DOCUMENT ME!
102 public Vector getSequences()
107 public SequenceI[] getSequencesArray()
109 SequenceI[] reply = new SequenceI[sequences.size()];
110 for (int i = 0; i < sequences.size(); i++)
112 reply[i] = (SequenceI) sequences.elementAt(i);
120 * @param i DOCUMENT ME!
122 * @return DOCUMENT ME!
124 public SequenceI getSequenceAt(int i)
126 if (i < sequences.size())
128 return (SequenceI) sequences.elementAt(i);
134 /** Adds a sequence to the alignment. Recalculates maxLength and size.
138 public void addSequence(SequenceI snew)
142 if (snew.getDatasetSequence() != null)
144 getDataset().addSequence(snew.getDatasetSequence());
148 Sequence ds = new Sequence(snew.getName(),
149 AlignSeq.extractGaps("-. ",
150 snew.getSequenceAsString()),
154 snew.setDatasetSequence(ds);
155 getDataset().addSequence(ds);
158 sequences.addElement(snew);
160 hiddenSequences.adjustHeightSequenceAdded();
163 /** Adds a sequence to the alignment. Recalculates maxLength and size.
167 public void setSequenceAt(int i, SequenceI snew)
169 SequenceI oldseq = getSequenceAt(i);
170 deleteSequence(oldseq);
172 sequences.setElementAt(snew, i);
178 * @return DOCUMENT ME!
180 public Vector getGroups()
185 public void destroyAlignment()
187 if(getDataset()!=null)
188 getDataset().destroyAlignment();
194 hiddenSequences = null;
201 * @param s DOCUMENT ME!
203 public void deleteSequence(SequenceI s)
205 deleteSequence(findIndex(s));
211 * @param i DOCUMENT ME!
213 public void deleteSequence(int i)
215 if (i > -1 && i < getHeight())
217 sequences.removeElementAt(i);
218 hiddenSequences.adjustHeightSequenceDeleted(i);
223 public SequenceGroup findGroup(SequenceI s)
225 for (int i = 0; i < this.groups.size(); i++)
227 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
229 if (sg.getSequences(null).contains(s))
241 * @param s DOCUMENT ME!
243 * @return DOCUMENT ME!
245 public SequenceGroup[] findAllGroups(SequenceI s)
247 Vector temp = new Vector();
249 int gSize = groups.size();
250 for (int i = 0; i < gSize; i++)
252 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
253 if (sg == null || sg.getSequences(null) == null)
255 this.deleteGroup(sg);
260 if (sg.getSequences(null).contains(s))
266 SequenceGroup[] ret = new SequenceGroup[temp.size()];
268 for (int i = 0; i < temp.size(); i++)
270 ret[i] = (SequenceGroup) temp.elementAt(i);
277 public void addGroup(SequenceGroup sg)
279 if (!groups.contains(sg))
281 if (hiddenSequences.getSize() > 0)
283 int i, iSize = sg.getSize();
284 for (i = 0; i < iSize; i++)
286 if (!sequences.contains(sg.getSequenceAt(i)))
288 sg.deleteSequence(sg.getSequenceAt(i), false);
294 if (sg.getSize() < 1)
300 groups.addElement(sg);
307 public void deleteAllGroups()
309 groups.removeAllElements();
313 public void deleteGroup(SequenceGroup g)
315 if (groups.contains(g))
317 groups.removeElement(g);
322 public SequenceI findName(String name)
326 while (i < sequences.size())
328 if (getSequenceAt(i).getName().equals(name))
330 return getSequenceAt(i);
339 public SequenceI[] findSequenceMatch(String name)
341 Vector matches = new Vector();
344 while (i < sequences.size())
346 if (getSequenceAt(i).getName().equals(name))
348 matches.addElement(getSequenceAt(i));
353 SequenceI[] result = new SequenceI[matches.size()];
354 for (i = 0; i < result.length; i++)
356 result[i] = (SequenceI) matches.elementAt(i);
364 public int findIndex(SequenceI s)
368 while (i < sequences.size())
370 if (s == getSequenceAt(i))
384 * @return DOCUMENT ME!
386 public int getHeight()
388 return sequences.size();
394 * @return DOCUMENT ME!
396 public int getWidth()
400 for (int i = 0; i < sequences.size(); i++)
402 if (getSequenceAt(i).getLength() > maxLength)
404 maxLength = getSequenceAt(i).getLength();
414 * @param gc DOCUMENT ME!
416 public void setGapCharacter(char gc)
420 for (int i = 0; i < sequences.size(); i++)
422 Sequence seq = (Sequence) sequences.elementAt(i);
423 seq.setSequence(seq.getSequenceAsString()
434 * @return DOCUMENT ME!
436 public char getGapCharacter()
444 * @return DOCUMENT ME!
446 public boolean isAligned()
448 int width = getWidth();
450 for (int i = 0; i < sequences.size(); i++)
452 if (getSequenceAt(i).getLength() != width)
464 * @param aa DOCUMENT ME!
466 public void deleteAnnotation(AlignmentAnnotation aa)
470 if (annotations != null)
472 aSize = annotations.length;
480 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
484 for (int i = 0; i < aSize; i++)
486 if (annotations[i] == aa)
491 temp[tIndex] = annotations[i];
501 * @param aa DOCUMENT ME!
503 public void addAnnotation(AlignmentAnnotation aa)
506 if (annotations != null)
508 aSize = annotations.length + 1;
511 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
513 temp[aSize - 1] = aa;
519 for (i = 0; i < (aSize - 1); i++)
521 temp[i] = annotations[i];
528 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
530 if (aa == null || annotations == null || annotations.length - 1 < index)
535 int aSize = annotations.length;
536 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
540 for (int i = 0; i < aSize; i++)
549 temp[i] = annotations[i];
553 temp[i] = annotations[i - 1];
563 * @return DOCUMENT ME!
565 public AlignmentAnnotation[] getAlignmentAnnotation()
570 public void setNucleotide(boolean b)
582 public boolean isNucleotide()
584 if (type == NUCLEOTIDE)
594 public void setDataset(Alignment data)
596 if (dataset == null && data == null)
598 // Create a new dataset for this alignment.
599 // Can only be done once, if dataset is not null
600 // This will not be performed
601 Sequence[] seqs = new Sequence[getHeight()];
602 SequenceI currentSeq;
603 for (int i = 0; i < getHeight(); i++)
605 currentSeq = getSequenceAt(i);
606 if (currentSeq.getDatasetSequence() != null)
608 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
612 seqs[i] = new Sequence(currentSeq.getName(),
613 AlignSeq.extractGaps(
614 jalview.util.Comparison.GapChars,
615 currentSeq.getSequenceAsString()
617 currentSeq.getStart(),
618 currentSeq.getEnd());
619 seqs[i].sequenceFeatures = currentSeq.getSequenceFeatures();
620 seqs[i].setDescription(currentSeq.getDescription());
621 getSequenceAt(i).setSequenceFeatures(null);
622 getSequenceAt(i).setDatasetSequence(seqs[i]);
626 dataset = new Alignment(seqs);
628 else if (dataset == null && data != null)
634 public Alignment getDataset()
639 public boolean padGaps()
641 boolean modified = false;
643 //Remove excess gaps from the end of alignment
647 for (int i = 0; i < sequences.size(); i++)
649 current = getSequenceAt(i);
650 for (int j = current.getLength(); j > maxLength; j--)
652 if (j > maxLength && !jalview.util.Comparison.isGap(
653 current.getCharAt(j)))
664 for (int i = 0; i < sequences.size();
667 current = getSequenceAt(i);
668 cLength = current.getLength();
670 if (cLength < maxLength)
672 current.insertCharAt(cLength,
673 maxLength - cLength, gapCharacter);
676 else if (current.getLength() > maxLength)
678 current.deleteChars(maxLength, current.getLength());
684 public HiddenSequences getHiddenSequences()
686 return hiddenSequences;
689 public CigarArray getCompactAlignment()
691 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
692 for (int i = 0; i < sequences.size(); i++)
694 alseqs[i] = new SeqCigar( (SequenceI) sequences.elementAt(i));
696 CigarArray cal = new CigarArray(alseqs);
697 cal.addOperation(CigarArray.M, getWidth());