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