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
28 /** Data structure to hold and manipulate a multiple sequence alignment
\r
30 public class Alignment implements AlignmentI
\r
32 protected Alignment dataset;
\r
33 protected Vector sequences;
\r
34 protected Vector groups = new Vector();
\r
35 protected Vector superGroup = new Vector();
\r
36 protected char gapCharacter = '-';
\r
37 protected Provenance provenance;
\r
38 protected int type = NUCLEOTIDE;
\r
39 public static final int PROTEIN = 0;
\r
40 public static final int NUCLEOTIDE = 1;
\r
42 /** DOCUMENT ME!! */
\r
43 public AlignmentAnnotation[] annotations;
\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
71 * @return DOCUMENT ME!
\r
73 public Vector getSequences()
\r
81 * @param i DOCUMENT ME!
\r
83 * @return DOCUMENT ME!
\r
85 public SequenceI getSequenceAt(int i)
\r
87 if (i < sequences.size())
\r
89 return (SequenceI) sequences.elementAt(i);
\r
95 /** Adds a sequence to the alignment. Recalculates maxLength and size.
\r
99 public void addSequence(SequenceI snew)
\r
101 sequences.addElement(snew);
\r
107 * @param seq DOCUMENT ME!
\r
109 public void addSequence(SequenceI[] seq)
\r
111 for (int i = 0; i < seq.length; i++)
\r
113 addSequence(seq[i]);
\r
117 /** Adds a sequence to the alignment. Recalculates maxLength and size.
\r
121 public void setSequenceAt(int i, SequenceI snew)
\r
123 SequenceI oldseq = getSequenceAt(i);
\r
124 deleteSequence(oldseq);
\r
126 sequences.setElementAt(snew, i);
\r
132 * @return DOCUMENT ME!
\r
134 public Vector getGroups()
\r
139 /** Takes out columns consisting entirely of gaps (-,.," ")
\r
141 public void removeGaps()
\r
144 int iSize = getWidth();
\r
146 for (int i = 0; i < iSize; i++)
\r
148 boolean delete = true;
\r
150 for (int j = 0; j < getHeight(); j++)
\r
152 current = getSequenceAt(j);
\r
154 if (current.getLength() > i)
\r
156 /* MC Should move this to a method somewhere */
\r
157 if (!jalview.util.Comparison.isGap(current.getCharAt(i)))
\r
166 deleteColumns(i, i);
\r
173 /** Removes a range of columns (start to end inclusive).
\r
175 * @param start Start column in the alignment
\r
176 * @param end End column in the alignment
\r
178 public void deleteColumns(int start, int end)
\r
180 deleteColumns(0, getHeight() - 1, start, end);
\r
186 * @param seq1 DOCUMENT ME!
\r
187 * @param seq2 DOCUMENT ME!
\r
188 * @param start DOCUMENT ME!
\r
189 * @param end DOCUMENT ME!
\r
191 public void deleteColumns(int seq1, int seq2, int start, int end)
\r
193 for (int i = 0; i <= (end - start); i++)
\r
195 for (int j = seq1; j <= seq2; j++)
\r
197 getSequenceAt(j).deleteCharAt(start);
\r
205 * @param i DOCUMENT ME!
\r
207 public void trimLeft(int i)
\r
209 for (int j = 0; j < getHeight(); j++)
\r
211 SequenceI s = getSequenceAt(j);
\r
212 int newstart = s.findPosition(i);
\r
214 s.setStart(newstart);
\r
215 s.setSequence(s.getSequence().substring(i));
\r
222 * @param i DOCUMENT ME!
\r
224 public void trimRight(int i)
\r
226 for (int j = 0; j < getHeight(); j++)
\r
228 SequenceI s = getSequenceAt(j);
\r
229 int newend = s.findPosition(i);
\r
232 s.setSequence(s.getSequence().substring(0, i + 1));
\r
239 * @param s DOCUMENT ME!
\r
241 public void deleteSequence(SequenceI s)
\r
243 for (int i = 0; i < getHeight(); i++)
\r
245 if (getSequenceAt(i) == s)
\r
255 * @param i DOCUMENT ME!
\r
257 public void deleteSequence(int i)
\r
259 sequences.removeElementAt(i);
\r
265 * @param threshold DOCUMENT ME!
\r
266 * @param sel DOCUMENT ME!
\r
268 * @return DOCUMENT ME!
\r
270 public Vector removeRedundancy(float threshold, Vector sel)
\r
272 Vector del = new Vector();
\r
274 for (int i = 1; i < sel.size(); i++)
\r
276 for (int j = 0; j < i; j++)
\r
278 // Only do the comparison if either have not been deleted
\r
279 if (!del.contains((SequenceI) sel.elementAt(i)) ||
\r
280 !del.contains((SequenceI) sel.elementAt(j)))
\r
282 // use PID instead of Comparison (which is really not pleasant)
\r
283 float pid = Comparison.PID((SequenceI) sel.elementAt(j),
\r
284 (SequenceI) sel.elementAt(i));
\r
286 if (pid >= threshold)
\r
288 // Delete the shortest one
\r
289 if (((SequenceI) sel.elementAt(j)).getSequence().length() > ((SequenceI) sel
\r
291 i)).getSequence().length())
\r
293 del.addElement(sel.elementAt(i));
\r
297 del.addElement(sel.elementAt(i));
\r
304 // Now delete the sequences
\r
305 for (int i = 0; i < del.size(); i++)
\r
307 deleteSequence((SequenceI) del.elementAt(i));
\r
314 public SequenceGroup findGroup(int i)
\r
316 return findGroup(getSequenceAt(i));
\r
320 public SequenceGroup findGroup(SequenceI s)
\r
322 for (int i = 0; i < this.groups.size(); i++)
\r
324 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
\r
326 if (sg.sequences.contains(s))
\r
338 * @param s DOCUMENT ME!
\r
340 * @return DOCUMENT ME!
\r
342 public SequenceGroup[] findAllGroups(SequenceI s)
\r
344 Vector temp = new Vector();
\r
346 int gSize = groups.size();
\r
347 for (int i = 0; i < gSize; i++)
\r
349 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
\r
350 if(sg==null || sg.sequences==null)
\r
352 this.deleteGroup(sg);
\r
357 if (sg.sequences.contains(s))
\r
359 temp.addElement(sg);
\r
363 SequenceGroup[] ret = new SequenceGroup[temp.size()];
\r
365 for (int i = 0; i < temp.size(); i++)
\r
367 ret[i] = (SequenceGroup) temp.elementAt(i);
\r
376 * @param sg DOCUMENT ME!
\r
378 public void addSuperGroup(SuperGroup sg)
\r
380 superGroup.addElement(sg);
\r
386 * @param sg DOCUMENT ME!
\r
388 public void removeSuperGroup(SuperGroup sg)
\r
390 superGroup.removeElement(sg);
\r
396 * @param sg DOCUMENT ME!
\r
398 * @return DOCUMENT ME!
\r
400 public SuperGroup getSuperGroup(SequenceGroup sg)
\r
402 for (int i = 0; i < this.superGroup.size(); i++)
\r
404 SuperGroup temp = (SuperGroup) superGroup.elementAt(i);
\r
406 if (temp.sequenceGroups.contains(sg))
\r
416 public void addGroup(SequenceGroup sg)
\r
418 if (!groups.contains(sg))
\r
420 groups.addElement(sg);
\r
427 public void deleteAllGroups()
\r
429 groups.removeAllElements();
\r
430 superGroup.removeAllElements();
\r
434 while (i < sequences.size())
\r
436 SequenceI s = getSequenceAt(i);
\r
437 s.setColor(java.awt.Color.white);
\r
443 public void deleteGroup(SequenceGroup g)
\r
445 if (groups.contains(g))
\r
447 groups.removeElement(g);
\r
452 public SequenceI findName(String name)
\r
456 while (i < sequences.size())
\r
458 if (getSequenceAt(i).getName().equals(name))
\r
460 return getSequenceAt(i);
\r
471 public int findIndex(SequenceI s)
\r
475 while (i < sequences.size())
\r
477 if (s == getSequenceAt(i))
\r
491 * @return DOCUMENT ME!
\r
493 public int getHeight()
\r
495 return sequences.size();
\r
501 * @return DOCUMENT ME!
\r
503 public int getWidth()
\r
505 int maxLength = -1;
\r
507 for (int i = 0; i < sequences.size(); i++)
\r
509 if (getSequenceAt(i).getLength() > maxLength)
\r
511 maxLength = getSequenceAt(i).getLength();
\r
521 * @return DOCUMENT ME!
\r
523 public int getMaxIdLength()
\r
528 while (i < sequences.size())
\r
530 SequenceI seq = getSequenceAt(i);
\r
531 String tmp = seq.getName() + "/" + seq.getStart() + "-" +
\r
534 if (tmp.length() > max)
\r
536 max = tmp.length();
\r
548 * @param gc DOCUMENT ME!
\r
550 public void setGapCharacter(char gc)
\r
554 for (int i = 0; i < sequences.size(); i++)
\r
556 Sequence seq = (Sequence) sequences.elementAt(i);
\r
557 seq.sequence = seq.sequence.replace('.', gc);
\r
558 seq.sequence = seq.sequence.replace('-', gc);
\r
559 seq.sequence = seq.sequence.replace(' ', gc);
\r
566 * @return DOCUMENT ME!
\r
568 public char getGapCharacter()
\r
570 return gapCharacter;
\r
576 * @return DOCUMENT ME!
\r
578 public Vector getAAFrequency()
\r
580 return AAFrequency.calculate(sequences, 0, getWidth());
\r
586 * @return DOCUMENT ME!
\r
588 public boolean isAligned()
\r
590 int width = getWidth();
\r
592 for (int i = 0; i < sequences.size(); i++)
\r
594 if (getSequenceAt(i).getLength() != width)
\r
606 * @param aa DOCUMENT ME!
\r
608 public void deleteAnnotation(AlignmentAnnotation aa)
\r
612 if (annotations != null)
\r
614 aSize = annotations.length;
\r
617 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
\r
621 for (int i = 0; i < aSize; i++)
\r
623 if (annotations[i] == aa)
\r
628 temp[tIndex] = annotations[i];
\r
632 annotations = temp;
\r
638 * @param aa DOCUMENT ME!
\r
640 public void addAnnotation(AlignmentAnnotation aa)
\r
644 if (annotations != null)
\r
646 aSize = annotations.length + 1;
\r
650 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
\r
655 for (i = 0; i < (aSize - 1); i++)
\r
657 temp[i] = annotations[i];
\r
663 annotations = temp;
\r
669 * @return DOCUMENT ME!
\r
671 public AlignmentAnnotation[] getAlignmentAnnotation()
\r
673 return annotations;
\r
676 public void setNucleotide(boolean b)
\r
684 public boolean isNucleotide()
\r
686 if(type==NUCLEOTIDE)
\r
692 public void setDataset(Alignment data)
\r
694 if(dataset==null && data==null)
\r
696 // Create a new dataset for this alignment.
\r
697 // Can only be done once, if dataset is not null
\r
698 // This will not be performed
\r
699 Sequence[] seqs = new Sequence[getHeight()];
\r
700 for (int i = 0; i < getHeight(); i++)
\r
703 seqs[i] = new Sequence(getSequenceAt(i).getName(),
\r
704 AlignSeq.extractGaps(
\r
705 jalview.util.Comparison.GapChars,
\r
706 getSequenceAt(i).getSequence()
\r
708 getSequenceAt(i).getStart(),
\r
709 getSequenceAt(i).getEnd());
\r
711 getSequenceAt(i).setDatasetSequence(seqs[i]);
\r
714 dataset = new Alignment(seqs);
\r
716 else if(dataset==null && data!=null)
\r
722 public Alignment getDataset()
\r
727 public void setProvenance(Provenance prov)
\r
731 public Provenance getProvenance()
\r