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;
26 import jalview.schemes.ResidueProperties;
28 import java.awt.Color;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
33 import java.util.Vector;
36 * Collects a set contiguous ranges on a set of sequences
41 public class SequenceGroup implements AnnotatedCollectionI
47 Conservation conserve;
51 boolean displayBoxes = true;
53 boolean displayText = true;
55 boolean colourText = false;
58 * after Olivier's non-conserved only character display
60 boolean showNonconserved = false;
65 private List<SequenceI> sequences = new ArrayList<SequenceI>();
68 * representative sequence for this group (if any)
70 private SequenceI seqrep = null;
75 * Colourscheme applied to group if any
77 public ColourSchemeI cs;
79 // start column (base 0)
82 // end column (base 0)
85 public Color outlineColour = Color.black;
87 public Color idColour = null;
89 public int thresholdTextColour = 0;
91 public Color textColour = Color.black;
93 public Color textColour2 = Color.white;
96 * consensus calculation property
98 private boolean ignoreGapsInConsensus = true;
101 * consensus calculation property
103 private boolean showSequenceLogo = false;
106 * flag indicating if logo should be rendered normalised
108 private boolean normaliseSequenceLogo;
111 * @return the includeAllConsSymbols
113 public boolean isShowSequenceLogo()
115 return showSequenceLogo;
119 * Creates a new SequenceGroup object.
121 public SequenceGroup()
123 groupName = "JGroup:" + this.hashCode();
127 * Creates a new SequenceGroup object.
132 * @param displayBoxes
136 * first column of group
138 * last column of group
140 public SequenceGroup(List<SequenceI> sequences, String groupName,
141 ColourSchemeI scheme, boolean displayBoxes, boolean displayText,
142 boolean colourText, int start, int end)
144 this.sequences = sequences;
145 this.groupName = groupName;
146 this.displayBoxes = displayBoxes;
147 this.displayText = displayText;
148 this.colourText = colourText;
152 recalcConservation();
160 public SequenceGroup(SequenceGroup seqsel)
164 sequences = new ArrayList<SequenceI>();
165 sequences.addAll(seqsel.sequences);
166 if (seqsel.groupName != null)
168 groupName = new String(seqsel.groupName);
170 displayBoxes = seqsel.displayBoxes;
171 displayText = seqsel.displayText;
172 colourText = seqsel.colourText;
173 startRes = seqsel.startRes;
174 endRes = seqsel.endRes;
176 if (seqsel.description != null)
178 description = new String(seqsel.description);
180 hidecols = seqsel.hidecols;
181 hidereps = seqsel.hidereps;
182 idColour = seqsel.idColour;
183 outlineColour = seqsel.outlineColour;
184 seqrep = seqsel.seqrep;
185 textColour = seqsel.textColour;
186 textColour2 = seqsel.textColour2;
187 thresholdTextColour = seqsel.thresholdTextColour;
188 width = seqsel.width;
189 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
190 if (seqsel.conserve != null)
192 recalcConservation(); // safer than
193 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
198 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
200 int iSize = sequences.size();
201 SequenceI[] seqs = new SequenceI[iSize];
202 SequenceI[] inorder = getSequencesInOrder(align);
204 for (int i = 0, ipos = 0; i < inorder.length; i++)
206 SequenceI seq = inorder[i];
208 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
209 if (seqs[ipos] != null)
211 seqs[ipos].setDescription(seq.getDescription());
212 seqs[ipos].setDBRefs(seq.getDBRefs());
213 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
214 if (seq.getDatasetSequence() != null)
216 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
219 if (seq.getAnnotation() != null)
221 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
222 // Only copy annotation that is either a score or referenced by the
223 // alignment's annotation vector
224 for (int a = 0; a < seq.getAnnotation().length; a++)
226 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
229 boolean found = false;
230 for (int pos = 0; pos < alann.length; pos++)
232 if (alann[pos] == tocopy)
243 AlignmentAnnotation newannot = new AlignmentAnnotation(
244 seq.getAnnotation()[a]);
245 newannot.restrict(startRes, endRes);
246 newannot.setSequenceRef(seqs[ipos]);
247 newannot.adjustForAlignment();
248 seqs[ipos].addAlignmentAnnotation(newannot);
258 if (iSize != inorder.length)
260 SequenceI[] nseqs = new SequenceI[iSize];
261 System.arraycopy(seqs, 0, nseqs, 0, iSize);
269 * If sequence ends in gaps, the end residue can be correctly calculated here
275 public int findEndRes(SequenceI seq)
280 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
282 ch = seq.getCharAt(j);
283 if (!jalview.util.Comparison.isGap((ch)))
291 eres += seq.getStart() - 1;
298 public List<SequenceI> getSequences()
304 public List<SequenceI> getSequences(
305 Map<SequenceI, SequenceCollectionI> hiddenReps)
307 if (hiddenReps == null)
309 // TODO: need a synchronizedCollection here ?
314 List<SequenceI> allSequences = new ArrayList<SequenceI>();
315 for (SequenceI seq : sequences)
317 allSequences.add(seq);
318 if (hiddenReps.containsKey(seq))
320 SequenceCollectionI hsg = hiddenReps.get(seq);
321 for (SequenceI seq2 : hsg.getSequences())
323 if (seq2 != seq && !allSequences.contains(seq2))
325 allSequences.add(seq2);
335 public SequenceI[] getSequencesAsArray(
336 Map<SequenceI, SequenceCollectionI> map)
338 List<SequenceI> tmp = getSequences(map);
343 return tmp.toArray(new SequenceI[tmp.size()]);
352 * @return DOCUMENT ME!
354 public boolean adjustForRemoveLeft(int col)
356 // return value is true if the group still exists
359 startRes = startRes - col;
364 endRes = endRes - col;
366 if (startRes > endRes)
373 // must delete this group!!
386 * @return DOCUMENT ME!
388 public boolean adjustForRemoveRight(int col)
407 * @return DOCUMENT ME!
409 public String getName()
414 public String getDescription()
425 public void setName(String name)
428 // TODO: URGENT: update dependent objects (annotation row)
431 public void setDescription(String desc)
439 * @return DOCUMENT ME!
441 public Conservation getConservation()
452 public void setConservation(Conservation c)
458 * Add s to this sequence group. If aligment sequence is already contained in
459 * group, it will not be added again, but recalculation may happen if the flag
463 * alignment sequence to be added
465 * true means Group's conservation should be recalculated
467 public void addSequence(SequenceI s, boolean recalc)
469 synchronized (sequences)
471 if (s != null && !sequences.contains(s))
478 recalcConservation();
484 * Max Gaps Threshold (percent) for performing a conservation calculation
486 private int consPercGaps = 25;
489 * @return Max Gaps Threshold for performing a conservation calculation
491 public int getConsPercGaps()
497 * set Max Gaps Threshold (percent) for performing a conservation calculation
499 * @param consPercGaps
501 public void setConsPercGaps(int consPercGaps)
503 this.consPercGaps = consPercGaps;
507 * calculate residue conservation and colourschemes for group - but only if
508 * necessary. returns true if the calculation resulted in a visible change to
511 public boolean recalcConservation()
513 return recalcConservation(false);
517 * calculate residue conservation for group - but only if necessary. returns
518 * true if the calculation resulted in a visible change to group
521 * when set, colourschemes for this group are not refreshed after
524 public boolean recalcConservation(boolean defer)
526 if (cs == null && consensus == null && conservation == null)
530 // TODO: try harder to detect changes in state in order to minimise
531 // recalculation effort
535 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
536 endRes + 1, showSequenceLogo);
537 if (consensus != null)
539 _updateConsensusRow(cnsns, sequences.size());
544 cs.setConsensus(cnsns);
548 if ((conservation != null)
549 || (cs != null && cs.conservationApplied()))
551 Conservation c = new Conservation(groupName,
552 ResidueProperties.propHash, 3, sequences, startRes,
555 c.verdict(false, consPercGaps);
556 if (conservation != null)
558 _updateConservationRow(c);
562 if (cs.conservationApplied())
564 cs.setConservation(c);
567 // eager update - will cause a refresh of overview regardless
570 if (cs != null && !defer)
572 // TODO: JAL-2034 should cs.alignmentChanged modify return state
573 cs.alignmentChanged(context != null ? context : this, null);
580 } catch (java.lang.OutOfMemoryError err)
583 System.out.println("Out of memory loading groups: " + err);
588 private void _updateConservationRow(Conservation c)
590 if (conservation == null)
595 conservation.label = "Conservation for " + getName();
596 conservation.description = "Conservation for group " + getName()
597 + " less than " + consPercGaps + "% gaps";
598 // preserve width if already set
599 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
602 conservation.annotations = null;
603 conservation.annotations = new Annotation[aWidth]; // should be alignment
605 c.completeAnnotations(conservation, null, startRes, endRes + 1);
608 public Hashtable[] consensusData = null;
610 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
612 if (consensus == null)
616 consensus.label = "Consensus for " + getName();
617 consensus.description = "Percent Identity";
618 consensusData = cnsns;
619 // preserve width if already set
620 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
623 consensus.annotations = null;
624 consensus.annotations = new Annotation[aWidth]; // should be alignment width
626 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
627 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
630 // ignoreGapsInConsensusCalculation);
635 * sequence to either add or remove from group
637 * flag passed to delete/addSequence to indicate if group properties
638 * should be recalculated
640 public void addOrRemove(SequenceI s, boolean recalc)
642 synchronized (sequences)
644 if (sequences.contains(s))
646 deleteSequence(s, recalc);
650 addSequence(s, recalc);
661 * true means recalculate conservation
663 public void deleteSequence(SequenceI s, boolean recalc)
665 synchronized (sequences)
671 recalcConservation();
679 * @return the first column selected by this group. Runs from 0<=i<N_cols
682 public int getStartRes()
689 * @return the groups last selected column. Runs from 0<=i<N_cols
692 public int getEndRes()
698 * Set the first column selected by this group. Runs from 0<=i<N_cols
702 public void setStartRes(int i)
708 * Set the groups last selected column. Runs from 0<=i<N_cols
712 public void setEndRes(int i)
718 * @return number of sequences in group
722 return sequences.size();
727 * @return the ith sequence
729 public SequenceI getSequenceAt(int i)
731 return sequences.get(i);
738 public void setColourText(boolean state)
746 * @return DOCUMENT ME!
748 public boolean getColourText()
759 public void setDisplayText(boolean state)
767 * @return DOCUMENT ME!
769 public boolean getDisplayText()
780 public void setDisplayBoxes(boolean state)
782 displayBoxes = state;
788 * @return DOCUMENT ME!
790 public boolean getDisplayBoxes()
796 * computes the width of current set of sequences and returns it
798 * @return DOCUMENT ME!
801 public int getWidth()
803 synchronized (sequences)
805 // MC This needs to get reset when characters are inserted and deleted
806 boolean first = true;
807 for (SequenceI seq : sequences)
809 if (first || seq.getLength() > width)
811 width = seq.getLength();
825 public void setOutlineColour(Color c)
833 * @return DOCUMENT ME!
835 public Color getOutlineColour()
837 return outlineColour;
842 * returns the sequences in the group ordered by the ordering given by al.
843 * this used to return an array with null entries regardless, new behaviour is
844 * below. TODO: verify that this does not affect use in applet or application
848 * @return SequenceI[] intersection of sequences in group with al, ordered by
849 * al, or null if group does not intersect with al
851 public SequenceI[] getSequencesInOrder(AlignmentI al)
853 return getSequencesInOrder(al, true);
857 * return an array representing the intersection of the group with al,
858 * optionally returning an array the size of al.getHeight() where nulls mark
859 * the non-intersected sequences
863 * @return null or array
865 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
867 synchronized (sequences)
869 int sSize = sequences.size();
870 int alHeight = al.getHeight();
872 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
875 for (int i = 0; i < alHeight && index < sSize; i++)
877 if (sequences.contains(al.getSequenceAt(i)))
879 seqs[(trim) ? index : i] = al.getSequenceAt(i);
891 if (index < seqs.length)
893 SequenceI[] dummy = seqs;
894 seqs = new SequenceI[index];
897 seqs[index] = dummy[index];
906 * @return the idColour
908 public Color getIdColour()
915 * the idColour to set
917 public void setIdColour(Color idColour)
919 this.idColour = idColour;
923 * @return the representative sequence for this group
926 public SequenceI getSeqrep()
932 * set the representative sequence for this group. Note - this affects the
933 * interpretation of the Hidereps attribute.
936 * the seqrep to set (null means no sequence representative)
939 public void setSeqrep(SequenceI seqrep)
941 this.seqrep = seqrep;
946 * @return true if group has a sequence representative
949 public boolean hasSeqrep()
951 return seqrep != null;
955 * visibility of rows or represented rows covered by group
957 private boolean hidereps = false;
960 * set visibility of sequences covered by (if no sequence representative is
961 * defined) or represented by this group.
965 public void setHidereps(boolean visibility)
967 hidereps = visibility;
972 * @return true if sequences represented (or covered) by this group should be
975 public boolean isHidereps()
981 * visibility of columns intersecting this group
983 private boolean hidecols = false;
986 * set intended visibility of columns covered by this group
990 public void setHideCols(boolean visibility)
992 hidecols = visibility;
997 * @return true if columns covered by group should be hidden
999 public boolean isHideCols()
1005 * create a new sequence group from the intersection of this group with an
1006 * alignment Hashtable of hidden representatives
1012 * @return new group containing sequences common to this group and alignment
1014 public SequenceGroup intersect(AlignmentI alignment,
1015 Map<SequenceI, SequenceCollectionI> map)
1017 SequenceGroup sgroup = new SequenceGroup(this);
1018 SequenceI[] insect = getSequencesInOrder(alignment);
1019 sgroup.sequences = new ArrayList<SequenceI>();
1020 for (int s = 0; insect != null && s < insect.length; s++)
1022 if (map == null || map.containsKey(insect[s]))
1024 sgroup.sequences.add(insect[s]);
1031 * @return the showUnconserved
1033 public boolean getShowNonconserved()
1035 return showNonconserved;
1039 * @param showNonconserved
1040 * the showUnconserved to set
1042 public void setShowNonconserved(boolean displayNonconserved)
1044 this.showNonconserved = displayNonconserved;
1047 AlignmentAnnotation consensus = null, conservation = null;
1050 * flag indicating if consensus histogram should be rendered
1052 private boolean showConsensusHistogram;
1055 * set this alignmentAnnotation object as the one used to render consensus
1060 public void setConsensus(AlignmentAnnotation aan)
1062 if (consensus == null)
1070 * @return automatically calculated consensus row note: the row is a stub if a
1071 * consensus calculation has not yet been performed on the group
1073 public AlignmentAnnotation getConsensus()
1075 // TODO get or calculate and get consensus annotation row for this group
1076 int aWidth = this.getWidth();
1084 if (consensus == null)
1086 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1087 100f, AlignmentAnnotation.BAR_GRAPH);
1088 consensus.hasText = true;
1089 consensus.autoCalculated = true;
1090 consensus.groupRef = this;
1091 consensus.label = "Consensus for " + getName();
1092 consensus.description = "Percent Identity";
1098 * set this alignmentAnnotation object as the one used to render consensus
1103 public void setConservationRow(AlignmentAnnotation aan)
1105 if (conservation == null)
1112 * get the conservation annotation row for this group
1114 * @return autoCalculated annotation row
1116 public AlignmentAnnotation getConservationRow()
1118 if (conservation == null)
1120 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1121 11f, AlignmentAnnotation.BAR_GRAPH);
1124 conservation.hasText = true;
1125 conservation.autoCalculated = true;
1126 conservation.groupRef = this;
1127 conservation.label = "Conservation for " + getName();
1128 conservation.description = "Conservation for group " + getName()
1129 + " less than " + consPercGaps + "% gaps";
1130 return conservation;
1135 * @return true if annotation rows have been instantiated for this group
1137 public boolean hasAnnotationRows()
1139 return consensus != null || conservation != null;
1142 public SequenceI getConsensusSeq()
1145 StringBuffer seqs = new StringBuffer();
1146 for (int i = 0; i < consensus.annotations.length; i++)
1148 if (consensus.annotations[i] != null)
1150 if (consensus.annotations[i].description.charAt(0) == '[')
1152 seqs.append(consensus.annotations[i].description.charAt(1));
1156 seqs.append(consensus.annotations[i].displayCharacter);
1161 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1163 sq.setDescription("Percentage Identity Consensus "
1164 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1168 public void setIgnoreGapsConsensus(boolean state)
1170 if (this.ignoreGapsInConsensus != state && consensus != null)
1172 ignoreGapsInConsensus = state;
1173 recalcConservation();
1175 ignoreGapsInConsensus = state;
1178 public boolean getIgnoreGapsConsensus()
1180 return ignoreGapsInConsensus;
1184 * @param showSequenceLogo
1185 * indicates if a sequence logo is shown for consensus annotation
1187 public void setshowSequenceLogo(boolean showSequenceLogo)
1189 // TODO: decouple calculation from settings update
1190 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1192 this.showSequenceLogo = showSequenceLogo;
1193 recalcConservation();
1195 this.showSequenceLogo = showSequenceLogo;
1200 * @param showConsHist
1201 * flag indicating if the consensus histogram for this group should
1204 public void setShowConsensusHistogram(boolean showConsHist)
1207 if (showConsensusHistogram != showConsHist && consensus != null)
1209 this.showConsensusHistogram = showConsHist;
1210 recalcConservation();
1212 this.showConsensusHistogram = showConsHist;
1216 * @return the showConsensusHistogram
1218 public boolean isShowConsensusHistogram()
1220 return showConsensusHistogram;
1224 * set flag indicating if logo should be normalised when rendered
1228 public void setNormaliseSequenceLogo(boolean norm)
1230 normaliseSequenceLogo = norm;
1233 public boolean isNormaliseSequenceLogo()
1235 return normaliseSequenceLogo;
1240 * returns a new array with all annotation involving this group
1242 public AlignmentAnnotation[] getAlignmentAnnotation()
1244 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1246 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1247 synchronized (sequences)
1249 for (SequenceI seq : sequences)
1251 AlignmentAnnotation[] aa = seq.getAnnotation();
1254 for (AlignmentAnnotation al : aa)
1256 if (al.groupRef == this)
1263 if (consensus != null)
1265 annot.add(consensus);
1267 if (conservation != null)
1269 annot.add(conservation);
1272 return annot.toArray(new AlignmentAnnotation[0]);
1276 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1278 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1279 for (AlignmentAnnotation a : getAlignmentAnnotation())
1281 if (a.getCalcId() == calcId)
1290 * Returns a list of annotations that match the specified sequenceRef, calcId
1291 * and label, ignoring null values.
1293 * @return list of AlignmentAnnotation objects
1296 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1297 String calcId, String label)
1299 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1300 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1302 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1303 && ann.sequenceRef != null && ann.sequenceRef == seq
1304 && ann.label != null && ann.label.equals(label))
1313 * Answer true if any annotation matches the calcId passed in (if not null).
1318 public boolean hasAnnotation(String calcId)
1320 if (calcId != null && !"".equals(calcId))
1322 for (AlignmentAnnotation a : getAlignmentAnnotation())
1324 if (a.getCalcId() == calcId)
1334 * Remove all sequences from the group (leaving other properties unchanged).
1338 synchronized (sequences)
1344 private AnnotatedCollectionI context;
1347 * set the alignment or group context for this group
1351 public void setContext(AnnotatedCollectionI context)
1353 this.context = context;
1359 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1362 public AnnotatedCollectionI getContext()