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