JAL-2349 JAL-3855 ContactListI.getPosition() to recover original position passed...
[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 getPosition()
91       {
92         return p;
93       }
94
95       @Override
96       public int getContactHeight()
97       {
98         return width;
99
100       }
101
102       @Override
103       public double getContactAt(int column)
104       {
105         List<Float> clist;
106         Float cl = null;
107         if (symmetric)
108         {
109           if (p < column)
110           {
111             clist = contacts.get(p);
112             cl = clist.get(column);
113           }
114           else
115           {
116             clist = contacts.get(column);
117             cl = clist.get(p);
118           }
119         }
120         else
121         {
122           clist = contacts.get(p);
123           cl = clist.get(column);
124         }
125         if (cl == null)
126         {
127           // return 0 not NaN ?
128           return Double.NaN;
129         }
130         return cl.doubleValue();
131       }
132     });
133   }
134
135   @Override
136   public float getMin()
137   {
138     return min;
139   }
140
141   @Override
142   public float getMax()
143   {
144     return max;
145   }
146
147   @Override
148   public boolean hasReferenceSeq()
149   {
150     // TODO Auto-generated method stub
151     return false;
152   }
153
154   @Override
155   public SequenceI getReferenceSeq()
156   {
157     // TODO Auto-generated method stub
158     return null;
159   }
160 }