2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.Conservation;
25 import jalview.schemes.ColourSchemeI;
27 import java.awt.Color;
28 import java.util.ArrayList;
29 import java.util.Hashtable;
30 import java.util.List;
32 import java.util.Vector;
35 * Collects a set contiguous ranges on a set of sequences
40 public class SequenceGroup implements AnnotatedCollectionI
46 Conservation conserve;
50 boolean displayBoxes = true;
52 boolean displayText = true;
54 boolean colourText = false;
57 * after Olivier's non-conserved only character display
59 boolean showNonconserved = false;
64 private List<SequenceI> sequences = new ArrayList<SequenceI>();
67 * representative sequence for this group (if any)
69 private SequenceI seqrep = null;
74 * Colourscheme applied to group if any
76 public ColourSchemeI cs;
78 // start column (base 0)
81 // end column (base 0)
84 public Color outlineColour = Color.black;
86 public Color idColour = null;
88 public int thresholdTextColour = 0;
90 public Color textColour = Color.black;
92 public Color textColour2 = Color.white;
95 * consensus calculation property
97 private boolean ignoreGapsInConsensus = true;
100 * consensus calculation property
102 private boolean showSequenceLogo = false;
105 * flag indicating if logo should be rendered normalised
107 private boolean normaliseSequenceLogo;
110 * @return the includeAllConsSymbols
112 public boolean isShowSequenceLogo()
114 return showSequenceLogo;
118 * Creates a new SequenceGroup object.
120 public SequenceGroup()
122 groupName = "JGroup:" + this.hashCode();
126 * Creates a new SequenceGroup object.
131 * @param displayBoxes
135 * first column of group
137 * last column of group
139 public SequenceGroup(List<SequenceI> sequences, String groupName,
140 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
141 boolean colourText, int start, int end)
143 this.sequences = sequences;
144 this.groupName = groupName;
145 this.displayBoxes = displayBoxes;
146 this.displayText = displayText;
147 this.colourText = colourText;
151 recalcConservation();
159 public SequenceGroup(SequenceGroup seqsel)
163 sequences = new ArrayList<SequenceI>();
164 sequences.addAll(seqsel.sequences);
165 if (seqsel.groupName != null)
167 groupName = new String(seqsel.groupName);
169 displayBoxes = seqsel.displayBoxes;
170 displayText = seqsel.displayText;
171 colourText = seqsel.colourText;
172 startRes = seqsel.startRes;
173 endRes = seqsel.endRes;
175 if (seqsel.description != null)
177 description = new String(seqsel.description);
179 hidecols = seqsel.hidecols;
180 hidereps = seqsel.hidereps;
181 idColour = seqsel.idColour;
182 outlineColour = seqsel.outlineColour;
183 seqrep = seqsel.seqrep;
184 textColour = seqsel.textColour;
185 textColour2 = seqsel.textColour2;
186 thresholdTextColour = seqsel.thresholdTextColour;
187 width = seqsel.width;
188 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
189 if (seqsel.conserve != null)
191 recalcConservation(); // safer than
192 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
197 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
199 int iSize = sequences.size();
200 SequenceI[] seqs = new SequenceI[iSize];
201 SequenceI[] inorder = getSequencesInOrder(align);
203 for (int i = 0, ipos = 0; i < inorder.length; i++)
205 SequenceI seq = inorder[i];
207 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
208 if (seqs[ipos] != null)
210 seqs[ipos].setDescription(seq.getDescription());
211 seqs[ipos].setDBRefs(seq.getDBRefs());
212 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
213 if (seq.getDatasetSequence() != null)
215 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
218 if (seq.getAnnotation() != null)
220 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
221 // Only copy annotation that is either a score or referenced by the
222 // alignment's annotation vector
223 for (int a = 0; a < seq.getAnnotation().length; a++)
225 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
228 boolean found = false;
229 for (int pos = 0; pos < alann.length; pos++)
231 if (alann[pos] == tocopy)
242 AlignmentAnnotation newannot = new AlignmentAnnotation(
243 seq.getAnnotation()[a]);
244 newannot.restrict(startRes, endRes);
245 newannot.setSequenceRef(seqs[ipos]);
246 newannot.adjustForAlignment();
247 seqs[ipos].addAlignmentAnnotation(newannot);
257 if (iSize != inorder.length)
259 SequenceI[] nseqs = new SequenceI[iSize];
260 System.arraycopy(seqs, 0, nseqs, 0, iSize);
268 * If sequence ends in gaps, the end residue can be correctly calculated here
274 public int findEndRes(SequenceI seq)
279 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
281 ch = seq.getCharAt(j);
282 if (!jalview.util.Comparison.isGap((ch)))
290 eres += seq.getStart() - 1;
297 public List<SequenceI> getSequences()
303 public List<SequenceI> getSequences(
304 Map<SequenceI, SequenceCollectionI> hiddenReps)
306 if (hiddenReps == null)
308 // TODO: need a synchronizedCollection here ?
313 List<SequenceI> allSequences = new ArrayList<SequenceI>();
314 for (SequenceI seq : sequences)
316 allSequences.add(seq);
317 if (hiddenReps.containsKey(seq))
319 SequenceCollectionI hsg = hiddenReps.get(seq);
320 for (SequenceI seq2 : hsg.getSequences())
322 if (seq2 != seq && !allSequences.contains(seq2))
324 allSequences.add(seq2);
334 public SequenceI[] getSequencesAsArray(
335 Map<SequenceI, SequenceCollectionI> map)
337 List<SequenceI> tmp = getSequences(map);
342 return tmp.toArray(new SequenceI[tmp.size()]);
351 * @return DOCUMENT ME!
353 public boolean adjustForRemoveLeft(int col)
355 // return value is true if the group still exists
358 startRes = startRes - col;
363 endRes = endRes - col;
365 if (startRes > endRes)
372 // must delete this group!!
385 * @return DOCUMENT ME!
387 public boolean adjustForRemoveRight(int col)
406 * @return DOCUMENT ME!
408 public String getName()
413 public String getDescription()
424 public void setName(String name)
427 // TODO: URGENT: update dependent objects (annotation row)
430 public void setDescription(String desc)
438 * @return DOCUMENT ME!
440 public Conservation getConservation()
451 public void setConservation(Conservation c)
457 * Add s to this sequence group. If aligment sequence is already contained in
458 * group, it will not be added again, but recalculation may happen if the flag
462 * alignment sequence to be added
464 * true means Group's conservation should be recalculated
466 public void addSequence(SequenceI s, boolean recalc)
468 synchronized (sequences)
470 if (s != null && !sequences.contains(s))
477 recalcConservation();
483 * Max Gaps Threshold (percent) for performing a conservation calculation
485 private int consPercGaps = 25;
488 * @return Max Gaps Threshold for performing a conservation calculation
490 public int getConsPercGaps()
496 * set Max Gaps Threshold (percent) for performing a conservation calculation
498 * @param consPercGaps
500 public void setConsPercGaps(int consPercGaps)
502 this.consPercGaps = consPercGaps;
506 * calculate residue conservation and colourschemes for group - but only if
507 * necessary. returns true if the calculation resulted in a visible change to
510 public boolean recalcConservation()
512 return recalcConservation(false);
516 * calculate residue conservation for group - but only if necessary. returns
517 * true if the calculation resulted in a visible change to group
520 * when set, colourschemes for this group are not refreshed after
523 public boolean recalcConservation(boolean defer)
525 if (cs == null && consensus == null && conservation == null)
529 // TODO: try harder to detect changes in state in order to minimise
530 // recalculation effort
534 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
535 endRes + 1, showSequenceLogo);
536 if (consensus != null)
538 _updateConsensusRow(cnsns, sequences.size());
543 cs.setConsensus(cnsns);
547 if ((conservation != null)
548 || (cs != null && cs.conservationApplied()))
550 Conservation c = new Conservation(groupName, 3, sequences,
551 startRes, endRes + 1);
553 c.verdict(false, consPercGaps);
554 if (conservation != null)
556 _updateConservationRow(c);
560 if (cs.conservationApplied())
562 cs.setConservation(c);
565 // eager update - will cause a refresh of overview regardless
568 if (cs != null && !defer)
570 // TODO: JAL-2034 should cs.alignmentChanged modify return state
571 cs.alignmentChanged(context != null ? context : this, null);
578 } catch (java.lang.OutOfMemoryError err)
581 System.out.println("Out of memory loading groups: " + err);
586 private void _updateConservationRow(Conservation c)
588 if (conservation == null)
593 conservation.label = "Conservation for " + getName();
594 conservation.description = "Conservation for group " + getName()
595 + " less than " + consPercGaps + "% gaps";
596 // preserve width if already set
597 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
600 conservation.annotations = null;
601 conservation.annotations = new Annotation[aWidth]; // should be alignment
603 c.completeAnnotations(conservation, null, startRes, endRes + 1);
606 public Hashtable[] consensusData = null;
608 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
610 if (consensus == null)
614 consensus.label = "Consensus for " + getName();
615 consensus.description = "Percent Identity";
616 consensusData = cnsns;
617 // preserve width if already set
618 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
621 consensus.annotations = null;
622 consensus.annotations = new Annotation[aWidth]; // should be alignment width
624 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
625 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
628 // ignoreGapsInConsensusCalculation);
633 * sequence to either add or remove from group
635 * flag passed to delete/addSequence to indicate if group properties
636 * should be recalculated
638 public void addOrRemove(SequenceI s, boolean recalc)
640 synchronized (sequences)
642 if (sequences.contains(s))
644 deleteSequence(s, recalc);
648 addSequence(s, recalc);
659 * true means recalculate conservation
661 public void deleteSequence(SequenceI s, boolean recalc)
663 synchronized (sequences)
669 recalcConservation();
677 * @return the first column selected by this group. Runs from 0<=i<N_cols
680 public int getStartRes()
687 * @return the groups last selected column. Runs from 0<=i<N_cols
690 public int getEndRes()
696 * Set the first column selected by this group. Runs from 0<=i<N_cols
700 public void setStartRes(int i)
706 * Set the groups last selected column. Runs from 0<=i<N_cols
710 public void setEndRes(int i)
716 * @return number of sequences in group
720 return sequences.size();
725 * @return the ith sequence
727 public SequenceI getSequenceAt(int i)
729 return sequences.get(i);
736 public void setColourText(boolean state)
744 * @return DOCUMENT ME!
746 public boolean getColourText()
757 public void setDisplayText(boolean state)
765 * @return DOCUMENT ME!
767 public boolean getDisplayText()
778 public void setDisplayBoxes(boolean state)
780 displayBoxes = state;
786 * @return DOCUMENT ME!
788 public boolean getDisplayBoxes()
794 * computes the width of current set of sequences and returns it
796 * @return DOCUMENT ME!
799 public int getWidth()
801 synchronized (sequences)
803 // MC This needs to get reset when characters are inserted and deleted
804 boolean first = true;
805 for (SequenceI seq : sequences)
807 if (first || seq.getLength() > width)
809 width = seq.getLength();
823 public void setOutlineColour(Color c)
831 * @return DOCUMENT ME!
833 public Color getOutlineColour()
835 return outlineColour;
840 * returns the sequences in the group ordered by the ordering given by al.
841 * this used to return an array with null entries regardless, new behaviour is
842 * below. TODO: verify that this does not affect use in applet or application
846 * @return SequenceI[] intersection of sequences in group with al, ordered by
847 * al, or null if group does not intersect with al
849 public SequenceI[] getSequencesInOrder(AlignmentI al)
851 return getSequencesInOrder(al, true);
855 * return an array representing the intersection of the group with al,
856 * optionally returning an array the size of al.getHeight() where nulls mark
857 * the non-intersected sequences
861 * @return null or array
863 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
865 synchronized (sequences)
867 int sSize = sequences.size();
868 int alHeight = al.getHeight();
870 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
873 for (int i = 0; i < alHeight && index < sSize; i++)
875 if (sequences.contains(al.getSequenceAt(i)))
877 seqs[(trim) ? index : i] = al.getSequenceAt(i);
889 if (index < seqs.length)
891 SequenceI[] dummy = seqs;
892 seqs = new SequenceI[index];
895 seqs[index] = dummy[index];
904 * @return the idColour
906 public Color getIdColour()
913 * the idColour to set
915 public void setIdColour(Color idColour)
917 this.idColour = idColour;
921 * @return the representative sequence for this group
924 public SequenceI getSeqrep()
930 * set the representative sequence for this group. Note - this affects the
931 * interpretation of the Hidereps attribute.
934 * the seqrep to set (null means no sequence representative)
937 public void setSeqrep(SequenceI seqrep)
939 this.seqrep = seqrep;
944 * @return true if group has a sequence representative
947 public boolean hasSeqrep()
949 return seqrep != null;
953 * visibility of rows or represented rows covered by group
955 private boolean hidereps = false;
958 * set visibility of sequences covered by (if no sequence representative is
959 * defined) or represented by this group.
963 public void setHidereps(boolean visibility)
965 hidereps = visibility;
970 * @return true if sequences represented (or covered) by this group should be
973 public boolean isHidereps()
979 * visibility of columns intersecting this group
981 private boolean hidecols = false;
984 * set intended visibility of columns covered by this group
988 public void setHideCols(boolean visibility)
990 hidecols = visibility;
995 * @return true if columns covered by group should be hidden
997 public boolean isHideCols()
1003 * create a new sequence group from the intersection of this group with an
1004 * alignment Hashtable of hidden representatives
1010 * @return new group containing sequences common to this group and alignment
1012 public SequenceGroup intersect(AlignmentI alignment,
1013 Map<SequenceI, SequenceCollectionI> map)
1015 SequenceGroup sgroup = new SequenceGroup(this);
1016 SequenceI[] insect = getSequencesInOrder(alignment);
1017 sgroup.sequences = new ArrayList<SequenceI>();
1018 for (int s = 0; insect != null && s < insect.length; s++)
1020 if (map == null || map.containsKey(insect[s]))
1022 sgroup.sequences.add(insect[s]);
1029 * @return the showUnconserved
1031 public boolean getShowNonconserved()
1033 return showNonconserved;
1037 * @param showNonconserved
1038 * the showUnconserved to set
1040 public void setShowNonconserved(boolean displayNonconserved)
1042 this.showNonconserved = displayNonconserved;
1045 AlignmentAnnotation consensus = null, conservation = null;
1048 * flag indicating if consensus histogram should be rendered
1050 private boolean showConsensusHistogram;
1053 * set this alignmentAnnotation object as the one used to render consensus
1058 public void setConsensus(AlignmentAnnotation aan)
1060 if (consensus == null)
1068 * @return automatically calculated consensus row note: the row is a stub if a
1069 * consensus calculation has not yet been performed on the group
1071 public AlignmentAnnotation getConsensus()
1073 // TODO get or calculate and get consensus annotation row for this group
1074 int aWidth = this.getWidth();
1082 if (consensus == null)
1084 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1085 100f, AlignmentAnnotation.BAR_GRAPH);
1086 consensus.hasText = true;
1087 consensus.autoCalculated = true;
1088 consensus.groupRef = this;
1089 consensus.label = "Consensus for " + getName();
1090 consensus.description = "Percent Identity";
1096 * set this alignmentAnnotation object as the one used to render consensus
1101 public void setConservationRow(AlignmentAnnotation aan)
1103 if (conservation == null)
1110 * get the conservation annotation row for this group
1112 * @return autoCalculated annotation row
1114 public AlignmentAnnotation getConservationRow()
1116 if (conservation == null)
1118 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1119 11f, AlignmentAnnotation.BAR_GRAPH);
1122 conservation.hasText = true;
1123 conservation.autoCalculated = true;
1124 conservation.groupRef = this;
1125 conservation.label = "Conservation for " + getName();
1126 conservation.description = "Conservation for group " + getName()
1127 + " less than " + consPercGaps + "% gaps";
1128 return conservation;
1133 * @return true if annotation rows have been instantiated for this group
1135 public boolean hasAnnotationRows()
1137 return consensus != null || conservation != null;
1140 public SequenceI getConsensusSeq()
1143 StringBuffer seqs = new StringBuffer();
1144 for (int i = 0; i < consensus.annotations.length; i++)
1146 if (consensus.annotations[i] != null)
1148 if (consensus.annotations[i].description.charAt(0) == '[')
1150 seqs.append(consensus.annotations[i].description.charAt(1));
1154 seqs.append(consensus.annotations[i].displayCharacter);
1159 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1161 sq.setDescription("Percentage Identity Consensus "
1162 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1166 public void setIgnoreGapsConsensus(boolean state)
1168 if (this.ignoreGapsInConsensus != state && consensus != null)
1170 ignoreGapsInConsensus = state;
1171 recalcConservation();
1173 ignoreGapsInConsensus = state;
1176 public boolean getIgnoreGapsConsensus()
1178 return ignoreGapsInConsensus;
1182 * @param showSequenceLogo
1183 * indicates if a sequence logo is shown for consensus annotation
1185 public void setshowSequenceLogo(boolean showSequenceLogo)
1187 // TODO: decouple calculation from settings update
1188 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1190 this.showSequenceLogo = showSequenceLogo;
1191 recalcConservation();
1193 this.showSequenceLogo = showSequenceLogo;
1198 * @param showConsHist
1199 * flag indicating if the consensus histogram for this group should
1202 public void setShowConsensusHistogram(boolean showConsHist)
1205 if (showConsensusHistogram != showConsHist && consensus != null)
1207 this.showConsensusHistogram = showConsHist;
1208 recalcConservation();
1210 this.showConsensusHistogram = showConsHist;
1214 * @return the showConsensusHistogram
1216 public boolean isShowConsensusHistogram()
1218 return showConsensusHistogram;
1222 * set flag indicating if logo should be normalised when rendered
1226 public void setNormaliseSequenceLogo(boolean norm)
1228 normaliseSequenceLogo = norm;
1231 public boolean isNormaliseSequenceLogo()
1233 return normaliseSequenceLogo;
1238 * returns a new array with all annotation involving this group
1240 public AlignmentAnnotation[] getAlignmentAnnotation()
1242 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1244 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1245 synchronized (sequences)
1247 for (SequenceI seq : sequences)
1249 AlignmentAnnotation[] aa = seq.getAnnotation();
1252 for (AlignmentAnnotation al : aa)
1254 if (al.groupRef == this)
1261 if (consensus != null)
1263 annot.add(consensus);
1265 if (conservation != null)
1267 annot.add(conservation);
1270 return annot.toArray(new AlignmentAnnotation[0]);
1274 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1276 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1277 for (AlignmentAnnotation a : getAlignmentAnnotation())
1279 if (a.getCalcId() == calcId)
1288 * Returns a list of annotations that match the specified sequenceRef, calcId
1289 * and label, ignoring null values.
1291 * @return list of AlignmentAnnotation objects
1294 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1295 String calcId, String label)
1297 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1298 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1300 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1301 && ann.sequenceRef != null && ann.sequenceRef == seq
1302 && ann.label != null && ann.label.equals(label))
1311 * Answer true if any annotation matches the calcId passed in (if not null).
1316 public boolean hasAnnotation(String calcId)
1318 if (calcId != null && !"".equals(calcId))
1320 for (AlignmentAnnotation a : getAlignmentAnnotation())
1322 if (a.getCalcId() == calcId)
1332 * Remove all sequences from the group (leaving other properties unchanged).
1336 synchronized (sequences)
1342 private AnnotatedCollectionI context;
1345 * set the alignment or group context for this group
1349 public void setContext(AnnotatedCollectionI context)
1351 this.context = context;
1357 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1360 public AnnotatedCollectionI getContext()