View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.numerics.clustering.visualization;
39  
40  import ffx.numerics.clustering.Cluster;
41  import ffx.numerics.clustering.ClusteringAlgorithm;
42  import ffx.numerics.clustering.CompleteLinkageStrategy;
43  import ffx.numerics.clustering.LinkageStrategy;
44  import ffx.numerics.clustering.PDistClusteringAlgorithm;
45  
46  import javax.swing.JFrame;
47  import javax.swing.JPanel;
48  import javax.swing.WindowConstants;
49  import java.awt.BorderLayout;
50  import java.awt.Color;
51  import java.awt.Frame;
52  
53  /**
54   * Simple Swing JFrame that hosts a DendrogramPanel to visualize a clustering
55   * result. Provides a demo main method to render example dendrograms.
56   *
57   * @author Lars Behnke, 2013
58   * @author Michael J. Schnieders
59   * @since 1.0
60   */
61  public class DendrogramFrame extends JFrame {
62  
63    /**
64     * Creates a frame displaying a dendrogram for the provided clustering result.
65     *
66     * @param cluster the root Cluster to visualize
67     */
68    public DendrogramFrame(Cluster cluster) {
69      setSize(500, 400);
70      setLocation(100, 200);
71      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
72  
73      JPanel content = new JPanel();
74      DendrogramPanel dp = new DendrogramPanel();
75  
76      setContentPane(content);
77      content.setBackground(Color.red);
78      content.setLayout(new BorderLayout());
79      content.add(dp, BorderLayout.CENTER);
80      dp.setBackground(Color.WHITE);
81      dp.setLineColor(Color.BLACK);
82      dp.setScaleValueDecimals(0);
83      dp.setScaleValueInterval(1);
84      dp.setShowDistances(false);
85  
86      dp.setModel(cluster);
87      setVisible(true);
88    }
89  
90    /**
91     * Demo entry point that creates two frames with example dendrograms.
92     *
93     * @param args CLI arguments (unused)
94     */
95    public static void main(String[] args) {
96      LinkageStrategy strategy = new CompleteLinkageStrategy();
97      Frame f1 = new DendrogramFrame(createSampleCluster(strategy));
98      f1.setSize(500, 400);
99      f1.setLocation(100, 200);
100     Frame f2 = new DendrogramFrame(createSampleCluster2(strategy));
101     f2.setSize(500, 400);
102     f2.setLocation(600, 200);
103   }
104 
105   /**
106    * Creates a small sample Cluster for demonstration purposes.
107    *
108    * @param strategy the LinkageStrategy used by the clustering algorithm
109    * @return a sample root Cluster
110    */
111   private static Cluster createSampleCluster(LinkageStrategy strategy) {
112     double[][] distances = new double[][]{
113         {1, 9, 7, 11, 14, 4, 3, 8, 10, 9, 2, 8, 6, 13, 10}
114     };
115     String[] names = new String[]{"O1", "O2", "O3", "O4", "O5", "O6"};
116     ClusteringAlgorithm alg = new PDistClusteringAlgorithm();
117     Cluster cluster = alg.performClustering(distances, names, strategy);
118     cluster.toConsole(0);
119     return cluster;
120   }
121 
122   /**
123    * Creates a second sample Cluster for demonstration purposes.
124    *
125    * @param strategy the LinkageStrategy used by the clustering algorithm
126    * @return a sample root Cluster
127    */
128   private static Cluster createSampleCluster2(LinkageStrategy strategy) {
129     double[][] distances = new double[][]{
130         {1, 9, 7, 11, 14, 12, 4, 3, 8, 10, 12, 9, 2, 8, 9, 6, 13, 11, 10, 7, 2}
131     };
132     String[] names = new String[]{"O1", "O2", "O3", "O4", "O5", "O6", "07"};
133     ClusteringAlgorithm alg = new PDistClusteringAlgorithm();
134     Cluster cluster = alg.performClustering(distances, names, strategy);
135     cluster.toConsole(0);
136     return cluster;
137   }
138 }