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 MappableContactMatrix<PAEContactMatrix> implements ContactMatrixI
40 int maxrow = 0, maxcol = 0;
47 @SuppressWarnings("unchecked")
48 public PAEContactMatrix(SequenceI _refSeq, Map<String, Object> pae_obj) throws FileFormatException
51 // convert the lists to primitive arrays and store
53 if (!MapUtils.containsAKey(pae_obj, "predicted_aligned_error", "pae"))
55 parse_version_1_pAE(pae_obj);
60 parse_version_2_pAE(pae_obj);
65 * construct a sequence associated PAE matrix directly from a float array
70 public PAEContactMatrix(SequenceI _refSeq, float[][] matrix)
74 for (float[] row : matrix)
76 if (row.length > maxcol)
89 maxrow = matrix.length;
95 * new matrix with specific mapping to a reference sequence
97 * @param newFromMapList
101 public PAEContactMatrix(SequenceI newRefSeq,
102 MapList newFromMapList, float[][] elements2, GroupSet grps2)
104 this(newRefSeq,elements2);
105 toSeq = newFromMapList;
110 * parse a sane JSON representation of the pAE
114 @SuppressWarnings("unchecked")
115 private void parse_version_2_pAE(Map<String, Object> pae_obj)
118 // look for a maxscore element - if there is one...
121 // this is never going to be reached by the integer rounding.. or is it ?
122 maxscore = ((Double) MapUtils.getFirst(pae_obj,
123 "max_predicted_aligned_error", "max_pae")).floatValue();
124 } catch (Throwable t)
126 // ignore if a key is not found.
128 List<List<Long>> scoreRows = ((List<List<Long>>) MapUtils
129 .getFirst(pae_obj, "predicted_aligned_error", "pae"));
130 elements = new float[scoreRows.size()][scoreRows.size()];
131 int row = 0, col = 0;
132 for (List<Long> scoreRow : scoreRows)
134 Iterator<Long> scores = scoreRow.iterator();
135 while (scores.hasNext())
137 Object d = scores.next();
139 if (d instanceof Double)
141 elements[row][col++] = ((Double) d).longValue();
145 elements[row][col++] = (float) ((Long) d).longValue();
148 if (maxscore < elements[row][col - 1])
150 maxscore = elements[row][col - 1];
161 * v1 format got ditched 28th July 2022 see
162 * https://alphafold.ebi.ac.uk/faq#:~:text=We%20updated%20the%20PAE%20JSON%20file%20format%20on%2028th%20July%202022
166 @SuppressWarnings("unchecked")
167 private void parse_version_1_pAE(Map<String, Object> pae_obj)
169 // assume indices are with respect to range defined by _refSeq on the
171 Iterator<Long> rows = ((List<Long>) pae_obj.get("residue1")).iterator();
172 Iterator<Long> cols = ((List<Long>) pae_obj.get("residue2")).iterator();
173 // two pass - to allocate the elements array
174 while (rows.hasNext())
176 int row = rows.next().intValue();
177 int col = cols.next().intValue();
188 rows = ((List<Long>) pae_obj.get("residue1")).iterator();
189 cols = ((List<Long>) pae_obj.get("residue2")).iterator();
190 Iterator<Double> scores = ((List<Double>) pae_obj.get("distance"))
192 elements = new float[maxrow][maxcol];
193 while (scores.hasNext())
195 float escore = scores.next().floatValue();
196 int row = rows.next().intValue();
197 int col = cols.next().intValue();
206 elements[row - 1][col - 1] = escore;
209 maxscore = ((Double) MapUtils.getFirst(pae_obj,
210 "max_predicted_aligned_error", "max_pae")).floatValue();
214 public ContactListI getContactList(final int column)
216 // final int _column;
217 // if (toSeq != null)
219 // int[] word = toSeq.locateInTo(column, column);
224 // _column = word[0];
230 if (column < 0 || column >= elements.length)
235 return new ContactListImpl(new ContactListProviderI()
238 public int getPosition()
244 public int getContactHeight()
250 public double getContactAt(int mcolumn)
252 if (mcolumn < 0 || mcolumn >= elements[column].length)
256 return elements[column][mcolumn];
262 protected double getElementAt(int _column, int i)
264 return elements[_column][i];
267 public float getMin()
273 public float getMax()
279 public String getAnnotDescr()
281 return "Predicted Alignment Error"+((refSeq==null) ? "" : (" for " + refSeq.getName()));
285 public String getAnnotLabel()
287 StringBuilder label = new StringBuilder("PAE Matrix");
288 //if (this.getReferenceSeq() != null)
290 // label.append(":").append(this.getReferenceSeq().getDisplayId(false));
292 return label.toString();
295 public static final String PAEMATRIX = "PAE_MATRIX";
298 public String getType()
304 public int getWidth()
310 public int getHeight()
315 public static void validateContactMatrixFile(String fileName) throws FileFormatException,IOException
317 FileInputStream infile=null;
319 infile = new FileInputStream(new File(fileName));
320 } catch (Throwable t)
322 new IOException("Couldn't open "+fileName,t);
326 JSONObject paeDict=null;
328 paeDict = EBIAlfaFold.parseJSONtoPAEContactMatrix(infile);
329 } catch (Throwable t)
331 new FileFormatException("Couldn't parse "+fileName+" as a JSON dict or array containing a dict");
334 PAEContactMatrix matrix = new PAEContactMatrix(new SequenceDummy("Predicted"), (Map<String,Object>)paeDict);
335 if (matrix.getWidth()<=0)
337 throw new FileFormatException("No data in PAE matrix read from '"+fileName+"'");
342 protected PAEContactMatrix newMappableContactMatrix(
343 SequenceI newRefSeq, MapList newFromMapList)
345 PAEContactMatrix pae=new PAEContactMatrix(newRefSeq, newFromMapList,
346 elements, new GroupSet(grps));