2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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;
21 import jalview.analysis.*;
23 import jalview.util.*;
27 /** Data structure to hold and manipulate a multiple sequence alignment
29 public class Alignment implements AlignmentI
31 protected Alignment dataset;
32 protected Vector sequences;
33 protected Vector groups = new Vector();
34 protected char gapCharacter = '-';
35 protected int type = NUCLEOTIDE;
36 public static final int PROTEIN = 0;
37 public static final int NUCLEOTIDE = 1;
40 public AlignmentAnnotation[] annotations;
42 HiddenSequences hiddenSequences = new HiddenSequences(this);
44 private void initAlignment(SequenceI[] seqs) {
47 if( jalview.util.Comparison.isNucleotide(seqs))
52 sequences = new Vector();
54 for (i = 0; i < seqs.length; i++)
56 sequences.addElement(seqs[i]);
60 /** Make an alignment from an array of Sequences.
64 public Alignment(SequenceI[] seqs)
69 * Make a new alignment from an array of SeqCigars
70 * @param seqs SeqCigar[]
72 public Alignment(SeqCigar[] alseqs) {
73 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs, gapCharacter, new ColumnSelection(), null);
77 * Make a new alignment from an CigarArray
78 * JBPNote - can only do this when compactAlignment does not contain hidden regions.
79 * JBPNote - must also check that compactAlignment resolves to a set of SeqCigars - or construct them appropriately.
80 * @param compactAlignment CigarArray
82 public static AlignmentI createAlignment(CigarArray compactAlignment) {
83 throw new Error("Alignment(CigarArray) not yet implemented");
84 // this(compactAlignment.refCigars);
90 * @return DOCUMENT ME!
92 public Vector getSequences()
97 public SequenceI [] getSequencesArray()
99 SequenceI [] reply = new SequenceI[sequences.size()];
100 for(int i=0; i<sequences.size(); i++)
102 reply[i] = (SequenceI)sequences.elementAt(i);
110 * @param i DOCUMENT ME!
112 * @return DOCUMENT ME!
114 public SequenceI getSequenceAt(int i)
116 if (i < sequences.size())
118 return (SequenceI) sequences.elementAt(i);
124 /** Adds a sequence to the alignment. Recalculates maxLength and size.
128 public void addSequence(SequenceI snew)
132 if(snew.getDatasetSequence()!=null)
134 System.out.println(snew.getName());
135 getDataset().addSequence(snew.getDatasetSequence());
139 Sequence ds = new Sequence(snew.getName(),
140 AlignSeq.extractGaps("-. ",
145 snew.setDatasetSequence(ds);
146 getDataset().addSequence(ds);
149 sequences.addElement(snew);
151 hiddenSequences.adjustHeightSequenceAdded();
155 /** Adds a sequence to the alignment. Recalculates maxLength and size.
159 public void setSequenceAt(int i, SequenceI snew)
161 SequenceI oldseq = getSequenceAt(i);
162 deleteSequence(oldseq);
164 sequences.setElementAt(snew, i);
170 * @return DOCUMENT ME!
172 public Vector getGroups()
180 * @param s DOCUMENT ME!
182 public void deleteSequence(SequenceI s)
184 deleteSequence(findIndex(s));
190 * @param i DOCUMENT ME!
192 public void deleteSequence(int i)
194 if(i>0 && i<getHeight())
196 sequences.removeElementAt(i);
197 hiddenSequences.adjustHeightSequenceDeleted(i);
203 public SequenceGroup findGroup(SequenceI s)
205 for (int i = 0; i < this.groups.size(); i++)
207 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
209 if (sg.getSequences(false).contains(s))
221 * @param s DOCUMENT ME!
223 * @return DOCUMENT ME!
225 public SequenceGroup[] findAllGroups(SequenceI s)
227 Vector temp = new Vector();
229 int gSize = groups.size();
230 for (int i = 0; i < gSize; i++)
232 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
233 if(sg==null || sg.getSequences(false)==null)
235 this.deleteGroup(sg);
240 if (sg.getSequences(false).contains(s))
246 SequenceGroup[] ret = new SequenceGroup[temp.size()];
248 for (int i = 0; i < temp.size(); i++)
250 ret[i] = (SequenceGroup) temp.elementAt(i);
259 public void addGroup(SequenceGroup sg)
261 if (!groups.contains(sg))
263 groups.addElement(sg);
270 public void deleteAllGroups()
272 groups.removeAllElements();
276 while (i < sequences.size())
278 SequenceI s = getSequenceAt(i);
279 s.setColor(java.awt.Color.white);
285 public void deleteGroup(SequenceGroup g)
287 if (groups.contains(g))
289 groups.removeElement(g);
294 public SequenceI findName(String name)
298 while (i < sequences.size())
300 if (getSequenceAt(i).getName().equals(name))
302 return getSequenceAt(i);
311 public SequenceI [] findSequenceMatch(String name)
313 Vector matches = new Vector();
316 while (i < sequences.size())
318 if (getSequenceAt(i).getName().equals(name))
320 matches.addElement(getSequenceAt(i));
325 SequenceI [] result = new SequenceI[matches.size()];
326 for(i=0; i<result.length; i++)
327 result[i] = (SequenceI)matches.elementAt(i);
335 public int findIndex(SequenceI s)
339 while (i < sequences.size())
341 if (s == getSequenceAt(i))
355 * @return DOCUMENT ME!
357 public int getHeight()
359 return sequences.size();
365 * @return DOCUMENT ME!
367 public int getWidth()
371 for (int i = 0; i < sequences.size(); i++)
373 if (getSequenceAt(i).getLength() > maxLength)
375 maxLength = getSequenceAt(i).getLength();
386 * @param gc DOCUMENT ME!
388 public void setGapCharacter(char gc)
392 for (int i = 0; i < sequences.size(); i++)
394 Sequence seq = (Sequence) sequences.elementAt(i);
395 seq.setSequence( seq.getSequence().replace('.', gc) );
396 seq.setSequence( seq.getSequence().replace('-', gc) );
397 seq.setSequence( seq.getSequence().replace(' ', gc) );
404 * @return DOCUMENT ME!
406 public char getGapCharacter()
415 * @return DOCUMENT ME!
417 public boolean isAligned()
419 int width = getWidth();
421 for (int i = 0; i < sequences.size(); i++)
423 if (getSequenceAt(i).getLength() != width)
435 * @param aa DOCUMENT ME!
437 public void deleteAnnotation(AlignmentAnnotation aa)
441 if (annotations != null)
443 aSize = annotations.length;
446 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
450 for (int i = 0; i < aSize; i++)
452 if (annotations[i] == aa)
457 temp[tIndex] = annotations[i];
465 public void adjustSequenceAnnotations()
467 if(annotations!=null)
469 for (int a = 0; a < annotations.length; a++)
471 if (annotations[a].sequenceRef != null)
473 annotations[a].adjustForAlignment();
482 * @param aa DOCUMENT ME!
484 public void addAnnotation(AlignmentAnnotation aa)
487 if (annotations != null)
489 aSize = annotations.length + 1;
492 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
500 for (i = 0; i < (aSize-1); i++)
502 temp[i] = annotations[i];
509 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
511 if(aa==null || annotations==null || annotations.length-1<index)
514 int aSize = annotations.length;
515 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
519 for (int i = 0; i < aSize; i++)
525 temp[i] = annotations[i];
527 temp[i] = annotations[i-1];
536 * @return DOCUMENT ME!
538 public AlignmentAnnotation[] getAlignmentAnnotation()
543 public void setNucleotide(boolean b)
551 public boolean isNucleotide()
559 public void setDataset(Alignment data)
561 if(dataset==null && data==null)
563 // Create a new dataset for this alignment.
564 // Can only be done once, if dataset is not null
565 // This will not be performed
566 Sequence[] seqs = new Sequence[getHeight()];
567 for (int i = 0; i < getHeight(); i++)
569 if(getSequenceAt(i).getDatasetSequence()!=null)
571 seqs[i] = (Sequence)getSequenceAt(i).getDatasetSequence();
575 seqs[i] = new Sequence(getSequenceAt(i).getName(),
576 AlignSeq.extractGaps(
577 jalview.util.Comparison.GapChars,
578 getSequenceAt(i).getSequence()
580 getSequenceAt(i).getStart(),
581 getSequenceAt(i).getEnd());
582 seqs[i].sequenceFeatures = getSequenceAt(i).getSequenceFeatures();
583 getSequenceAt(i).setSequenceFeatures(null);
584 getSequenceAt(i).setDatasetSequence(seqs[i]);
588 dataset = new Alignment(seqs);
590 else if(dataset==null && data!=null)
596 public Alignment getDataset()
601 public boolean padGaps() {
602 boolean modified=false;
604 //Remove excess gaps from the end of alignment
608 for (int i = 0; i < sequences.size(); i++)
610 current = getSequenceAt(i);
611 for (int j = current.getLength(); j > maxLength; j--)
613 if (j > maxLength && !jalview.util.Comparison.isGap(
614 current.getCharAt(j)))
624 for (int i = 0; i < sequences.size();
627 current = getSequenceAt(i);
629 if (current.getLength() < maxLength)
631 current.insertCharAt(maxLength - 1, gapCharacter);
634 else if(current.getLength() > maxLength)
636 current.deleteChars(maxLength, current.getLength());
642 public HiddenSequences getHiddenSequences()
644 return hiddenSequences;
646 SequenceI [] getVisibleAndRepresentedSeqs()
648 if(hiddenSequences==null || hiddenSequences.getSize()<1)
649 return getSequencesArray();
651 Vector seqs = new Vector();
653 SequenceGroup hidden;
654 for (int i = 0; i < sequences.size(); i++)
656 seq = (SequenceI) sequences.elementAt(i);
657 seqs.addElement(seq);
658 hidden = seq.getHiddenSequences();
661 for(int j=0; j<hidden.getSize(false); j++)
663 seqs.addElement(hidden.getSequenceAt(j));
667 SequenceI [] result = new SequenceI[seqs.size()];
668 for(int i=0; i<seqs.size(); i++)
669 result[i] = (SequenceI)seqs.elementAt(i);
675 public CigarArray getCompactAlignment()
677 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
678 for (int i=0; i<sequences.size(); i++) {
679 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
681 CigarArray cal = new CigarArray(alseqs);
682 cal.addOperation(CigarArray.M, getWidth());