1 package jalview.datamodel;
3 import java.util.ArrayList;
5 import java.util.StringTokenizer;
7 import jalview.bin.Console;
9 public abstract class ContactMatrix extends GroupSetHolder
10 implements ContactMatrixI
13 * are contacts reflexive ?
15 boolean symmetric = true;
17 public ContactMatrix(boolean symmetric)
19 this.symmetric = symmetric;
22 List<List<Float>> contacts = null;
24 int width = 0, numcontacts = 0;
26 float min = 0f, max = 0f;
28 public void addContact(int left, int right, float strength)
30 if (left < 0 || right < 0)
32 throw new Error(new RuntimeException(
33 "Cannot have negative indices for contact left=" + left
34 + " right=" + right + " strength=" + strength));
48 // TODO: use sparse list for efficiency ?
49 contacts = new ArrayList<List<Float>>();
51 List<Float> clist = contacts.get(left);
54 clist = new ArrayList<Float>();
55 contacts.set(left, clist);
57 Float last = clist.set(right, strength);
58 // TODO: if last is non null, may need to recompute range
59 checkBounds(strength);
66 private void checkBounds(float strength)
79 public ContactListI getContactList(final int column)
81 if (column < 0 || column >= width)
86 return new ContactListImpl(new ContactListProviderI()
91 public int getPosition()
97 public int getContactHeight()
104 public double getContactAt(int column)
106 Float cl = getFloatElementAt(column, p);
109 // return 0 not NaN ?
112 return cl.doubleValue();
117 private Float getFloatElementAt(int column, int p)
126 clist = contacts.get(p);
127 cl = clist.get(column);
131 clist = contacts.get(column);
137 clist = contacts.get(p);
138 cl = clist.get(column);
144 public double getElementAt(int column, int row)
146 Float cl = getFloatElementAt(column, row);
151 throw (new RuntimeException("Out of Bounds " + column + "," + row));
155 public float getMin()
161 public float getMax()
167 public String getAnnotLabel()
169 return "Contact Matrix";
173 public String getAnnotDescr()
175 return "Contact Matrix";
178 public static String contactToFloatString(ContactMatrixI cm)
180 StringBuilder sb = new StringBuilder();
181 for (int c = 0; c < cm.getWidth(); c++)
183 ContactListI cl = cm.getContactList(c);
187 for (int h = 0; h <= cl.getContactHeight(); h++)
191 if (sb.length() - lastsb > 320)
195 lastsb = sb.length();
202 sb.append(cl.getContactAt(h));
206 return sb.toString();
209 public static float[][] fromFloatStringToContacts(String values, int cols,
212 float[][] vals = new float[cols][rows];
213 StringTokenizer tabsep = new StringTokenizer(values, "" + '\t' + '\n');
215 while (tabsep.hasMoreTokens())
217 double elem = Double.valueOf(tabsep.nextToken());
218 vals[c][r++] = (float) elem;
219 if (r >= vals[c].length)
224 if (c >= vals.length)
229 if (tabsep.hasMoreElements())
232 "Ignoring additional elements for Float string to contact matrix parsing.");