Search in sources :

Example 91 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project cytoscape-impl by cytoscape.

the class VisualStyleSerializer method createVisualStyles.

/**
 * This method creates a collection of VisualStyle objects based on the provided Vizmap object.
 * @param vizmap A Vizmap object containing a representation of VisualStyles.
 * @return A collection of VisualStyle objects.
 */
public Set<VisualStyle> createVisualStyles(final Vizmap vizmap) {
    final Set<VisualStyle> styles = new HashSet<>();
    final RenderingEngineManager renderingEngineManager = serviceRegistrar.getService(RenderingEngineManager.class);
    lexicon = renderingEngineManager.getDefaultVisualLexicon();
    if (lexicon == null) {
        logger.warn("Cannot create visual styles because there is no default Visual Lexicon");
        return styles;
    }
    if (vizmap != null) {
        final VisualStyleFactory visualStyleFactory = serviceRegistrar.getService(VisualStyleFactory.class);
        final List<org.cytoscape.io.internal.util.vizmap.model.VisualStyle> vsModelList = vizmap.getVisualStyle();
        for (org.cytoscape.io.internal.util.vizmap.model.VisualStyle vsModel : vsModelList) {
            final String styleName = vsModel.getName();
            // Each new style should be created from the default one:
            final VisualStyle vs = visualStyleFactory.createVisualStyle(styleName);
            // Set the visual properties and mappings:
            if (vsModel.getNetwork() != null)
                createVisualProperties(vs, CyNetwork.class, vsModel.getNetwork().getVisualProperty());
            if (vsModel.getNode() != null)
                createVisualProperties(vs, CyNode.class, vsModel.getNode().getVisualProperty());
            if (vsModel.getEdge() != null)
                createVisualProperties(vs, CyEdge.class, vsModel.getEdge().getVisualProperty());
            // Restore dependency
            restoreDependencies(vs, vsModel);
            styles.add(vs);
        }
    }
    return styles;
}
Also used : RenderingEngineManager(org.cytoscape.view.presentation.RenderingEngineManager) CyNetwork(org.cytoscape.model.CyNetwork) VisualStyleFactory(org.cytoscape.view.vizmap.VisualStyleFactory) CyEdge(org.cytoscape.model.CyEdge) CyNode(org.cytoscape.model.CyNode) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) HashSet(java.util.HashSet)

Example 92 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project cytoscape-impl by cytoscape.

the class VisualStyleSerializer method createVisualStyles.

/**
 * This method creates a collection of VisualStyle objects based on the provided Properties object.
 * Used to convert old (2.x) vizmap.props format to visual styles.
 * @param vizmap A Properties object containing a representation of VisualStyles.
 * @return A collection of VisualStyle objects.
 */
public Set<VisualStyle> createVisualStyles(final Properties props) {
    // Convert properties to Vizmap:
    Vizmap vizmap = new Vizmap();
    List<org.cytoscape.io.internal.util.vizmap.model.VisualStyle> vizmapStyles = vizmap.getVisualStyle();
    // Group properties keys/values by visual style name:
    Map<String, Map<String, String>> styleNamesMap = new HashMap<>();
    Set<String> propNames = props.stringPropertyNames();
    for (String key : propNames) {
        String value = props.getProperty(key);
        String styleName = CalculatorConverter.parseStyleName(key);
        if (styleName != null) {
            // Add each style name and its properties to a map
            Map<String, String> keyValueMap = styleNamesMap.get(styleName);
            if (keyValueMap == null) {
                keyValueMap = new HashMap<String, String>();
                styleNamesMap.put(styleName, keyValueMap);
            }
            keyValueMap.put(key, value);
        }
    }
    // Create a Visual Style for each style name:
    for (Entry<String, Map<String, String>> entry : styleNamesMap.entrySet()) {
        String styleName = entry.getKey();
        org.cytoscape.io.internal.util.vizmap.model.VisualStyle vs = new org.cytoscape.io.internal.util.vizmap.model.VisualStyle();
        vs.setName(styleName);
        vs.setNetwork(new Network());
        vs.setNode(new Node());
        vs.setEdge(new Edge());
        // Create and set the visual properties and mappings:
        Map<String, String> vsProps = entry.getValue();
        for (Entry<String, String> p : vsProps.entrySet()) {
            String key = p.getKey();
            String value = p.getValue();
            Set<CalculatorConverter> convs = calculatorConverterFactory.getConverters(key);
            for (CalculatorConverter c : convs) {
                c.convert(vs, value, props);
            }
        }
        vizmapStyles.add(vs);
    }
    return createVisualStyles(vizmap);
}
Also used : HashMap(java.util.HashMap) CyNode(org.cytoscape.model.CyNode) Node(org.cytoscape.io.internal.util.vizmap.model.Node) CyNetwork(org.cytoscape.model.CyNetwork) Network(org.cytoscape.io.internal.util.vizmap.model.Network) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) Vizmap(org.cytoscape.io.internal.util.vizmap.model.Vizmap) HashMap(java.util.HashMap) Map(java.util.Map) Edge(org.cytoscape.io.internal.util.vizmap.model.Edge) CyEdge(org.cytoscape.model.CyEdge)

