Author: clopes
Date: 2011-12-05 12:38:30 -0800 (Mon, 05 Dec 2011)
New Revision: 27692

Modified:
   
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/session/Cy2SessionReaderImpl.java
   
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/AttributeValueUtil.java
   
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/HandleNode.java
   
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/ReadDataManager.java
   
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/XGMMLWriter.java
Log:
Fixed bug when reading XGMML with XLinks (edges could be parsed before creating 
source/target nodes).
Fixed saving/restoring network view width/height.
When saving view-format XGMML files into the cys file, the XGMML writer only 
writes edge tags that have locked visual properties, to prevent empty edge tags.

Modified: 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/session/Cy2SessionReaderImpl.java
===================================================================
--- 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/session/Cy2SessionReaderImpl.java
        2011-12-03 00:36:19 UTC (rev 27691)
+++ 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/session/Cy2SessionReaderImpl.java
        2011-12-05 20:38:30 UTC (rev 27692)
@@ -44,6 +44,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
+import java.math.BigInteger;
 import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -74,10 +75,13 @@
 import org.cytoscape.property.bookmark.Bookmarks;
 import org.cytoscape.property.session.Child;
 import org.cytoscape.property.session.Cysession;
+import org.cytoscape.property.session.Desktop;
 import org.cytoscape.property.session.Edge;
 import org.cytoscape.property.session.Network;
+import org.cytoscape.property.session.NetworkFrame;
 import org.cytoscape.property.session.Node;
 import org.cytoscape.view.model.CyNetworkView;
+import org.cytoscape.view.presentation.property.MinimalVisualLexicon;
 
 /**
  * Session reader implementation that handles the Cytoscape 2.x session format.
@@ -203,7 +207,7 @@
                                is = findEntry(entryName);
                                
                                if (is != null) {
-                                       rootNetwork = extractNetworks(is, 
entryName, parent, childNet.isViewAvailable());
+                                       rootNetwork = 
extractNetworksAndViews(is, entryName, parent, childNet.isViewAvailable());
                                } else {
                                        logger.error("Cannot find network file 
\"" + entryName + "\": ");
                                }
@@ -236,8 +240,8 @@
         * @return The root-network of the extracted networks
         * @throws Exception
         */
