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