|
| 1 | +package edu.uab.mukhtarlab.wkshelldecomposition.internal.task; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import org.cytoscape.model.*; |
| 5 | +import org.cytoscape.view.model.CyNetworkView; |
| 6 | +import org.cytoscape.view.model.View; |
| 7 | +import org.cytoscape.view.presentation.property.BasicVisualLexicon; |
| 8 | +import org.cytoscape.view.vizmap.VisualMappingFunctionFactory; |
| 9 | +import org.cytoscape.view.vizmap.VisualStyle; |
| 10 | +import org.cytoscape.view.vizmap.VisualStyleFactory; |
| 11 | +import org.cytoscape.view.vizmap.mappings.BoundaryRangeValues; |
| 12 | +import org.cytoscape.view.vizmap.mappings.ContinuousMapping; |
| 13 | +import org.cytoscape.view.vizmap.mappings.DiscreteMapping; |
| 14 | +import org.cytoscape.view.vizmap.mappings.PassthroughMapping; |
| 15 | +import org.cytoscape.work.ObservableTask; |
| 16 | +import org.cytoscape.work.TaskMonitor; |
| 17 | +import org.cytoscape.work.json.JSONResult; |
| 18 | + |
| 19 | +import java.awt.*; |
| 20 | +import java.util.*; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * Performs the weighted k-shell decomposition |
| 26 | + */ |
| 27 | + |
| 28 | +public class ConcentricLayoutTask implements ObservableTask { |
| 29 | + |
| 30 | + private boolean cancelled; |
| 31 | + private final CyNetwork network; |
| 32 | + private final CyNetworkView nView; |
| 33 | + private final VisualStyleFactory visualStyleFactoryServiceRef; |
| 34 | + private final VisualMappingFunctionFactory vmfFactoryC; |
| 35 | + private final VisualMappingFunctionFactory vmfFactoryP; |
| 36 | + |
| 37 | + public ConcentricLayoutTask( |
| 38 | + CyNetwork network, |
| 39 | + CyNetworkView nView, |
| 40 | + VisualStyleFactory visualStyleFactoryServiceRef, |
| 41 | + VisualMappingFunctionFactory vmfFactoryC, |
| 42 | + VisualMappingFunctionFactory vmfFactoryP |
| 43 | + ) { |
| 44 | + this.network = network; |
| 45 | + this.nView = nView; |
| 46 | + this.visualStyleFactoryServiceRef = visualStyleFactoryServiceRef; |
| 47 | + this.vmfFactoryC = vmfFactoryC; |
| 48 | + this.vmfFactoryP = vmfFactoryP; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Runs the layout |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public void run(TaskMonitor tm) throws Exception { |
| 56 | + double nodeDiameter = 20.0; |
| 57 | + |
| 58 | + int layers = 20; |
| 59 | + |
| 60 | + int currentNode = 0; |
| 61 | + int nodesPerCircle = 1; |
| 62 | + int currentCircle = 0; |
| 63 | + double circleGap = 5.0; |
| 64 | + int currentLayer = 0; |
| 65 | + double layerGap = 50.0; |
| 66 | + |
| 67 | + double currentPositionRadius = 0; |
| 68 | + double angle = 0; |
| 69 | + double x = 0; |
| 70 | + double y = 0; |
| 71 | + double angleOffset = 0; |
| 72 | + |
| 73 | + CyRow row; |
| 74 | + Integer percentileBucket; |
| 75 | + Integer shell; |
| 76 | + double circlePerimeter; |
| 77 | + int maxShell = 0; |
| 78 | + |
| 79 | + List<CyNode> nodes = network.getNodeList(); |
| 80 | + //sort nodes by bucket, shell |
| 81 | + Comparator<CyNode> comparator = new Comparator<CyNode>() { |
| 82 | + @Override |
| 83 | + public int compare(CyNode a, CyNode b) { |
| 84 | + CyRow aRow = network.getRow(a); |
| 85 | + CyRow bRow = network.getRow(b); |
| 86 | + |
| 87 | + Integer aShell = aRow.get("_wkshell", Integer.class); |
| 88 | + Integer bShell = bRow.get("_wkshell", Integer.class); |
| 89 | + aShell = (aShell!=null) ? aShell : 0; |
| 90 | + bShell = (bShell!=null) ? bShell : 0; |
| 91 | + if(bShell!=aShell) { |
| 92 | + return bShell - aShell; |
| 93 | + } else { |
| 94 | + Integer aBucket = aRow.get("_wks_percentile_bucket", Integer.class); |
| 95 | + Integer bBucket = bRow.get("_wks_percentile_bucket", Integer.class); |
| 96 | + aBucket = (aBucket!=null) ? aBucket : 0; |
| 97 | + bBucket = (bBucket!=null) ? bBucket : 0; |
| 98 | + return bBucket - aBucket; |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + }; |
| 103 | + nodes.sort(comparator); |
| 104 | + |
| 105 | + boolean layerChange = false; |
| 106 | + boolean circleChange = false; |
| 107 | + |
| 108 | + for (final CyNode node : nodes) { |
| 109 | + View<CyNode> nodeView = nView.getNodeView(node); |
| 110 | + row = network.getRow(node); |
| 111 | + |
| 112 | + //Get max shell for gradient mapping later |
| 113 | + shell = row.get("_wkshell", Integer.class); |
| 114 | + shell = (shell!=null) ? shell : 0; |
| 115 | + if(shell>maxShell) { |
| 116 | + maxShell = shell; |
| 117 | + } |
| 118 | + |
| 119 | + //For unimplemented layering |
| 120 | + percentileBucket = row.get("_wks_percentile_bucket", Integer.class); |
| 121 | + percentileBucket = (percentileBucket!=null) ? percentileBucket : 0; |
| 122 | + layerChange = false;//((layers-1-(percentileBucket/5))>currentLayer); |
| 123 | + if(layerChange) { |
| 124 | + currentLayer = layers-1-(percentileBucket/5); |
| 125 | + } |
| 126 | + |
| 127 | + //Does this node need to start a new circle for filling |
| 128 | + circleChange = (currentNode>=(nodesPerCircle)); |
| 129 | + if(circleChange) { |
| 130 | + currentCircle++; |
| 131 | + } |
| 132 | + |
| 133 | + //If new circle for filling, update radius |
| 134 | + if(layerChange || circleChange){ |
| 135 | + currentNode = 0; |
| 136 | + currentPositionRadius = (((double) currentCircle)*(nodeDiameter+circleGap)) + (((double) currentLayer)*layerGap); |
| 137 | + circlePerimeter = Math.PI*currentPositionRadius*2.0; |
| 138 | + nodesPerCircle = (int) (circlePerimeter/((nodeDiameter+circleGap))); |
| 139 | + } |
| 140 | + |
| 141 | + //TODO Maybe |
| 142 | + //Add an extra angle offset for the last circle if its empty to evenly space out the nodes within the circle |
| 143 | + //Would have to if on last circle and how many nodes there are in the last circle |
| 144 | + |
| 145 | + |
| 146 | + angle = Math.toRadians((((double) currentNode)/((double) nodesPerCircle))*360.0); |
| 147 | + x = Math.cos(angle) * currentPositionRadius; |
| 148 | + y = Math.sin(angle) * currentPositionRadius; |
| 149 | + |
| 150 | + nodeView.setVisualProperty(BasicVisualLexicon.NODE_X_LOCATION,x); |
| 151 | + nodeView.setVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION,y); |
| 152 | + |
| 153 | + currentNode++; |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + VisualStyle vs = getVisualStyle(nodeDiameter, maxShell); |
| 159 | + vs.apply(nView); |
| 160 | + |
| 161 | + nView.updateView(); |
| 162 | + nView.fitContent(); |
| 163 | + } |
| 164 | + |
| 165 | + private VisualStyle getVisualStyle(double nodeDiameter, int maxShell) { |
| 166 | + VisualStyle vs = visualStyleFactoryServiceRef.createVisualStyle("Shell gradient"); |
| 167 | + vs.setDefaultValue(BasicVisualLexicon.NODE_BORDER_PAINT, Color.BLACK); //Default handles null shell |
| 168 | + //Mapping for border paint handled below |
| 169 | + vs.setDefaultValue(BasicVisualLexicon.NODE_BORDER_WIDTH, nodeDiameter/10.0); |
| 170 | + vs.setDefaultValue(BasicVisualLexicon.NODE_FILL_COLOR, Color.WHITE); //Default handles null shell |
| 171 | + //Mapping for fill color handled below |
| 172 | + vs.setDefaultValue(BasicVisualLexicon.NODE_HEIGHT, nodeDiameter); |
| 173 | + //Mapping for label handled below |
| 174 | + vs.setDefaultValue(BasicVisualLexicon.NODE_LABEL_COLOR, Color.BLACK); |
| 175 | + vs.setDefaultValue(BasicVisualLexicon.NODE_LABEL_FONT_SIZE, (int)(nodeDiameter*0.3)); |
| 176 | + vs.setDefaultValue(BasicVisualLexicon.NODE_TRANSPARENCY, 255); |
| 177 | + vs.setDefaultValue(BasicVisualLexicon.NODE_WIDTH, nodeDiameter); |
| 178 | + |
| 179 | + vs.setDefaultValue(BasicVisualLexicon.NODE_SIZE, nodeDiameter); |
| 180 | + vs.setDefaultValue(BasicVisualLexicon.NODE_OPACITY, 200); |
| 181 | + |
| 182 | + |
| 183 | + //Fill Color |
| 184 | + ContinuousMapping fillMapping = (ContinuousMapping) |
| 185 | + vmfFactoryC.createVisualMappingFunction("_wkshell", Integer.class, BasicVisualLexicon.NODE_FILL_COLOR); |
| 186 | + // Define the points |
| 187 | + Double val1 = 1d; |
| 188 | + BoundaryRangeValues<Paint> brv1 = new BoundaryRangeValues<Paint>(Color.BLUE, Color.BLUE, Color.BLUE); |
| 189 | + Double val2 = (double) maxShell; |
| 190 | + BoundaryRangeValues<Paint> brv2 = new BoundaryRangeValues<Paint>(Color.RED, Color.RED, Color.RED); |
| 191 | + // Set the points |
| 192 | + fillMapping.addPoint(val1, brv1); |
| 193 | + fillMapping.addPoint(val2, brv2); |
| 194 | + // add the mapping to visual style |
| 195 | + vs.addVisualMappingFunction(fillMapping); |
| 196 | + |
| 197 | + //Border paint |
| 198 | + ContinuousMapping borderMapping = (ContinuousMapping) |
| 199 | + vmfFactoryC.createVisualMappingFunction("_wkshell", Integer.class, BasicVisualLexicon.NODE_BORDER_PAINT); |
| 200 | + //Reuse points from fill color |
| 201 | + // Set the points |
| 202 | + borderMapping.addPoint(val1, brv1); |
| 203 | + borderMapping.addPoint(val2, brv2); |
| 204 | + //add the mapping to visual style |
| 205 | + vs.addVisualMappingFunction(borderMapping); |
| 206 | + |
| 207 | + //Label |
| 208 | + PassthroughMapping labelMapping = (PassthroughMapping) |
| 209 | + vmfFactoryP.createVisualMappingFunction("name", String.class, BasicVisualLexicon.NODE_LABEL); |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + vs.addVisualMappingFunction(labelMapping); |
| 214 | + |
| 215 | + return vs; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public void cancel() { |
| 220 | + cancelled = true; |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 225 | + public Object getResults(Class type) { |
| 226 | + if (type == CyNetworkView.class) |
| 227 | + return nView; |
| 228 | + |
| 229 | + if (type == String.class) { |
| 230 | + return "Created view: " + nView; |
| 231 | + } |
| 232 | + |
| 233 | + if (type == JSONResult.class) { |
| 234 | + Gson gson = new Gson(); |
| 235 | + JSONResult res = () -> { return gson.toJson(nView); }; |
| 236 | + |
| 237 | + return res; |
| 238 | + } |
| 239 | + |
| 240 | + return null; |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public List<Class<?>> getResultClasses() { |
| 245 | + return Arrays.asList(CyNetworkView.class, String.class, JSONResult.class); |
| 246 | + } |
| 247 | +} |
0 commit comments