-       private CyRootNetwork extractNetworks(InputStream is, String entryName, 
CyRootNetwork parent, boolean createView)
-                       throws Exception {
+       private CyRootNetwork extractNetworksAndViews(InputStream is, String 
entryName, CyRootNetwork parent,
+                       boolean createView) throws Exception {
                this.tm.setStatusMessage("Extracting network: " + entryName);
                
                CyRootNetwork rootNetwork = null;
@@ -264,19 +268,23 @@
                                views = new CyNetworkView[netArray.length];
 
                        int i = 0;
+                       String netName = null;
 
                        for (CyNetwork net : netArray) {
-                               networkLookup.put(entryName, net);
+                               netName = net.getCyRow().get(CyNetwork.NAME, 
String.class);
+                               
+                               networkLookup.put(netName, net);
                                networks.add(net);
                                
                                if (createView) {
-                                       views[i] = 
reader.buildCyNetworkView(net);
+                                       CyNetworkView netView = 
reader.buildCyNetworkView(net);
+                                       views[i] = netView;
                                        i++;
                                }
                        }
 
                        if (views != null) {
-                               networkViewLookup.put(entryName, views);
+                               networkViewLookup.put(netName, views);
                                networkViews.addAll(Arrays.asList(views));
                        }
                }
@@ -354,66 +362,93 @@
        }
 
        private void processNetworks() throws Exception {
-               if (cysession.getNetworkTree() == null)
-                       return;
-
-               for (final Network net : 
cysession.getNetworkTree().getNetwork()) {
-                       // We no longer have the concept of one top-level 
network root,
-                       // so let's ignore a network with that name.
-                       if (net.getId().equals(NETWORK_ROOT))
-                               continue;
-
-                       final String fileName = net.getFilename();
-                       final CyNetworkView view = getNetworkView(fileName);
-
-                       if (view != null) {
-                               String vsName = net.getVisualStyle();
-
-                               if (vsName != null)
-                                       visualStyleMap.put(view, vsName);
+               if (cysession == null) return;
+               
+               // Network attributes and visual styles
+               if (cysession.getNetworkTree() != null) {
+                       for (final Network net : 
cysession.getNetworkTree().getNetwork()) {
+                               // We no longer have the concept of one 
top-level network root,
+                               // so let's ignore a network with that name.
+                               if (net.getId().equals(NETWORK_ROOT))
+                                       continue;
+       
+                               final String netName = net.getId();
+                               final CyNetworkView[] views = 
getNetworkViews(netName);
+       
+                               if (views != null) {
+                                       for (CyNetworkView nv : views) {
+                                               String vsName = 
net.getVisualStyle();
+               
+                                               if (vsName != null)
+                                                       visualStyleMap.put(nv, 
vsName);
+                                       }
+                               }
+       
+                               final CyNetwork cyNet = getNetwork(netName);
+       
+                               if (cyNet != null) {
+                                       // TODO: check if should delete from 
network
+//                                     if (net.getHiddenNodes() != null)
+//                                             setBooleanNodeAttr(cyNet, 
net.getHiddenNodes().getNode().iterator(), "hidden");
+//                                     if (net.getHiddenEdges() != null)
+//                                             setBooleanEdgeAttr(cyNet, 
net.getHiddenEdges().getEdge().iterator(), "hidden");
+       
+                                       // From Cytoscape 3.0, the selection 
info is stored inside CyTables,
+                                       // but 2.x stores that info in the 
cysession.xml file.
+                                       if (net.getSelectedNodes() != null)
+                                               setBooleanNodeAttr(cyNet, 
net.getSelectedNodes().getNode().iterator(), CyNetwork.SELECTED);
+                                       if (net.getSelectedEdges() != null)
+                                               setBooleanEdgeAttr(cyNet, 
net.getSelectedEdges().getEdge().iterator(), CyNetwork.SELECTED);
+                               }
                        }
+               }
+               
+               // Network view sizes
+               if (cysession.getSessionState() != null) {
+                       Desktop desktop = 
cysession.getSessionState().getDesktop();
+                       
+                       if (desktop != null && desktop.getNetworkFrames() != 
null) {
+                               List<NetworkFrame> frames = 
desktop.getNetworkFrames().getNetworkFrame();
 
-                       final CyNetwork cyNet = getNetwork(fileName);
-
-                       if (cyNet != null) {
-                               // TODO: check if should delete from network
-//                             if (net.getHiddenNodes() != null)
-//                                     setBooleanNodeAttr(cyNet, 
net.getHiddenNodes().getNode().iterator(), "hidden");
-//                             if (net.getHiddenEdges() != null)
-//                                     setBooleanEdgeAttr(cyNet, 
net.getHiddenEdges().getEdge().iterator(), "hidden");
-
-                               // From Cytoscape 3.0, the selection info is 
stored inside CyTables,
-                               // but 2.x stores that info in the 
cysession.xml file.
-                               if (net.getSelectedNodes() != null)
-                                       setBooleanNodeAttr(cyNet, 
net.getSelectedNodes().getNode().iterator(), CyNetwork.SELECTED);
-                               if (net.getSelectedEdges() != null)
-                                       setBooleanEdgeAttr(cyNet, 
net.getSelectedEdges().getEdge().iterator(), CyNetwork.SELECTED);
+                               for (NetworkFrame nf : frames) {
+                                       // Set sizes
+                                       CyNetworkView[] views = 
getNetworkViews(nf.getFrameID());
+                                       
+                                       if (views != null) {
+                                               for (CyNetworkView nv : views) {
+                                                       BigInteger w = 
nf.getWidth();
+                                                       BigInteger h = 
nf.getHeight();
+       
+                                                       if (w != null)
+                                                               
nv.setVisualProperty(MinimalVisualLexicon.NETWORK_WIDTH, w.doubleValue());
+                                                       if (h != null)
+                                                               
nv.setVisualProperty(MinimalVisualLexicon.NETWORK_HEIGHT, h.doubleValue());
+                                               }
+                                       }
+                               }
                        }
                }
        }
 
