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;
39  
40  import ffx.numerics.clustering.visualization.DendrogramPanel;
41  import ffx.utilities.FFXTest;
42  
43  import javax.swing.*;
44  import java.awt.*;
45  import java.io.BufferedReader;
46  import java.io.IOException;
47  import java.io.InputStreamReader;
48  import java.util.ArrayList;
49  import java.util.List;
50  
51  /**
52   * @author Lars Behnke (lars.behnke@bruker.com)
53   */
54  public class CoordTest extends FFXTest {
55  
56      private static Cluster importCluster() throws IOException {
57          List<Coord> coords = readCoordinates();
58  
59          double[][] distances = new double[coords.size()][coords.size()];
60          String[] names = new String[coords.size()];
61          for (int row = 0; row < coords.size(); row++) {
62              Coord coord1 = coords.get(row);
63              for (int col = row+1; col < coords.size(); col++) {
64                  Coord coord2 = coords.get(col);
65                  double d = Math.sqrt(Math.pow(coord2.getX()-coord1.getX(), 2)+ Math.pow(coord2.getY()-coord1.getY(), 2));
66                  distances[row][col] = d;
67                  distances[col][row] = d;
68              }
69              names[row] = ""+row;
70          }
71          ClusteringAlgorithm alg = new DefaultClusteringAlgorithm();
72          Cluster cluster = alg.performClustering(distances, names,
73                  new AverageLinkageStrategy());
74          return cluster;
75      }
76  
77  
78      public static void main(String[] args) throws Exception {
79          JFrame frame = new JFrame();
80          frame.setSize(1024, 768);
81          frame.setLocation(400, 300);
82          frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
83  
84          JPanel content = new JPanel();
85          DendrogramPanel dp = new DendrogramPanel();
86  
87          frame.setContentPane(content);
88          content.setBackground(Color.red);
89          content.setLayout(new BorderLayout());
90          content.add(dp, BorderLayout.CENTER);
91          dp.setBackground(Color.WHITE);
92          dp.setLineColor(Color.BLACK);
93          dp.setScaleValueDecimals(0);
94          dp.setScaleValueInterval(1);
95          dp.setShowDistances(false);
96  
97          Cluster cluster = importCluster();
98          dp.setModel(cluster);
99          frame.setVisible(true);
100     }
101 
102     private static List<Coord> readCoordinates() throws IOException {
103         List<Coord> coordList = new ArrayList<Coord>();
104         BufferedReader br = new BufferedReader(new InputStreamReader(CoordTest.class.getResourceAsStream("/testData1.txt")));
105         String line;
106 
107         while ((line = br.readLine()) != null) {
108             String[] elems = line.split(" ");
109             if (elems.length != 2) {
110                 continue;
111             }
112             int x;
113             int y;
114 
115             try {
116                 x = Integer.parseInt(elems[0]);
117                 y = Integer.parseInt(elems[1]);
118             } catch (Exception e) {
119                 continue;
120             }
121             coordList.add(new Coord(x, y));
122         }
123         return coordList;
124     }
125 
126     public static class Coord {
127         private double x;
128         private double y;
129         public Coord(double x, double y) {
130             this.x = x;
131             this.y = y;
132         }
133 
134         public double getX() {
135             return x;
136         }
137 
138         public double getY() {
139             return y;
140         }
141     }
142 }