Search in sources :

Example 71 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class HandleEdge method handle.

@Override
public ParseState handle(final String tag, final Attributes atts, final ParseState current) throws SAXException {
    // Get the label, id, source and target
    Object id = null;
    String href = atts.getValue(ReadDataManager.XLINK, "href");
    if (href == null) {
        // Create the edge:
        id = getId(atts);
        String label = getLabel(atts);
        Object sourceId = asLongOrString(atts.getValue("source"));
        Object targetId = asLongOrString(atts.getValue("target"));
        String isDirected = atts.getValue("cy:directed");
        String sourceAlias = null;
        String targetAlias = null;
        // no longer users
        String interaction = "";
        if (label != null) {
            // Parse out the interaction (if this is from Cytoscape)
            // parts[0] = source alias
            // parts[1] = interaction
            // parts[2] = target alias
            final String[] parts = SPLIT.split(label);
            if (parts.length == 3) {
                sourceAlias = parts[0];
                interaction = parts[1];
                targetAlias = parts[2];
            }
        }
        final boolean directed;
        if (isDirected == null) {
            // xgmml files made by pre-3.0 cytoscape and strictly
            // upstream-XGMML conforming files
            // won't have directedness flag, in which case use the
            // graph-global directedness setting.
            // 
            // (org.xml.sax.Attributes.getValue() returns null if attribute
            // does not exists)
            // 
            // This is the correct way to read the edge-directionality of
            // non-cytoscape xgmml files as well.
            directed = manager.currentNetworkIsDirected;
        } else {
            // parse directedness flag
            directed = ObjectTypeMap.fromXGMMLBoolean(isDirected);
        }
        CyNode sourceNode = null;
        CyNode targetNode = null;
        if (sourceId != null)
            sourceNode = manager.getCache().getNode(sourceId);
        if (targetId != null)
            targetNode = manager.getCache().getNode(targetId);
        if (sourceNode == null && sourceAlias != null)
            sourceNode = manager.getCache().getNode(sourceAlias);
        if (targetNode == null && targetAlias != null)
            targetNode = manager.getCache().getNode(targetAlias);
        if (label == null || label.isEmpty())
            label = String.format("%s (%s) %s", sourceId, (directed ? "directed" : "undirected"), targetId);
        CyNetwork net = manager.getCurrentNetwork();
        CyEdge edge = null;
        if (sourceNode != null && targetNode != null) {
            // We need to do this because of groups that are exported from
            // Cytoscape 2.x.
            // The problem is that internal edges are duplicated in the
            // document when the group is expanded,
            // but we don't want to create them twice.
            boolean checkDuplicate = manager.getCache().hasNetworkPointers() && manager.getDocumentVersion() > 0.0 && manager.getDocumentVersion() < 3.0;
            edge = checkDuplicate ? manager.getCache().getEdge(id) : null;
            if (edge == null) {
                edge = manager.createEdge(sourceNode, targetNode, id, label, directed, net);
            } else if (net instanceof CySubNetwork && !net.containsEdge(edge)) {
                ((CySubNetwork) net).addEdge(edge);
            }
        } else {
            edge = manager.createEdge(sourceId, targetId, id, label, directed, net);
        }
        if (edge != null) {
            if (!manager.isSessionFormat() || manager.getDocumentVersion() < 3.0) {
                // the root-network only.
                if (!net.containsEdge(edge))
                    net = manager.getRootNetwork();
                if (net != null && net.containsEdge(edge)) {
                    CyRow row = net.getRow(edge);
                    row.set(CyNetwork.NAME, label);
                    row.set(CyEdge.INTERACTION, interaction);
                }
                if (manager.getRootNetwork() != null && !manager.getRootNetwork().equals(net)) {
                    CyRow row = manager.getRootNetwork().getRow(edge);
                    row.set(CyNetwork.NAME, label);
                    row.set(CyEdge.INTERACTION, interaction);
                }
            }
            manager.setCurrentElement(edge);
        } else {
            throw new SAXException("Cannot create edge from XGMML (id=" + id + " label=" + label + " source=" + sourceId + " target=" + targetId + "): source or target node not found");
        }
    } else {
        // The edge might not have been created yet!
        // Save the reference so it can be added to the network after the
        // whole graph is parsed.
        manager.addElementLink(href, CyEdge.class);
    }
    return current;
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) CyNode(org.cytoscape.model.CyNode) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) SAXException(org.xml.sax.SAXException)

Example 72 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class HandleNode method handle.

