Search in sources :

Example 1 with Network

use of org.cytoscape.io.internal.util.session.model.Network in project cytoscape-impl by cytoscape.

the class Cy2SessionReaderImpl method extractSessionState.

private void extractSessionState(InputStream is, String entryName) throws Exception {
    CyPropertyReader reader = propertyReaderMgr.getReader(is, entryName);
    reader.run(taskMonitor);
    cysession = (Cysession) reader.getProperty();
    if (cysession != null) {
        // Network attributes and visual styles
        if (cysession.getNetworkTree() != null) {
            for (final Network net : cysession.getNetworkTree().getNetwork()) {
                // so let's ignore a network with that name.
                if (net.getId().equals(NETWORK_ROOT))
                    continue;
                final String netName = net.getId();
                List<Node> selNodes = null;
                List<Edge> selEdges = null;
                if (net.getSelectedNodes() != null)
                    selNodes = net.getSelectedNodes().getNode();
                if (net.getSelectedEdges() != null)
                    selEdges = net.getSelectedEdges().getEdge();
                nodeSelectionLookup.put(netName, selNodes);
                edgeSelectionLookup.put(netName, selEdges);
            }
        }
        // Convert the old cysession to core the required 3.0 core plugin files:
        // Actually we just need to extract the "networkFrames" and "cytopanels" data from the Cysession object
        // and write an XML file that will be parsed by swing-application.
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
        sb.append("<sessionState documentVersion=\"1.0\">\n");
        sb.append("    <networkFrames>\n");
        if (cysession.getSessionState() != null && cysession.getSessionState().getDesktop() != null) {
            NetworkFrames netFrames = cysession.getSessionState().getDesktop().getNetworkFrames();
            if (netFrames != null) {
                for (NetworkFrame nf : netFrames.getNetworkFrame()) {
                    String id = nf.getFrameID();
                    String x = nf.getX() != null ? nf.getX().toString() : "0";
                    String y = nf.getY() != null ? nf.getY().toString() : "0";
                    sb.append("        <networkFrame networkViewID=\"" + id + "\" x=\"" + x + "\" y=\"" + y + "\"/>\n");
                }
            }
        }
        sb.append("    </networkFrames>\n");
        sb.append("    <cytopanels>\n");
        SessionState sessionState = cysession.getSessionState();
        if (sessionState != null) {
            Cytopanels cytopanels = sessionState.getCytopanels();
            if (cytopanels != null) {
                List<Cytopanel> cytopanelsList = cytopanels.getCytopanel();
                for (Cytopanel cytopanel : cytopanelsList) {
                    String id = cytopanel.getId();
                    String state = cytopanel.getPanelState();
                    String selection = cytopanel.getSelectedPanel();
                    sb.append("        <cytopanel id=\"" + id + "\">\n");
                    sb.append("            <panelState>" + state + "</panelState>\n");
                    sb.append("            <selectedPanel>" + selection + "</selectedPanel>\n");
                    sb.append("        </cytopanel>\n");
                }
            }
        }
        sb.append("    </cytopanels>\n");
        sb.append("</sessionState>");
        // Extract it as an app file now:
        ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
        extractPluginEntry(bais, cysession.getId() + "/" + PLUGINS_FOLDER + "org.cytoscape.swing-application/session_state.xml");
    }
}
Also used : SessionState(org.cytoscape.io.internal.util.session.model.SessionState) CyNode(org.cytoscape.model.CyNode) Node(org.cytoscape.io.internal.util.session.model.Node) NetworkFrames(org.cytoscape.io.internal.util.session.model.NetworkFrames) ByteArrayInputStream(java.io.ByteArrayInputStream) Cytopanels(org.cytoscape.io.internal.util.session.model.Cytopanels) CyNetwork(org.cytoscape.model.CyNetwork) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) Network(org.cytoscape.io.internal.util.session.model.Network) CyPropertyReader(org.cytoscape.io.read.CyPropertyReader) Edge(org.cytoscape.io.internal.util.session.model.Edge) CyEdge(org.cytoscape.model.CyEdge) NetworkFrame(org.cytoscape.io.internal.util.session.model.NetworkFrame) Cytopanel(org.cytoscape.io.internal.util.session.model.Cytopanel)

Example 2 with Network

