2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 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()
176 /** Takes out columns consisting entirely of gaps (-,.," ")
178 public void removeGaps()
180 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
181 int j, jSize = seqs.length;
184 for (int i = 0; i < jSize; i++)
186 if (seqs[i].getLength() > width)
188 width = seqs[i].getLength();
192 int startCol = -1, endCol = -1;
193 boolean delete = true;
194 for (int i = 0; i < width; i++)
198 for (j = 0; j < jSize; j++)
200 if (seqs[j].getLength() > i)
202 if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i)))
213 if(delete && startCol==-1)
219 if (!delete && startCol > -1)
221 deleteColumns(seqs, startCol, endCol);
222 width -= (endCol - startCol);
223 i -= (endCol - startCol);
229 if (delete && startCol > -1)
231 deleteColumns(seqs, startCol, endCol);
235 /** Removes a range of columns (start to end inclusive).
237 * @param seqs Sequences to remove columns from
238 * @param start Start column in the alignment
239 * @param end End column in the alignment
241 public void deleteColumns(SequenceI [] seqs, int start, int end)
243 for(int i=0; i<seqs.length; i++)
244 seqs[i].deleteChars(start, end);
251 * @param i DOCUMENT ME!
253 public void trimLeft(int i)
255 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
256 int j, jSize = seqs.length;
257 for (j = 0; j < jSize; j++)
259 int newstart = seqs[j].findPosition(i);
261 if(i>seqs[j].getLength())
263 sequences.removeElement(seqs[j]);
269 seqs[j].setStart(newstart);
270 seqs[j].setSequence(seqs[j].getSequence().substring(i));
278 * @param i DOCUMENT ME!
280 public void trimRight(int i)
282 SequenceI[] seqs = getVisibleAndRepresentedSeqs();
283 int j, jSize = seqs.length;
284 for (j = 0; j < jSize; j++)
286 int newend = seqs[j].findPosition(i);
288 seqs[j].setEnd(newend);
289 if(seqs[j].getLength()>i)
290 seqs[j].setSequence(seqs[j].getSequence().substring(0, i + 1));
297 * @param s DOCUMENT ME!
299 public void deleteSequence(SequenceI s)
301 for (int i = 0; i < getHeight(); i++)
303 if (getSequenceAt(i) == s)
313 * @param i DOCUMENT ME!
315 public void deleteSequence(int i)
317 sequences.removeElementAt(i);
322 public SequenceGroup findGroup(SequenceI s)
324 for (int i = 0; i < this.groups.size(); i++)
326 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
328 if (sg.getSequences(false).contains(s))
340 * @param s DOCUMENT ME!
342 * @return DOCUMENT ME!
344 public SequenceGroup[] findAllGroups(SequenceI s)
346 Vector temp = new Vector();
348 int gSize = groups.size();
349 for (int i = 0; i < gSize; i++)
351 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
352 if(sg==null || sg.getSequences(false)==null)
354 this.deleteGroup(sg);
359 if (sg.getSequences(false).contains(s))
365 SequenceGroup[] ret = new SequenceGroup[temp.size()];
367 for (int i = 0; i < temp.size(); i++)
369 ret[i] = (SequenceGroup) temp.elementAt(i);
378 public void addGroup(SequenceGroup sg)
380 if (!groups.contains(sg))
382 groups.addElement(sg);
389 public void deleteAllGroups()
391 groups.removeAllElements();
395 while (i < sequences.size())
397 Sequence s = (Sequence)getSequenceAt(i);
398 s.hiddenSequences = null;
399 s.setColor(java.awt.Color.white);
405 public void deleteGroup(SequenceGroup g)
407 if (groups.contains(g))
409 //remove any hidden representatives
410 // for(int i=0; i<g.getsiz
412 groups.removeElement(g);
417 public SequenceI findName(String name)
421 while (i < sequences.size())
423 if (getSequenceAt(i).getName().equals(name))
425 return getSequenceAt(i);
434 public SequenceI [] findSequenceMatch(String name)
436 Vector matches = new Vector();
439 while (i < sequences.size())
441 if (getSequenceAt(i).getName().equals(name))
443 matches.addElement(getSequenceAt(i));
448 SequenceI [] result = new SequenceI[matches.size()];
449 for(i=0; i<result.length; i++)
450 result[i] = (SequenceI)matches.elementAt(i);
458 public int findIndex(SequenceI s)
462 while (i < sequences.size())
464 if (s == getSequenceAt(i))
478 * @return DOCUMENT ME!
480 public int getHeight()
482 return sequences.size();
488 * @return DOCUMENT ME!
490 public int getWidth()
494 for (int i = 0; i < sequences.size(); i++)
496 if (getSequenceAt(i).getLength() > maxLength)
498 maxLength = getSequenceAt(i).getLength();
508 * @return DOCUMENT ME!
510 public int getMaxIdLength()
515 while (i < sequences.size())
517 SequenceI seq = getSequenceAt(i);
518 String tmp = seq.getName() + "/" + seq.getStart() + "-" +
521 if (tmp.length() > max)
535 * @param gc DOCUMENT ME!
537 public void setGapCharacter(char gc)
541 for (int i = 0; i < sequences.size(); i++)
543 Sequence seq = (Sequence) sequences.elementAt(i);
544 seq.setSequence( seq.getSequence().replace('.', gc) );
545 seq.setSequence( seq.getSequence().replace('-', gc) );
546 seq.setSequence( seq.getSequence().replace(' ', gc) );
553 * @return DOCUMENT ME!
555 public char getGapCharacter()
563 * @return DOCUMENT ME!
565 public Vector getAAFrequency()
567 return AAFrequency.calculate(sequences, 0, getWidth());
573 * @return DOCUMENT ME!
575 public boolean isAligned()
577 int width = getWidth();
579 for (int i = 0; i < sequences.size(); i++)
581 if (getSequenceAt(i).getLength() != width)
593 * @param aa DOCUMENT ME!
595 public void deleteAnnotation(AlignmentAnnotation aa)
599 if (annotations != null)
601 aSize = annotations.length;
604 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
608 for (int i = 0; i < aSize; i++)
610 if (annotations[i] == aa)
615 temp[tIndex] = annotations[i];
623 public void adjustSequenceAnnotations()
625 if(annotations!=null)
627 for (int a = 0; a < annotations.length; a++)
629 if (annotations[a].sequenceRef != null)
631 annotations[a].adjustForAlignment();
640 * @param aa DOCUMENT ME!
642 public void addAnnotation(AlignmentAnnotation aa)
645 if (annotations != null)
647 aSize = annotations.length + 1;
650 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
658 for (i = 0; i < (aSize-1); i++)
660 temp[i] = annotations[i];
667 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
669 if(aa==null || annotations==null || annotations.length-1<index)
672 int aSize = annotations.length;
673 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
677 for (int i = 0; i < aSize; i++)
683 temp[i] = annotations[i];
685 temp[i] = annotations[i-1];
694 * @return DOCUMENT ME!
696 public AlignmentAnnotation[] getAlignmentAnnotation()
701 public void setNucleotide(boolean b)
709 public boolean isNucleotide()
717 public void setDataset(Alignment data)
719 if(dataset==null && data==null)
721 // Create a new dataset for this alignment.
722 // Can only be done once, if dataset is not null
723 // This will not be performed
724 Sequence[] seqs = new Sequence[getHeight()];
725 for (int i = 0; i < getHeight(); i++)
727 if(getSequenceAt(i).getDatasetSequence()!=null)
729 seqs[i] = (Sequence)getSequenceAt(i).getDatasetSequence();
733 seqs[i] = new Sequence(getSequenceAt(i).getName(),
734 AlignSeq.extractGaps(
735 jalview.util.Comparison.GapChars,
736 getSequenceAt(i).getSequence()
738 getSequenceAt(i).getStart(),
739 getSequenceAt(i).getEnd());
740 seqs[i].sequenceFeatures = getSequenceAt(i).getSequenceFeatures();
741 getSequenceAt(i).setSequenceFeatures(null);
742 getSequenceAt(i).setDatasetSequence(seqs[i]);
746 dataset = new Alignment(seqs);
748 else if(dataset==null && data!=null)
754 public Alignment getDataset()
759 public boolean padGaps() {
760 boolean modified=false;
762 //Remove excess gaps from the end of alignment
766 for (int i = 0; i < sequences.size(); i++)
768 current = getSequenceAt(i);
769 for (int j = current.getLength(); j > maxLength; j--)
771 if (j > maxLength && !jalview.util.Comparison.isGap(
772 current.getCharAt(j)))
782 for (int i = 0; i < sequences.size();
785 current = getSequenceAt(i);
787 if (current.getLength() < maxLength)
789 current.insertCharAt(maxLength - 1, gapCharacter);
792 else if(current.getLength() > maxLength)
794 current.deleteChars(maxLength, current.getLength());
800 public HiddenSequences getHiddenSequences()
802 return hiddenSequences;
804 SequenceI [] getVisibleAndRepresentedSeqs()
806 if(hiddenSequences==null || hiddenSequences.getSize()<1)
807 return getSequencesArray();
809 Vector seqs = new Vector();
811 SequenceGroup hidden;
812 for (int i = 0; i < sequences.size(); i++)
814 seq = (SequenceI) sequences.elementAt(i);
815 seqs.addElement(seq);
816 hidden = seq.getHiddenSequences();
819 for(int j=0; j<hidden.getSize(false); j++)
821 seqs.addElement(hidden.getSequenceAt(j));
825 SequenceI [] result = new SequenceI[seqs.size()];
826 for(int i=0; i<seqs.size(); i++)
827 result[i] = (SequenceI)seqs.elementAt(i);
833 public CigarArray getCompactAlignment()
835 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
836 for (int i=0; i<sequences.size(); i++) {
837 alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
839 return new CigarArray(alseqs);