JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / models / templates / Benchmark.java
1 package fr.orsay.lri.varna.models.templates;
2
3 import java.awt.geom.Line2D;
4 import java.util.Arrays;
5
6 import fr.orsay.lri.varna.models.geom.LinesIntersect;
7 import fr.orsay.lri.varna.models.rna.RNA;
8
9 /**
10  * @author Raphael Champeimont
11  *
12  */
13 public class Benchmark {
14         private RNA rna;
15         
16         // number of backbone crossings
17         public int backboneCrossings;
18         // average distance between unpaired consecutive bases
19         public double averageUnpairedDistance;
20         // median distance between consecutive bases
21         public double medianConsecutiveBaseDistance;
22         // Number of consecutive bases that are too near from each other
23         public int tooNearConsecutiveBases;
24         // Number of consecutive bases that are too far from each other
25         public int tooFarConsecutiveBases;
26         
27         
28         
29         // Parameters
30         public double targetConsecutiveBaseDistance = RNA.LOOP_DISTANCE;
31         // If consecutive bases are nearer that tooNearFactor * target distance, we call them too near.
32         public double tooNearFactor = 0.5;
33         // If consecutive bases are further that tooFarFactor * target distance, we call them too far.
34         public double tooFarFactor = 2;
35         
36         
37         
38         public Benchmark(RNA rna) {
39                 this.rna = rna;
40                 computeAll();
41         }
42         
43         private void computeAll() {
44                 // Compute number of backbone crossings
45                 {
46                         int n = rna.getSize();
47                         Line2D.Double[] lines = new Line2D.Double[n-1];
48                         for (int i=0; i<n-1; i++) {
49                                 lines[i] = new Line2D.Double(rna.getCoords(i), rna.getCoords(i+1));
50                         }
51                         int intersectLines = 0;
52                         for (int i=0; i<n-1; i++) {
53                                 for (int j=i+2; j<n-1; j++) {
54                                         if (LinesIntersect.linesIntersect(lines[i], lines[j])) {
55                                                 intersectLines++;
56                                         }
57                                 }
58                         }
59                         backboneCrossings = intersectLines;
60                 }
61                 
62                 // Stats about distances between consecutive bases not both part of an helix
63                 {
64                         int n = rna.getSize();
65                         double sum = 0;
66                         int count = 0;
67                         for (int i=0; i<n-1; i++) {
68                                 int indexOfAssociatedBase1 = rna.getBaseAt(i).getElementStructure();
69                                 int indexOfAssociatedBase2 = rna.getBaseAt(i+1).getElementStructure();
70                                 if (indexOfAssociatedBase1 != -1 || indexOfAssociatedBase2 != -1) {
71                                         // If they are not both associated (ie. not part of an helix)
72                                         sum += rna.getBaseAt(i).getCoords().distance(rna.getBaseAt(i+1).getCoords());
73                                         count++;
74                                 }
75                         }
76                         averageUnpairedDistance = sum / count;
77                 }
78                 
79                 // Stats about base distances
80                 {
81                         int n = rna.getSize();
82                         double distances[] = new double[n-1];
83                         for (int i=0; i<n-1; i++) {
84                                 // If they are not both associated (ie. not part of an helix)
85                                 distances[i] = rna.getBaseAt(i).getCoords().distance(rna.getBaseAt(i+1).getCoords());
86                         }
87                         Arrays.sort(distances);
88                         double median = distances[distances.length/2];
89                         medianConsecutiveBaseDistance = median;
90                         
91                         // We take the median as target distance, and count
92                         // the number of consecutive bases with a distance too different
93                         tooNearConsecutiveBases = 0;
94                         tooFarConsecutiveBases = 0;
95                         for (int i=0; i<n-1; i++) {
96                                 if (distances[i] < tooNearFactor*targetConsecutiveBaseDistance) {
97                                         tooNearConsecutiveBases++;
98                                 }
99                                 if (distances[i] > tooFarFactor*targetConsecutiveBaseDistance) {
100                                         tooFarConsecutiveBases++;
101                                 }
102                         }
103                 }
104         }
105         
106         @SuppressWarnings("unused")
107         private int percent(int a, int b) {
108                 return (int) Math.round((double) a / (double) b * 100.0);
109         }
110
111         public void printAll() {
112                 System.out.println("Benchmark:");
113                 System.out.println("\tBackbone crossings = " + backboneCrossings);
114                 System.out.println("\tAverage unpaired distance = " + averageUnpairedDistance);
115                 System.out.println("\tMedian of consecutive base distance = " + medianConsecutiveBaseDistance);
116                 System.out.println("\tNumber of too near consecutive bases = " + tooNearConsecutiveBases); // + " ie. " + percent(tooNearConsecutiveBases, rna.getSize()-1) + " %");
117                 System.out.println("\tNumber of too far consecutive bases = " + tooFarConsecutiveBases); // + " ie. " + percent(tooFarConsecutiveBases, rna.getSize()-1) + " %");
118         }
119 }
120