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 getDataset().addSequence(snew.getDatasetSequence());
138 Sequence ds = new Sequence(snew.getName(),
139 AlignSeq.extractGaps("-. ",
144 snew.setDatasetSequence(ds);
145 getDataset().addSequence(ds);
148 sequences.addElement(snew);
150 hiddenSequences.adjustHeightSequenceAdded();
154 /** Adds a sequence to the alignment. Recalculates maxLength and size.
158 public void setSequenceAt(int i, SequenceI snew)
160 SequenceI oldseq = getSequenceAt(i);
161 deleteSequence(oldseq);
163 sequences.setElementAt(snew, i);
169 * @return DOCUMENT ME!
171 public Vector getGroups()
179 * @param s DOCUMENT ME!
181 public void deleteSequence(SequenceI s)
183 deleteSequence(findIndex(s));
189 * @param i DOCUMENT ME!
191 public void deleteSequence(int i)
193 if(i>-1 && i<getHeight())
195 sequences.removeElementAt(i);
196 hiddenSequences.adjustHeightSequenceDeleted(i);
202 public SequenceGroup findGroup(SequenceI s)
204 for (int i = 0; i < this.groups.size(); i++)
206 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
208 if (sg.getSequences(false).contains(s))
220 * @param s DOCUMENT ME!
222 * @return DOCUMENT ME!
224 public SequenceGroup[] findAllGroups(SequenceI s)
226 Vector temp = new Vector();
228 int gSize = groups.size();
229 for (int i = 0; i < gSize; i++)
231 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
232 if(sg==null || sg.getSequences(false)==null)
234 this.deleteGroup(sg);
239 if (sg.getSequences(false).contains(s))
245 SequenceGroup[] ret = new SequenceGroup[temp.size()];
247 for (int i = 0; i < temp.size(); i++)
249 ret[i] = (SequenceGroup) temp.elementAt(i);
258 public void addGroup(SequenceGroup sg)
260 if (!groups.contains(sg))
262 groups.addElement(sg);
269 public void deleteAllGroups()
271 groups.removeAllElements();
275 public void deleteGroup(SequenceGroup g)
277 if (groups.contains(g))
279 groups.removeElement(g);
284 public SequenceI findName(String name)
288 while (i < sequences.size())
290 if (getSequenceAt(i).getName().equals(name))
292 return getSequenceAt(i);
301 public SequenceI [] findSequenceMatch(String name)
303 Vector matches = new Vector();
306 while (i < sequences.size())
308 if (getSequenceAt(i).getName().equals(name))
310 matches.addElement(getSequenceAt(i));
315 SequenceI [] result = new SequenceI[matches.size()];
316 for(i=0; i<result.length; i++)
317 result[i] = (SequenceI)matches.elementAt(i);
325 public int findIndex(SequenceI s)
329 while (i < sequences.size())
331 if (s == getSequenceAt(i))
345 * @return DOCUMENT ME!
347 public int getHeight()
349 return sequences.size();
355 * @return DOCUMENT ME!
357 public int getWidth()
361 for (int i = 0; i < sequences.size(); i++)
363 if (getSequenceAt(i).getLength() > maxLength)
365 maxLength = getSequenceAt(i).getLength();
376 * @param gc DOCUMENT ME!
378 public void setGapCharacter(char gc)
382 for (int i = 0; i < sequences.size(); i++)
384 Sequence seq = (Sequence) sequences.elementAt(i);
385 seq.setSequence( seq.getSequence().replace('.', gc) );
386 seq.setSequence( seq.getSequence().replace('-', gc) );
387 seq.setSequence( seq.getSequence().replace(' ', gc) );
394 * @return DOCUMENT ME!
396 public char getGapCharacter()
405 * @return DOCUMENT ME!
407 public boolean isAligned()
409 int width = getWidth();
411 for (int i = 0; i < sequences.size(); i++)
413 if (getSequenceAt(i).getLength() != width)
425 * @param aa DOCUMENT ME!
427 public void deleteAnnotation(AlignmentAnnotation aa)
431 if (annotations != null)
433 aSize = annotations.length;
439 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
443 for (int i = 0; i < aSize; i++)
445 if (annotations[i] == aa)
450 temp[tIndex] = annotations[i];
458 public void adjustSequenceAnnotations()
460 if(annotations!=null)
462 for (int a = 0; a < annotations.length; a++)
464 if (annotations[a].sequenceRef != null)
466 annotations[a].adjustForAlignment();
475 * @param aa DOCUMENT ME!
477 public void addAnnotation(AlignmentAnnotation aa)
480 if (annotations != null)
482 aSize = annotations.length + 1;
485 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
493 for (i = 0; i < (aSize-1); i++)
495 temp[i] = annotations[i];
502 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
504 if(aa==null || annotations==null || annotations.length-1<index)
507 int aSize = annotations.length;
508 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
512 for (int i = 0; i < aSize; i++)
518 temp[i] = annotations[i];
520 temp[i] = annotations[i-1];
529 * @return DOCUMENT ME!
531 public AlignmentAnnotation[] getAlignmentAnnotation()
536 public void setNucleotide(boolean b)
544 public boolean isNucleotide()
552 public void setDataset(Alignment data)
554 if(dataset==null && data==null)
556 // Create a new dataset for this alignment.
557 // Can only be done once, if dataset is not null
558 // This will not be performed
559 Sequence[] seqs = new Sequence[getHeight()];
560 SequenceI currentSeq;
561 for (int i = 0; i < getHeight(); i++)
563 currentSeq = getSequenceAt(i);
564 if(currentSeq.getDatasetSequence()!=null)
566 seqs[i] = (Sequence)currentSeq.getDatasetSequence();
570 seqs[i] = new Sequence(currentSeq.getName(),
571 AlignSeq.extractGaps(
572 jalview.util.Comparison.GapChars,
573 currentSeq.getSequence()
575 currentSeq.getStart(),
576 currentSeq.getEnd());
577 seqs[i].sequenceFeatures = currentSeq.getSequenceFeatures();
578 seqs[i].setDescription(currentSeq.getDescription());
579 getSequenceAt(i).setSequenceFeatures(null);
580 getSequenceAt(i).setDatasetSequence(seqs[i]);
584 dataset = new Alignment(seqs);
586 else if(dataset==null && data!=null)
592 public Alignment getDataset()
597 public boolean padGaps()
599 boolean modified=false;
601 //Remove excess gaps from the end of alignment
605 for (int i = 0; i < sequences.size(); i++)
607 current = getSequenceAt(i);
608 for (int j = current.getLength(); j > maxLength; j--)
610 if (j > maxLength && !jalview.util.Comparison.isGap(
611 current.getCharAt(j)))
622 for (int i = 0; i < sequences.size();
625 current = getSequenceAt(i);
626 cLength = current.getLength();
628 if (cLength < maxLength)
630 current.insertCharAt(cLength,
631 maxLength-cLength, 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());