d80a7198677810e164315fa60be718c454d4b86a
[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).getMappableContactList(_aa.sequenceRef, column);
38       }
39     }
40     // TODO: could resolve sequence position to column position here
41     // TODO: what about for complexes - where contactMatrix may involve two or
42     // more sequences
43     return cm.getContactList(column);
44   }
45
46   @Override
47   public AlignmentAnnotation addContactList(ContactMatrixI cm)
48   {
49
50     AlignmentAnnotation aa = new AlignmentAnnotation(cm.getAnnotLabel(),
51             cm.getAnnotDescr(), new Annotation[0]);
52     aa.graph = AlignmentAnnotation.CONTACT_MAP;
53     aa.graphMin = cm.getMin();
54     aa.graphMax = cm.getMax();
55     aa.editable = false;
56     aa.calcId = cm.getType();
57
58     contactmaps.put(aa.annotationId, cm);
59     // TODO: contact matrices could be intra or inter - more than one refseq
60     // possible!
61     if (cm instanceof MappableContactMatrixI)
62     {
63       aa.setSequenceRef(((MappableContactMatrixI) cm).getReferenceSeq());
64     }
65     return aa;
66   }
67
68   @Override
69   public ContactMatrixI getContactMatrixFor(AlignmentAnnotation ann)
70   {
71     return contactmaps == null ? null : contactmaps.get(ann.annotationId);
72   }
73
74   @Override
75   public void addContactListFor(AlignmentAnnotation annotation,
76           ContactMatrixI cm)
77   {
78     // update annotation with data from contact map
79     annotation.graphMin = cm.getMin();
80     annotation.graphMax = cm.getMax();
81     annotation.editable = false;
82     annotation.graph = AlignmentAnnotation.CONTACT_MAP;
83     annotation.calcId = cm.getType();
84     if (annotation.label == null || "".equals(annotation.label))
85     {
86       annotation.label = cm.getAnnotLabel();
87
88     }
89     if (annotation.description == null || "".equals(annotation.description))
90     {
91       annotation.description = cm.getAnnotDescr();
92     }
93     contactmaps.put(annotation.annotationId, cm);
94   }
95 }