package jalview.datamodel; import jalview.ws.params.InvalidArgumentException; import java.util.ArrayList; import java.util.List; public class ContactMatrix implements ContactMatrixI { /** * are contacts reflexive ? */ boolean symmetric = true; public ContactMatrix(boolean symmetric) { this.symmetric = symmetric; } List> contacts = null; int width = 0, numcontacts = 0; float min = 0f, max = 0f; public void addContact(int left, int right, float strength) { if (left < 0 || right < 0) { throw new Error(new InvalidArgumentException( "Cannot have negative indices for contact left=" + left + " right=" + right + " strength=" + strength)); } if (symmetric) { if (left > right) { // swap int r = right; right = left; left = r; } } if (contacts == null) { // TODO: use sparse list for efficiency ? contacts = new ArrayList>(); } List clist = contacts.get(left); if (clist == null) { clist = new ArrayList(); contacts.set(left, clist); } Float last = clist.set(right, strength); // TODO: if last is non null, may need to recompute range checkBounds(strength); if (last == null) { numcontacts++; } } private void checkBounds(float strength) { if (min > strength) { min = strength; } if (max < strength) { max = strength; } } @Override public ContactListI getContactList(final int column) { if (column < 0 || column >= width) { return null; } return new ContactListImpl(new ContactListProviderI() { int p = column; @Override public int getPosition() { return p; } @Override public int getContactHeight() { return width; } @Override public double getContactAt(int column) { List clist; Float cl = null; if (symmetric) { if (p < column) { clist = contacts.get(p); cl = clist.get(column); } else { clist = contacts.get(column); cl = clist.get(p); } } else { clist = contacts.get(p); cl = clist.get(column); } if (cl == null) { // return 0 not NaN ? return Double.NaN; } return cl.doubleValue(); } }); } @Override public float getMin() { return min; } @Override public float getMax() { return max; } @Override public boolean hasReferenceSeq() { // TODO Auto-generated method stub return false; } @Override public SequenceI getReferenceSeq() { // TODO Auto-generated method stub return null; } }