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(seqsel.sequences);
122 groupName = new String(seqsel.groupName);
123 displayBoxes = seqsel.displayBoxes;
124 displayText = seqsel.displayText;
125 colourText = seqsel.colourText;
126 startRes = seqsel.startRes;
127 endRes = seqsel.endRes;
129 if (seqsel.description!=null)
130 description = new String(seqsel.description);
131 groupName = new String(seqsel.groupName);
132 hidecols = seqsel.hidecols;
133 hidereps = seqsel.hidereps;
134 idColour = seqsel.idColour;
135 outlineColour = seqsel.outlineColour;
136 seqrep = seqsel.seqrep;
137 textColour = seqsel.textColour;
138 textColour2 = seqsel.textColour2;
139 thresholdTextColour = seqsel.thresholdTextColour;
140 width = seqsel.width;
141 if (seqsel.conserve!=null)
143 recalcConservation(); // safer than
144 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
149 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
151 int iSize = sequences.size();
152 SequenceI[] seqs = new SequenceI[iSize];
153 SequenceI[] inorder = getSequencesInOrder(align);
155 for (int i = 0, ipos = 0; i < inorder.length; i++)
157 SequenceI seq = inorder[i];
159 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
160 if (seqs[ipos] != null)
162 seqs[ipos].setDescription(seq.getDescription());
163 seqs[ipos].setDBRef(seq.getDBRef());
164 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
165 if (seq.getDatasetSequence() != null)
167 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
170 if (seq.getAnnotation() != null)
172 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
173 // Only copy annotation that is either a score or referenced by the
174 // alignment's annotation vector
175 for (int a = 0; a < seq.getAnnotation().length; a++)
177 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
180 boolean found = false;
181 for (int pos = 0; pos < alann.length; pos++)
183 if (alann[pos] == tocopy)
192 AlignmentAnnotation newannot = new AlignmentAnnotation(seq
193 .getAnnotation()[a]);
194 newannot.restrict(startRes, endRes);
195 newannot.setSequenceRef(seqs[ipos]);
196 newannot.adjustForAlignment();
197 seqs[ipos].addAlignmentAnnotation(newannot);
207 if (iSize != inorder.length)
209 SequenceI[] nseqs = new SequenceI[iSize];
210 System.arraycopy(seqs, 0, nseqs, 0, iSize);
218 * If sequence ends in gaps, the end residue can be correctly calculated here
224 public int findEndRes(SequenceI seq)
229 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
231 ch = seq.getCharAt(j);
232 if (!jalview.util.Comparison.isGap((ch)))
240 eres += seq.getStart() - 1;
246 public Vector getSequences(Hashtable hiddenReps)
248 if (hiddenReps == null)
254 Vector allSequences = new Vector();
256 for (int i = 0; i < sequences.size(); i++)
258 seq = (SequenceI) sequences.elementAt(i);
259 allSequences.addElement(seq);
260 if (hiddenReps.containsKey(seq))
262 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
263 for (int h = 0; h < hsg.getSize(); h++)
265 seq2 = hsg.getSequenceAt(h);
266 if (seq2 != seq && !allSequences.contains(seq2))
268 allSequences.addElement(seq2);
278 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
280 Vector tmp = getSequences(hiddenReps);
285 SequenceI[] result = new SequenceI[tmp.size()];
286 for (int i = 0; i < result.length; i++)
288 result[i] = (SequenceI) tmp.elementAt(i);
300 * @return DOCUMENT ME!
302 public boolean adjustForRemoveLeft(int col)
304 // return value is true if the group still exists
307 startRes = startRes - col;
312 endRes = endRes - col;
314 if (startRes > endRes)
321 // must delete this group!!
334 * @return DOCUMENT ME!
336 public boolean adjustForRemoveRight(int col)
355 * @return DOCUMENT ME!
357 public String getName()
362 public String getDescription()
373 public void setName(String name)
378 public void setDescription(String desc)
386 * @return DOCUMENT ME!
388 public Conservation getConservation()
399 public void setConservation(Conservation c)
405 * Add s to this sequence group
408 * alignment sequence to be added
410 * true means Group's conservation should be recalculated
412 public void addSequence(SequenceI s, boolean recalc)
414 if (s != null && !sequences.contains(s))
416 sequences.addElement(s);
421 recalcConservation();
426 * calculate residue conservation for group
428 public void recalcConservation()
437 cs.setConsensus(AAFrequency
438 .calculate(sequences, startRes, endRes + 1));
440 if (cs instanceof ClustalxColourScheme)
442 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
445 if (cs.conservationApplied())
447 Conservation c = new Conservation(groupName,
448 ResidueProperties.propHash, 3, sequences, startRes,
451 c.verdict(false, 25);
453 cs.setConservation(c);
455 if (cs instanceof ClustalxColourScheme)
457 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
460 } catch (java.lang.OutOfMemoryError err)
463 System.out.println("Out of memory loading groups: " + err);
476 public void addOrRemove(SequenceI s, boolean recalc)
478 if (sequences.contains(s))
480 deleteSequence(s, recalc);
484 addSequence(s, recalc);
496 public void deleteSequence(SequenceI s, boolean recalc)
498 sequences.removeElement(s);
502 recalcConservation();
509 * @return DOCUMENT ME!
511 public int getStartRes()
519 * @return DOCUMENT ME!
521 public int getEndRes()
532 public void setStartRes(int i)
543 public void setEndRes(int i)
551 * @return DOCUMENT ME!
555 return sequences.size();
564 * @return DOCUMENT ME!
566 public SequenceI getSequenceAt(int i)
568 return (SequenceI) sequences.elementAt(i);
577 public void setColourText(boolean state)
585 * @return DOCUMENT ME!
587 public boolean getColourText()
598 public void setDisplayText(boolean state)
606 * @return DOCUMENT ME!
608 public boolean getDisplayText()
619 public void setDisplayBoxes(boolean state)
621 displayBoxes = state;
627 * @return DOCUMENT ME!
629 public boolean getDisplayBoxes()
637 * @return DOCUMENT ME!
639 public int getWidth()
641 // MC This needs to get reset when characters are inserted and deleted
642 if (sequences.size() > 0)
644 width = ((SequenceI) sequences.elementAt(0)).getLength();
647 for (int i = 1; i < sequences.size(); i++)
649 SequenceI seq = (SequenceI) sequences.elementAt(i);
651 if (seq.getLength() > width)
653 width = seq.getLength();
666 public void setOutlineColour(Color c)
674 * @return DOCUMENT ME!
676 public Color getOutlineColour()
678 return outlineColour;
683 * returns the sequences in the group ordered by the ordering given by al
687 * @return SequenceI[]
689 public SequenceI[] getSequencesInOrder(AlignmentI al)
691 int sSize = sequences.size();
692 int alHeight = al.getHeight();
694 SequenceI[] seqs = new SequenceI[sSize];
697 for (int i = 0; i < alHeight && index < sSize; i++)
699 if (sequences.contains(al.getSequenceAt(i)))
701 seqs[index++] = al.getSequenceAt(i);
709 * @return the idColour
711 public Color getIdColour()
718 * the idColour to set
720 public void setIdColour(Color idColour)
722 this.idColour = idColour;
726 * @return the representative sequence for this group
728 public SequenceI getSeqrep()
734 * set the representative sequence for this group.
735 * Note - this affects the interpretation of the Hidereps attribute.
736 * @param seqrep the seqrep to set (null means no sequence representative)
738 public void setSeqrep(SequenceI seqrep)
740 this.seqrep = seqrep;
744 * @return true if group has a sequence representative
746 public boolean hasSeqrep()
748 return seqrep != null;
751 * visibility of rows or represented rows covered by group
753 private boolean hidereps=false;
755 * set visibility of sequences covered by (if no sequence representative is defined)
756 * or represented by this group.
759 public void setHidereps(boolean visibility)
761 hidereps = visibility;
765 * @return true if sequences represented (or covered) by this group should be hidden
767 public boolean isHidereps()
772 * visibility of columns intersecting this group
774 private boolean hidecols=false;
776 * set intended visibility of columns covered by this group
779 public void setHideCols(boolean visibility)
781 hidecols = visibility;
785 * @return true if columns covered by group should be hidden
787 public boolean isHideCols()
792 * create a new sequence group from the intersection of this group
793 * with an alignment Hashtable of hidden representatives
795 * @param alignment (may not be null)
796 * @param hashtable (may be null)
797 * @return new group containing sequences common to this group and alignment
799 public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
801 SequenceGroup sgroup = new SequenceGroup(this);
802 Enumeration en = getSequences(hashtable).elements();
803 while (en.hasMoreElements())
805 SequenceI elem = (SequenceI) en.nextElement();
806 if (alignment.getSequences().contains(elem))
808 sgroup.addSequence(elem, false);