1 package jalview.datamodel;
3 import java.util.ArrayList;
5 import java.util.StringTokenizer;
7 import jalview.bin.Console;
9 public abstract class ContactMatrix implements ContactMatrixI
12 * are contacts reflexive ?
14 boolean symmetric = true;
16 public ContactMatrix(boolean symmetric)
18 this.symmetric = symmetric;
21 List<List<Float>> contacts = null;
23 int width = 0, numcontacts = 0;
25 float min = 0f, max = 0f;
27 public void addContact(int left, int right, float strength)
29 if (left < 0 || right < 0)
31 throw new Error(new RuntimeException(
32 "Cannot have negative indices for contact left=" + left
33 + " right=" + right + " strength=" + strength));
47 // TODO: use sparse list for efficiency ?
48 contacts = new ArrayList<List<Float>>();
50 List<Float> clist = contacts.get(left);
53 clist = new ArrayList<Float>();
54 contacts.set(left, clist);
56 Float last = clist.set(right, strength);
57 // TODO: if last is non null, may need to recompute range
58 checkBounds(strength);
65 private void checkBounds(float strength)
78 public ContactListI getContactList(final int column)
80 if (column < 0 || column >= width)
85 return new ContactListImpl(new ContactListProviderI()
90 public int getPosition()
96 public int getContactHeight()
103 public double getContactAt(int column)
111 clist = contacts.get(p);
112 cl = clist.get(column);
116 clist = contacts.get(column);
122 clist = contacts.get(p);
123 cl = clist.get(column);
127 // return 0 not NaN ?
130 return cl.doubleValue();
136 public float getMin()
142 public float getMax()
148 public boolean hasReferenceSeq()
150 // TODO Auto-generated method stub
155 public SequenceI getReferenceSeq()
157 // TODO Auto-generated method stub
162 public String getAnnotLabel()
164 return "Contact Matrix";
168 public String getAnnotDescr()
170 return "Contact Matrix";
173 public static String contactToFloatString(ContactMatrixI cm)
175 StringBuilder sb = new StringBuilder();
176 for (int c = 0; c < cm.getWidth(); c++)
178 ContactListI cl = cm.getContactList(c);
181 for (int h = 0; h <= cl.getContactHeight(); h++)
187 sb.append(cl.getContactAt(h));
191 return sb.toString();
194 public static float[][] fromFloatStringToContacts(String values, int cols,
197 float[][] vals = new float[cols][rows];
198 StringTokenizer tabsep = new StringTokenizer(values, "" + '\t');
201 while (tabsep.hasMoreTokens())
203 double elem = Double.valueOf(tabsep.nextToken());
204 vals[c][r++] = (float) elem;
205 if (r >= vals[c].length)
210 if (c >= vals.length)
216 if (tabsep.hasMoreElements())
219 "Ignoring additional elements for Float string to contact matrix parsing.");