1 package jalview.datamodel;
4 import java.util.ArrayList;
5 import java.util.BitSet;
6 import java.util.HashMap;
8 import java.util.StringTokenizer;
10 import jalview.bin.Console;
12 public abstract class ContactMatrix implements ContactMatrixI
15 * are contacts reflexive ?
17 boolean symmetric = true;
19 public ContactMatrix(boolean symmetric)
21 this.symmetric = symmetric;
24 List<List<Float>> contacts = null;
26 int width = 0, numcontacts = 0;
28 float min = 0f, max = 0f;
30 public void addContact(int left, int right, float strength)
32 if (left < 0 || right < 0)
34 throw new Error(new RuntimeException(
35 "Cannot have negative indices for contact left=" + left
36 + " right=" + right + " strength=" + strength));
50 // TODO: use sparse list for efficiency ?
51 contacts = new ArrayList<List<Float>>();
53 List<Float> clist = contacts.get(left);
56 clist = new ArrayList<Float>();
57 contacts.set(left, clist);
59 Float last = clist.set(right, strength);
60 // TODO: if last is non null, may need to recompute range
61 checkBounds(strength);
68 private void checkBounds(float strength)
81 public ContactListI getContactList(final int column)
83 if (column < 0 || column >= width)
88 return new ContactListImpl(new ContactListProviderI()
93 public int getPosition()
99 public int getContactHeight()
106 public double getContactAt(int column)
114 clist = contacts.get(p);
115 cl = clist.get(column);
119 clist = contacts.get(column);
125 clist = contacts.get(p);
126 cl = clist.get(column);
130 // return 0 not NaN ?
133 return cl.doubleValue();
139 public float getMin()
145 public float getMax()
151 public boolean hasReferenceSeq()
153 // TODO Auto-generated method stub
158 public SequenceI getReferenceSeq()
160 // TODO Auto-generated method stub
165 public String getAnnotLabel()
167 return "Contact Matrix";
171 public String getAnnotDescr()
173 return "Contact Matrix";
176 List<BitSet> groups = null;
179 public void updateGroups(List<BitSet> colGroups)
182 colorMap = new HashMap<>();
186 public boolean hasGroups()
188 return groups != null && groups.size() > 0;
192 public List<BitSet> getGroups()
198 public BitSet getGroupsFor(int column)
200 for (BitSet gp : groups)
207 return ContactMatrixI.super.getGroupsFor(column);
210 HashMap<BitSet, Color> colorMap = new HashMap<>();
213 public Color getColourForGroup(BitSet bs)
219 Color groupCol = colorMap.get(bs);
220 if (groupCol == null)
228 public void setColorForGroup(BitSet bs, Color color)
230 colorMap.put(bs, color);
233 public static String contactToFloatString(ContactMatrixI cm)
235 StringBuilder sb = new StringBuilder();
236 for (int c = 0; c < cm.getWidth(); c++)
238 ContactListI cl = cm.getContactList(c);
241 for (int h = 0; h <= cl.getContactHeight(); h++)
247 sb.append(cl.getContactAt(h));
251 return sb.toString();
254 public static float[][] fromFloatStringToContacts(String values, int cols,
257 float[][] vals = new float[cols][rows];
258 StringTokenizer tabsep = new StringTokenizer(values, "" + '\t');
261 while (tabsep.hasMoreTokens())
263 double elem = Double.valueOf(tabsep.nextToken());
264 vals[c][r++] = (float) elem;
265 if (r >= vals[c].length)
270 if (c >= vals.length)
276 if (tabsep.hasMoreElements())
279 "Ignoring additional elements for Float string to contact matrix parsing.");