2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.Enumeration;
31 import java.util.Hashtable;
32 import java.util.List;
34 import java.util.Vector;
37 * Collects a set contiguous ranges on a set of sequences
42 public class SequenceGroup implements AnnotatedCollectionI
48 Conservation conserve;
52 boolean displayBoxes = true;
54 boolean displayText = true;
56 boolean colourText = false;
59 * after Olivier's non-conserved only character display
61 boolean showNonconserved = false;
66 private Vector<SequenceI> sequences = new Vector<SequenceI>();
69 * representative sequence for this group (if any)
71 private SequenceI seqrep = null;
76 * Colourscheme applied to group if any
78 public ColourSchemeI cs;
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(Vector 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 Vector();
164 Enumeration<SequenceI> sq = seqsel.sequences.elements();
165 while (sq.hasMoreElements())
167 sequences.addElement(sq.nextElement());
170 if (seqsel.groupName != null)
172 groupName = new String(seqsel.groupName);
174 displayBoxes = seqsel.displayBoxes;
175 displayText = seqsel.displayText;
176 colourText = seqsel.colourText;
177 startRes = seqsel.startRes;
178 endRes = seqsel.endRes;
180 if (seqsel.description != null)
182 description = new String(seqsel.description);
184 hidecols = seqsel.hidecols;
185 hidereps = seqsel.hidereps;
186 idColour = seqsel.idColour;
187 outlineColour = seqsel.outlineColour;
188 seqrep = seqsel.seqrep;
189 textColour = seqsel.textColour;
190 textColour2 = seqsel.textColour2;
191 thresholdTextColour = seqsel.thresholdTextColour;
192 width = seqsel.width;
193 ignoreGapsInConsensus = seqsel.ignoreGapsInConsensus;
194 if (seqsel.conserve != null)
196 recalcConservation(); // safer than
197 // aaFrequency = (Vector) seqsel.aaFrequency.clone(); // ??
202 public SequenceI[] getSelectionAsNewSequences(AlignmentI align)
204 int iSize = sequences.size();
205 SequenceI[] seqs = new SequenceI[iSize];
206 SequenceI[] inorder = getSequencesInOrder(align);
208 for (int i = 0, ipos = 0; i < inorder.length; i++)
210 SequenceI seq = inorder[i];
212 seqs[ipos] = seq.getSubSequence(startRes, endRes + 1);
213 if (seqs[ipos] != null)
215 seqs[ipos].setDescription(seq.getDescription());
216 seqs[ipos].setDBRef(seq.getDBRef());
217 seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures());
218 if (seq.getDatasetSequence() != null)
220 seqs[ipos].setDatasetSequence(seq.getDatasetSequence());
223 if (seq.getAnnotation() != null)
225 AlignmentAnnotation[] alann = align.getAlignmentAnnotation();
226 // Only copy annotation that is either a score or referenced by the
227 // alignment's annotation vector
228 for (int a = 0; a < seq.getAnnotation().length; a++)
230 AlignmentAnnotation tocopy = seq.getAnnotation()[a];
233 boolean found = false;
234 for (int pos = 0; pos < alann.length; pos++)
236 if (alann[pos] == tocopy)
247 AlignmentAnnotation newannot = new AlignmentAnnotation(
248 seq.getAnnotation()[a]);
249 newannot.restrict(startRes, endRes);
250 newannot.setSequenceRef(seqs[ipos]);
251 newannot.adjustForAlignment();
252 seqs[ipos].addAlignmentAnnotation(newannot);
262 if (iSize != inorder.length)
264 SequenceI[] nseqs = new SequenceI[iSize];
265 System.arraycopy(seqs, 0, nseqs, 0, iSize);
273 * If sequence ends in gaps, the end residue can be correctly calculated here
279 public int findEndRes(SequenceI seq)
284 for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++)
286 ch = seq.getCharAt(j);
287 if (!jalview.util.Comparison.isGap((ch)))
295 eres += seq.getStart() - 1;
302 public List<SequenceI> getSequences()
308 public List<SequenceI> getSequences(
309 Map<SequenceI, SequenceCollectionI> hiddenReps)
311 if (hiddenReps == null)
317 Vector allSequences = new Vector();
319 for (int i = 0; i < sequences.size(); i++)
321 seq = sequences.elementAt(i);
322 allSequences.addElement(seq);
323 if (hiddenReps.containsKey(seq))
325 SequenceCollectionI hsg = hiddenReps.get(seq);
326 for (SequenceI seq2 : hsg.getSequences())
328 if (seq2 != seq && !allSequences.contains(seq2))
330 allSequences.addElement(seq2);
340 public SequenceI[] getSequencesAsArray(
341 Map<SequenceI, SequenceCollectionI> map)
343 List<SequenceI> tmp = getSequences(map);
348 return tmp.toArray(new SequenceI[tmp.size()]);
357 * @return DOCUMENT ME!
359 public boolean adjustForRemoveLeft(int col)
361 // return value is true if the group still exists
364 startRes = startRes - col;
369 endRes = endRes - col;
371 if (startRes > endRes)
378 // must delete this group!!
391 * @return DOCUMENT ME!
393 public boolean adjustForRemoveRight(int col)
412 * @return DOCUMENT ME!
414 public String getName()
419 public String getDescription()
430 public void setName(String name)
433 // TODO: URGENT: update dependent objects (annotation row)
436 public void setDescription(String desc)
444 * @return DOCUMENT ME!
446 public Conservation getConservation()
457 public void setConservation(Conservation c)
463 * Add s to this sequence group. If aligment sequence is already contained in
464 * group, it will not be added again, but recalculation may happen if the flag
468 * alignment sequence to be added
470 * true means Group's conservation should be recalculated
472 public void addSequence(SequenceI s, boolean recalc)
474 if (s != null && !sequences.contains(s))
476 sequences.addElement(s);
481 recalcConservation();
486 * Max Gaps Threshold (percent) for performing a conservation calculation
488 private int consPercGaps = 25;
491 * @return Max Gaps Threshold for performing a conservation calculation
493 public int getConsPercGaps()
499 * set Max Gaps Threshold (percent) for performing a conservation calculation
501 * @param consPercGaps
503 public void setConsPercGaps(int consPercGaps)
505 this.consPercGaps = consPercGaps;
509 * calculate residue conservation for group - but only if necessary.
511 public void recalcConservation()
513 if (cs == null && consensus == null && conservation == null)
519 Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes,
520 endRes + 1, showSequenceLogo);
521 if (consensus != null)
523 _updateConsensusRow(cnsns, sequences.size());
527 cs.setConsensus(cnsns);
530 if ((conservation != null)
531 || (cs != null && cs.conservationApplied()))
533 Conservation c = new Conservation(groupName,
534 ResidueProperties.propHash, 3, sequences, startRes,
537 c.verdict(false, consPercGaps);
538 if (conservation != null)
540 _updateConservationRow(c);
544 if (cs.conservationApplied())
546 cs.setConservation(c);
552 cs.alignmentChanged(context != null ? context : this, null);
554 } catch (java.lang.OutOfMemoryError err)
557 System.out.println("Out of memory loading groups: " + err);
562 private void _updateConservationRow(Conservation c)
564 if (conservation == null)
569 conservation.label = "Conservation for " + getName();
570 conservation.description = "Conservation for group " + getName()
571 + " less than " + consPercGaps + "% gaps";
572 // preserve width if already set
573 int aWidth = (conservation.annotations != null) ? (endRes < conservation.annotations.length ? conservation.annotations.length
576 conservation.annotations = null;
577 conservation.annotations = new Annotation[aWidth]; // should be alignment
579 c.completeAnnotations(conservation, null, startRes, endRes + 1);
582 public Hashtable[] consensusData = null;
584 private void _updateConsensusRow(Hashtable[] cnsns, long nseq)
586 if (consensus == null)
590 consensus.label = "Consensus for " + getName();
591 consensus.description = "Percent Identity";
592 consensusData = cnsns;
593 // preserve width if already set
594 int aWidth = (consensus.annotations != null) ? (endRes < consensus.annotations.length ? consensus.annotations.length
597 consensus.annotations = null;
598 consensus.annotations = new Annotation[aWidth]; // should be alignment width
600 AAFrequency.completeConsensus(consensus, cnsns, startRes, endRes + 1,
601 ignoreGapsInConsensus, showSequenceLogo, nseq); // TODO: setting
604 // ignoreGapsInConsensusCalculation);
609 * sequence to either add or remove from group
611 * flag passed to delete/addSequence to indicate if group properties
612 * should be recalculated
614 public void addOrRemove(SequenceI s, boolean recalc)
616 if (sequences.contains(s))
618 deleteSequence(s, recalc);
622 addSequence(s, recalc);
634 public void deleteSequence(SequenceI s, boolean recalc)
636 sequences.removeElement(s);
640 recalcConservation();
647 * @return the first column selected by this group. Runs from 0<=i<N_cols
650 public int getStartRes()
657 * @return the groups last selected column. Runs from 0<=i<N_cols
660 public int getEndRes()
666 * Set the first column selected by this group. Runs from 0<=i<N_cols
670 public void setStartRes(int i)
676 * Set the groups last selected column. Runs from 0<=i<N_cols
680 public void setEndRes(int i)
688 * @return DOCUMENT ME!
692 return sequences.size();
701 * @return DOCUMENT ME!
703 public SequenceI getSequenceAt(int i)
705 return sequences.elementAt(i);
714 public void setColourText(boolean state)
722 * @return DOCUMENT ME!
724 public boolean getColourText()
735 public void setDisplayText(boolean state)
743 * @return DOCUMENT ME!
745 public boolean getDisplayText()
756 public void setDisplayBoxes(boolean state)
758 displayBoxes = state;
764 * @return DOCUMENT ME!
766 public boolean getDisplayBoxes()
774 * @return DOCUMENT ME!
777 public int getWidth()
779 // MC This needs to get reset when characters are inserted and deleted
780 if (sequences.size() > 0)
782 width = sequences.elementAt(0).getLength();
785 for (int i = 1; i < sequences.size(); i++)
787 SequenceI seq = sequences.elementAt(i);
789 if (seq.getLength() > width)
791 width = seq.getLength();
804 public void setOutlineColour(Color c)
812 * @return DOCUMENT ME!
814 public Color getOutlineColour()
816 return outlineColour;
821 * returns the sequences in the group ordered by the ordering given by al.
822 * this used to return an array with null entries regardless, new behaviour is
823 * below. TODO: verify that this does not affect use in applet or application
827 * @return SequenceI[] intersection of sequences in group with al, ordered by
828 * al, or null if group does not intersect with al
830 public SequenceI[] getSequencesInOrder(AlignmentI al)
832 return getSequencesInOrder(al, true);
836 * return an array representing the intersection of the group with al,
837 * optionally returning an array the size of al.getHeight() where nulls mark
838 * the non-intersected sequences
842 * @return null or array
844 public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim)
846 int sSize = sequences.size();
847 int alHeight = al.getHeight();
849 SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight];
852 for (int i = 0; i < alHeight && index < sSize; i++)
854 if (sequences.contains(al.getSequenceAt(i)))
856 seqs[(trim) ? index : i] = al.getSequenceAt(i);
868 if (index < seqs.length)
870 SequenceI[] dummy = seqs;
871 seqs = new SequenceI[index];
874 seqs[index] = dummy[index];
882 * @return the idColour
884 public Color getIdColour()
891 * the idColour to set
893 public void setIdColour(Color idColour)
895 this.idColour = idColour;
899 * @return the representative sequence for this group
901 public SequenceI getSeqrep()
907 * set the representative sequence for this group. Note - this affects the
908 * interpretation of the Hidereps attribute.
911 * the seqrep to set (null means no sequence representative)
913 public void setSeqrep(SequenceI seqrep)
915 this.seqrep = seqrep;
920 * @return true if group has a sequence representative
922 public boolean hasSeqrep()
924 return seqrep != null;
928 * visibility of rows or represented rows covered by group
930 private boolean hidereps = false;
933 * set visibility of sequences covered by (if no sequence representative is
934 * defined) or represented by this group.
938 public void setHidereps(boolean visibility)
940 hidereps = visibility;
945 * @return true if sequences represented (or covered) by this group should be
948 public boolean isHidereps()
954 * visibility of columns intersecting this group
956 private boolean hidecols = false;
959 * set intended visibility of columns covered by this group
963 public void setHideCols(boolean visibility)
965 hidecols = visibility;
970 * @return true if columns covered by group should be hidden
972 public boolean isHideCols()
978 * create a new sequence group from the intersection of this group with an
979 * alignment Hashtable of hidden representatives
985 * @return new group containing sequences common to this group and alignment
987 public SequenceGroup intersect(AlignmentI alignment,
988 Map<SequenceI, SequenceCollectionI> map)
990 SequenceGroup sgroup = new SequenceGroup(this);
991 SequenceI[] insect = getSequencesInOrder(alignment);
992 sgroup.sequences = new Vector();
993 for (int s = 0; insect != null && s < insect.length; s++)
995 if (map == null || map.containsKey(insect[s]))
997 sgroup.sequences.addElement(insect[s]);
1000 // Enumeration en =getSequences(hashtable).elements();
1001 // while (en.hasMoreElements())
1003 // SequenceI elem = (SequenceI) en.nextElement();
1004 // if (alignment.getSequences().contains(elem))
1006 // sgroup.addSequence(elem, false);
1013 * @return the showUnconserved
1015 public boolean getShowNonconserved()
1017 return showNonconserved;
1021 * @param showNonconserved
1022 * the showUnconserved to set
1024 public void setShowNonconserved(boolean displayNonconserved)
1026 this.showNonconserved = displayNonconserved;
1029 AlignmentAnnotation consensus = null, conservation = null;
1032 * flag indicating if consensus histogram should be rendered
1034 private boolean showConsensusHistogram;
1037 * set this alignmentAnnotation object as the one used to render consensus
1042 public void setConsensus(AlignmentAnnotation aan)
1044 if (consensus == null)
1052 * @return automatically calculated consensus row
1054 public AlignmentAnnotation getConsensus()
1056 // TODO get or calculate and get consensus annotation row for this group
1057 int aWidth = this.getWidth();
1065 if (consensus == null)
1067 consensus = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1068 100f, AlignmentAnnotation.BAR_GRAPH);
1069 consensus.hasText = true;
1070 consensus.autoCalculated = true;
1071 consensus.groupRef = this;
1072 consensus.label = "Consensus for " + getName();
1073 consensus.description = "Percent Identity";
1079 * set this alignmentAnnotation object as the one used to render consensus
1084 public void setConservationRow(AlignmentAnnotation aan)
1086 if (conservation == null)
1093 * get the conservation annotation row for this group
1095 * @return autoCalculated annotation row
1097 public AlignmentAnnotation getConservationRow()
1099 if (conservation == null)
1101 conservation = new AlignmentAnnotation("", "", new Annotation[1], 0f,
1102 11f, AlignmentAnnotation.BAR_GRAPH);
1105 conservation.hasText = true;
1106 conservation.autoCalculated = true;
1107 conservation.groupRef = this;
1108 conservation.label = "Conservation for " + getName();
1109 conservation.description = "Conservation for group " + getName()
1110 + " less than " + consPercGaps + "% gaps";
1111 return conservation;
1116 * @return true if annotation rows have been instantiated for this group
1118 public boolean hasAnnotationRows()
1120 return consensus != null || conservation != null;
1123 public SequenceI getConsensusSeq()
1126 StringBuffer seqs = new StringBuffer();
1127 for (int i = 0; i < consensus.annotations.length; i++)
1129 if (consensus.annotations[i] != null)
1131 if (consensus.annotations[i].description.charAt(0) == '[')
1133 seqs.append(consensus.annotations[i].description.charAt(1));
1137 seqs.append(consensus.annotations[i].displayCharacter);
1142 SequenceI sq = new Sequence("Group" + getName() + " Consensus",
1144 sq.setDescription("Percentage Identity Consensus "
1145 + ((ignoreGapsInConsensus) ? " without gaps" : ""));
1149 public void setIgnoreGapsConsensus(boolean state)
1151 if (this.ignoreGapsInConsensus != state && consensus != null)
1153 ignoreGapsInConsensus = state;
1154 recalcConservation();
1156 ignoreGapsInConsensus = state;
1159 public boolean getIgnoreGapsConsensus()
1161 return ignoreGapsInConsensus;
1165 * @param showSequenceLogo
1166 * indicates if a sequence logo is shown for consensus annotation
1168 public void setshowSequenceLogo(boolean showSequenceLogo)
1170 // TODO: decouple calculation from settings update
1171 if (this.showSequenceLogo != showSequenceLogo && consensus != null)
1173 this.showSequenceLogo = showSequenceLogo;
1174 recalcConservation();
1176 this.showSequenceLogo = showSequenceLogo;
1181 * @param showConsHist
1182 * flag indicating if the consensus histogram for this group should
1185 public void setShowConsensusHistogram(boolean showConsHist)
1188 if (showConsensusHistogram != showConsHist && consensus != null)
1190 this.showConsensusHistogram = showConsHist;
1191 recalcConservation();
1193 this.showConsensusHistogram = showConsHist;
1197 * @return the showConsensusHistogram
1199 public boolean isShowConsensusHistogram()
1201 return showConsensusHistogram;
1205 * set flag indicating if logo should be normalised when rendered
1209 public void setNormaliseSequenceLogo(boolean norm)
1211 normaliseSequenceLogo = norm;
1214 public boolean isNormaliseSequenceLogo()
1216 return normaliseSequenceLogo;
1221 * returns a new array with all annotation involving this group
1223 public AlignmentAnnotation[] getAlignmentAnnotation()
1225 // TODO add in other methods like 'getAlignmentAnnotation(String label),
1227 ArrayList<AlignmentAnnotation> annot = new ArrayList<AlignmentAnnotation>();
1228 for (SequenceI seq : sequences)
1230 AlignmentAnnotation[] aa = seq.getAnnotation();
1233 for (AlignmentAnnotation al : aa)
1235 if (al.groupRef == this)
1242 if (consensus != null)
1244 annot.add(consensus);
1246 if (conservation != null)
1248 annot.add(conservation);
1250 return annot.toArray(new AlignmentAnnotation[0]);
1254 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1256 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1257 for (AlignmentAnnotation a : getAlignmentAnnotation())
1259 if (a.getCalcId() == calcId)
1268 * Returns a list of annotations that match the specified sequenceRef, calcId
1269 * and label, ignoring null values.
1271 * @return list of AlignmentAnnotation objects
1274 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1275 String calcId, String label)
1277 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1278 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1280 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1281 && ann.sequenceRef != null && ann.sequenceRef == seq
1282 && ann.label != null && ann.label.equals(label))
1291 * Answer true if any annotation matches the calcId passed in (if not null).
1296 public boolean hasAnnotation(String calcId)
1298 if (calcId != null && !"".equals(calcId))
1300 for (AlignmentAnnotation a : getAlignmentAnnotation())
1302 if (a.getCalcId() == calcId)
1316 private AnnotatedCollectionI context;
1319 * set the alignment or group context for this group
1323 public void setContext(AnnotatedCollectionI context)
1325 this.context = context;
1331 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1334 public AnnotatedCollectionI getContext()