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()
176 /** Takes out columns consisting entirely of gaps (-,.," ")
178 public void removeGaps() {
179 removeGaps((ShiftList)null);
182 * remove gaps in alignment - recording any frame shifts in shiftrecord
183 * intended to be passed to ColumnSelection.compensateForEdits(shiftrecord)
186 public void removeGaps(ShiftList shiftrecord) {
187 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
188 int j, jSize = seqs.length;
191 for (int i = 0; i < jSize; i++)
193 if (seqs[i].getLength() > width)
195 width = seqs[i].getLength();
199 int startCol = -1, endCol = -1;
200 boolean delete = true;
201 for (int i = 0; i < width; i++)
205 for (j = 0; j < jSize; j++)
207 if (seqs[j].getLength() > i)
209 if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i)))
220 if(delete && startCol==-1)
226 if (!delete && startCol > -1)
228 deleteColumns(seqs, startCol, endCol);
229 if (shiftrecord!=null) {
230 shiftrecord.addShift(startCol, 1+endCol-startCol);
232 width -= (endCol - startCol);
233 i -= (endCol - startCol);
239 if (delete && startCol > -1)
241 deleteColumns(seqs, startCol, endCol);
242 if (shiftrecord!=null) {
243 shiftrecord.addShift(startCol, 1+endCol-startCol);
248 /** Removes a range of columns (start to end inclusive).
250 * @param seqs Sequences to remove columns from
251 * @param start Start column in the alignment
252 * @param end End column in the alignment
254 public void deleteColumns(SequenceI [] seqs, int start, int end)
256 for(int i=0; i<seqs.length; i++)
257 seqs[i].deleteChars(start, end);
264 * @param i DOCUMENT ME!
266 public void trimLeft(int i)
268 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
269 int j, jSize = seqs.length;
270 for (j = 0; j < jSize; j++)
272 int newstart = seqs[j].findPosition(i);
274 if(i>seqs[j].getLength())
276 sequences.removeElement(seqs[j]);
282 seqs[j].setStart(newstart);
283 seqs[j].setSequence(seqs[j].getSequence().substring(i));
291 * @param i DOCUMENT ME!
293 public void trimRight(int i)
295 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
296 int j, jSize = seqs.length;
297 for (j = 0; j < jSize; j++)
299 int newend = seqs[j].findPosition(i);
301 seqs[j].setEnd(newend);
302 if(seqs[j].getLength()>i)
303 seqs[j].setSequence(seqs[j].getSequence().substring(0, i + 1));
310 * @param s DOCUMENT ME!
312 public void deleteSequence(SequenceI s)
314 deleteSequence(findIndex(s));
320 * @param i DOCUMENT ME!
322 public void deleteSequence(int i)
324 sequences.removeElementAt(i);
325 hiddenSequences.adjustHeightSequenceDeleted(i);
330 public SequenceGroup findGroup(SequenceI s)
332 for (int i = 0; i < this.groups.size(); i++)
334 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
336 if (sg.getSequences(false).contains(s))
348 * @param s DOCUMENT ME!
350 * @return DOCUMENT ME!
352 public SequenceGroup[] findAllGroups(SequenceI s)
354 Vector temp = new Vector();
356 int gSize = groups.size();
357 for (int i = 0; i < gSize; i++)
359 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
360 if(sg==null || sg.getSequences(false)==null)
362 this.deleteGroup(sg);
367 if (sg.getSequences(false).contains(s))
373 SequenceGroup[] ret = new SequenceGroup[temp.size()];
375 for (int i = 0; i < temp.size(); i++)
377 ret[i] = (SequenceGroup) temp.elementAt(i);
386 public void addGroup(SequenceGroup sg)
388 if (!groups.contains(sg))
390 groups.addElement(sg);
397 public void deleteAllGroups()
399 groups.removeAllElements();
403 while (i < sequences.size())
405 SequenceI s = getSequenceAt(i);
406 s.setColor(java.awt.Color.white);
412 public void deleteGroup(SequenceGroup g)
414 if (groups.contains(g))
416 groups.removeElement(g);
421 public SequenceI findName(String name)
425 while (i < sequences.size())
427 if (getSequenceAt(i).getName().equals(name))
429 return getSequenceAt(i);
438 public SequenceI [] findSequenceMatch(String name)
440 Vector matches = new Vector();
443 while (i < sequences.size())
445 if (getSequenceAt(i).getName().equals(name))
447 matches.addElement(getSequenceAt(i));
452 SequenceI [] result = new SequenceI[matches.size()];
453 for(i=0; i<result.length; i++)
454 result[i] = (SequenceI)matches.elementAt(i);
462 public int findIndex(SequenceI s)
466 while (i < sequences.size())
468 if (s == getSequenceAt(i))
482 * @return DOCUMENT ME!
484 public int getHeight()
486 return sequences.size();
492 * @return DOCUMENT ME!
494 public int getWidth()
498 for (int i = 0; i < sequences.size(); i++)
500 if (getSequenceAt(i).getLength() > maxLength)
502 maxLength = getSequenceAt(i).getLength();
512 * @return DOCUMENT ME!
514 public int getMaxIdLength()
519 while (i < sequences.size())
521 SequenceI seq = getSequenceAt(i);
522 String tmp = seq.getName() + "/" + seq.getStart() + "-" +
525 if (tmp.length() > max)
539 * @param gc DOCUMENT ME!
541 public void setGapCharacter(char gc)
545 for (int i = 0; i < sequences.size(); i++)
547 Sequence seq = (Sequence) sequences.elementAt(i);
548 seq.setSequence( seq.getSequence().replace('.', gc) );
549 seq.setSequence( seq.getSequence().replace('-', gc) );
550 seq.setSequence( seq.getSequence().replace(' ', gc) );
557 * @return DOCUMENT ME!
559 public char getGapCharacter()
568 * @return DOCUMENT ME!
570 public boolean isAligned()
572 int width = getWidth();
574 for (int i = 0; i < sequences.size(); i++)
576 if (getSequenceAt(i).getLength() != width)
588 * @param aa DOCUMENT ME!
590 public void deleteAnnotation(AlignmentAnnotation aa)
594 if (annotations != null)
596 aSize = annotations.length;
599 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
603 for (int i = 0; i < aSize; i++)
605 if (annotations[i] == aa)
610 temp[tIndex] = annotations[i];
618 public void adjustSequenceAnnotations()
620 if(annotations!=null)
622 for (int a = 0; a < annotations.length; a++)
624 if (annotations[a].sequenceRef != null)
626 annotations[a].adjustForAlignment();
635 * @param aa DOCUMENT ME!
637 public void addAnnotation(AlignmentAnnotation aa)
640 if (annotations != null)
642 aSize = annotations.length + 1;
645 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
653 for (i = 0; i < (aSize-1); i++)
655 temp[i] = annotations[i];
662 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
664 if(aa==null || annotations==null || annotations.length-1<index)
667 int aSize = annotations.length;
668 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
672 for (int i = 0; i < aSize; i++)
678 temp[i] = annotations[i];
680 temp[i] = annotations[i-1];
689 * @return DOCUMENT ME!
691 public AlignmentAnnotation[] getAlignmentAnnotation()
696 public void setNucleotide(boolean b)
704 public boolean isNucleotide()
712 public void setDataset(Alignment data)
714 if(dataset==null && data==null)
716 // Create a new dataset for this alignment.
717 // Can only be done once, if dataset is not null
718 // This will not be performed
719 Sequence[] seqs = new Sequence[getHeight()];
720 for (int i = 0; i < getHeight(); i++)
722 if(getSequenceAt(i).getDatasetSequence()!=null)
724 seqs[i] = (Sequence)getSequenceAt(i).getDatasetSequence();
728 seqs[i] = new Sequence(getSequenceAt(i).getName(),
729 AlignSeq.extractGaps(
730 jalview.util.Comparison.GapChars,
731 getSequenceAt(i).getSequence()
733 getSequenceAt(i).getStart(),
734 getSequenceAt(i).getEnd());
735 seqs[i].sequenceFeatures = getSequenceAt(i).getSequenceFeatures();
736 getSequenceAt(i).setSequenceFeatures(null);
737 getSequenceAt(i).setDatasetSequence(seqs[i]);
741 dataset = new Alignment(seqs);
743 else if(dataset==null && data!=null)
749 public Alignment getDataset()
754 public boolean padGaps() {
755 boolean modified=false;
757 //Remove excess gaps from the end of alignment
761 for (int i = 0; i < sequences.size(); i++)
763 current = getSequenceAt(i);
764 for (int j = current.getLength(); j > maxLength; j--)
766 if (j > maxLength && !jalview.util.Comparison.isGap(
767 current.getCharAt(j)))
777 for (int i = 0; i < sequences.size();
780 current = getSequenceAt(i);
782 if (current.getLength() < maxLength)
784 current.insertCharAt(maxLength - 1, gapCharacter);
787 else if(current.getLength() > maxLength)
789 current.deleteChars(maxLength, current.getLength());
795 public HiddenSequences getHiddenSequences()
797 return hiddenSequences;
799 SequenceI [] getVisibleAndRepresentedSeqs()
801 if(hiddenSequences==null || hiddenSequences.getSize()<1)
802 return getSequencesArray();
804 Vector seqs = new Vector();
806 SequenceGroup hidden;
807 for (int i = 0; i < sequences.size(); i++)
809 seq = (SequenceI) sequences.elementAt(i);
810 seqs.addElement(seq);
811 hidden = seq.getHiddenSequences();
814 for(int j=0; j<hidden.getSize(false); j++)
816 seqs.addElement(hidden.getSequenceAt(j));
820 SequenceI [] result = new SequenceI[seqs.size()];
821 for(int i=0; i<seqs.size(); i++)
822 result[i] = (SequenceI)seqs.elementAt(i);
828 public CigarArray getCompactAlignment()
830 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
831 for (int i=0; i<sequences.size(); i++) {
832 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
834 CigarArray cal = new CigarArray(alseqs);
835 cal.addOperation(CigarArray.M, getWidth());