2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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;
50 * after Olivier's non-conserved only character display
52 boolean showUnconserved = false;
57 private Vector sequences = new Vector();
59 * representative sequence for this group (if any)
61 private SequenceI seqrep = null;
65 * Colourscheme applied to group if any */
66 public ColourSchemeI cs;
72 public Color outlineColour = Color.black;
74 public Color idColour = null;
76 public int thresholdTextColour = 0;
78 public Color textColour = Color.black;
80 public Color textColour2 = Color.white;
83 * consensus calculation property
85 private boolean ignoreGapsInConsensus=true;
87 * consensus calculation property
89 private boolean includeAllConsSymbols=false;
92 * @return the includeAllConsSymbols
94 public boolean isIncludeAllConsSymbols()
96 return includeAllConsSymbols;
101 * Creates a new SequenceGroup object.
103 public SequenceGroup()
105 groupName = "JGroup:" + this.hashCode();
109 * Creates a new SequenceGroup object.
114 * @param displayBoxes
118 * first column of group
120 * last column of group
122 public SequenceGroup(Vector sequences, String groupName,
123 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
124 boolean colourText, int start, int end)
126 this.sequences = sequences;
127 this.groupName = groupName;
128 this.displayBoxes = displayBoxes;
129 this.displayText = displayText;
130 this.colourText = colourText;
134 recalcConservation();
140 public SequenceGroup(SequenceGroup seqsel)
144 sequences=new Vector();
145 Enumeration sq = seqsel.sequences.elements();
146 while (sq.hasMoreElements()) {
147 sequences.addElement(sq.nextElement());
149 if (seqsel.groupName!=null)
151 groupName = new String(seqsel.groupName);
153 displayBoxes = seqsel.displayBoxes;
154 displayText = seqsel.displayText;
155 colourText = seqsel.colourText;
156 startRes = seqsel.startRes;
157 endRes = seqsel.endRes;
159 if (seqsel.description!=null)
160 description = new String(seqsel.description);
161 hidecols = seqsel.hidecols;
162 hidereps = seqsel.hidereps;
163 idColour = seqsel.idColour;
164 outlineColour = seqsel.outlineColour;
165 seqrep = seqsel.seqrep;
166 textColour = seqsel.textColour;
167 textColour2 = seqsel.textColour2;
168 thresholdTextColour = seqsel.thresholdTextColour;
169 width = seqsel.width;
170 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
171 if (seqsel.conserve!=null)
173 recalcConservation(); // safer than
174 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
179 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
181 int iSize = sequences.size();
182 SequenceI[] seqs = new SequenceI[iSize];
183 SequenceI[] inorder = getSequencesInOrder(align);
185 for (int i = 0, ipos = 0; i < inorder.length; i++)
187 SequenceI seq = inorder[i];
189 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
190 if (seqs[ipos] != null)
192 seqs[ipos].setDescription(seq.getDescription());
193 seqs[ipos].setDBRef(seq.getDBRef());
194 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
195 if (seq.getDatasetSequence() != null)
197 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
200 if (seq.getAnnotation() != null)
202 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
203 // Only copy annotation that is either a score or referenced by the
204 // alignment's annotation vector
205 for (int a = 0; a < seq.getAnnotation().length; a++)
207 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
210 boolean found = false;
211 for (int pos = 0; pos < alann.length; pos++)
213 if (alann[pos] == tocopy)
222 AlignmentAnnotation newannot = new AlignmentAnnotation(seq
223 .getAnnotation()[a]);
224 newannot.restrict(startRes, endRes);
225 newannot.setSequenceRef(seqs[ipos]);
226 newannot.adjustForAlignment();
227 seqs[ipos].addAlignmentAnnotation(newannot);
237 if (iSize != inorder.length)
239 SequenceI[] nseqs = new SequenceI[iSize];
240 System.arraycopy(seqs, 0, nseqs, 0, iSize);
248 * If sequence ends in gaps, the end residue can be correctly calculated here
254 public int findEndRes(SequenceI seq)
259 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
261 ch = seq.getCharAt(j);
262 if (!jalview.util.Comparison.isGap((ch)))
270 eres += seq.getStart() - 1;
276 public Vector getSequences(Hashtable hiddenReps)
278 if (hiddenReps == null)
284 Vector allSequences = new Vector();
286 for (int i = 0; i < sequences.size(); i++)
288 seq = (SequenceI) sequences.elementAt(i);
289 allSequences.addElement(seq);
290 if (hiddenReps.containsKey(seq))
292 SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq);
293 for (int h = 0; h < hsg.getSize(); h++)
295 seq2 = hsg.getSequenceAt(h);
296 if (seq2 != seq && !allSequences.contains(seq2))
298 allSequences.addElement(seq2);
308 public SequenceI[] getSequencesAsArray(Hashtable hiddenReps)
310 Vector tmp = getSequences(hiddenReps);
315 SequenceI[] result = new SequenceI[tmp.size()];
316 for (int i = 0; i < result.length; i++)
318 result[i] = (SequenceI) tmp.elementAt(i);
330 * @return DOCUMENT ME!
332 public boolean adjustForRemoveLeft(int col)
334 // return value is true if the group still exists
337 startRes = startRes - col;
342 endRes = endRes - col;
344 if (startRes > endRes)
351 // must delete this group!!
364 * @return DOCUMENT ME!
366 public boolean adjustForRemoveRight(int col)
385 * @return DOCUMENT ME!
387 public String getName()
392 public String getDescription()
403 public void setName(String name)
406 // TODO: URGENT: update dependent objects (annotation row)
409 public void setDescription(String desc)
417 * @return DOCUMENT ME!
419 public Conservation getConservation()
430 public void setConservation(Conservation c)
436 * Add s to this sequence group
439 * alignment sequence to be added
441 * true means Group's conservation should be recalculated
443 public void addSequence(SequenceI s, boolean recalc)
445 if (s != null && !sequences.contains(s))
447 sequences.addElement(s);
452 recalcConservation();
457 * calculate residue conservation for group - but only if necessary.
459 public void recalcConservation()
461 if (cs == null && consensus == null && conservation == null)
468 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes, endRes + 1, includeAllConsSymbols);
469 if (consensus != null)
471 _updateConsensusRow(cnsns);
475 cs.setConsensus(cnsns);
477 if (cs instanceof ClustalxColourScheme)
479 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
483 if ((conservation!=null) || (cs!=null && cs.conservationApplied()))
485 Conservation c = new Conservation(groupName,
486 ResidueProperties.propHash, 3, sequences, startRes,
489 c.verdict(false, 25);
490 if (conservation!=null)
492 _updateConservationRow(c);
496 cs.setConservation(c);
498 if (cs instanceof ClustalxColourScheme)
500 ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth());
504 } catch (java.lang.OutOfMemoryError err)
507 System.out.println("Out of memory loading groups: " + err);
512 private void _updateConservationRow(Conservation c)
514 if (conservation==null)
518 // preserve width if already set
519 int aWidth = (conservation.annotations!=null) ? (endRes<conservation.annotations.length ? conservation.annotations.length : endRes+1) : endRes+1;
520 conservation.annotations = null;
521 conservation.annotations = new Annotation[aWidth]; // should be alignment width
522 c.completeAnnotations(conservation,null, startRes, endRes+1);
524 public Hashtable[] consensusData = null;
525 private void _updateConsensusRow(Hashtable[] cnsns)
531 consensusData = cnsns;
532 // preserve width if already set
533 int aWidth = (consensus.annotations!=null) ? (endRes<consensus.annotations.length ? consensus.annotations.length : endRes+1) : endRes+1;
534 consensus.annotations = null;
535 consensus.annotations = new Annotation[aWidth]; // should be alignment width
537 AAFrequency.completeConsensus(consensus,cnsns,startRes,endRes+1,ignoreGapsInConsensus, includeAllConsSymbols); // TODO: setting container for ignoreGapsInConsensusCalculation);
541 * @param s sequence to either add or remove from group
542 * @param recalc flag passed to delete/addSequence to indicate if group properties should be recalculated
544 public void addOrRemove(SequenceI s, boolean recalc)
546 if (sequences.contains(s))
548 deleteSequence(s, recalc);
552 addSequence(s, recalc);
564 public void deleteSequence(SequenceI s, boolean recalc)
566 sequences.removeElement(s);
570 recalcConservation();
577 * @return DOCUMENT ME!
579 public int getStartRes()
587 * @return DOCUMENT ME!
589 public int getEndRes()
600 public void setStartRes(int i)
611 public void setEndRes(int i)
619 * @return DOCUMENT ME!
623 return sequences.size();
632 * @return DOCUMENT ME!
634 public SequenceI getSequenceAt(int i)
636 return (SequenceI) sequences.elementAt(i);
645 public void setColourText(boolean state)
653 * @return DOCUMENT ME!
655 public boolean getColourText()
666 public void setDisplayText(boolean state)
674 * @return DOCUMENT ME!
676 public boolean getDisplayText()
687 public void setDisplayBoxes(boolean state)
689 displayBoxes = state;
695 * @return DOCUMENT ME!
697 public boolean getDisplayBoxes()
705 * @return DOCUMENT ME!
707 public int getWidth()
709 // MC This needs to get reset when characters are inserted and deleted
710 if (sequences.size() > 0)
712 width = ((SequenceI) sequences.elementAt(0)).getLength();
715 for (int i = 1; i < sequences.size(); i++)
717 SequenceI seq = (SequenceI) sequences.elementAt(i);
719 if (seq.getLength() > width)
721 width = seq.getLength();
734 public void setOutlineColour(Color c)
742 * @return DOCUMENT ME!
744 public Color getOutlineColour()
746 return outlineColour;
751 * returns the sequences in the group ordered by the ordering given by al.
752 * this used to return an array with null entries regardless, new behaviour is below.
753 * TODO: verify that this does not affect use in applet or application
756 * @return SequenceI[] intersection of sequences in group with al, ordered by al, or null if group does not intersect with al
758 public SequenceI[] getSequencesInOrder(AlignmentI al)
760 int sSize = sequences.size();
761 int alHeight = al.getHeight();
763 SequenceI[] seqs = new SequenceI[sSize];
766 for (int i = 0; i < alHeight && index < sSize; i++)
768 if (sequences.contains(al.getSequenceAt(i)))
770 seqs[index++] = al.getSequenceAt(i);
777 if (index<seqs.length)
779 SequenceI[] dummy = seqs;
780 seqs = new SequenceI[index];
783 seqs[index] = dummy[index];
791 * @return the idColour
793 public Color getIdColour()
800 * the idColour to set
802 public void setIdColour(Color idColour)
804 this.idColour = idColour;
808 * @return the representative sequence for this group
810 public SequenceI getSeqrep()
816 * set the representative sequence for this group.
817 * Note - this affects the interpretation of the Hidereps attribute.
818 * @param seqrep the seqrep to set (null means no sequence representative)
820 public void setSeqrep(SequenceI seqrep)
822 this.seqrep = seqrep;
826 * @return true if group has a sequence representative
828 public boolean hasSeqrep()
830 return seqrep != null;
833 * visibility of rows or represented rows covered by group
835 private boolean hidereps=false;
837 * set visibility of sequences covered by (if no sequence representative is defined)
838 * or represented by this group.
841 public void setHidereps(boolean visibility)
843 hidereps = visibility;
847 * @return true if sequences represented (or covered) by this group should be hidden
849 public boolean isHidereps()
854 * visibility of columns intersecting this group
856 private boolean hidecols=false;
858 * set intended visibility of columns covered by this group
861 public void setHideCols(boolean visibility)
863 hidecols = visibility;
867 * @return true if columns covered by group should be hidden
869 public boolean isHideCols()
874 * create a new sequence group from the intersection of this group
875 * with an alignment Hashtable of hidden representatives
877 * @param alignment (may not be null)
878 * @param hashtable (may be null)
879 * @return new group containing sequences common to this group and alignment
881 public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable)
883 SequenceGroup sgroup = new SequenceGroup(this);
884 SequenceI[] insect=getSequencesInOrder(alignment);
885 sgroup.sequences = new Vector();
886 for (int s=0;insect!=null && s<insect.length;s++) {
887 if (hashtable==null || hashtable.containsKey(insect[s]))
889 sgroup.sequences.addElement(insect[s]); }
891 //Enumeration en =getSequences(hashtable).elements();
892 //while (en.hasMoreElements())
894 // SequenceI elem = (SequenceI) en.nextElement();
895 // if (alignment.getSequences().contains(elem))
897 // sgroup.addSequence(elem, false);
904 * @return the showUnconserved
906 public boolean getShowunconserved()
908 return showUnconserved;
912 * @param showUnconserved the showUnconserved to set
914 public void setShowunconserved(boolean displayNonconserved)
916 this.showUnconserved = displayNonconserved;
918 AlignmentAnnotation consensus=null,conservation=null;
921 * @return automatically calculated consensus row
923 public AlignmentAnnotation getConsensus()
925 // TODO get or calculate and get consensus annotation row for this group
926 int aWidth = this.getWidth();
936 consensus = new AlignmentAnnotation("Consensus for "+getName(), "PID",
937 new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
938 consensus.hasText = true;
939 consensus.autoCalculated = true;
940 consensus.groupRef = this;
945 * get the conservation annotation row for this group
946 * @return autoCalculated annotation row
948 public AlignmentAnnotation getConservationRow() {
949 int ConsPercGaps = 25;
950 if (conservation == null) {
951 conservation = new AlignmentAnnotation("Conservation for "+getName(),
952 "Conservation for group "+getName()+" less than " + ConsPercGaps
953 + "% gaps", new Annotation[1], 0f, 11f,
954 AlignmentAnnotation.BAR_GRAPH);
955 conservation.hasText = true;
956 conservation.autoCalculated = true;
957 conservation.groupRef = this;
965 * @return true if annotation rows have been instantiated for this group
967 public boolean hasAnnotationRows()
969 return consensus!=null || conservation!=null;
972 public SequenceI getConsensusSeq()
975 StringBuffer seqs = new StringBuffer();
976 for (int i = 0; i < consensus.annotations.length; i++)
978 if (consensus.annotations[i] != null)
980 if (consensus.annotations[i].description.charAt(0) == '[')
982 seqs.append(consensus.annotations[i].description.charAt(1));
986 seqs.append(consensus.annotations[i].displayCharacter);
991 SequenceI sq = new Sequence("Group"+getName()+" Consensus", seqs.toString());
992 sq.setDescription("Percentage Identity Consensus "
993 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
997 public void setIgnoreGapsConsensus(boolean state)
999 if (this.ignoreGapsInConsensus!=state && consensus!=null)
1001 ignoreGapsInConsensus = state;
1002 recalcConservation();
1004 ignoreGapsInConsensus = state;
1006 public boolean getIgnoreGapsConsensus()
1008 return ignoreGapsInConsensus;
1011 * @param includeAllConsSymbols the includeAllConsSymbols to set
1013 public void setIncludeAllConsSymbols(boolean includeAllConsSymbols)
1015 if (this.includeAllConsSymbols!=includeAllConsSymbols && consensus!=null) {
1016 this.includeAllConsSymbols = includeAllConsSymbols;
1017 recalcConservation();
1019 this.includeAllConsSymbols = includeAllConsSymbols;