use of org.cytoscape.io.internal.util.session.model.Network in project cytoscape-impl by cytoscape.

the class Cy2SessionReaderImpl method extractNetworks.

private void extractNetworks(TaskMonitor tm) throws JAXBException, IOException {
    // Extract the XGMML files
    Map<String, Network> netMap = new HashMap<>();
    for (Network curNet : cysession.getNetworkTree().getNetwork()) {
        netMap.put(curNet.getId(), curNet);
    }
    final CyRootNetworkManager rootNetworkManager = serviceRegistrar.getService(CyRootNetworkManager.class);
    walkNetworkTree(netMap.get(NETWORK_ROOT), null, netMap, tm, rootNetworkManager);
}
Also used : CyRootNetworkManager(org.cytoscape.model.subnetwork.CyRootNetworkManager) HashMap(java.util.HashMap) CyNetwork(org.cytoscape.model.CyNetwork) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) Network(org.cytoscape.io.internal.util.session.model.Network)

Example 3 with Network

use of org.cytoscape.io.internal.util.session.model.Network in project cytoscape-impl by cytoscape.

the class Cy2SessionReaderImpl method walkNetworkTree.

private void walkNetworkTree(final Network net, CyNetwork parent, final Map<String, Network> netMap, final TaskMonitor tm, final CyRootNetworkManager rootNetworkManager) {
    // Get the list of children under this root
    final List<Child> children = net.getChild();
    // Traverse using recursive call
    final int numChildren = children.size();
    Child child = null;
    Network childNet = null;
    for (int i = 0; i < numChildren; i++) {
        if (cancelled)
            return;
        child = children.get(i);
        childNet = netMap.get(child.getId());
        String entryName = xgmmlEntries.get(childNet.getFilename());
        InputStream is = null;
        // This is the original Cytoscape 2 parent.
        CyNetwork cy2Parent = null;
        try {
            is = findEntry(entryName);
            if (is != null) {
                tm.setStatusMessage("Extracting network: " + entryName);
                cy2Parent = extractNetworksAndViews(is, entryName, parent, childNet.isViewAvailable());
                // Every 2.x network should be a child of the same root-network.
                if (parent == null)
                    parent = rootNetworkManager.getRootNetwork(cy2Parent);
            } else {
                logger.error("Cannot find network file \"" + entryName + "\": ");
            }
        } catch (final Exception e) {
            final String message = "Unable to read XGMML file \"" + childNet.getFilename() + "\": " + e.getMessage();
            logger.error(message, e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (final Exception ex) {
                    logger.error("Unable to close XGMML input stream.", ex);
                }
                is = null;
            }
            // Always try to load child networks, even if the parent network is bad
            if (!cancelled && childNet.getChild().size() != 0)
                walkNetworkTree(childNet, cy2Parent, netMap, tm, rootNetworkManager);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CyNetwork(org.cytoscape.model.CyNetwork) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) Network(org.cytoscape.io.internal.util.session.model.Network) CyNetwork(org.cytoscape.model.CyNetwork) Child(org.cytoscape.io.internal.util.session.model.Child) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 4 with Network

use of org.cytoscape.io.internal.util.session.model.Network in project cytoscape-impl by cytoscape.

the class Cy2SessionReaderImpl method processNetworks.

private void processNetworks() throws Exception {
    if (cysession == null)
        return;
    // Network attributes and visual styles
    if (cysession.getNetworkTree() != null) {
        for (final Network net : cysession.getNetworkTree().getNetwork()) {
            if (cancelled)
                return;
            // so let's ignore a network with that name.
            if (net.getId().equals(NETWORK_ROOT))
                continue;
            final String netName = net.getId();
            final CyNetworkView view = getNetworkView(netName);
            if (view != null) {
                // Populate the visual style map
                String vsName = net.getVisualStyle();
                if (vsName != null)
                    visualStyleMap.put(view, vsName);
                // Convert 2.x hidden state (cysession.xml) to 3.x visual properties
                if (net.getHiddenEdges() != null) {
                    for (final Edge edgeObject : net.getHiddenEdges().getEdge()) {
                        if (cancelled)
                            return;
                        final String name = edgeObject.getId();
                        final CyEdge e = cache.getEdge(name);
                        if (e != null) {
                            final View<CyEdge> ev = view.getEdgeView(e);
                            if (ev != null)
                                ev.setLockedValue(BasicVisualLexicon.EDGE_VISIBLE, false);
                            else
                                logger.error("Cannot restore hidden state of edge \"" + name + "\": Edge view not found.");
                        } else {
                            logger.error("Cannot restore hidden state of edge \"" + name + "\": Edge not found.");
                        }
                    }
                }
                if (net.getHiddenNodes() != null) {
                    for (final Node nodeObject : net.getHiddenNodes().getNode()) {
                        if (cancelled)
                            return;
                        final String name = nodeObject.getId();
                        final CyNode n = cache.getNodeByName(name);
                        if (n != null) {
                            final View<CyNode> nv = view.getNodeView(n);
                            if (nv != null)
                                nv.setLockedValue(BasicVisualLexicon.NODE_VISIBLE, false);
                            else
                                logger.error("Cannot restore hidden state of node \"" + name + "\": Node view not found.");
                        } else {
                            logger.error("Cannot restore hidden state of node \"" + name + "\": Node not found.");
                        }
                    }
                }
            }
        }
    }
    // Network view sizes
    if (cysession.getSessionState() != null) {
        Desktop desktop = cysession.getSessionState().getDesktop();
        if (desktop != null && desktop.getNetworkFrames() != null) {
            List<NetworkFrame> frames = desktop.getNetworkFrames().getNetworkFrame();
            for (final NetworkFrame nf : frames) {
                if (cancelled)
                    return;
                // Set sizes
                final CyNetworkView view = getNetworkView(nf.getFrameID());
                if (view != null) {
                    BigInteger w = nf.getWidth();
                    BigInteger h = nf.getHeight();
                    if (w != null)
                        view.setVisualProperty(BasicVisualLexicon.NETWORK_WIDTH, w.doubleValue());
                    if (h != null)
                        view.setVisualProperty(BasicVisualLexicon.NETWORK_HEIGHT, h.doubleValue());
                }
            }
        }
    }
    if (!networks.isEmpty()) {
        // Select the last network that has a view, since Cy2 files do not contain the network selection info
        final CyNetwork[] array = networks.toArray(new CyNetwork[networks.size()]);
        for (int i = array.length - 1; i >= 0; i--) {
            final CyNetwork net = array[i];
            final CyRow row = net.getRow(net);
            final String netName = row.get(CyNetwork.NAME, String.class);
            final CyNetworkView view = networkViewLookup.get(netName);
            if (view != null) {
                row.set(CyNetwork.SELECTED, true);
                break;
            }
        }
    }
}
Also used : CyNode(org.cytoscape.model.CyNode) Node(org.cytoscape.io.internal.util.session.model.Node) CyNetwork(org.cytoscape.model.CyNetwork) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) Desktop(org.cytoscape.io.internal.util.session.model.Desktop) CyNetwork(org.cytoscape.model.CyNetwork) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) Network(org.cytoscape.io.internal.util.session.model.Network) BigInteger(java.math.BigInteger) CyNode(org.cytoscape.model.CyNode) CyNetworkView(org.cytoscape.view.model.CyNetworkView) Edge(org.cytoscape.io.internal.util.session.model.Edge) CyEdge(org.cytoscape.model.CyEdge) NetworkFrame(org.cytoscape.io.internal.util.session.model.NetworkFrame)

Aggregations

Network (org.cytoscape.io.internal.util.session.model.Network)4 CyNetwork (org.cytoscape.model.CyNetwork)4 CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Edge (org.cytoscape.io.internal.util.session.model.Edge)2 NetworkFrame (org.cytoscape.io.internal.util.session.model.NetworkFrame)2 Node (org.cytoscape.io.internal.util.session.model.Node)2 CyEdge (org.cytoscape.model.CyEdge)2 CyNode (org.cytoscape.model.CyNode)2 BufferedInputStream (java.io.BufferedInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 JAXBException (javax.xml.bind.JAXBException)1 Child (org.cytoscape.io.internal.util.session.model.Child)1 Cytopanel (org.cytoscape.io.internal.util.session.model.Cytopanel)1 Cytopanels (org.cytoscape.io.internal.util.session.model.Cytopanels)1 Desktop (org.cytoscape.io.internal.util.session.model.Desktop)1