JAL-2349 JAL-3855 resolve sequence position for contact lookup and catch out bound...
[jalview.git] / src / jalview / ws / datamodel / alphafold / PAEContactMatrix.java
1 package jalview.ws.datamodel.alphafold;
2
3 import java.util.Iterator;
4 import java.util.List;
5 import java.util.Map;
6
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
13 public class PAEContactMatrix implements ContactMatrixI
14 {
15
16   SequenceI refSeq = null;
17
18   int maxrow = 0, maxcol = 0;
19
20   int[] indices1, indices2;
21
22   float[][] elements;
23
24   float maxscore;
25
26   @SuppressWarnings("unchecked")
27   public PAEContactMatrix(SequenceI _refSeq, Map<String, Object> pae_obj)
28           throws Exception
29   {
30     refSeq = _refSeq;
31     while (refSeq.getDatasetSequence() != null)
32     {
33       refSeq = refSeq.getDatasetSequence();
34     }
35     // convert the lists to primitive arrays and store
36     int length = _refSeq.getEnd() - _refSeq.getStart() + 1;
37
38     // assume indices are with respect to range defined by _refSeq on the
39     // dataset refSeq
40     Iterator<Long> rows = ((List<Long>) pae_obj.get("residue1")).iterator();
41     Iterator<Long> cols = ((List<Long>) pae_obj.get("residue2")).iterator();
42     Iterator<Double> scores = ((List<Double>) pae_obj.get("distance"))
43             .iterator();
44
45     elements = new float[length][length];
46     while (scores.hasNext())
47     {
48       float escore = scores.next().floatValue();
49       int row = rows.next().intValue();
50       int col = cols.next().intValue();
51       if (maxrow < row)
52       {
53         maxrow = row;
54       }
55       if (maxcol < col)
56       {
57         maxcol = col;
58       }
59       elements[row - 1][col - 1] = escore;
60     }
61
62     maxscore = ((Double) pae_obj.get("max_predicted_aligned_error"))
63             .floatValue();
64   }
65
66   @Override
67   public ContactListI getContactList(final int _column)
68   {
69     if (_column < 0 || _column >= elements.length)
70     {
71       return null;
72     }
73
74     return new ContactListImpl(new ContactListProviderI()
75     {
76       @Override
77       public int getContactHeight()
78       {
79         return maxcol - 1;
80       }
81
82       @Override
83       public double getContactAt(int column)
84       {
85         if (column < 0 || column >= elements[_column].length)
86         {
87           return -1;
88         }
89         // TODO Auto-generated method stub
90         return elements[_column][column];
91       }
92     });
93   }
94
95   @Override
96   public float getMin()
97   {
98     return 0;
99   }
100
101   @Override
102   public float getMax()
103   {
104     return maxscore;
105   }
106
107   @Override
108   public boolean hasReferenceSeq()
109   {
110     return (refSeq != null);
111   }
112
113   @Override
114   public SequenceI getReferenceSeq()
115   {
116     return refSeq;
117   }
118
119 }