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()
188 * @param s DOCUMENT ME!
190 public void deleteSequence(SequenceI s)
192 deleteSequence(findIndex(s));
198 * @param i DOCUMENT ME!
200 public void deleteSequence(int i)
202 if (i > -1 && i < getHeight())
204 sequences.removeElementAt(i);
205 hiddenSequences.adjustHeightSequenceDeleted(i);
210 public SequenceGroup findGroup(SequenceI s)
212 for (int i = 0; i < this.groups.size(); i++)
214 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
216 if (sg.getSequences(null).contains(s))
228 * @param s DOCUMENT ME!
230 * @return DOCUMENT ME!
232 public SequenceGroup[] findAllGroups(SequenceI s)
234 Vector temp = new Vector();
236 int gSize = groups.size();
237 for (int i = 0; i < gSize; i++)
239 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
240 if (sg == null || sg.getSequences(null) == null)
242 this.deleteGroup(sg);
247 if (sg.getSequences(null).contains(s))
253 SequenceGroup[] ret = new SequenceGroup[temp.size()];
255 for (int i = 0; i < temp.size(); i++)
257 ret[i] = (SequenceGroup) temp.elementAt(i);
264 public void addGroup(SequenceGroup sg)
266 if (!groups.contains(sg))
268 if (hiddenSequences.getSize() > 0)
270 int i, iSize = sg.getSize();
271 for (i = 0; i < iSize; i++)
273 if (!sequences.contains(sg.getSequenceAt(i)))
275 sg.deleteSequence(sg.getSequenceAt(i), false);
281 if (sg.getSize() < 1)
287 groups.addElement(sg);
294 public void deleteAllGroups()
296 groups.removeAllElements();
300 public void deleteGroup(SequenceGroup g)
302 if (groups.contains(g))
304 groups.removeElement(g);
309 public SequenceI findName(String name)
313 while (i < sequences.size())
315 if (getSequenceAt(i).getName().equals(name))
317 return getSequenceAt(i);
326 public SequenceI[] findSequenceMatch(String name)
328 Vector matches = new Vector();
331 while (i < sequences.size())
333 if (getSequenceAt(i).getName().equals(name))
335 matches.addElement(getSequenceAt(i));
340 SequenceI[] result = new SequenceI[matches.size()];
341 for (i = 0; i < result.length; i++)
343 result[i] = (SequenceI) matches.elementAt(i);
351 public int findIndex(SequenceI s)
355 while (i < sequences.size())
357 if (s == getSequenceAt(i))
371 * @return DOCUMENT ME!
373 public int getHeight()
375 return sequences.size();
381 * @return DOCUMENT ME!
383 public int getWidth()
387 for (int i = 0; i < sequences.size(); i++)
389 if (getSequenceAt(i).getLength() > maxLength)
391 maxLength = getSequenceAt(i).getLength();
401 * @param gc DOCUMENT ME!
403 public void setGapCharacter(char gc)
407 for (int i = 0; i < sequences.size(); i++)
409 Sequence seq = (Sequence) sequences.elementAt(i);
410 seq.setSequence(seq.getSequenceAsString()
421 * @return DOCUMENT ME!
423 public char getGapCharacter()
431 * @return DOCUMENT ME!
433 public boolean isAligned()
435 int width = getWidth();
437 for (int i = 0; i < sequences.size(); i++)
439 if (getSequenceAt(i).getLength() != width)
451 * @param aa DOCUMENT ME!
453 public void deleteAnnotation(AlignmentAnnotation aa)
457 if (annotations != null)
459 aSize = annotations.length;
467 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
471 for (int i = 0; i < aSize; i++)
473 if (annotations[i] == aa)
478 temp[tIndex] = annotations[i];
488 * @param aa DOCUMENT ME!
490 public void addAnnotation(AlignmentAnnotation aa)
493 if (annotations != null)
495 aSize = annotations.length + 1;
498 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
500 temp[aSize - 1] = aa;
506 for (i = 0; i < (aSize - 1); i++)
508 temp[i] = annotations[i];
515 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
517 if (aa == null || annotations == null || annotations.length - 1 < index)
522 int aSize = annotations.length;
523 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
527 for (int i = 0; i < aSize; i++)
536 temp[i] = annotations[i];
540 temp[i] = annotations[i - 1];
550 * @return DOCUMENT ME!
552 public AlignmentAnnotation[] getAlignmentAnnotation()
557 public void setNucleotide(boolean b)
569 public boolean isNucleotide()
571 if (type == NUCLEOTIDE)
581 public void setDataset(Alignment data)
583 if (dataset == null && data == null)
585 // Create a new dataset for this alignment.
586 // Can only be done once, if dataset is not null
587 // This will not be performed
588 Sequence[] seqs = new Sequence[getHeight()];
589 SequenceI currentSeq;
590 for (int i = 0; i < getHeight(); i++)
592 currentSeq = getSequenceAt(i);
593 if (currentSeq.getDatasetSequence() != null)
595 seqs[i] = (Sequence) currentSeq.getDatasetSequence();
599 seqs[i] = new Sequence(currentSeq.getName(),
600 AlignSeq.extractGaps(
601 jalview.util.Comparison.GapChars,
602 currentSeq.getSequenceAsString()
604 currentSeq.getStart(),
605 currentSeq.getEnd());
606 seqs[i].sequenceFeatures = currentSeq.getSequenceFeatures();
607 seqs[i].setDescription(currentSeq.getDescription());
608 getSequenceAt(i).setSequenceFeatures(null);
609 getSequenceAt(i).setDatasetSequence(seqs[i]);
613 dataset = new Alignment(seqs);
615 else if (dataset == null && data != null)
621 public Alignment getDataset()
626 public boolean padGaps()
628 boolean modified = false;
630 //Remove excess gaps from the end of alignment
634 for (int i = 0; i < sequences.size(); i++)
636 current = getSequenceAt(i);
637 for (int j = current.getLength(); j > maxLength; j--)
639 if (j > maxLength && !jalview.util.Comparison.isGap(
640 current.getCharAt(j)))
651 for (int i = 0; i < sequences.size();
654 current = getSequenceAt(i);
655 cLength = current.getLength();
657 if (cLength < maxLength)
659 current.insertCharAt(cLength,
660 maxLength - cLength, gapCharacter);
663 else if (current.getLength() > maxLength)
665 current.deleteChars(maxLength, current.getLength());
671 public HiddenSequences getHiddenSequences()
673 return hiddenSequences;
676 public CigarArray getCompactAlignment()
678 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
679 for (int i = 0; i < sequences.size(); i++)
681 alseqs[i] = new SeqCigar( (SequenceI) sequences.elementAt(i));
683 CigarArray cal = new CigarArray(alseqs);
684 cal.addOperation(CigarArray.M, getWidth());