2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.datamodel;
\r
21 import jalview.analysis.*;
\r
23 import jalview.util.*;
\r
27 /** Data structure to hold and manipulate a multiple sequence alignment
\r
29 public class Alignment implements AlignmentI
\r
31 protected Alignment dataset;
\r
32 protected Vector sequences;
\r
33 protected Vector groups = new Vector();
\r
34 protected char gapCharacter = '-';
\r
35 protected int type = NUCLEOTIDE;
\r
36 public static final int PROTEIN = 0;
\r
37 public static final int NUCLEOTIDE = 1;
\r
39 /** DOCUMENT ME!! */
\r
40 public AlignmentAnnotation[] annotations;
\r
42 HiddenSequences hiddenSequences = new HiddenSequences(this);
\r
45 /** Make an alignment from an array of Sequences.
\r
49 public Alignment(SequenceI[] seqs)
\r
53 if( jalview.util.Comparison.isNucleotide(seqs))
\r
58 sequences = new Vector();
\r
60 for (i = 0; i < seqs.length; i++)
\r
62 sequences.addElement(seqs[i]);
\r
69 * @return DOCUMENT ME!
\r
71 public Vector getSequences()
\r
76 public SequenceI [] getSequencesArray()
\r
78 SequenceI [] reply = new SequenceI[sequences.size()];
\r
79 for(int i=0; i<sequences.size(); i++)
\r
81 reply[i] = (SequenceI)sequences.elementAt(i);
\r
89 * @param i DOCUMENT ME!
\r
91 * @return DOCUMENT ME!
\r
93 public SequenceI getSequenceAt(int i)
\r
95 if (i < sequences.size())
\r
97 return (SequenceI) sequences.elementAt(i);
\r
103 /** Adds a sequence to the alignment. Recalculates maxLength and size.
\r
107 public void addSequence(SequenceI snew)
\r
111 Sequence ds = new Sequence(snew.getName(),
\r
112 AlignSeq.extractGaps("-. ", snew.getSequence()),
\r
116 snew.setDatasetSequence(ds);
\r
117 getDataset().addSequence(ds);
\r
120 sequences.addElement(snew);
\r
124 /** Adds a sequence to the alignment. Recalculates maxLength and size.
\r
128 public void setSequenceAt(int i, SequenceI snew)
\r
130 SequenceI oldseq = getSequenceAt(i);
\r
131 deleteSequence(oldseq);
\r
133 sequences.setElementAt(snew, i);
\r
139 * @return DOCUMENT ME!
\r
141 public Vector getGroups()
\r
146 /** Takes out columns consisting entirely of gaps (-,.," ")
\r
148 public void removeGaps()
\r
151 int iSize = getWidth();
\r
153 for (int i = 0; i < iSize; i++)
\r
155 boolean delete = true;
\r
157 for (int j = 0; j < getHeight(); j++)
\r
159 current = getSequenceAt(j);
\r
161 if (current.getLength() > i)
\r
163 /* MC Should move this to a method somewhere */
\r
164 if (!jalview.util.Comparison.isGap(current.getCharAt(i)))
\r
173 deleteColumns(i, i);
\r
180 /** Removes a range of columns (start to end inclusive).
\r
182 * @param start Start column in the alignment
\r
183 * @param end End column in the alignment
\r
185 public void deleteColumns(int start, int end)
\r
187 deleteColumns(0, getHeight() - 1, start, end);
\r
193 * @param seq1 DOCUMENT ME!
\r
194 * @param seq2 DOCUMENT ME!
\r
195 * @param start DOCUMENT ME!
\r
196 * @param end DOCUMENT ME!
\r
198 public void deleteColumns(int seq1, int seq2, int start, int end)
\r
200 for (int i = 0; i <= (end - start); i++)
\r
202 for (int j = seq1; j <= seq2; j++)
\r
204 getSequenceAt(j).deleteCharAt(start);
\r
212 * @param i DOCUMENT ME!
\r
214 public void trimLeft(int i)
\r
216 int j, jSize = getHeight();
\r
217 for (j = 0; j < jSize; j++)
\r
219 SequenceI s = getSequenceAt(j);
\r
220 int newstart = s.findPosition(i);
\r
222 if(i>s.getLength())
\r
224 sequences.removeElement(s);
\r
230 s.setStart(newstart);
\r
231 s.setSequence(s.getSequence().substring(i));
\r
239 * @param i DOCUMENT ME!
\r
241 public void trimRight(int i)
\r
243 for (int j = 0; j < getHeight(); j++)
\r
245 SequenceI s = getSequenceAt(j);
\r
246 int newend = s.findPosition(i);
\r
249 if(s.getLength()>i)
\r
250 s.setSequence(s.getSequence().substring(0, i + 1));
\r
257 * @param s DOCUMENT ME!
\r
259 public void deleteSequence(SequenceI s)
\r
261 for (int i = 0; i < getHeight(); i++)
\r
263 if (getSequenceAt(i) == s)
\r
273 * @param i DOCUMENT ME!
\r
275 public void deleteSequence(int i)
\r
277 sequences.removeElementAt(i);
\r
282 public SequenceGroup findGroup(SequenceI s)
\r
284 for (int i = 0; i < this.groups.size(); i++)
\r
286 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
\r
288 if (sg.getSequences(false).contains(s))
\r
300 * @param s DOCUMENT ME!
\r
302 * @return DOCUMENT ME!
\r
304 public SequenceGroup[] findAllGroups(SequenceI s)
\r
306 Vector temp = new Vector();
\r
308 int gSize = groups.size();
\r
309 for (int i = 0; i < gSize; i++)
\r
311 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
\r
312 if(sg==null || sg.getSequences(false)==null)
\r
314 this.deleteGroup(sg);
\r
319 if (sg.getSequences(false).contains(s))
\r
321 temp.addElement(sg);
\r
325 SequenceGroup[] ret = new SequenceGroup[temp.size()];
\r
327 for (int i = 0; i < temp.size(); i++)
\r
329 ret[i] = (SequenceGroup) temp.elementAt(i);
\r
338 public void addGroup(SequenceGroup sg)
\r
340 if (!groups.contains(sg))
\r
342 groups.addElement(sg);
\r
349 public void deleteAllGroups()
\r
351 groups.removeAllElements();
\r
355 while (i < sequences.size())
\r
357 SequenceI s = getSequenceAt(i);
\r
358 s.setColor(java.awt.Color.white);
\r
364 public void deleteGroup(SequenceGroup g)
\r
366 if (groups.contains(g))
\r
368 groups.removeElement(g);
\r
373 public SequenceI findName(String name)
\r
377 while (i < sequences.size())
\r
379 if (getSequenceAt(i).getName().equals(name))
\r
381 return getSequenceAt(i);
\r
392 public int findIndex(SequenceI s)
\r
396 while (i < sequences.size())
\r
398 if (s == getSequenceAt(i))
\r
412 * @return DOCUMENT ME!
\r
414 public int getHeight()
\r
416 return sequences.size();
\r
422 * @return DOCUMENT ME!
\r
424 public int getWidth()
\r
426 int maxLength = -1;
\r
428 for (int i = 0; i < sequences.size(); i++)
\r
430 if (getSequenceAt(i).getLength() > maxLength)
\r
432 maxLength = getSequenceAt(i).getLength();
\r
442 * @return DOCUMENT ME!
\r
444 public int getMaxIdLength()
\r
449 while (i < sequences.size())
\r
451 SequenceI seq = getSequenceAt(i);
\r
452 String tmp = seq.getName() + "/" + seq.getStart() + "-" +
\r
455 if (tmp.length() > max)
\r
457 max = tmp.length();
\r
469 * @param gc DOCUMENT ME!
\r
471 public void setGapCharacter(char gc)
\r
475 for (int i = 0; i < sequences.size(); i++)
\r
477 Sequence seq = (Sequence) sequences.elementAt(i);
\r
478 seq.setSequence( seq.getSequence().replace('.', gc) );
\r
479 seq.setSequence( seq.getSequence().replace('-', gc) );
\r
480 seq.setSequence( seq.getSequence().replace(' ', gc) );
\r
487 * @return DOCUMENT ME!
\r
489 public char getGapCharacter()
\r
491 return gapCharacter;
\r
497 * @return DOCUMENT ME!
\r
499 public Vector getAAFrequency()
\r
501 return AAFrequency.calculate(sequences, 0, getWidth());
\r
507 * @return DOCUMENT ME!
\r
509 public boolean isAligned()
\r
511 int width = getWidth();
\r
513 for (int i = 0; i < sequences.size(); i++)
\r
515 if (getSequenceAt(i).getLength() != width)
\r
527 * @param aa DOCUMENT ME!
\r
529 public void deleteAnnotation(AlignmentAnnotation aa)
\r
533 if (annotations != null)
\r
535 aSize = annotations.length;
\r
538 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
\r
542 for (int i = 0; i < aSize; i++)
\r
544 if (annotations[i] == aa)
\r
549 temp[tIndex] = annotations[i];
\r
553 annotations = temp;
\r
557 public void adjustSequenceAnnotations()
\r
559 if(annotations!=null)
\r
561 for (int a = 0; a < annotations.length; a++)
\r
563 if (annotations[a].sequenceRef != null)
\r
565 annotations[a].adjustForAlignment();
\r
574 * @param aa DOCUMENT ME!
\r
576 public void addAnnotation(AlignmentAnnotation aa)
\r
579 if (annotations != null)
\r
581 aSize = annotations.length + 1;
\r
584 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
\r
586 temp[aSize-1] = aa;
\r
592 for (i = 0; i < (aSize-1); i++)
\r
594 temp[i] = annotations[i];
\r
598 annotations = temp;
\r
601 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
\r
603 if(aa==null || annotations==null || annotations.length-1<index)
\r
606 int aSize = annotations.length;
\r
607 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
\r
611 for (int i = 0; i < aSize; i++)
\r
617 temp[i] = annotations[i];
\r
619 temp[i] = annotations[i-1];
\r
622 annotations = temp;
\r
628 * @return DOCUMENT ME!
\r
630 public AlignmentAnnotation[] getAlignmentAnnotation()
\r
632 return annotations;
\r
635 public void setNucleotide(boolean b)
\r
643 public boolean isNucleotide()
\r
645 if(type==NUCLEOTIDE)
\r
651 public void setDataset(Alignment data)
\r
653 if(dataset==null && data==null)
\r
655 // Create a new dataset for this alignment.
\r
656 // Can only be done once, if dataset is not null
\r
657 // This will not be performed
\r
658 Sequence[] seqs = new Sequence[getHeight()];
\r
659 for (int i = 0; i < getHeight(); i++)
\r
662 seqs[i] = new Sequence(getSequenceAt(i).getName(),
\r
663 AlignSeq.extractGaps(
\r
664 jalview.util.Comparison.GapChars,
\r
665 getSequenceAt(i).getSequence()
\r
667 getSequenceAt(i).getStart(),
\r
668 getSequenceAt(i).getEnd());
\r
670 getSequenceAt(i).setDatasetSequence(seqs[i]);
\r
673 dataset = new Alignment(seqs);
\r
675 else if(dataset==null && data!=null)
\r
681 public Alignment getDataset()
\r
686 public boolean padGaps() {
\r
687 boolean modified=false;
\r
689 //Remove excess gaps from the end of alignment
\r
690 int maxLength = -1;
\r
693 for (int i = 0; i < sequences.size(); i++)
\r
695 current = getSequenceAt(i);
\r
696 for (int j = current.getLength(); j > maxLength; j--)
\r
698 if (j > maxLength && !jalview.util.Comparison.isGap(
\r
699 current.getCharAt(j)))
\r
709 for (int i = 0; i < sequences.size();
\r
712 current = getSequenceAt(i);
\r
714 if (current.getLength() < maxLength)
\r
716 current.insertCharAt(maxLength - 1, gapCharacter);
\r
719 else if(current.getLength() > maxLength)
\r
721 current.deleteChars(maxLength, current.getLength());
\r
727 public HiddenSequences getHiddenSequences()
\r
729 return hiddenSequences;
\r