-       private CyNetworkView getNetworkView(final String name) {
+       private CyNetworkView[] getNetworkViews(final String name) {
+               CyNetworkView[] views = null;
+               
                for (String s : networkViewLookup.keySet()) {
                        String decode = s;
 
                        try {
                                decode = URLDecoder.decode(s, "UTF-8");
+                               
+                               if (decode.endsWith(name)) {
+                                       // this is OK since XGMML only ever 
reads one network
+                                       views = networkViewLookup.get(s);
+                               }
                        } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
-                               return null;
                        }
-
-                       if (decode.endsWith("/" + name)) {
-                               // this is OK since XGMML only ever reads one 
network
-                               CyNetworkView[] views = 
networkViewLookup.get(s);
-
-                               if (views != null && views.length > 0) {
-                                       return views[0];
-                               }
-                       }
                }
 
-               return null;
+               return views;
        }
 
        private CyNetwork getNetwork(final String name) {

Modified: 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/AttributeValueUtil.java
===================================================================
--- 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/AttributeValueUtil.java
    2011-12-03 00:36:19 UTC (rev 27691)
+++ 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/AttributeValueUtil.java
    2011-12-05 20:38:30 UTC (rev 27692)
@@ -1,6 +1,8 @@
 package org.cytoscape.io.internal.read.xgmml.handler;
 
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.cytoscape.io.internal.read.xgmml.MetadataEntries;
 import org.cytoscape.io.internal.read.xgmml.MetadataParser;
@@ -23,6 +25,8 @@
     static final String ATTR_LABEL = "label";
     static final String ATTR_VALUE = "value";
     static final String LOCKED_VISUAL_PROPS = "lockedVisualProperties";
+    
+    static final Pattern XLINK_PATTERN = Pattern.compile(".*#(\\d+)");
 
     private Locator locator;
 
@@ -218,4 +222,9 @@
        
        return version;
        }
+    
+       public static String getIdFromXLink(String href) {
+               Matcher matcher = XLINK_PATTERN.matcher(href);
+               return matcher.matches() ? matcher.group(1) : null;
+       }
 }

Modified: 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/HandleNode.java
===================================================================
--- 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/HandleNode.java
    2011-12-03 00:36:19 UTC (rev 27691)
+++ 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/HandleNode.java
    2011-12-05 20:38:30 UTC (rev 27692)
@@ -1,7 +1,9 @@
 package org.cytoscape.io.internal.read.xgmml.handler;
 
 import org.cytoscape.io.internal.read.xgmml.ParseState;
+import org.cytoscape.model.CyNetwork;
 import org.cytoscape.model.CyNode;
+import org.cytoscape.model.subnetwork.CySubNetwork;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 
@@ -26,9 +28,22 @@
                                node.getCyRow().set(CyNode.NAME, label);
                        }
                } 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);
+                       // Try to get the node from the internal cache
+                       String id = AttributeValueUtil.getIdFromXLink(href);
+                       CyNode node = manager.getNode(id);
+                       
+                       if (node != null) {
+                               CyNetwork net = manager.getCurrentNetwork();
+                               
+                               if (net instanceof CySubNetwork)
+                                       ((CySubNetwork) net).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);
+                       }
                }
                
                return current;

Modified: 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/ReadDataManager.java
===================================================================
--- 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/ReadDataManager.java
       2011-12-03 00:36:19 UTC (rev 27691)
+++ 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/xgmml/handler/ReadDataManager.java
       2011-12-05 20:38:30 UTC (rev 27692)
