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 java.awt.Color;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.BitSet;
27 import java.util.HashMap;
28 import java.util.List;
30 import jalview.analysis.AverageDistanceEngine;
31 import jalview.bin.Console;
33 public class GroupSet implements GroupSetI
35 List<BitSet> groups = Arrays.asList();
37 public GroupSet(GroupSet grps)
40 colorMap = new HashMap<BitSet, Color>(grps.colorMap);
41 groups = new ArrayList<BitSet>(grps.groups);
44 treeType = grps.treeType;
49 // TODO Auto-generated constructor stub
52 public GroupSet(boolean abs2, float thresh2, List<BitSet> groups2,
53 String treeType2, String newick2)
63 public boolean hasGroups()
65 return groups != null;
71 public String getNewick()
77 public boolean hasTree()
79 return newick != null && newick.length() > 0;
86 String treeType = null;
89 public void updateGroups(List<BitSet> colGroups)
91 if (colGroups != null)
98 public BitSet getGroupsFor(int column)
102 for (BitSet gp : groups)
110 // return singleton set;
111 BitSet bs = new BitSet();
116 HashMap<BitSet, Color> colorMap = new HashMap<>();
119 public Color getColourForGroup(BitSet bs)
125 Color groupCol = colorMap.get(bs);
126 if (groupCol == null)
134 public void setColorForGroup(BitSet bs, Color color)
136 colorMap.put(bs, color);
140 public void restoreGroups(List<BitSet> newgroups, String treeMethod,
141 String tree, double thresh2)
143 treeType = treeMethod;
151 public boolean hasCutHeight()
153 return groups != null && thresh != 0;
157 public double getCutHeight()
163 public String getTreeMethod()
168 public static GroupSet makeGroups(ContactMatrixI matrix, boolean autoCut)
170 return makeGroups(matrix, autoCut, 0, autoCut);
173 public static GroupSet makeGroups(ContactMatrixI matrix, boolean auto,
174 float thresh, boolean abs)
176 AverageDistanceEngine clusterer = new AverageDistanceEngine(null, null,
178 double height = clusterer.findHeight(clusterer.getTopNode());
179 Console.debug("Column tree height: " + height);
180 String newick = new jalview.io.NewickFile(clusterer.getTopNode(), false,
182 String treeType = "UPGMA";
183 Console.trace("Newick string\n" + newick);
185 List<BinaryNode> nodegroups;
191 BinaryNode bn = clusterer.getTopNode();
192 while (p-- > 0 & bn.left() != null)
194 if (bn.left() != null)
198 if (bn.left() != null)
203 thresh = Math.max((float) (rootw / height) - 0.01f, 0);
205 nodegroups = clusterer.groupNodes(thresh);
209 if (abs ? (height > thresh) : (0 < thresh && thresh < 1))
211 cut = abs ? thresh : (float) (thresh * height);
212 Console.debug("Threshold " + cut + " for height=" + height);
213 nodegroups = clusterer.groupNodes(cut);
217 nodegroups = new ArrayList<BinaryNode>();
218 nodegroups.add(clusterer.getTopNode());
222 List<BitSet> groups = new ArrayList<>();
223 for (BinaryNode root : nodegroups)
225 BitSet gpset = new BitSet();
226 for (BinaryNode leaf : clusterer.findLeaves(root))
228 gpset.set((Integer) leaf.element());
232 GroupSet grps = new GroupSet(abs, (cut == -1f) ? thresh : cut, groups,
238 public List<BitSet> getGroups()