Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.datamodel

File ContactMatrix.java

 

Coverage histogram

../../img/srcFileCovDistChart4.png
49% of files have more coverage

Code metrics

44
77
15
1
256
204
39
0.51
5.13
15
2.6

Classes

Class Line # Actions
ContactMatrix 29 77 39
0.3161764731.6%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.datamodel;
22   
23    import java.util.ArrayList;
24    import java.util.List;
25    import java.util.StringTokenizer;
26   
27    import jalview.bin.Console;
28   
 
29    public abstract class ContactMatrix extends GroupSetHolder
30    implements ContactMatrixI
31    {
32    /**
33    * are contacts reflexive ?
34    */
35    boolean symmetric = true;
36   
 
37  0 toggle public ContactMatrix(boolean symmetric)
38    {
39  0 this.symmetric = symmetric;
40    }
41   
42    List<List<Float>> contacts = null;
43   
44    int width = 0, numcontacts = 0;
45   
46    float min = 0f, max = 0f;
47   
 
48  0 toggle public void addContact(int left, int right, float strength)
49    {
50  0 if (left < 0 || right < 0)
51    {
52  0 throw new Error(new RuntimeException(
53    "Cannot have negative indices for contact left=" + left
54    + " right=" + right + " strength=" + strength));
55    }
56  0 if (symmetric)
57    {
58  0 if (left > right)
59    {
60    // swap
61  0 int r = right;
62  0 right = left;
63  0 left = r;
64    }
65    }
66  0 if (contacts == null)
67    {
68    // TODO: use sparse list for efficiency ?
69  0 contacts = new ArrayList<List<Float>>();
70    }
71  0 List<Float> clist = contacts.get(left);
72  0 if (clist == null)
73    {
74  0 clist = new ArrayList<Float>();
75  0 contacts.set(left, clist);
76    }
77  0 Float last = clist.set(right, strength);
78    // TODO: if last is non null, may need to recompute range
79  0 checkBounds(strength);
80  0 if (last == null)
81    {
82  0 numcontacts++;
83    }
84    }
85   
 
86  0 toggle private void checkBounds(float strength)
87    {
88  0 if (min > strength)
89    {
90  0 min = strength;
91    }
92  0 if (max < strength)
93    {
94  0 max = strength;
95    }
96    }
97   
 
98  0 toggle @Override
99    public ContactListI getContactList(final int column)
100    {
101  0 if (column < 0 || column >= width)
102    {
103  0 return null;
104    }
105   
106  0 return new ContactListImpl(new ContactListProviderI()
107    {
108    int p = column;
109   
 
110  0 toggle @Override
111    public int getPosition()
112    {
113  0 return p;
114    }
115   
 
116  0 toggle @Override
117    public int getContactHeight()
118    {
119  0 return width;
120   
121    }
122   
 
123  0 toggle @Override
124    public double getContactAt(int column)
125    {
126  0 Float cl = getFloatElementAt(column, p);
127  0 if (cl == null)
128    {
129    // return 0 not NaN ?
130  0 return Double.NaN;
131    }
132  0 return cl.doubleValue();
133    }
134    });
135    }
136   
 
137  0 toggle private Float getFloatElementAt(int column, int p)
138    {
139   
140  0 List<Float> clist;
141  0 Float cl = null;
142  0 if (symmetric)
143    {
144  0 if (p < column)
145    {
146  0 clist = contacts.get(p);
147  0 cl = clist.get(column);
148    }
149    else
150    {
151  0 clist = contacts.get(column);
152  0 cl = clist.get(p);
153    }
154    }
155    else
156    {
157  0 clist = contacts.get(p);
158  0 cl = clist.get(column);
159    }
160  0 return cl;
161    }
162   
 
163  0 toggle @Override
164    public double getElementAt(int column, int row)
165    {
166  0 Float cl = getFloatElementAt(column, row);
167  0 if (cl != null)
168    {
169  0 return cl;
170    }
171  0 throw (new RuntimeException("Out of Bounds " + column + "," + row));
172    }
173   
 
174  0 toggle @Override
175    public float getMin()
176    {
177  0 return min;
178    }
179   
 
180  0 toggle @Override
181    public float getMax()
182    {
183  0 return max;
184    }
185   
 
186  0 toggle @Override
187    public String getAnnotLabel()
188    {
189  0 return "Contact Matrix";
190    }
191   
 
192  0 toggle @Override
193    public String getAnnotDescr()
194    {
195  0 return "Contact Matrix";
196    }
197   
 
198  5 toggle public static String contactToFloatString(ContactMatrixI cm)
199    {
200  5 StringBuilder sb = new StringBuilder();
201  2049 for (int c = 0; c < cm.getWidth(); c++)
202    {
203  2044 ContactListI cl = cm.getContactList(c);
204  2044 long lastsb = -1;
205  2044 if (cl != null)
206    {
207  4002528 for (int h = 0; h <= cl.getContactHeight(); h++)
208    {
209  4000484 if (sb.length() > 0)
210    {
211  4000479 if (sb.length() - lastsb > 320)
212    {
213    // newline
214  56466 sb.append('\n');
215  56466 lastsb = sb.length();
216    }
217    else
218    {
219  3944013 sb.append('\t');
220    }
221    }
222  4000484 sb.append(cl.getContactAt(h));
223    }
224    }
225    }
226  5 return sb.toString();
227    }
228   
 
229  4 toggle public static float[][] fromFloatStringToContacts(String values, int cols,
230    int rows)
231    {
232  4 float[][] vals = new float[cols][rows];
233  4 StringTokenizer tabsep = new StringTokenizer(values, "" + '\t' + '\n');
234  4 int c = 0, r = 0;
235  4000363 while (tabsep.hasMoreTokens())
236    {
237  4000363 double elem = Double.valueOf(tabsep.nextToken());
238  4000363 vals[c][r++] = (float) elem;
239  4000363 if (r >= vals[c].length)
240    {
241  2033 r = 0;
242  2033 c++;
243    }
244  4000363 if (c >= vals.length)
245    {
246  4 break;
247    }
248    }
249  4 if (tabsep.hasMoreElements())
250    {
251  0 Console.warn(
252    "Ignoring additional elements for Float string to contact matrix parsing.");
253    }
254  4 return vals;
255    }
256    }