Merge branch 'develop' (JAL-4102 2.11.2.6 patch release) into features/r2_11_2_alphaf...
[jalview.git] / src / jalview / datamodel / ContactListImpl.java
1 package jalview.datamodel;
2
3 /**
4  * helper class to compute min/max/mean for a range on a contact list
5  * 
6  * @author jprocter
7  *
8  */
9 public class ContactListImpl implements ContactListI
10 {
11   ContactListProviderI clist;
12
13   public static ContactListI newContactList(ContactListProviderI list)
14   {
15     return new ContactListImpl(list);
16   }
17
18   public ContactListImpl(ContactListProviderI list)
19   {
20     clist = list;
21   }
22
23   @Override
24   public int getPosition()
25   {
26     return clist.getPosition();
27   }
28
29   @Override
30   public double getContactAt(int column)
31   {
32     return clist.getContactAt(column);
33   }
34
35   @Override
36   public int getContactHeight()
37   {
38     return clist.getContactHeight();
39   }
40
41   @Override
42   public ContactRange getRangeFor(int from_column, int to_column)
43   {
44     if (clist instanceof ContactListI)
45     {
46       // clist may implement getRangeFor in a more efficient way, so use theirs
47       return ((ContactListI) clist).getRangeFor(from_column, to_column);
48     }
49     if (from_column < 0)
50     {
51       from_column = 0;
52     }
53     if (to_column > getContactHeight())
54     {
55       to_column = getContactHeight();
56     }
57     ContactRange cr = new ContactRange();
58     cr.setFrom_column(from_column);
59     cr.setTo_column(to_column);
60     double tot = 0;
61     for (int i = from_column; i <= to_column; i++)
62     {
63       double contact = getContactAt(i);
64       tot += contact;
65       if (i == from_column)
66       {
67         cr.setMin(contact);
68         cr.setMax(contact);
69         cr.setMinPos(i);
70         cr.setMaxPos(i);
71       }
72       else
73       {
74         if (cr.getMax() < contact)
75         {
76           cr.setMax(contact);
77           cr.setMaxPos(i);
78         }
79         if (cr.getMin() < contact)
80         {
81           cr.setMin(contact);
82           cr.setMinPos(i);
83         }
84       }
85     }
86     if (tot > 0)
87     {
88       cr.setMean(tot / (1 + to_column - from_column));
89     }
90     else
91     {
92       cr.setMean(tot);
93     }
94     return cr;
95   }
96
97 }