@Override
public ParseState handle(final String tag, final Attributes atts, final ParseState current) throws SAXException {
    final String href = atts.getValue(ReadDataManager.XLINK, "href");
    Object id = null;
    String label = null;
    CyNode node = null;
    final CyNetwork curNet = manager.getCurrentNetwork();
    final CyNetwork rootNet = manager.getRootNetwork();
    if (href == null) {
        // Create the node
        id = getId(atts);
        label = atts.getValue("label");
        if (label == null)
            // For backwards compatibility
            label = atts.getValue("name");
        node = manager.getCache().getNode(id);
        if (node == null)
            node = manager.createNode(id, label, curNet);
        else if (curNet instanceof CySubNetwork)
            manager.addNode(node, label, (CySubNetwork) curNet);
        if (label != null && (!manager.isSessionFormat() || manager.getDocumentVersion() < 3.0)) {
            if (!manager.isSessionFormat()) {
                if (!curNet.containsNode(node) && curNet instanceof CySubNetwork) {
                    // The node should be node in root network, it does not exist in current subnetwork yet
                    CySubNetwork subnet = (CySubNetwork) curNet;
                    subnet.addNode(node);
                    node = subnet.getNode(node.getSUID());
                }
            }
            curNet.getRow(node).set(CyNetwork.NAME, label);
            if (rootNet != null && curNet != rootNet)
                rootNet.getRow(node).set(CyNetwork.NAME, label);
        }
    } else {
        // Try to get the node from the internal cache
        id = XGMMLParseUtil.getIdFromXLink(href);
        node = manager.getCache().getNode(id);
        if (node != null) {
            if (curNet instanceof CySubNetwork)
                ((CySubNetwork) curNet).addNode(node);
            else
                logger.error("Cannot add existing node \"" + id + "\" to a network which is not a CySubNetwork");
        } else {
            // The node might not have been created yet!
            // So just save the reference so it can be added to the network after the whole graph is parsed.
            manager.addElementLink(href, CyNode.class);
        }
    }
    if (node != null)
        manager.setCurrentElement(node);
    return current;
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) CyNode(org.cytoscape.model.CyNode) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork)

Example 73 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class SessionXGMMLNetworkWriterTest method testRegisteredSubNetworkSavedIfSessionSavePolicy.

@Test
public void testRegisteredSubNetworkSavedIfSessionSavePolicy() throws UnsupportedEncodingException {
    CySubNetwork sn = rootNet.addSubNetwork();
    // It doesn't matter
    setRegistered(sn, true);
    write(rootNet);
    assertEquals("1", evalString("/x:graph/x:att/x:graph[@id=" + sn.getSUID() + "]/@cy:registered"));
}
Also used : CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) Test(org.junit.Test)

Example 74 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class SessionXGMMLNetworkWriterTest method testRegisteredNetworkPointerSavedUnderRootGraph.

@Test
public void testRegisteredNetworkPointerSavedUnderRootGraph() throws UnsupportedEncodingException {
    CySubNetwork sn = rootNet.addSubNetwork();
    setRegistered(sn, true);
    CyNode n = net.getNodeList().get(0);
    n.setNetworkPointer(sn);
    write(rootNet);
    // The subnetwork is saved under the root graph, because it is registered
    assertEquals(0, evalNumber("count(/x:graph/x:att/x:graph[@id=" + n.getSUID() + "])"));
    // The nested node graph (network pointer) is an XLink to that graph
    assertEquals("#" + sn.getSUID(), evalString("//x:node[@id=" + n.getSUID() + "]/x:att/x:graph/@xlink:href"));
}
Also used : CyNode(org.cytoscape.model.CyNode) CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) Test(org.junit.Test)

Example 75 with CySubNetwork

use of org.cytoscape.model.subnetwork.CySubNetwork in project cytoscape-impl by cytoscape.

the class SessionXGMMLNetworkWriterTest method testSubNetworkIgnoredIfNotSessionSavePolicy.

@Test
public void testSubNetworkIgnoredIfNotSessionSavePolicy() throws UnsupportedEncodingException {
    CySubNetwork sn = rootNet.addSubNetwork(SavePolicy.DO_NOT_SAVE);
    // It doesn't matter
    setRegistered(sn, true);
    write(rootNet);
    assertEquals(2, evalNumber("count(//x:graph)"));
    assertEquals(0, evalNumber("count(//x:graph[@id=" + sn.getSUID() + "])"));
}
Also used : CySubNetwork(org.cytoscape.model.subnetwork.CySubNetwork) Test(org.junit.Test)

Aggregations

CySubNetwork (org.cytoscape.model.subnetwork.CySubNetwork)88 CyNode (org.cytoscape.model.CyNode)44 CyRootNetwork (org.cytoscape.model.subnetwork.CyRootNetwork)35 CyNetwork (org.cytoscape.model.CyNetwork)31 CyEdge (org.cytoscape.model.CyEdge)28 Test (org.junit.Test)19 CyRow (org.cytoscape.model.CyRow)15 ArrayList (java.util.ArrayList)12 CyTable (org.cytoscape.model.CyTable)11 CyNetworkView (org.cytoscape.view.model.CyNetworkView)11 HashSet (java.util.HashSet)7 CyGroup (org.cytoscape.group.CyGroup)7 HashMap (java.util.HashMap)6 CyNetworkManager (org.cytoscape.model.CyNetworkManager)6 CyApplicationManager (org.cytoscape.application.CyApplicationManager)5 CyEventHelper (org.cytoscape.event.CyEventHelper)5 CyNetworkViewManager (org.cytoscape.view.model.CyNetworkViewManager)5 Dimension (java.awt.Dimension)3 GroupCollapsedEvent (org.cytoscape.group.events.GroupCollapsedEvent)3 VisualStyle (org.cytoscape.view.vizmap.VisualStyle)3