925025fc3c70ba4ee4babe2bb1a2d9b2c1f54111
[jalview.git] / src / jalview / datamodel / ContactMatrixI.java
1 package jalview.datamodel;
2
3 import java.awt.Color;
4 import java.util.Arrays;
5 import java.util.BitSet;
6 import java.util.List;
7
8 import jalview.util.ColorUtils;
9 import jalview.ws.datamodel.MappableContactMatrixI;
10
11 public interface ContactMatrixI
12 {
13
14   ContactListI getContactList(int column);
15
16   float getMin();
17
18   float getMax();
19
20   String getAnnotDescr();
21
22   String getAnnotLabel();
23
24   /**
25    * string indicating how the contactMatrix should be rendered - stored in
26    * calcId
27    * 
28    * @return
29    */
30   String getType();
31
32   int getWidth();
33   int getHeight();
34   public GroupSetI getGroupSet();
35
36   /// proxy methods to simplify use of the interface
37   /// Mappable contact matrices can override these to perform mapping
38
39   default public boolean hasGroupSet()
40   {
41     return getGroupSet() != null;
42   }
43
44   default boolean hasGroups()
45   {
46     return hasGroupSet() && getGroupSet().hasGroups();
47   }
48
49   default BitSet getGroupsFor(int column)
50   {
51     if (!hasGroupSet())
52     {
53       BitSet colbitset = new BitSet();
54       colbitset.set(column);
55       return colbitset;
56     }
57     return getGroupSet().getGroupsFor(column);
58   }
59
60   default List<BitSet> getGroups()
61   {
62     if (!hasGroupSet())
63     {
64       return Arrays.asList();
65     }
66     return getGroupSet().getGroups();
67   }
68
69   default boolean hasTree()
70   {
71     return hasGroupSet() ? getGroupSet().hasTree() : false;
72   }
73
74   /**
75    * Newick representation of clustered matrix
76    * 
77    * @return null unless hasTree is true
78    */
79   default String getNewick()
80   {
81     return hasGroupSet() ? getGroupSet().getNewick() : null;
82   }
83
84   default String getTreeMethod()
85   {
86     return hasGroupSet() ? getGroupSet().getTreeMethod() : null;
87   }
88
89   default boolean hasCutHeight()
90   {
91     return hasGroupSet() ? getGroupSet().hasCutHeight() : false;
92   }
93
94   default double getCutHeight()
95   {
96     return hasGroupSet() ? getGroupSet().getCutHeight() : 0;
97   }
98
99   default void updateGroups(List<BitSet> colGroups)
100   {
101     if (hasGroupSet())
102     {
103       getGroupSet().updateGroups(colGroups);
104     }
105   }
106
107   default void setColorForGroup(BitSet bs, Color color)
108   {
109     if (hasGroupSet())
110     {
111       getGroupSet().setColorForGroup(bs, color);
112     }
113   }
114
115   default Color getColourForGroup(BitSet bs)
116   {
117     if (hasGroupSet())
118     {
119       return getGroupSet().getColourForGroup(bs);
120     }
121     else
122     {
123       return Color.white;
124     }
125   }
126
127   void setGroupSet(GroupSet makeGroups);
128
129   default void randomlyReColourGroups() {
130     if (hasGroupSet())
131     {
132       GroupSetI groups = getGroupSet();
133       for (BitSet group:groups.getGroups())
134       {
135         groups.setColorForGroup(group, ColorUtils.getARandomColor());
136       }
137     }
138   }
139
140   default void transferGroupColorsTo(AlignmentAnnotation aa)
141   {
142     if (hasGroupSet())
143     {
144       GroupSetI groups = getGroupSet();
145       // stash colors in linked annotation row.
146       // doesn't work yet. TESTS!
147       int sstart = aa.sequenceRef != null ? aa.sequenceRef.getStart() - 1
148               : 0;
149       Annotation ae;
150       Color gpcol = null;
151       int[] seqpos = null;
152       for (BitSet gp : groups.getGroups())
153       {
154         gpcol = groups.getColourForGroup(gp);
155         for (int p = gp.nextSetBit(0); p >= 0
156                 && p < Integer.MAX_VALUE; p = gp.nextSetBit(p + 1))
157         {
158           if (this instanceof MappableContactMatrixI)
159           {
160             MappableContactMatrixI mcm = (MappableContactMatrixI) this;
161             seqpos = mcm.getMappedPositionsFor(aa.sequenceRef, p);
162             if (seqpos == null)
163             {
164               // no mapping for this column.
165               continue;
166             }
167             // TODO: handle ranges...
168             ae = aa.getAnnotationForPosition(seqpos[0]);
169           }
170           else
171           {
172             ae = aa.getAnnotationForPosition(p + sstart);
173           }
174           if (ae != null)
175           {
176             ae.colour = gpcol.brighter().darker();
177           }
178         }
179       }
180     }
181   }
182   
183   /**
184    * look up the colour for a column in the associated contact matrix 
185    * @return Color.white or assigned colour
186    */
187   default Color getGroupColorForPosition(int column)
188   {
189     if (hasGroupSet())
190     {
191       GroupSetI groups = getGroupSet();
192       for (BitSet gp:groups.getGroups())
193       {
194         if (gp.get(column))
195         {
196           return groups.getColourForGroup(gp);
197         }
198       }
199     }
200     return Color.white;
201   }
202   
203 }