use of org.cytoscape.io.internal.util.session.model.Node 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");
}
}
use of org.cytoscape.io.internal.util.session.model.Node in project cytoscape-impl by cytoscape.
the class Cy2SessionReaderImpl method extractNetworksAndViews.
/**
* @param is
* @param entryName
* @param parent
* @param createView
* @return The top-level network that was extracted from the XGMML file.
* @throws Exception
*/
private CyNetwork extractNetworksAndViews(final InputStream is, final String entryName, final CyNetwork parent, final boolean createView) throws Exception {
CyNetwork topNetwork = null;
final CyNetworkReader reader = networkReaderMgr.getReader(is, entryName);
if (parent != null) {
if (reader instanceof SessionXGMMLNetworkReader) {
((SessionXGMMLNetworkReader) reader).setParent(parent);
} else {
logger.error("CyNetworkReader should be an instance of XGMMLNetworkReader. " + "Cannot extract network as sub-nertwork of: " + entryName);
}
}
reader.run(taskMonitor);
final CyNetwork[] netArray = reader.getNetworks();
if (netArray != null && netArray.length > 0) {
topNetwork = netArray[0];
for (int i = 0; i < netArray.length; i++) {
// Process each CyNetwork
final CyNetwork net = netArray[i];
final String netName = net.getRow(net).get(CyNetwork.NAME, String.class);
networkLookup.put(netName, net);
// Add parent network attribute to a column in the hidden table,
// to preserve the network hierarchy info from Cytoscape 2.x
final CyRow hRow = net.getRow(net, CyNetwork.HIDDEN_ATTRS);
final CyTable hTbl = hRow.getTable();
if (parent instanceof CySubNetwork) {
if (hTbl.getColumn(CY3_PARENT_NETWORK_COLUMN) == null)
hTbl.createColumn(CY3_PARENT_NETWORK_COLUMN, Long.class, false);
hRow.set(CY3_PARENT_NETWORK_COLUMN, parent.getSUID());
}
final CyTable tbl = net.getRow(net, CyNetwork.LOCAL_ATTRS).getTable();
// (e.g. the user imported a Cy3 XGMML that contains this attribute into Cy2)
if (tbl.getColumn(CY2_PARENT_NETWORK_COLUMN) != null && parent instanceof CySubNetwork == false)
tbl.deleteColumn(CY2_PARENT_NETWORK_COLUMN);
// Restore node/edge selection
List<Node> selNodes = nodeSelectionLookup.get(netName);
List<Edge> selEdges = edgeSelectionLookup.get(netName);
if (selNodes != null)
setBooleanNodeAttr(net, selNodes, SELECTED, DEFAULT_ATTRS);
if (selEdges != null)
setBooleanEdgeAttr(net, selEdges, SELECTED, DEFAULT_ATTRS);
networks.add(net);
if (!cancelled && i == 0 && createView) {
// Create a network view for the first network only,
// which is supposed to be the top-level one
final CyNetworkView view = reader.buildCyNetworkView(net);
networkViewLookup.put(netName, view);
networkViews.add(view);
cache.cache(netName, view);
}
}
}
return topNetwork;
}
use of org.cytoscape.io.internal.util.session.model.Node in project cytoscape-impl by cytoscape.
the class Cy2SessionReaderImpl method setBooleanNodeAttr.
private void setBooleanNodeAttr(final CyNetwork net, final List<Node> nodes, final String attrName, final String tableName) {
if (net != null && nodes != null) {
// set attr values based on ids
for (final Node nodeObject : nodes) {
if (cancelled)
return;
String name = nodeObject.getId();
// The XGMML node "id" is only used internally, by the XGMML parser--the name is the real ID.
CyNode n = cache.getNodeByName(name);
if (n != null)
net.getRow(n, tableName).set(attrName, true);
else
logger.error("Cannot restore boolean node attr \"" + name + "\": node not found.");
}
}
}
use of org.cytoscape.io.internal.util.session.model.Node 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;
}
}
}
}
Aggregations