JAL-4090 release weds 25th Oct 23 - JAL-4308 release notes
[jalview.git] / src / jalview / datamodel / ContactMapHolder.java
1 package jalview.datamodel;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import jalview.ws.datamodel.MappableContactMatrixI;
9
10 public class ContactMapHolder implements ContactMapHolderI
11 {
12
13   Map<Object, ContactMatrixI> contactmaps = new HashMap<>();
14
15   @Override
16   public Collection<ContactMatrixI> getContactMaps()
17   {
18     if (contactmaps != null && contactmaps.size() > 0)
19     {
20       return contactmaps.values();
21     }
22     return Collections.EMPTY_LIST;
23   }
24
25   @Override
26   public ContactListI getContactListFor(AlignmentAnnotation _aa, int column)
27   {
28     ContactMatrixI cm = contactmaps.get(_aa.annotationId);
29     if (cm == null)
30     {
31       return null;
32     }
33     if (cm instanceof MappableContactMatrixI)
34     {
35       if (_aa.sequenceRef != null)
36       {
37         return ((MappableContactMatrixI) cm)
38                 .getMappableContactList(_aa.sequenceRef, column);
39       }
40     }
41     // TODO: could resolve sequence position to column position here
42     // TODO: what about for complexes - where contactMatrix may involve two or
43     // more sequences
44     return cm.getContactList(column);
45   }
46
47   @Override
48   public AlignmentAnnotation addContactList(ContactMatrixI cm)
49   {
50
51     AlignmentAnnotation aa = new AlignmentAnnotation(cm.getAnnotLabel(),
52             cm.getAnnotDescr(), new Annotation[0]);
53     aa.graph = AlignmentAnnotation.CONTACT_MAP;
54     aa.graphMin = cm.getMin();
55     aa.graphMax = cm.getMax();
56     aa.editable = false;
57     aa.calcId = cm.getType();
58
59     contactmaps.put(aa.annotationId, cm);
60     // TODO: contact matrices could be intra or inter - more than one refseq
61     // possible!
62     if (cm instanceof MappableContactMatrixI)
63     {
64       aa.setSequenceRef(((MappableContactMatrixI) cm).getReferenceSeq());
65     }
66     return aa;
67   }
68
69   @Override
70   public ContactMatrixI getContactMatrixFor(AlignmentAnnotation ann)
71   {
72     return contactmaps == null ? null : contactmaps.get(ann.annotationId);
73   }
74
75   @Override
76   public void addContactListFor(AlignmentAnnotation annotation,
77           ContactMatrixI cm)
78   {
79     // update annotation with data from contact map
80     annotation.graphMin = cm.getMin();
81     annotation.graphMax = cm.getMax();
82     annotation.editable = false;
83     annotation.graph = AlignmentAnnotation.CONTACT_MAP;
84     annotation.calcId = cm.getType();
85     if (annotation.label == null || "".equals(annotation.label))
86     {
87       annotation.label = cm.getAnnotLabel();
88
89     }
90     if (annotation.description == null || "".equals(annotation.description))
91     {
92       annotation.description = cm.getAnnotDescr();
93     }
94     contactmaps.put(annotation.annotationId, cm);
95   }
96 }