2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.StringTokenizer;
27 import jalview.bin.Console;
29 public abstract class ContactMatrix extends GroupSetHolder
30 implements ContactMatrixI
33 * are contacts reflexive ?
35 boolean symmetric = true;
37 public ContactMatrix(boolean symmetric)
39 this.symmetric = symmetric;
42 List<List<Float>> contacts = null;
44 int width = 0, numcontacts = 0;
46 float min = 0f, max = 0f;
48 public void addContact(int left, int right, float strength)
50 if (left < 0 || right < 0)
52 throw new Error(new RuntimeException(
53 "Cannot have negative indices for contact left=" + left
54 + " right=" + right + " strength=" + strength));
68 // TODO: use sparse list for efficiency ?
69 contacts = new ArrayList<List<Float>>();
71 List<Float> clist = contacts.get(left);
74 clist = new ArrayList<Float>();
75 contacts.set(left, clist);
77 Float last = clist.set(right, strength);
78 // TODO: if last is non null, may need to recompute range
79 checkBounds(strength);
86 private void checkBounds(float strength)
99 public ContactListI getContactList(final int column)
101 if (column < 0 || column >= width)
106 return new ContactListImpl(new ContactListProviderI()
111 public int getPosition()
117 public int getContactHeight()
124 public double getContactAt(int column)
126 Float cl = getFloatElementAt(column, p);
129 // return 0 not NaN ?
132 return cl.doubleValue();
137 private Float getFloatElementAt(int column, int p)
146 clist = contacts.get(p);
147 cl = clist.get(column);
151 clist = contacts.get(column);
157 clist = contacts.get(p);
158 cl = clist.get(column);
164 public double getElementAt(int column, int row)
166 Float cl = getFloatElementAt(column, row);
171 throw (new RuntimeException("Out of Bounds " + column + "," + row));
175 public float getMin()
181 public float getMax()
187 public String getAnnotLabel()
189 return "Contact Matrix";
193 public String getAnnotDescr()
195 return "Contact Matrix";
198 public static String contactToFloatString(ContactMatrixI cm)
200 StringBuilder sb = new StringBuilder();
201 for (int c = 0; c < cm.getWidth(); c++)
203 ContactListI cl = cm.getContactList(c);
207 for (int h = 0; h <= cl.getContactHeight(); h++)
211 if (sb.length() - lastsb > 320)
215 lastsb = sb.length();
222 sb.append(cl.getContactAt(h));
226 return sb.toString();
229 public static float[][] fromFloatStringToContacts(String values, int cols,
232 float[][] vals = new float[cols][rows];
233 StringTokenizer tabsep = new StringTokenizer(values, "" + '\t' + '\n');
235 while (tabsep.hasMoreTokens())
237 double elem = Double.valueOf(tabsep.nextToken());
238 vals[c][r++] = (float) elem;
239 if (r >= vals[c].length)
244 if (c >= vals.length)
249 if (tabsep.hasMoreElements())
252 "Ignoring additional elements for Float string to contact matrix parsing.");