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);
150 sequences.addElement(snew);
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()
175 /** Takes out columns consisting entirely of gaps (-,.," ")
177 public void removeGaps() {
178 removeGaps((ShiftList)null);
181 * remove gaps in alignment - recording any frame shifts in shiftrecord
182 * intended to be passed to ColumnSelection.compensateForEdits(shiftrecord)
185 public void removeGaps(ShiftList shiftrecord) {
186 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
187 int j, jSize = seqs.length;
190 for (int i = 0; i < jSize; i++)
192 if (seqs[i].getLength() > width)
194 width = seqs[i].getLength();
198 int startCol = -1, endCol = -1;
199 boolean delete = true;
200 for (int i = 0; i < width; i++)
204 for (j = 0; j < jSize; j++)
206 if (seqs[j].getLength() > i)
208 if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i)))
219 if(delete && startCol==-1)
225 if (!delete && startCol > -1)
227 deleteColumns(seqs, startCol, endCol);
228 if (shiftrecord!=null) {
229 shiftrecord.addShift(startCol, 1+endCol-startCol);
231 width -= (endCol - startCol);
232 i -= (endCol - startCol);
238 if (delete && startCol > -1)
240 deleteColumns(seqs, startCol, endCol);
241 if (shiftrecord!=null) {
242 shiftrecord.addShift(startCol, 1+endCol-startCol);
247 /** Removes a range of columns (start to end inclusive).
249 * @param seqs Sequences to remove columns from
250 * @param start Start column in the alignment
251 * @param end End column in the alignment
253 public void deleteColumns(SequenceI [] seqs, int start, int end)
255 for(int i=0; i<seqs.length; i++)
256 seqs[i].deleteChars(start, end);
263 * @param i DOCUMENT ME!
265 public void trimLeft(int i)
267 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
268 int j, jSize = seqs.length;
269 for (j = 0; j < jSize; j++)
271 int newstart = seqs[j].findPosition(i);
273 if(i>seqs[j].getLength())
275 sequences.removeElement(seqs[j]);
281 seqs[j].setStart(newstart);
282 seqs[j].setSequence(seqs[j].getSequence().substring(i));
290 * @param i DOCUMENT ME!
292 public void trimRight(int i)
294 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
295 int j, jSize = seqs.length;
296 for (j = 0; j < jSize; j++)
298 int newend = seqs[j].findPosition(i);
300 seqs[j].setEnd(newend);
301 if(seqs[j].getLength()>i)
302 seqs[j].setSequence(seqs[j].getSequence().substring(0, i + 1));
309 * @param s DOCUMENT ME!
311 public void deleteSequence(SequenceI s)
313 for (int i = 0; i < getHeight(); i++)
315 if (getSequenceAt(i) == s)
325 * @param i DOCUMENT ME!
327 public void deleteSequence(int i)
329 sequences.removeElementAt(i);
334 public SequenceGroup findGroup(SequenceI s)
336 for (int i = 0; i < this.groups.size(); i++)
338 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
340 if (sg.getSequences(false).contains(s))
352 * @param s DOCUMENT ME!
354 * @return DOCUMENT ME!
356 public SequenceGroup[] findAllGroups(SequenceI s)
358 Vector temp = new Vector();
360 int gSize = groups.size();
361 for (int i = 0; i < gSize; i++)
363 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
364 if(sg==null || sg.getSequences(false)==null)
366 this.deleteGroup(sg);
371 if (sg.getSequences(false).contains(s))
377 SequenceGroup[] ret = new SequenceGroup[temp.size()];
379 for (int i = 0; i < temp.size(); i++)
381 ret[i] = (SequenceGroup) temp.elementAt(i);
390 public void addGroup(SequenceGroup sg)
392 if (!groups.contains(sg))
394 groups.addElement(sg);
401 public void deleteAllGroups()
403 groups.removeAllElements();
407 while (i < sequences.size())
409 SequenceI s = getSequenceAt(i);
410 s.setColor(java.awt.Color.white);
416 public void deleteGroup(SequenceGroup g)
418 if (groups.contains(g))
420 groups.removeElement(g);
425 public SequenceI findName(String name)
429 while (i < sequences.size())
431 if (getSequenceAt(i).getName().equals(name))
433 return getSequenceAt(i);
442 public SequenceI [] findSequenceMatch(String name)
444 Vector matches = new Vector();
447 while (i < sequences.size())
449 if (getSequenceAt(i).getName().equals(name))
451 matches.addElement(getSequenceAt(i));
456 SequenceI [] result = new SequenceI[matches.size()];
457 for(i=0; i<result.length; i++)
458 result[i] = (SequenceI)matches.elementAt(i);
466 public int findIndex(SequenceI s)
470 while (i < sequences.size())
472 if (s == getSequenceAt(i))
486 * @return DOCUMENT ME!
488 public int getHeight()
490 return sequences.size();
496 * @return DOCUMENT ME!
498 public int getWidth()
502 for (int i = 0; i < sequences.size(); i++)
504 if (getSequenceAt(i).getLength() > maxLength)
506 maxLength = getSequenceAt(i).getLength();
516 * @return DOCUMENT ME!
518 public int getMaxIdLength()
523 while (i < sequences.size())
525 SequenceI seq = getSequenceAt(i);
526 String tmp = seq.getName() + "/" + seq.getStart() + "-" +
529 if (tmp.length() > max)
543 * @param gc DOCUMENT ME!
545 public void setGapCharacter(char gc)
549 for (int i = 0; i < sequences.size(); i++)
551 Sequence seq = (Sequence) sequences.elementAt(i);
552 seq.setSequence( seq.getSequence().replace('.', gc) );
553 seq.setSequence( seq.getSequence().replace('-', gc) );
554 seq.setSequence( seq.getSequence().replace(' ', gc) );
561 * @return DOCUMENT ME!
563 public char getGapCharacter()
572 * @return DOCUMENT ME!
574 public boolean isAligned()
576 int width = getWidth();
578 for (int i = 0; i < sequences.size(); i++)
580 if (getSequenceAt(i).getLength() != width)
592 * @param aa DOCUMENT ME!
594 public void deleteAnnotation(AlignmentAnnotation aa)
598 if (annotations != null)
600 aSize = annotations.length;
603 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
607 for (int i = 0; i < aSize; i++)
609 if (annotations[i] == aa)
614 temp[tIndex] = annotations[i];
622 public void adjustSequenceAnnotations()
624 if(annotations!=null)
626 for (int a = 0; a < annotations.length; a++)
628 if (annotations[a].sequenceRef != null)
630 annotations[a].adjustForAlignment();
639 * @param aa DOCUMENT ME!
641 public void addAnnotation(AlignmentAnnotation aa)
644 if (annotations != null)
646 aSize = annotations.length + 1;
649 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
657 for (i = 0; i < (aSize-1); i++)
659 temp[i] = annotations[i];
666 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
668 if(aa==null || annotations==null || annotations.length-1<index)
671 int aSize = annotations.length;
672 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
676 for (int i = 0; i < aSize; i++)
682 temp[i] = annotations[i];
684 temp[i] = annotations[i-1];
693 * @return DOCUMENT ME!
695 public AlignmentAnnotation[] getAlignmentAnnotation()
700 public void setNucleotide(boolean b)
708 public boolean isNucleotide()
716 public void setDataset(Alignment data)
718 if(dataset==null && data==null)
720 // Create a new dataset for this alignment.
721 // Can only be done once, if dataset is not null
722 // This will not be performed
723 Sequence[] seqs = new Sequence[getHeight()];
724 for (int i = 0; i < getHeight(); i++)
726 if(getSequenceAt(i).getDatasetSequence()!=null)
728 seqs[i] = (Sequence)getSequenceAt(i).getDatasetSequence();
732 seqs[i] = new Sequence(getSequenceAt(i).getName(),
733 AlignSeq.extractGaps(
734 jalview.util.Comparison.GapChars,
735 getSequenceAt(i).getSequence()
737 getSequenceAt(i).getStart(),
738 getSequenceAt(i).getEnd());
739 seqs[i].sequenceFeatures = getSequenceAt(i).getSequenceFeatures();
740 getSequenceAt(i).setSequenceFeatures(null);
741 getSequenceAt(i).setDatasetSequence(seqs[i]);
745 dataset = new Alignment(seqs);
747 else if(dataset==null && data!=null)
753 public Alignment getDataset()
758 public boolean padGaps() {
759 boolean modified=false;
761 //Remove excess gaps from the end of alignment
765 for (int i = 0; i < sequences.size(); i++)
767 current = getSequenceAt(i);
768 for (int j = current.getLength(); j > maxLength; j--)
770 if (j > maxLength && !jalview.util.Comparison.isGap(
771 current.getCharAt(j)))
781 for (int i = 0; i < sequences.size();
784 current = getSequenceAt(i);
786 if (current.getLength() < maxLength)
788 current.insertCharAt(maxLength - 1, gapCharacter);
791 else if(current.getLength() > maxLength)
793 current.deleteChars(maxLength, current.getLength());
799 public HiddenSequences getHiddenSequences()
801 return hiddenSequences;
803 SequenceI [] getVisibleAndRepresentedSeqs()
805 if(hiddenSequences==null || hiddenSequences.getSize()<1)
806 return getSequencesArray();
808 Vector seqs = new Vector();
810 SequenceGroup hidden;
811 for (int i = 0; i < sequences.size(); i++)
813 seq = (SequenceI) sequences.elementAt(i);
814 seqs.addElement(seq);
815 hidden = seq.getHiddenSequences();
818 for(int j=0; j<hidden.getSize(false); j++)
820 seqs.addElement(hidden.getSequenceAt(j));
824 SequenceI [] result = new SequenceI[seqs.size()];
825 for(int i=0; i<seqs.size(); i++)
826 result[i] = (SequenceI)seqs.elementAt(i);
832 public CigarArray getCompactAlignment()
834 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
835 for (int i=0; i<sequences.size(); i++) {
836 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
838 CigarArray cal = new CigarArray(alseqs);
839 cal.addOperation(CigarArray.M, getWidth());