7cd9dc9a693543e8d5188909ee2b083ed1ef7f62
[jalview.git] / src / jalview / datamodel / ContactListImpl.java
1 package jalview.datamodel;
2
3 import java.awt.Color;
4
5 import jalview.renderer.ContactGeometry.contactInterval;
6
7 /**
8  * helper class to compute min/max/mean for a range on a contact list
9  * 
10  * @author jprocter
11  *
12  */
13 public class ContactListImpl implements ContactListI
14 {
15   ContactListProviderI clist;
16
17   public static ContactListI newContactList(ContactListProviderI list)
18   {
19     return new ContactListImpl(list);
20   }
21
22   public ContactListImpl(ContactListProviderI list)
23   {
24     clist = list;
25   }
26
27   @Override
28   public int getPosition()
29   {
30     return clist.getPosition();
31   }
32
33   @Override
34   public double getContactAt(int column)
35   {
36     return clist.getContactAt(column);
37   }
38
39   @Override
40   public int getContactHeight()
41   {
42     return clist.getContactHeight();
43   }
44
45   @Override
46   public ContactRange getRangeFor(int from_column, int to_column)
47   {
48     // TODO: consider caching ContactRange for a particular call ?
49     if (clist instanceof ContactListI)
50     {
51       // clist may implement getRangeFor in a more efficient way, so use theirs
52       return ((ContactListI) clist).getRangeFor(from_column, to_column);
53     }
54     if (from_column < 0)
55     {
56       from_column = 0;
57     }
58     if (to_column >= getContactHeight())
59     {
60       to_column = getContactHeight() - 1;
61     }
62     ContactRange cr = new ContactRange();
63     cr.setFrom_column(from_column);
64     cr.setTo_column(to_column);
65     double tot = 0;
66     for (int i = from_column; i <= to_column; i++)
67     {
68       double contact = getContactAt(i);
69       tot += contact;
70       if (i == from_column)
71       {
72         cr.setMin(contact);
73         cr.setMax(contact);
74         cr.setMinPos(i);
75         cr.setMaxPos(i);
76       }
77       else
78       {
79         if (cr.getMax() < contact)
80         {
81           cr.setMax(contact);
82           cr.setMaxPos(i);
83         }
84         if (cr.getMin() < contact)
85         {
86           cr.setMin(contact);
87           cr.setMinPos(i);
88         }
89       }
90     }
91     if (tot > 0 && to_column > from_column)
92     {
93       cr.setMean(tot / (1 + to_column - from_column));
94     }
95     else
96     {
97       cr.setMean(tot);
98     }
99     return cr;
100   }
101
102   @Override
103   public int[] getMappedPositionsFor(int cStart, int cEnd)
104   {
105     return clist.getMappedPositionsFor(cStart, cEnd);
106   }
107
108   @Override
109   public Color getColourForGroup()
110   {
111     return clist.getColourForGroup();
112   }
113 }