1 package jalview.ws.datamodel.alphafold;
4 import java.io.BufferedInputStream;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.BitSet;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
14 import java.util.Map.Entry;
16 import org.json.simple.JSONObject;
18 import jalview.analysis.AverageDistanceEngine;
19 import jalview.bin.Console;
20 import jalview.datamodel.Annotation;
21 import jalview.datamodel.BinaryNode;
22 import jalview.datamodel.ContactListI;
23 import jalview.datamodel.ContactListImpl;
24 import jalview.datamodel.ContactListProviderI;
25 import jalview.datamodel.ContactMatrixI;
26 import jalview.datamodel.GroupSet;
27 import jalview.datamodel.GroupSetI;
28 import jalview.datamodel.Mapping;
29 import jalview.datamodel.SequenceDummy;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.DataSourceType;
32 import jalview.io.FileFormatException;
33 import jalview.io.FileParse;
34 import jalview.util.MapList;
35 import jalview.util.MapUtils;
36 import jalview.ws.dbsources.EBIAlfaFold;
38 public class PAEContactMatrix extends
39 MappableContactMatrix<PAEContactMatrix> implements ContactMatrixI
43 int maxrow = 0, maxcol = 0;
51 @SuppressWarnings("unchecked")
52 public PAEContactMatrix(SequenceI _refSeq, Map<String, Object> pae_obj)
53 throws FileFormatException
56 // convert the lists to primitive arrays and store
58 if (!MapUtils.containsAKey(pae_obj, "predicted_aligned_error", "pae"))
60 parse_version_1_pAE(pae_obj);
65 parse_version_2_pAE(pae_obj);
70 * construct a sequence associated PAE matrix directly from a float array
75 public PAEContactMatrix(SequenceI _refSeq, float[][] matrix)
79 for (float[] row : matrix)
81 if (row.length > maxcol)
94 maxrow = matrix.length;
100 * new matrix with specific mapping to a reference sequence
103 * @param newFromMapList
107 public PAEContactMatrix(SequenceI newRefSeq, MapList newFromMapList,
108 float[][] elements2, GroupSet grps2)
110 this(newRefSeq, elements2);
111 toSeq = newFromMapList;
116 * parse a sane JSON representation of the pAE
120 @SuppressWarnings("unchecked")
121 private void parse_version_2_pAE(Map<String, Object> pae_obj)
124 // look for a maxscore element - if there is one...
127 // this is never going to be reached by the integer rounding.. or is it ?
128 maxscore = ((Double) MapUtils.getFirst(pae_obj,
129 "max_predicted_aligned_error", "max_pae")).floatValue();
130 } catch (Throwable t)
132 // ignore if a key is not found.
134 List<List<Long>> scoreRows = ((List<List<Long>>) MapUtils
135 .getFirst(pae_obj, "predicted_aligned_error", "pae"));
136 elements = new float[scoreRows.size()][scoreRows.size()];
137 int row = 0, col = 0;
138 for (List<Long> scoreRow : scoreRows)
140 Iterator<Long> scores = scoreRow.iterator();
141 while (scores.hasNext())
143 Object d = scores.next();
144 if (d instanceof Double)
146 elements[row][col++] = ((Double) d).longValue();
150 elements[row][col++] = (float) ((Long) d).longValue();
153 if (maxscore < elements[row][col - 1])
155 maxscore = elements[row][col - 1];
166 * v1 format got ditched 28th July 2022 see
167 * https://alphafold.ebi.ac.uk/faq#:~:text=We%20updated%20the%20PAE%20JSON%20file%20format%20on%2028th%20July%202022
171 @SuppressWarnings("unchecked")
172 private void parse_version_1_pAE(Map<String, Object> pae_obj)
174 // assume indices are with respect to range defined by _refSeq on the
176 Iterator<Long> rows = ((List<Long>) pae_obj.get("residue1")).iterator();
177 Iterator<Long> cols = ((List<Long>) pae_obj.get("residue2")).iterator();
178 // two pass - to allocate the elements array
179 while (rows.hasNext())
181 int row = rows.next().intValue();
182 int col = cols.next().intValue();
193 rows = ((List<Long>) pae_obj.get("residue1")).iterator();
194 cols = ((List<Long>) pae_obj.get("residue2")).iterator();
195 Iterator<Double> scores = ((List<Double>) pae_obj.get("distance"))
197 elements = new float[maxrow][maxcol];
198 while (scores.hasNext())
200 float escore = scores.next().floatValue();
201 int row = rows.next().intValue();
202 int col = cols.next().intValue();
211 elements[row - 1][col - 1] = escore;
214 maxscore = ((Double) MapUtils.getFirst(pae_obj,
215 "max_predicted_aligned_error", "max_pae")).floatValue();
219 public ContactListI getContactList(final int column)
221 if (column < 0 || column >= elements.length)
226 return new ContactListImpl(new ContactListProviderI()
229 public int getPosition()
235 public int getContactHeight()
241 public double getContactAt(int mcolumn)
243 if (mcolumn < 0 || mcolumn >= elements[column].length)
247 return elements[column][mcolumn];
253 protected double getElementAt(int _column, int i)
255 return elements[_column][i];
259 public float getMin()
265 public float getMax()
271 public String getAnnotDescr()
273 return "Predicted Alignment Error"
274 + ((refSeq == null) ? "" : (" for " + refSeq.getName()));
278 public String getAnnotLabel()
280 StringBuilder label = new StringBuilder("PAE Matrix");
281 // if (this.getReferenceSeq() != null)
283 // label.append(":").append(this.getReferenceSeq().getDisplayId(false));
285 return label.toString();
288 public static final String PAEMATRIX = "PAE_MATRIX";
291 public String getType()
297 public int getWidth()
303 public int getHeight()
307 public static void validateContactMatrixFile(String fileName)
308 throws FileFormatException, IOException
310 FileInputStream infile = null;
313 infile = new FileInputStream(new File(fileName));
314 } catch (Throwable t)
316 new IOException("Couldn't open " + fileName, t);
318 JSONObject paeDict = null;
321 paeDict = EBIAlfaFold.parseJSONtoPAEContactMatrix(infile);
322 } catch (Throwable t)
324 new FileFormatException("Couldn't parse " + fileName
325 + " as a JSON dict or array containing a dict");
328 PAEContactMatrix matrix = new PAEContactMatrix(
329 new SequenceDummy("Predicted"), (Map<String, Object>) paeDict);
330 if (matrix.getWidth() <= 0)
332 throw new FileFormatException(
333 "No data in PAE matrix read from '" + fileName + "'");
337 protected PAEContactMatrix newMappableContactMatrix(SequenceI newRefSeq,
338 MapList newFromMapList)
340 PAEContactMatrix pae = new PAEContactMatrix(newRefSeq, newFromMapList,
341 elements, new GroupSet(grps));