2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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;
25 import jalview.analysis.*;
26 import jalview.schemes.*;
29 * Collects a set contiguous ranges on a set of sequences
34 public class SequenceGroup
40 Conservation conserve;
44 boolean displayBoxes = true;
46 boolean displayText = true;
48 boolean colourText = false;
52 private Vector sequences = new Vector();
54 * representative sequence for this group (if any)
56 private SequenceI seqrep = null;
60 * Colourscheme applied to group if any */
61 public ColourSchemeI cs;
67 public Color outlineColour = Color.black;
69 public Color idColour = null;
71 public int thresholdTextColour = 0;
73 public Color textColour = Color.black;
75 public Color textColour2 = Color.white;
78 * Creates a new SequenceGroup object.
80 public SequenceGroup()
82 groupName = "JGroup:" + this.hashCode();
86 * Creates a new SequenceGroup object.
95 * first column of group
97 * last column of group
99 public SequenceGroup(Vector sequences, String groupName,
100 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
101 boolean colourText, int start, int end)
103 this.sequences = sequences;
104 this.groupName = groupName;
105 this.displayBoxes = displayBoxes;
106 this.displayText = displayText;
107 this.colourText = colourText;
111 recalcConservation();
114 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
116 int iSize = sequences.size();
117 SequenceI[] seqs = new SequenceI[iSize];
118 SequenceI[] inorder = getSequencesInOrder(align);
120 for (int i = 0, ipos = 0; i < inorder.length; i++)
122 SequenceI seq = inorder[i];
124 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
125 if (seqs[ipos] != null)
127 seqs[ipos].setDescription(seq.getDescription());
128 seqs[ipos].setDBRef(seq.getDBRef());
129 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
130 if (seq.getDatasetSequence() != null)
132 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
135 if (seq.getAnnotation() != null)
137 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
138 // Only copy annotation that is either a score or referenced by the
139 // alignment's annotation vector
140 for (int a = 0; a < seq.getAnnotation().length; a++)
142 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
145 boolean found = false;
146 for (int pos = 0; pos < alann.length; pos++)
148 if (alann[pos] == tocopy)
157 AlignmentAnnotation newannot = new AlignmentAnnotation(seq
158 .getAnnotation()[a]);
159 newannot.restrict(startRes, endRes);
160 newannot.setSequenceRef(seqs[ipos]);
161 newannot.adjustForAlignment();
162 seqs[ipos].addAlignmentAnnotation(newannot);
172 if (iSize != inorder.length)
174 SequenceI[] nseqs = new SequenceI[iSize];
175 System.arraycopy(seqs, 0, nseqs, 0, iSize);
183 * If sequence ends in gaps, the end residue can be correctly calculated here
189 public int findEndRes(SequenceI seq)
194 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
196 ch = seq.getCharAt(j);
197 if (!jalview.util.Comparison.isGap((ch)))
205 eres += seq.getStart() - 1;
211 public Vector getSequences(Hashtable hiddenReps)
213 if (hiddenReps == null)
219 Vector allSequences = new Vector();
221 for (int i = 0; i < sequences.size(); i++)
223 seq = (SequenceI) sequences.elementAt(i);
224 allSequences.addElement(seq);
225 if (hiddenReps.containsKey(seq))
227 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
228 for (int h = 0; h < hsg.getSize(); h++)
230 seq2 = hsg.getSequenceAt(h);
231 if (seq2 != seq && !allSequences.contains(seq2))
233 allSequences.addElement(seq2);
243 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
245 Vector tmp = getSequences(hiddenReps);
250 SequenceI[] result = new SequenceI[tmp.size()];
251 for (int i = 0; i < result.length; i++)
253 result[i] = (SequenceI) tmp.elementAt(i);
265 * @return DOCUMENT ME!
267 public boolean adjustForRemoveLeft(int col)
269 // return value is true if the group still exists
272 startRes = startRes - col;
277 endRes = endRes - col;
279 if (startRes > endRes)
286 // must delete this group!!
299 * @return DOCUMENT ME!
301 public boolean adjustForRemoveRight(int col)
320 * @return DOCUMENT ME!
322 public String getName()
327 public String getDescription()
338 public void setName(String name)
343 public void setDescription(String desc)
351 * @return DOCUMENT ME!
353 public Conservation getConservation()
364 public void setConservation(Conservation c)
370 * Add s to this sequence group
373 * alignment sequence to be added
375 * true means Group's conservation should be recalculated
377 public void addSequence(SequenceI s, boolean recalc)
379 if (s != null && !sequences.contains(s))
381 sequences.addElement(s);
386 recalcConservation();
391 * calculate residue conservation for group
393 public void recalcConservation()
402 cs.setConsensus(AAFrequency
403 .calculate(sequences, startRes, endRes + 1));
405 if (cs instanceof ClustalxColourScheme)
407 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
410 if (cs.conservationApplied())
412 Conservation c = new Conservation(groupName,
413 ResidueProperties.propHash, 3, sequences, startRes,
416 c.verdict(false, 25);
418 cs.setConservation(c);
420 if (cs instanceof ClustalxColourScheme)
422 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
425 } catch (java.lang.OutOfMemoryError err)
428 System.out.println("Out of memory loading groups: " + err);
441 public void addOrRemove(SequenceI s, boolean recalc)
443 if (sequences.contains(s))
445 deleteSequence(s, recalc);
449 addSequence(s, recalc);
461 public void deleteSequence(SequenceI s, boolean recalc)
463 sequences.removeElement(s);
467 recalcConservation();
474 * @return DOCUMENT ME!
476 public int getStartRes()
484 * @return DOCUMENT ME!
486 public int getEndRes()
497 public void setStartRes(int i)
508 public void setEndRes(int i)
516 * @return DOCUMENT ME!
520 return sequences.size();
529 * @return DOCUMENT ME!
531 public SequenceI getSequenceAt(int i)
533 return (SequenceI) sequences.elementAt(i);
542 public void setColourText(boolean state)
550 * @return DOCUMENT ME!
552 public boolean getColourText()
563 public void setDisplayText(boolean state)
571 * @return DOCUMENT ME!
573 public boolean getDisplayText()
584 public void setDisplayBoxes(boolean state)
586 displayBoxes = state;
592 * @return DOCUMENT ME!
594 public boolean getDisplayBoxes()
602 * @return DOCUMENT ME!
604 public int getWidth()
606 // MC This needs to get reset when characters are inserted and deleted
607 if (sequences.size() > 0)
609 width = ((SequenceI) sequences.elementAt(0)).getLength();
612 for (int i = 1; i < sequences.size(); i++)
614 SequenceI seq = (SequenceI) sequences.elementAt(i);
616 if (seq.getLength() > width)
618 width = seq.getLength();
631 public void setOutlineColour(Color c)
639 * @return DOCUMENT ME!
641 public Color getOutlineColour()
643 return outlineColour;
648 * returns the sequences in the group ordered by the ordering given by al
652 * @return SequenceI[]
654 public SequenceI[] getSequencesInOrder(AlignmentI al)
656 int sSize = sequences.size();
657 int alHeight = al.getHeight();
659 SequenceI[] seqs = new SequenceI[sSize];
662 for (int i = 0; i < alHeight && index < sSize; i++)
664 if (sequences.contains(al.getSequenceAt(i)))
666 seqs[index++] = al.getSequenceAt(i);
674 * @return the idColour
676 public Color getIdColour()
683 * the idColour to set
685 public void setIdColour(Color idColour)
687 this.idColour = idColour;
691 * @return the representative sequence for this group
693 public SequenceI getSeqrep()
699 * set the representative sequence for this group.
700 * Note - this affects the interpretation of the Hidereps attribute.
701 * @param seqrep the seqrep to set (null means no sequence representative)
703 public void setSeqrep(SequenceI seqrep)
705 this.seqrep = seqrep;
709 * @return true if group has a sequence representative
711 public boolean hasSeqrep()
713 return seqrep != null;
716 * visibility of rows or represented rows covered by group
718 private boolean hidereps=false;
720 * set visibility of sequences covered by (if no sequence representative is defined)
721 * or represented by this group.
724 public void setHidereps(boolean visibility)
726 hidereps = visibility;
730 * @return true if sequences represented (or covered) by this group should be hidden
732 public boolean isHidereps()
737 * visibility of columns intersecting this group
739 private boolean hidecols=false;
741 * set intended visibility of columns covered by this group
744 public void setHideCols(boolean visibility)
746 hidecols = visibility;
750 * @return true if columns covered by group should be hidden
752 public boolean isHideCols()