1 package jalview.datamodel;
4 import java.math.BigInteger;
5 import java.util.ArrayList;
6 import java.util.BitSet;
7 import java.util.HashMap;
9 import java.util.Spliterator;
10 import java.util.StringTokenizer;
12 import jalview.bin.Console;
14 public abstract class ContactMatrix implements ContactMatrixI
17 * are contacts reflexive ?
19 boolean symmetric = true;
21 public ContactMatrix(boolean symmetric)
23 this.symmetric = symmetric;
26 List<List<Float>> contacts = null;
28 int width = 0, numcontacts = 0;
30 float min = 0f, max = 0f;
32 public void addContact(int left, int right, float strength)
34 if (left < 0 || right < 0)
36 throw new Error(new RuntimeException(
37 "Cannot have negative indices for contact left=" + left
38 + " right=" + right + " strength=" + strength));
52 // TODO: use sparse list for efficiency ?
53 contacts = new ArrayList<List<Float>>();
55 List<Float> clist = contacts.get(left);
58 clist = new ArrayList<Float>();
59 contacts.set(left, clist);
61 Float last = clist.set(right, strength);
62 // TODO: if last is non null, may need to recompute range
63 checkBounds(strength);
70 private void checkBounds(float strength)
83 public ContactListI getContactList(final int column)
85 if (column < 0 || column >= width)
90 return new ContactListImpl(new ContactListProviderI()
95 public int getPosition()
101 public int getContactHeight()
108 public double getContactAt(int column)
116 clist = contacts.get(p);
117 cl = clist.get(column);
121 clist = contacts.get(column);
127 clist = contacts.get(p);
128 cl = clist.get(column);
132 // return 0 not NaN ?
135 return cl.doubleValue();
141 public float getMin()
147 public float getMax()
153 public boolean hasReferenceSeq()
155 // TODO Auto-generated method stub
160 public SequenceI getReferenceSeq()
162 // TODO Auto-generated method stub
167 public String getAnnotLabel()
169 return "Contact Matrix";
173 public String getAnnotDescr()
175 return "Contact Matrix";
177 List<BitSet> groups=null;
179 public void updateGroups(List<BitSet> colGroups)
182 colorMap=new HashMap<>();
185 public boolean hasGroups()
187 return groups!=null && groups.size()>0;
190 public List<BitSet> getGroups()
195 public BitSet getGroupsFor(int column)
197 for (BitSet gp:groups) {
203 return ContactMatrixI.super.getGroupsFor(column);
205 HashMap<BitSet,Color> colorMap = new HashMap<>();
207 public Color getColourForGroup(BitSet bs)
212 Color groupCol=colorMap.get(bs);
220 public void setColorForGroup(BitSet bs,Color color)
222 colorMap.put(bs,color);
224 public static String contactToFloatString(ContactMatrixI cm)
226 StringBuilder sb = new StringBuilder();
227 for (int c=0;c<cm.getWidth();c++)
229 ContactListI cl=cm.getContactList(c);
231 for (int h=0;h<=cl.getContactHeight();h++)
236 sb.append(cl.getContactAt(h));
240 return sb.toString();
243 public static float[][] fromFloatStringToContacts(String values, int cols,
246 float[][] vals = new float[cols][rows];
247 StringTokenizer tabsep = new StringTokenizer(values,""+'\t');
250 while (tabsep.hasMoreTokens())
252 double elem = Double.valueOf(tabsep.nextToken());
253 vals[c][r++]=(float) elem;
254 if (r>=vals[c].length)
265 if (tabsep.hasMoreElements())
267 Console.warn("Ignoring additional elements for Float string to contact matrix parsing.");