JAL-2349 allow contact matrix to provide labels for its annotation row
[jalview.git] / src / jalview / datamodel / ContactMatrix.java
1 package jalview.datamodel;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class ContactMatrix implements ContactMatrixI
7 {
8   /**
9    * are contacts reflexive ?
10    */
11   boolean symmetric = true;
12
13   public ContactMatrix(boolean symmetric)
14   {
15     this.symmetric = symmetric;
16   }
17
18   List<List<Float>> contacts = null;
19
20   int width = 0, numcontacts = 0;
21
22   float min = 0f, max = 0f;
23
24   public void addContact(int left, int right, float strength)
25   {
26     if (left < 0 || right < 0)
27     {
28       throw new Error(new RuntimeException(
29               "Cannot have negative indices for contact left=" + left
30                       + " right=" + right + " strength=" + strength));
31     }
32     if (symmetric)
33     {
34       if (left > right)
35       {
36         // swap
37         int r = right;
38         right = left;
39         left = r;
40       }
41     }
42     if (contacts == null)
43     {
44       // TODO: use sparse list for efficiency ?
45       contacts = new ArrayList<List<Float>>();
46     }
47     List<Float> clist = contacts.get(left);
48     if (clist == null)
49     {
50       clist = new ArrayList<Float>();
51       contacts.set(left, clist);
52     }
53     Float last = clist.set(right, strength);
54     // TODO: if last is non null, may need to recompute range
55     checkBounds(strength);
56     if (last == null)
57     {
58       numcontacts++;
59     }
60   }
61
62   private void checkBounds(float strength)
63   {
64     if (min > strength)
65     {
66       min = strength;
67     }
68     if (max < strength)
69     {
70       max = strength;
71     }
72   }
73
74   @Override
75   public ContactListI getContactList(final int column)
76   {
77     if (column < 0 || column >= width)
78     {
79       return null;
80     }
81
82     return new ContactListImpl(new ContactListProviderI()
83     {
84       int p = column;
85
86       @Override
87       public int getPosition()
88       {
89         return p;
90       }
91
92       @Override
93       public int getContactHeight()
94       {
95         return width;
96
97       }
98
99       @Override
100       public double getContactAt(int column)
101       {
102         List<Float> clist;
103         Float cl = null;
104         if (symmetric)
105         {
106           if (p < column)
107           {
108             clist = contacts.get(p);
109             cl = clist.get(column);
110           }
111           else
112           {
113             clist = contacts.get(column);
114             cl = clist.get(p);
115           }
116         }
117         else
118         {
119           clist = contacts.get(p);
120           cl = clist.get(column);
121         }
122         if (cl == null)
123         {
124           // return 0 not NaN ?
125           return Double.NaN;
126         }
127         return cl.doubleValue();
128       }
129     });
130   }
131
132   @Override
133   public float getMin()
134   {
135     return min;
136   }
137
138   @Override
139   public float getMax()
140   {
141     return max;
142   }
143
144   @Override
145   public boolean hasReferenceSeq()
146   {
147     // TODO Auto-generated method stub
148     return false;
149   }
150
151   @Override
152   public SequenceI getReferenceSeq()
153   {
154     // TODO Auto-generated method stub
155     return null;
156   }
157
158   @Override
159   public String getAnnotLabel()
160   {
161     return "Contact Matrix";
162   }
163
164   @Override
165   public String getAnnotDescr()
166   {
167     return "Contact Matrix";
168   }
169 }