Example 93 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project clusterMaker2 by RBVI.

the class FeatureVectorCluster method run.

public void run(TaskMonitor monitor) {
    this.monitor = monitor;
    monitor.setTitle("Performing " + getName());
    List<String> nodeAttributeList = context.nodeAttributeList.getSelectedValues();
    // Sanity check all of our settings
    if (nodeAttributeList == null || nodeAttributeList.size() == 0) {
        if (monitor != null) {
            monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no attribute list selected");
        }
        return;
    }
    String[] attributeArray = new String[nodeAttributeList.size()];
    int index = 0;
    for (String attr : nodeAttributeList) {
        attributeArray[index++] = "node." + attr;
    }
    // To make debugging easier, sort the attribute array
    Arrays.sort(attributeArray);
    if (monitor != null) {
        monitor.setProgress(0.0);
        monitor.setStatusMessage("Initializaing");
    }
    // Create the matrix
    // Matrix matrix = new Matrix(network, attributeArray, false, context.ignoreMissing, context.selectedOnly);
    CyMatrix matrix = CyMatrixFactory.makeSmallMatrix(network, attributeArray, false, context.ignoreMissing, context.selectedOnly, false);
    if (monitor != null) {
        monitor.setProgress(0.1);
        monitor.setStatusMessage("Calculating edge distances");
        if (canceled)
            return;
    }
    // Handle special cases
    if (context.zeroMissing)
        matrix.setMissingToZero();
    int nNodes = matrix.nRows();
    // For each node, get the distance to all other nodes
    double maxdistance = Double.MIN_VALUE;
    double mindistance = Double.MAX_VALUE;
    double[][] distanceMatrix = new double[nNodes][nNodes];
    for (int i = 0; i < nNodes; i++) {
        for (int j = i + 1; j < nNodes; j++) {
            double distance = context.metric.getSelectedValue().getMetric(matrix, matrix, i, j);
            maxdistance = Math.max(maxdistance, distance);
            mindistance = Math.min(mindistance, distance);
            distanceMatrix[i][j] = distance;
        }
        if (canceled)
            return;
        monitor.setProgress((double) i / ((double) nNodes * 4));
    }
    monitor.setStatusMessage("Min distance = " + mindistance + ", max distance = " + maxdistance);
    monitor.setStatusMessage("Assigning values to edges");
    List<CyEdge> edgeList = new ArrayList<CyEdge>();
    double scale = maxdistance - mindistance;
    CyNetwork newNet = null;
    if (context.createNewNetwork) {
        newNet = ModelUtils.createChildNetwork(clusterManager, network, network.getNodeList(), null, "--clustered");
    }
    for (int i = 0; i < nNodes; i++) {
        for (int j = i + 1; j < nNodes; j++) {
            // time = System.currentTimeMillis();
            // This scales the distances to be between 0.0 and 1.0
            double distance = (distanceMatrix[i][j] - mindistance) / scale;
            if (context.createNewNetwork == true && distance > context.edgeCutoff && context.edgeCutoff != 0.0)
                continue;
            CyNode source = (CyNode) ModelUtils.getNetworkObjectWithName(network, matrix.getRowLabel(i), CyNode.class);
            CyNode target = (CyNode) ModelUtils.getNetworkObjectWithName(network, matrix.getRowLabel(j), CyNode.class);
            // time = System.currentTimeMillis();
            if (context.createNewNetwork == true) {
                CyEdge edge = newNet.addEdge(source, target, false);
                ModelUtils.createAndSet(newNet, edge, context.edgeAttribute, distance, Double.class, null);
                edgeList.add(edge);
            } else {
                List<CyEdge> connectingEdges = network.getConnectingEdgeList(source, target, CyEdge.Type.ANY);
                // time = System.currentTimeMillis();
                if (connectingEdges == null || connectingEdges.size() == 0)
                    continue;
                CyEdge edge = connectingEdges.get(0);
                ModelUtils.createAndSet(network, edge, context.edgeAttribute, distance, Double.class, null);
            }
        }
        if (canceled)
            return;
        monitor.setProgress(25.0 + (75.0 * (double) i / (double) nNodes) / 100.0);
    }
    System.out.println("Network created -- creating view");
    // If we're supposed to, create the new network
    if (context.createNewNetwork) {
        VisualStyle style = ViewUtils.getCurrentVisualStyle(clusterManager);
        CyNetworkView view = ViewUtils.createView(clusterManager, newNet, false);
        ViewUtils.doLayout(clusterManager, view, monitor, "force-directed");
        ViewUtils.setVisualStyle(clusterManager, view, style);
        ViewUtils.registerView(clusterManager, view);
    }
    /*
		System.out.println("Created "+newEdges+" edges");
		System.out.println("Edge creation time: "+edgeCreateTime+"ms");
		System.out.println("Network add time: "+networkAddTime+"ms");
		System.out.println("Edge fetch time: "+edgeFetchTime+"ms");
		System.out.println("Node fetch time: "+nodeFetchTime+"ms");
		System.out.println("Set attribute time: "+setAttributeTime+"ms");
		*/
    monitor.setStatusMessage("Complete");
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyEdge(org.cytoscape.model.CyEdge) CyNode(org.cytoscape.model.CyNode) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CyNetworkView(org.cytoscape.view.model.CyNetworkView)

Example 94 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project clusterMaker2 by RBVI.

the class ViewUtils method copyStyle.

public static VisualStyle copyStyle(ClusterManager manager, VisualStyle style, String suffix) {
    VisualStyle newStyle = manager.getService(VisualStyleFactory.class).createVisualStyle(style);
    newStyle.setTitle(style.getTitle() + suffix);
    manager.getService(VisualMappingManager.class).addVisualStyle(newStyle);
    return newStyle;
}
Also used : VisualStyle(org.cytoscape.view.vizmap.VisualStyle) VisualMappingManager(org.cytoscape.view.vizmap.VisualMappingManager) VisualStyleFactory(org.cytoscape.view.vizmap.VisualStyleFactory)

Example 95 with VisualStyle

use of org.cytoscape.view.vizmap.VisualStyle in project clusterMaker2 by RBVI.

the class NewNetworkView method createClusteredNetwork.

@SuppressWarnings("unchecked")
private void createClusteredNetwork(String clusterAttribute, TaskMonitor monitor) {
    boolean isFuzzy = isFuzzy(clusterAttribute);
    // System.out.println("isFuzzy = "+isFuzzy);
    // Get the clustering parameters
    Map<String, String> params = getParams();
    List<CyNode> nodeList = new ArrayList<CyNode>();
    Map<Integer, List<CyNode>> clusterMap = getClusterMap(clusterAttribute, nodeList);
    // Special handling for edge weight thresholds
    EdgeWeightConverter converter = edgeConverterList.getConverter(getParam(params, "converter"));
    String dataAttribute = getParam(params, "dataAttribute");
    double cutOff = 0.0;
    if (getParam(params, "edgeCutOff") != null)
        cutOff = Double.parseDouble(getParam(params, "edgeCutOff"));
    HashMap<CyEdge, CyEdge> edgeMap = new HashMap<CyEdge, CyEdge>();
    List<CyEdge> edgeList = new ArrayList<CyEdge>();
    // System.out.println("Getting the edges");
    for (Integer cluster : clusterMap.keySet()) {
        // Get the list of nodes
        List<CyNode> clusterNodes = clusterMap.get(cluster);
        // Get the list of edges
        List<CyEdge> connectingEdges = ModelUtils.getConnectingEdges(network, clusterNodes);
        for (CyEdge edge : connectingEdges) {
            if (converter != null && dataAttribute != null) {
                if (edgeWeightCheck(edge, dataAttribute, converter, cutOff))
                    continue;
            }
            edgeMap.put(edge, edge);
            // Add the cluster attribute to the edge so we can style it later
            ModelUtils.createAndSetLocal(network, edge, clusterAttribute, new Integer(1), Integer.class, null);
            edgeList.add(edge);
        }
    }
    // System.out.println("Getting the style");
    VisualStyle style = ViewUtils.getCurrentVisualStyle(manager);
    // System.out.println("Creating the network");
    CyNetwork newNetwork = ModelUtils.createChildNetwork(manager, network, nodeList, edgeList, "--clustered");
    // Now, copy the cluster attribute from the original network to this one
    ModelUtils.copyLocalColumn(network, newNetwork, CyNode.class, clusterAttribute);
    // Copy the clustering information over
    ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__clusterType");
    ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__clusterAttribute");
    ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__clusterParams");
    // Finally, if we're fuzzy, see if we had an initial seed and copy that over
    if (isFuzzy && ModelUtils.hasAttribute(network, network, "__fuzzifierSeed")) {
        ModelUtils.copyLocalColumn(network, newNetwork, CyNetwork.class, "__fuzzifierSeed");
        String seedAttribute = network.getRow(network, CyNetwork.LOCAL_ATTRS).get("__fuzzifierSeed", String.class);
        ModelUtils.copyLocalColumn(network, newNetwork, CyNode.class, seedAttribute);
    }
    // System.out.println("Getting the view");
    networkView = ViewUtils.createView(manager, newNetwork, false);
    ViewUtils.doLayout(manager, networkView, monitor, "force-directed");
    // Now, if we're supposed to, restore the inter-cluster edges
    if (restoreEdges || (context != null && context.restoreEdges)) {
        for (CyEdge edge : network.getEdgeList()) {
            if (!edgeMap.containsKey(edge)) {
                ((CySubNetwork) networkView.getModel()).addEdge(edge);
                ModelUtils.createAndSetLocal(networkView.getModel(), edge, clusterAttribute, new Integer(0), Integer.class, null);
            }
        }
        style = styleNewView(style, clusterAttribute);
    }
    // System.out.println("Setting the style");
    ViewUtils.setVisualStyle(manager, networkView, style);
    if (isFuzzy) {
        long fuzzyClusterTableSUID = network.getRow(network).get(clusterAttribute + "_Table.SUID", Long.class);
        newNetwork.getRow(newNetwork).set(clusterAttribute + "_Table.SUID", fuzzyClusterTableSUID);
        // System.out.println("NetworkName: "+ network.getRow(network).get(CyNetwork.NAME, String.class));
        // System.out.println("Fuzzy Table SUID: " + fuzzyClusterTableSUID );
        CyTable fuzzyClusterTable = manager.getTableManager().getTable(fuzzyClusterTableSUID);
        // System.out.println("Creating membership edges");
        createMembershipEdges(newNetwork, networkView, manager, fuzzyClusterTable);
    }
    ViewUtils.registerView(manager, networkView);
    return;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyEdge(org.cytoscape.model.CyEdge) CyTable(org.cytoscape.model.CyTable) EdgeWeightConverter(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.edgeConverters.EdgeWeightConverter) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork)

Aggregations

VisualStyle (org.cytoscape.view.vizmap.VisualStyle)100 CyNetworkView (org.cytoscape.view.model.CyNetworkView)42 VisualMappingManager (org.cytoscape.view.vizmap.VisualMappingManager)37 CyNetwork (org.cytoscape.model.CyNetwork)35 CyNode (org.cytoscape.model.CyNode)30 CyEdge (org.cytoscape.model.CyEdge)24 CyEventHelper (org.cytoscape.event.CyEventHelper)14 HashSet (java.util.HashSet)13 VisualProperty (org.cytoscape.view.model.VisualProperty)12 DiscreteMapping (org.cytoscape.view.vizmap.mappings.DiscreteMapping)12 Paint (java.awt.Paint)11 HashMap (java.util.HashMap)11 CyApplicationManager (org.cytoscape.application.CyApplicationManager)11 CyNetworkViewManager (org.cytoscape.view.model.CyNetworkViewManager)11 Color (java.awt.Color)10 ArrayList (java.util.ArrayList)10 RenderingEngineManager (org.cytoscape.view.presentation.RenderingEngineManager)9 BasicVisualLexicon (org.cytoscape.view.presentation.property.BasicVisualLexicon)9 ContinuousMapping (org.cytoscape.view.vizmap.mappings.ContinuousMapping)9 View (org.cytoscape.view.model.View)8