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;
42 @SuppressWarnings("unchecked")
43 public PAEContactMatrix(SequenceI _refSeq, Map<String, Object> pae_obj)
46 // convert the lists to primitive arrays and store
48 if (!MapUtils.containsAKey(pae_obj, "predicted_aligned_error", "pae"))
50 parse_version_1_pAE(pae_obj);
55 parse_version_2_pAE(pae_obj);
60 * construct a sequence associated PAE matrix directly from a float array
65 public PAEContactMatrix(SequenceI _refSeq, float[][] matrix)
69 for (float[] row : matrix)
71 if (row.length > maxcol)
84 maxrow = matrix.length;
90 * parse a sane JSON representation of the pAE
94 @SuppressWarnings("unchecked")
95 private void parse_version_2_pAE(Map<String, Object> pae_obj)
97 // this is never going to be reached by the integer rounding.. or is it ?
98 maxscore = ((Double) MapUtils.getFirst(pae_obj,
99 "max_predicted_aligned_error", "max_pae")).floatValue();
100 List<List<Long>> scoreRows = ((List<List<Long>>) MapUtils
101 .getFirst(pae_obj, "predicted_aligned_error", "pae"));
102 elements = new float[scoreRows.size()][scoreRows.size()];
103 int row = 0, col = 0;
104 for (List<Long> scoreRow : scoreRows)
106 Iterator<Long> scores = scoreRow.iterator();
107 while (scores.hasNext())
109 Object d = scores.next();
110 if (d instanceof Double)
111 elements[row][col++] = ((Double) d).longValue();
113 elements[row][col++] = (float) ((Long) d).longValue();
123 * v1 format got ditched 28th July 2022 see
124 * https://alphafold.ebi.ac.uk/faq#:~:text=We%20updated%20the%20PAE%20JSON%20file%20format%20on%2028th%20July%202022
128 @SuppressWarnings("unchecked")
129 private void parse_version_1_pAE(Map<String, Object> pae_obj)
131 // assume indices are with respect to range defined by _refSeq on the
133 Iterator<Long> rows = ((List<Long>) pae_obj.get("residue1")).iterator();
134 Iterator<Long> cols = ((List<Long>) pae_obj.get("residue2")).iterator();
135 Iterator<Double> scores = ((List<Double>) pae_obj.get("distance"))
137 // assume square matrix
138 elements = new float[length][length];
139 while (scores.hasNext())
141 float escore = scores.next().floatValue();
142 int row = rows.next().intValue();
143 int col = cols.next().intValue();
152 elements[row - 1][col - 1] = escore;
155 maxscore = ((Double) MapUtils.getFirst(pae_obj,
156 "max_predicted_aligned_error", "max_pae")).floatValue();
160 public ContactListI getContactList(final int _column)
162 if (_column < 0 || _column >= elements.length)
167 return new ContactListImpl(new ContactListProviderI()
170 public int getPosition()
176 public int getContactHeight()
182 public double getContactAt(int column)
184 if (column < 0 || column >= elements[_column].length)
188 return elements[_column][column];
194 public float getMin()
200 public float getMax()
206 public boolean hasReferenceSeq()
208 return (refSeq != null);
212 public SequenceI getReferenceSeq()
218 public String getAnnotDescr()
220 return "Predicted Alignment Error for " + refSeq.getName();
224 public String getAnnotLabel()
226 StringBuilder label = new StringBuilder("pAE Matrix");
227 // if (this.getReferenceSeq() != null)
228 // label.append(":").append(this.getReferenceSeq().getDisplayId(false));
229 return label.toString();
232 public static final String PAEMATRIX = "PAE_MATRIX";
235 public String getType()
241 public int getWidth()
247 public int getHeight()