@@ -40,7 +40,7 @@
 public class ReadDataManager {
 
        protected final static String XLINK = "http://www.w3.org/1999/xlink";;
-
+       
        /* RDF Data */
        protected String RDFDate;
        protected String RDFTitle;
@@ -471,7 +471,7 @@
        
        protected void addElementLink(String href, Class<? extends 
CyTableEntry> clazz) {
                Map<CyNetwork, Set<String>> map = null;
-               String id = href.replace("#", "").trim();
+               String id = AttributeValueUtil.getIdFromXLink(href);
                
                if (clazz == CyNode.class)      map = nodeLinkMap;
                else if (clazz == CyEdge.class) map = edgeLinkMap;

Modified: 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/XGMMLWriter.java
===================================================================
--- 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/XGMMLWriter.java
  2011-12-03 00:36:19 UTC (rev 27691)
+++ 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/XGMMLWriter.java
  2011-12-05 20:38:30 UTC (rev 27692)
@@ -554,21 +554,34 @@
      * @throws IOException
      */
        private void writeEdgeView(View<CyEdge> view) throws IOException {
-               writeElement("<edge");
-               writeAttributePair("id", view.getSUID());
-               writeAttributePair("label", getLabel(view.getModel()));
-               writeAttributePair("cy:edgeId", view.getModel().getSUID());
-               write(">\n");
-               depth++;
-
-               // Write the edge graphics
-               writeGraphics(view);
-
-               depth--;
-               writeElement("</edge>\n");
+               // It is not necessary to write edges that have no locked 
visual properties
+               boolean hasLockedVisualProps = false;
+               Collection<VisualProperty<?>> visualProperties = 
visualLexicon.getAllDescendants(MinimalVisualLexicon.EDGE);
+               
+               for (VisualProperty<?> vp : visualProperties) {
+                       if (view.isValueLocked(vp)) {
+                               hasLockedVisualProps = true;
+                               break;
+                       }
+               }
+               
+               if (hasLockedVisualProps) {
+                       writeElement("<edge");
+                       writeAttributePair("id", view.getSUID());
+                       writeAttributePair("label", getLabel(view.getModel()));
+                       writeAttributePair("cy:edgeId", 
view.getModel().getSUID());
+                       write(">\n");
+                       depth++;
+       
+                       // Write the edge graphics
+                       writeGraphics(view);
+       
+                       depth--;
+                       writeElement("</edge>\n");
+               }
        }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings({"unchecked", "rawtypes"})
        private void writeGraphics(View<? extends CyTableEntry> view) throws 
IOException {
         if (view == null) return;
         writeElement("<graphics");
@@ -595,8 +608,10 @@
                        
             Object value = view.getVisualProperty(vp);
 
-            if (value != null && view.isValueLocked(vp))
+            if (value != null && view.isValueLocked(vp)) {
                lockedProperties.add(vp);
+               continue;
+            }
             
             // Use XGMML graphics attribute names for some visual properties
             String key = getGraphicsKey(vp);
@@ -723,15 +738,18 @@
     }
     
     /**
+     * Do not use this method with locked visual properties.
         * @param element
         * @param attName
         * @return
         */
     private boolean ignoreGraphicsAttribute(final CyTableEntry element, String 
attName) {
+       // If a session format, only those visual properties that belong to the 
view
+       // (not a visual style) should be saved in the XGMML file.
        boolean b = (sessionFormat && (element instanceof CyNode) && 
!attName.matches("x|y|z"));
                b = b || (sessionFormat && (element instanceof CyEdge));
                b = b || (sessionFormat && (element instanceof CyNetwork) && 
-                                 
!attName.matches(MinimalVisualLexicon.NETWORK_BACKGROUND_PAINT.getIdString()));
+                                 
attName.matches(MinimalVisualLexicon.NETWORK_BACKGROUND_PAINT.getIdString()));
                
                return b;
        }

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to