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();
117 public SequenceGroup(SequenceGroup seqsel)
121 sequences=new Vector();
122 Enumeration sq = seqsel.sequences.elements();
123 while (sq.hasMoreElements()) {
124 sequences.addElement(sq.nextElement());
126 if (seqsel.groupName!=null)
128 groupName = new String(seqsel.groupName);
130 displayBoxes = seqsel.displayBoxes;
131 displayText = seqsel.displayText;
132 colourText = seqsel.colourText;
133 startRes = seqsel.startRes;
134 endRes = seqsel.endRes;
136 if (seqsel.description!=null)
137 description = new String(seqsel.description);
138 hidecols = seqsel.hidecols;
139 hidereps = seqsel.hidereps;
140 idColour = seqsel.idColour;
141 outlineColour = seqsel.outlineColour;
142 seqrep = seqsel.seqrep;
143 textColour = seqsel.textColour;
144 textColour2 = seqsel.textColour2;
145 thresholdTextColour = seqsel.thresholdTextColour;
146 width = seqsel.width;
147 if (seqsel.conserve!=null)
149 recalcConservation(); // safer than
150 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
155 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
157 int iSize = sequences.size();
158 SequenceI[] seqs = new SequenceI[iSize];
159 SequenceI[] inorder = getSequencesInOrder(align);
161 for (int i = 0, ipos = 0; i < inorder.length; i++)
163 SequenceI seq = inorder[i];
165 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
166 if (seqs[ipos] != null)
168 seqs[ipos].setDescription(seq.getDescription());
169 seqs[ipos].setDBRef(seq.getDBRef());
170 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
171 if (seq.getDatasetSequence() != null)
173 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
176 if (seq.getAnnotation() != null)
178 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
179 // Only copy annotation that is either a score or referenced by the
180 // alignment's annotation vector
181 for (int a = 0; a < seq.getAnnotation().length; a++)
183 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
186 boolean found = false;
187 for (int pos = 0; pos < alann.length; pos++)
189 if (alann[pos] == tocopy)
198 AlignmentAnnotation newannot = new AlignmentAnnotation(seq
199 .getAnnotation()[a]);
200 newannot.restrict(startRes, endRes);
201 newannot.setSequenceRef(seqs[ipos]);
202 newannot.adjustForAlignment();
203 seqs[ipos].addAlignmentAnnotation(newannot);
213 if (iSize != inorder.length)
215 SequenceI[] nseqs = new SequenceI[iSize];
216 System.arraycopy(seqs, 0, nseqs, 0, iSize);
224 * If sequence ends in gaps, the end residue can be correctly calculated here
230 public int findEndRes(SequenceI seq)
235 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
237 ch = seq.getCharAt(j);
238 if (!jalview.util.Comparison.isGap((ch)))
246 eres += seq.getStart() - 1;
252 public Vector getSequences(Hashtable hiddenReps)
254 if (hiddenReps == null)
260 Vector allSequences = new Vector();
262 for (int i = 0; i < sequences.size(); i++)
264 seq = (SequenceI) sequences.elementAt(i);
265 allSequences.addElement(seq);
266 if (hiddenReps.containsKey(seq))
268 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
269 for (int h = 0; h < hsg.getSize(); h++)
271 seq2 = hsg.getSequenceAt(h);
272 if (seq2 != seq && !allSequences.contains(seq2))
274 allSequences.addElement(seq2);
284 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
286 Vector tmp = getSequences(hiddenReps);
291 SequenceI[] result = new SequenceI[tmp.size()];
292 for (int i = 0; i < result.length; i++)
294 result[i] = (SequenceI) tmp.elementAt(i);
306 * @return DOCUMENT ME!
308 public boolean adjustForRemoveLeft(int col)
310 // return value is true if the group still exists
313 startRes = startRes - col;
318 endRes = endRes - col;
320 if (startRes > endRes)
327 // must delete this group!!
340 * @return DOCUMENT ME!
342 public boolean adjustForRemoveRight(int col)
361 * @return DOCUMENT ME!
363 public String getName()
368 public String getDescription()
379 public void setName(String name)
384 public void setDescription(String desc)
392 * @return DOCUMENT ME!
394 public Conservation getConservation()
405 public void setConservation(Conservation c)
411 * Add s to this sequence group
414 * alignment sequence to be added
416 * true means Group's conservation should be recalculated
418 public void addSequence(SequenceI s, boolean recalc)
420 if (s != null && !sequences.contains(s))
422 sequences.addElement(s);
427 recalcConservation();
432 * calculate residue conservation for group
434 public void recalcConservation()
443 cs.setConsensus(AAFrequency
444 .calculate(sequences, startRes, endRes + 1));
446 if (cs instanceof ClustalxColourScheme)
448 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
451 if (cs.conservationApplied())
453 Conservation c = new Conservation(groupName,
454 ResidueProperties.propHash, 3, sequences, startRes,
457 c.verdict(false, 25);
459 cs.setConservation(c);
461 if (cs instanceof ClustalxColourScheme)
463 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
466 } catch (java.lang.OutOfMemoryError err)
469 System.out.println("Out of memory loading groups: " + err);
482 public void addOrRemove(SequenceI s, boolean recalc)
484 if (sequences.contains(s))
486 deleteSequence(s, recalc);
490 addSequence(s, recalc);
502 public void deleteSequence(SequenceI s, boolean recalc)
504 sequences.removeElement(s);
508 recalcConservation();
515 * @return DOCUMENT ME!
517 public int getStartRes()
525 * @return DOCUMENT ME!
527 public int getEndRes()
538 public void setStartRes(int i)
549 public void setEndRes(int i)
557 * @return DOCUMENT ME!
561 return sequences.size();
570 * @return DOCUMENT ME!
572 public SequenceI getSequenceAt(int i)
574 return (SequenceI) sequences.elementAt(i);
583 public void setColourText(boolean state)
591 * @return DOCUMENT ME!
593 public boolean getColourText()
604 public void setDisplayText(boolean state)
612 * @return DOCUMENT ME!
614 public boolean getDisplayText()
625 public void setDisplayBoxes(boolean state)
627 displayBoxes = state;
633 * @return DOCUMENT ME!
635 public boolean getDisplayBoxes()
643 * @return DOCUMENT ME!
645 public int getWidth()
647 // MC This needs to get reset when characters are inserted and deleted
648 if (sequences.size() > 0)
650 width = ((SequenceI) sequences.elementAt(0)).getLength();
653 for (int i = 1; i < sequences.size(); i++)
655 SequenceI seq = (SequenceI) sequences.elementAt(i);
657 if (seq.getLength() > width)
659 width = seq.getLength();
672 public void setOutlineColour(Color c)
680 * @return DOCUMENT ME!
682 public Color getOutlineColour()
684 return outlineColour;
689 * returns the sequences in the group ordered by the ordering given by al
693 * @return SequenceI[]
695 public SequenceI[] getSequencesInOrder(AlignmentI al)
697 int sSize = sequences.size();
698 int alHeight = al.getHeight();
700 SequenceI[] seqs = new SequenceI[sSize];
703 for (int i = 0; i < alHeight && index < sSize; i++)
705 if (sequences.contains(al.getSequenceAt(i)))
707 seqs[index++] = al.getSequenceAt(i);
715 * @return the idColour
717 public Color getIdColour()
724 * the idColour to set
726 public void setIdColour(Color idColour)
728 this.idColour = idColour;
732 * @return the representative sequence for this group
734 public SequenceI getSeqrep()
740 * set the representative sequence for this group.
741 * Note - this affects the interpretation of the Hidereps attribute.
742 * @param seqrep the seqrep to set (null means no sequence representative)
744 public void setSeqrep(SequenceI seqrep)
746 this.seqrep = seqrep;
750 * @return true if group has a sequence representative
752 public boolean hasSeqrep()
754 return seqrep != null;
757 * visibility of rows or represented rows covered by group
759 private boolean hidereps=false;
761 * set visibility of sequences covered by (if no sequence representative is defined)
762 * or represented by this group.
765 public void setHidereps(boolean visibility)
767 hidereps = visibility;
771 * @return true if sequences represented (or covered) by this group should be hidden
773 public boolean isHidereps()
778 * visibility of columns intersecting this group
780 private boolean hidecols=false;
782 * set intended visibility of columns covered by this group
785 public void setHideCols(boolean visibility)
787 hidecols = visibility;
791 * @return true if columns covered by group should be hidden
793 public boolean isHideCols()
798 * create a new sequence group from the intersection of this group
799 * with an alignment Hashtable of hidden representatives
801 * @param alignment (may not be null)
802 * @param hashtable (may be null)
803 * @return new group containing sequences common to this group and alignment
805 public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
807 SequenceGroup sgroup = new SequenceGroup(this);
808 Enumeration en = getSequences(hashtable).elements();
809 while (en.hasMoreElements())
811 SequenceI elem = (SequenceI) en.nextElement();
812 if (alignment.getSequences().contains(elem))
814 sgroup.addSequence(elem, false);