1 package jalview.ws.datamodel.alphafold;
3 import java.util.Iterator;
7 import jalview.datamodel.ContactListI;
8 import jalview.datamodel.ContactListImpl;
9 import jalview.datamodel.ContactListProviderI;
10 import jalview.datamodel.ContactMatrixI;
11 import jalview.datamodel.SequenceI;
12 import jalview.util.MapUtils;
14 public class PAEContactMatrix implements ContactMatrixI
17 SequenceI refSeq = null;
20 * the length that refSeq is expected to be (excluding gaps, of course)
24 int maxrow = 0, maxcol = 0;
26 int[] indices1, indices2;
32 private void setRefSeq(SequenceI _refSeq)
35 while (refSeq.getDatasetSequence() != null)
37 refSeq = refSeq.getDatasetSequence();
39 length = _refSeq.getEnd() - _refSeq.getStart() + 1;
41 @SuppressWarnings("unchecked")
42 public PAEContactMatrix(SequenceI _refSeq, Map<String, Object> pae_obj)
45 // convert the lists to primitive arrays and store
47 if (!MapUtils.containsAKey(pae_obj, "predicted_aligned_error", "pae"))
49 parse_version_1_pAE(pae_obj);
54 parse_version_2_pAE(pae_obj);
58 * construct a sequence associated PAE matrix directly from a float array
62 public PAEContactMatrix(SequenceI _refSeq, float[][] matrix)
66 for (float[] row:matrix)
68 if (row.length>maxcol)
86 * parse a sane JSON representation of the pAE
90 @SuppressWarnings("unchecked")
91 private void parse_version_2_pAE(Map<String, Object> pae_obj)
93 elements = new float[length][length];
94 // this is never going to be reached by the integer rounding.. or is it ?
95 maxscore = ((Double) MapUtils.getFirst(pae_obj,
96 "max_predicted_aligned_error", "max_pae")).floatValue();
97 Iterator<List<Long>> scoreRows = ((List<List<Long>>) MapUtils
98 .getFirst(pae_obj, "predicted_aligned_error", "pae"))
100 int row = 0, col = 0;
101 while (scoreRows.hasNext())
103 Iterator<Long> scores = scoreRows.next().iterator();
104 while (scores.hasNext())
106 Object d = scores.next();
107 if (d instanceof Double)
108 elements[row][col++] = ((Double) d).longValue();
110 elements[row][col++] = (float) d;
120 * v1 format got ditched 28th July 2022 see
121 * https://alphafold.ebi.ac.uk/faq#:~:text=We%20updated%20the%20PAE%20JSON%20file%20format%20on%2028th%20July%202022
125 @SuppressWarnings("unchecked")
126 private void parse_version_1_pAE(Map<String, Object> pae_obj)
128 // assume indices are with respect to range defined by _refSeq on the
130 Iterator<Long> rows = ((List<Long>) pae_obj.get("residue1")).iterator();
131 Iterator<Long> cols = ((List<Long>) pae_obj.get("residue2")).iterator();
132 Iterator<Double> scores = ((List<Double>) pae_obj.get("distance"))
135 elements = new float[length][length];
136 while (scores.hasNext())
138 float escore = scores.next().floatValue();
139 int row = rows.next().intValue();
140 int col = cols.next().intValue();
149 elements[row - 1][col - 1] = escore;
152 maxscore = ((Double) MapUtils.getFirst(pae_obj,
153 "max_predicted_aligned_error", "max_pae")).floatValue();
157 public ContactListI getContactList(final int _column)
159 if (_column < 0 || _column >= elements.length)
164 return new ContactListImpl(new ContactListProviderI()
167 public int getPosition()
173 public int getContactHeight()
179 public double getContactAt(int column)
181 if (column < 0 || column >= elements[_column].length)
185 return elements[_column][column];
191 public float getMin()
197 public float getMax()
203 public boolean hasReferenceSeq()
205 return (refSeq != null);
209 public SequenceI getReferenceSeq()
215 public String getAnnotDescr()
217 return "Predicted Alignment Error for " + refSeq.getName();
221 public String getAnnotLabel()
226 public static final String PAEMATRIX="PAE_MATRIX";
228 public String getType()
233 public int getWidth()
238 public int getHeight()