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