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 while (i < sequences.size())
277 SequenceI s = getSequenceAt(i);
278 s.setColor(java.awt.Color.white);
284 public void deleteGroup(SequenceGroup g)
286 if (groups.contains(g))
288 groups.removeElement(g);
293 public SequenceI findName(String name)
297 while (i < sequences.size())
299 if (getSequenceAt(i).getName().equals(name))
301 return getSequenceAt(i);
310 public SequenceI [] findSequenceMatch(String name)
312 Vector matches = new Vector();
315 while (i < sequences.size())
317 if (getSequenceAt(i).getName().equals(name))
319 matches.addElement(getSequenceAt(i));
324 SequenceI [] result = new SequenceI[matches.size()];
325 for(i=0; i<result.length; i++)
326 result[i] = (SequenceI)matches.elementAt(i);
334 public int findIndex(SequenceI s)
338 while (i < sequences.size())
340 if (s == getSequenceAt(i))
354 * @return DOCUMENT ME!
356 public int getHeight()
358 return sequences.size();
364 * @return DOCUMENT ME!
366 public int getWidth()
370 for (int i = 0; i < sequences.size(); i++)
372 if (getSequenceAt(i).getLength() > maxLength)
374 maxLength = getSequenceAt(i).getLength();
385 * @param gc DOCUMENT ME!
387 public void setGapCharacter(char gc)
391 for (int i = 0; i < sequences.size(); i++)
393 Sequence seq = (Sequence) sequences.elementAt(i);
394 seq.setSequence( seq.getSequence().replace('.', gc) );
395 seq.setSequence( seq.getSequence().replace('-', gc) );
396 seq.setSequence( seq.getSequence().replace(' ', gc) );
403 * @return DOCUMENT ME!
405 public char getGapCharacter()
414 * @return DOCUMENT ME!
416 public boolean isAligned()
418 int width = getWidth();
420 for (int i = 0; i < sequences.size(); i++)
422 if (getSequenceAt(i).getLength() != width)
434 * @param aa DOCUMENT ME!
436 public void deleteAnnotation(AlignmentAnnotation aa)
440 if (annotations != null)
442 aSize = annotations.length;
448 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
452 for (int i = 0; i < aSize; i++)
454 if (annotations[i] == aa)
459 temp[tIndex] = annotations[i];
467 public void adjustSequenceAnnotations()
469 if(annotations!=null)
471 for (int a = 0; a < annotations.length; a++)
473 if (annotations[a].sequenceRef != null)
475 annotations[a].adjustForAlignment();
484 * @param aa DOCUMENT ME!
486 public void addAnnotation(AlignmentAnnotation aa)
489 if (annotations != null)
491 aSize = annotations.length + 1;
494 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
502 for (i = 0; i < (aSize-1); i++)
504 temp[i] = annotations[i];
511 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
513 if(aa==null || annotations==null || annotations.length-1<index)
516 int aSize = annotations.length;
517 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
521 for (int i = 0; i < aSize; i++)
527 temp[i] = annotations[i];
529 temp[i] = annotations[i-1];
538 * @return DOCUMENT ME!
540 public AlignmentAnnotation[] getAlignmentAnnotation()
545 public void setNucleotide(boolean b)
553 public boolean isNucleotide()
561 public void setDataset(Alignment data)
563 if(dataset==null && data==null)
565 // Create a new dataset for this alignment.
566 // Can only be done once, if dataset is not null
567 // This will not be performed
568 Sequence[] seqs = new Sequence[getHeight()];
569 SequenceI currentSeq;
570 for (int i = 0; i < getHeight(); i++)
572 currentSeq = getSequenceAt(i);
573 if(currentSeq.getDatasetSequence()!=null)
575 seqs[i] = (Sequence)currentSeq.getDatasetSequence();
579 seqs[i] = new Sequence(currentSeq.getName(),
580 AlignSeq.extractGaps(
581 jalview.util.Comparison.GapChars,
582 currentSeq.getSequence()
584 currentSeq.getStart(),
585 currentSeq.getEnd());
586 seqs[i].sequenceFeatures = currentSeq.getSequenceFeatures();
587 seqs[i].setDescription(currentSeq.getDescription());
588 getSequenceAt(i).setSequenceFeatures(null);
589 getSequenceAt(i).setDatasetSequence(seqs[i]);
593 dataset = new Alignment(seqs);
595 else if(dataset==null && data!=null)
601 public Alignment getDataset()
606 public boolean padGaps()
608 boolean modified=false;
610 //Remove excess gaps from the end of alignment
614 for (int i = 0; i < sequences.size(); i++)
616 current = getSequenceAt(i);
617 for (int j = current.getLength(); j > maxLength; j--)
619 if (j > maxLength && !jalview.util.Comparison.isGap(
620 current.getCharAt(j)))
631 for (int i = 0; i < sequences.size();
634 current = getSequenceAt(i);
635 cLength = current.getLength();
637 if (cLength < maxLength)
639 current.insertCharAt(cLength,
640 maxLength-cLength, gapCharacter);
643 else if(current.getLength() > maxLength)
645 current.deleteChars(maxLength, current.getLength());
651 public HiddenSequences getHiddenSequences()
653 return hiddenSequences;
655 SequenceI [] getVisibleAndRepresentedSeqs()
657 if(hiddenSequences==null || hiddenSequences.getSize()<1)
658 return getSequencesArray();
660 Vector seqs = new Vector();
662 SequenceGroup hidden;
663 for (int i = 0; i < sequences.size(); i++)
665 seq = (SequenceI) sequences.elementAt(i);
666 seqs.addElement(seq);
667 hidden = seq.getHiddenSequences();
670 for(int j=0; j<hidden.getSize(false); j++)
672 seqs.addElement(hidden.getSequenceAt(j));
676 SequenceI [] result = new SequenceI[seqs.size()];
677 for(int i=0; i<seqs.size(); i++)
678 result[i] = (SequenceI)seqs.elementAt(i);
684 public CigarArray getCompactAlignment()
686 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
687 for (int i=0; i<sequences.size(); i++) {
688 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
690 CigarArray cal = new CigarArray(alseqs);
691 cal.addOperation(CigarArray.M, getWidth());