@Test public void testGetIndex() { CyNode n1 = net.addNode(); CyNode n2 = net.addNode(); assertTrue("index >= 0", n1.getSUID() >= 0); assertTrue("index >= 0", n2.getSUID() >= 0); }
// self nested networks are allowed @Test public void testSetSelfNestedNetwork() { CyNode n1 = net.addNode(); n1.setNetworkPointer(net); assertNotNull(n1.getNetworkPointer()); assertEquals(net, n1.getNetworkPointer()); }
private void addNodes() { for (CyNode node : nodeList) { PRNode prNode = new PRNode(node); graph.addVertex(prNode); idToNode.put(node.getSUID(), prNode); } }
private void createEdge(final Interaction itr, final CySubNetwork subNetwork) { CyNode sourceNode = nMap.get(itr.getSource()); if (sourceNode == null) { sourceNode = subNetwork.addNode(); subNetwork.getRow(sourceNode).set(CyNetwork.NAME, itr.getSource()); nMap.put(itr.getSource(), subNetwork.getRootNetwork().getNode(sourceNode.getSUID())); } for (final String target : itr.getTargets()) { CyNode targetNode = nMap.get(target); if (targetNode == null) { targetNode = subNetwork.addNode(); subNetwork.getRow(targetNode).set(CyNetwork.NAME, target); nMap.put(target, subNetwork.getRootNetwork().getNode(targetNode.getSUID())); } // Add the sourceNode and targetNode to subNetwork if (!subNetwork.containsNode(sourceNode)) { subNetwork.addNode(sourceNode); } if (!subNetwork.containsNode(targetNode)) { subNetwork.addNode(targetNode); } final CyEdge edge = subNetwork.addEdge(sourceNode, targetNode, true); subNetwork.getRow(edge).set(CyNetwork.NAME, getEdgeName(itr, target)); subNetwork.getRow(edge).set(CyEdge.INTERACTION, itr.getType()); } }
@Test public void testSetNestedNetwork() { CyNode n1 = net.addNode(); CyNetwork net2 = mock(CyNetwork.class); n1.setNetworkPointer(net2); assertNotNull(n1.getNetworkPointer()); assertEquals(net2, n1.getNetworkPointer()); }
@Test public void testRemoveNodeIndexStaysConstant() { assertEquals(0, net.getNodeCount()); CyNode n0 = net.addNode(); CyNode n1 = net.addNode(); long n0i = n0.getSUID(); long n1i = n1.getSUID(); net.removeNodes(Collections.singletonList(n0)); long nnli = n1.getSUID(); assertEquals(nnli, n1i); }
@Test public void testRemoveNodeGetNodeFromIndex() { assertEquals(0, net.getNodeCount()); CyNode n0 = net.addNode(); CyNode n1 = net.addNode(); long n0i = n0.getSUID(); long n1i = n1.getSUID(); assertEquals(net.getNode(n0i), n0); assertEquals(net.getNode(n1i), n1); assertTrue(net.removeNodes(Collections.singletonList(n0))); assertNull(net.getNode(n0i)); assertEquals(n1, net.getNode(n1i)); }
private Collection getNeighbors(CyNode node) { Set result = new HashSet(); Collection edges = network.getAdjacentEdgeList(node, org.cytoscape.model.CyEdge.Type.ANY); if (edges == null || edges.size() == 0) return result; Long targetID = node.getSUID(); for (Iterator iterator = edges.iterator(); iterator.hasNext(); ) { CyEdge curEdge = (CyEdge) iterator.next(); if (curEdge.getSource().getSUID() != targetID) result.add(curEdge.getSource()); else if (curEdge.getTarget().getSUID() != targetID) result.add(curEdge.getTarget()); } return result; }
// null nested networks are allowed @Test public void testSetNullNestedNetwork() { CyNode n1 = net.addNode(); // put a real network here first CyNetwork net2 = mock(CyNetwork.class); n1.setNetworkPointer(net2); assertNotNull(n1.getNetworkPointer()); assertEquals(net2, n1.getNetworkPointer()); // now put a null network to verify that we've "unset" things n1.setNetworkPointer(null); assertNull(n1.getNetworkPointer()); }
DNodeView( final VisualLexicon lexicon, final DGraphView graphView, final CyNode model, final VisualMappingManager vmm, final CyNetworkViewManager netViewMgr, final CyEventHelper eventHelper) { super(model, lexicon, eventHelper); if (graphView == null) throw new NullPointerException("View must never be null."); if (lexicon == null) throw new NullPointerException("Lexicon must never be null."); this.vmm = vmm; this.labelPosition = new ObjectPositionImpl(); this.netViewMgr = netViewMgr; this.modelIdx = model.getSUID(); // Initialize custom graphics pool. cgMap = new HashMap<VisualProperty<?>, Set<CustomGraphicLayer>>(); this.graphView = graphView; }
@Override public void run(TaskMonitor taskMonitor) throws Exception { desktopApp.getJFrame().getGlassPane().setVisible(true); taskMonitor.setTitle("Gene Set / Mutation Analysis"); taskMonitor.setStatusMessage("Loading file..."); taskMonitor.setProgress(.25d); try { Map<String, Integer> geneToSampleNumber = null; Map<String, String> geneToSampleString = null; Map<String, Set<String>> sampleToGenes = null; Set<String> selectedGenes = null; if (format.equals("MAF")) { sampleToGenes = new MATFileLoader().loadSampleToGenes(file.getAbsolutePath(), chooseHomoGenes); selectedGenes = CancerAnalysisUtilitites.selectGenesInSamples(sampleCutoffValue, sampleToGenes); } else if (format.equals("GeneSample")) { geneToSampleNumber = new HashMap<String, Integer>(); geneToSampleString = new HashMap<String, String>(); loadGeneSampleFile(file, geneToSampleNumber, geneToSampleString); selectedGenes = selectGenesBasedOnSampleCutoff(geneToSampleNumber, sampleCutoffValue); } else if (format.equals("GeneSet")) { selectedGenes = loadGeneSetFile(file); } // Check if it is possible to construct the network // given the sample size. if (useLinkers) { taskMonitor.setStatusMessage("Checking FI Network size..."); FINetworkService fiService = PlugInScopeObjectManager.getManager().getNetworkService(); Integer cutoff = fiService.getNetworkBuildSizeCutoff(); if (cutoff != null && selectedGenes.size() >= cutoff) { JOptionPane.showMessageDialog( desktopApp.getJFrame(), "The size of the gene set is too big. Linker genes should not be used!\n" + "Please try again without using linker genes.", "Error in Building Network", JOptionPane.ERROR_MESSAGE); desktopApp.getJFrame().getGlassPane().setVisible(false); return; } } CytoPanel controlPane = desktopApp.getCytoPanel(CytoPanelName.WEST); int selectedIndex = controlPane.getSelectedIndex(); taskMonitor.setStatusMessage("Constructing FI Network..."); taskMonitor.setProgress(.50d); CyNetwork network = constructFINetwork(selectedGenes, file.getName()); network.getDefaultNetworkTable().getRow(network.getSUID()).set("name", file.getName()); if (network == null) { JOptionPane.showMessageDialog( desktopApp.getJFrame(), "Cannot find any functional interaction among provided genes.\n" + "No network can be constructed.\n" + "Note: only human gene names are supported.", "Empty Network", JOptionPane.INFORMATION_MESSAGE); desktopApp.getJFrame().getGlassPane().setVisible(false); return; } netManager.addNetwork(network); // Fix for missing default value persistence in CyTables // Should be remedied in the 3.1 api CyTable nodeTable = network.getDefaultNodeTable(); for (Object name : network.getNodeList()) { CyNode node = (CyNode) name; Long nodeSUID = node.getSUID(); nodeTable.getRow(nodeSUID).set("isLinker", false); } controlPane.setSelectedIndex(selectedIndex); if (sampleToGenes != null) { geneToSampleNumber = new HashMap<String, Integer>(); geneToSampleString = new HashMap<String, String>(); Map<String, Set<String>> geneToSamples = InteractionUtilities.switchKeyValues(sampleToGenes); geneToSamples.keySet().retainAll(selectedGenes); for (String gene : geneToSamples.keySet()) { Set<String> samples = geneToSamples.get(gene); geneToSampleNumber.put(gene, samples.size()); geneToSampleString.put(gene, InteractionUtilities.joinStringElements(";", samples)); } } taskMonitor.setStatusMessage("Formatting network attributes..."); taskMonitor.setProgress(.65d); CyTableManager tableManager = new CyTableManager(); CyNetworkView view = viewFactory.createNetworkView(network); tableManager.storeFINetworkVersion(view); tableManager.storeDataSetType(network, CyTableFormatter.getSampleMutationData()); viewManager.addNetworkView(view); if (geneToSampleNumber != null && !geneToSampleNumber.isEmpty()) { tableManager.loadNodeAttributesByName(view, "sampleNumber", geneToSampleNumber); } if (geneToSampleString != null && !geneToSampleString.isEmpty()) { tableManager.loadNodeAttributesByName(view, "samples", geneToSampleString); } // Check if linker genes are to be used. if (useLinkers) { taskMonitor.setStatusMessage("Fetching linker genes..."); Map<String, Boolean> geneToIsLinker = new HashMap<String, Boolean>(); for (Object name : network.getNodeList()) { CyNode node = (CyNode) name; Long suid = node.getSUID(); String nodeName = network.getDefaultNodeTable().getRow(suid).get("name", String.class); geneToIsLinker.put(nodeName, !selectedGenes.contains(nodeName)); } tableManager.loadNodeAttributesByName(view, "isLinker", geneToIsLinker); } if (fetchFIAnnotations) { taskMonitor.setStatusMessage("Fetching FI annotations..."); new FIAnnotationHelper().annotateFIs(view, new RESTFulFIService(), tableManager); } if (view.getModel().getEdgeCount() != 0) { for (CyEdge edge : view.getModel().getEdgeList()) { tableManager.storeEdgeName(edge, view); } } BundleContext context = PlugInScopeObjectManager.getManager().getBundleContext(); ServiceReference visHelperRef = context.getServiceReference(FIVisualStyle.class.getName()); if (visHelperRef != null) { FIVisualStyleImpl styleHelper = (FIVisualStyleImpl) context.getService(visHelperRef); styleHelper.setVisualStyle(view); styleHelper.setLayout(); } // BundleContext context = // PlugInScopeObjectManager.getManager().getBundleContext(); // ServiceReference styleHelperRef = // context.getServiceReference(FIVisualStyleImpl.class.getName()); // FIVisualStyleImpl styleHelper = (FIVisualStyleImpl) // context.getService(styleHelperRef); taskMonitor.setProgress(1.0d); } catch (Exception e) { JOptionPane.showMessageDialog( desktopApp.getJFrame(), "Error in Loading File: " + e.getMessage(), "Error in Loading", JOptionPane.ERROR_MESSAGE); desktopApp.getJFrame().getGlassPane().setVisible(false); } desktopApp.getJFrame().getGlassPane().setVisible(false); }
@Override public void serialize(CyNetworkView networkView, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.useDefaultPrettyPrinter(); jgen.writeStartObject(); jgen.writeStringField( CytoscapeJsNetworkModule.FORMAT_VERSION_TAG, CytoscapeJsNetworkModule.FORMAT_VERSION); jgen.writeStringField(CytoscapeJsNetworkModule.GENERATED_BY_TAG, "cytoscape-" + version); jgen.writeStringField( CytoscapeJsNetworkModule.TARGET_CYJS_VERSION_TAG, CytoscapeJsNetworkModule.CYTOSCAPEJS_VERSION); final CyNetwork network = networkView.getModel(); // Serialize network data table jgen.writeObjectFieldStart(DATA.getTag()); jgen.writeObject(network.getRow(network)); jgen.writeEndObject(); jgen.writeObjectFieldStart(ELEMENTS.getTag()); // Write array final List<CyNode> nodes = network.getNodeList(); final List<CyEdge> edges = network.getEdgeList(); jgen.writeArrayFieldStart(NODES.getTag()); for (final CyNode node : nodes) { jgen.writeStartObject(); // Data field jgen.writeObjectFieldStart(DATA.getTag()); jgen.writeStringField(ID.getTag(), node.getSUID().toString()); // Write CyRow in "data" field jgen.writeObject(network.getRow(node)); jgen.writeEndObject(); // Position and other visual props jgen.writeObject(networkView.getNodeView(node)); // Special case for cytoscape.js format: // - Selected jgen.writeBooleanField( CyNetwork.SELECTED, network.getRow(node).get(CyNetwork.SELECTED, Boolean.class)); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeArrayFieldStart(EDGES.getTag()); for (final CyEdge edge : edges) { jgen.writeStartObject(); jgen.writeObjectFieldStart(DATA.getTag()); jgen.writeStringField(ID.getTag(), edge.getSUID().toString()); jgen.writeStringField(SOURCE.getTag(), edge.getSource().getSUID().toString()); jgen.writeStringField(TARGET.getTag(), edge.getTarget().getSUID().toString()); // Write CyRow in "data" field jgen.writeObject(network.getRow(edge)); jgen.writeEndObject(); // Special case for cytoscape.js format: // - Selected jgen.writeBooleanField( CyNetwork.SELECTED, network.getRow(edge).get(CyNetwork.SELECTED, Boolean.class)); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeEndObject(); }
// by default a node should have a null nested network @Test public void testInitGetNestedNetwork() { CyNode n1 = net.addNode(); assertNull(n1.getNetworkPointer()); }
/** * Calculates the node distances. * * @return the <code>int[][]</code> array of calculated distances or null if the task was canceled * or there was an error */ public int[][] calculate() { int currentProgress = 0; this.maxValue = distances.length; CyNode[] nodes = new CyNode[nodesList.size()]; // TODO: REMOVE // System.err.println( "Calculating all node distances.. for: " // +nodesList.size()+" and "+nodes.length ); // We don't have to make new Integers all the time, so we store the index // Objects in this array for reuse. Integer[] integers = new Integer[nodes.length]; // Fill the nodes array with the nodes in their proper index locations. // int index; CyNode from_node; for (int i = 0; i < nodes.length; i++) { from_node = (CyNode) nodesList.get(i); if (from_node == null) { continue; } int index = ((Integer) nodeIndexToMatrixIndexMap.get(from_node.getSUID())).intValue(); // index = ((Integer) nodeIndexToMatrixIndexMap.get(new // Integer(from_node.getRootGraphIndex()))).intValue(); if ((index < 0) || (index >= nodes.length)) { System.err.println( "WARNING: GraphNode \"" + from_node + "\" has an index value that is out of range: " + index + ". Graph indices should be maintained such " + "that no index is unused."); return null; } if (nodes[index] != null) { System.err.println( "WARNING: GraphNode \"" + from_node + "\" has an index value ( " + index + " ) that is the same as " + "that of another GraphNode ( \"" + nodes[index] + "\" ). Graph indices should be maintained such " + "that indices are unique."); return null; } nodes[index] = from_node; Integer in = new Integer(index); integers[index] = in; } LinkedList queue = new LinkedList(); boolean[] completed_nodes = new boolean[nodes.length]; Iterator neighbors; CyNode to_node; CyNode neighbor; int neighbor_index; int to_node_distance; int neighbor_distance; for (int from_node_index = 0; from_node_index < nodes.length; from_node_index++) { if (this.interrupted) { // The task was canceled this.distances = null; return this.distances; } from_node = nodes[from_node_index]; if (from_node == null) { // Make the distances in this row all Integer.MAX_VALUE. if (distances[from_node_index] == null) { distances[from_node_index] = new int[nodes.length]; } Arrays.fill(distances[from_node_index], Integer.MAX_VALUE); continue; } // TODO: REMOVE // System.err.print( "Calculating node distances from graph node " + // from_node ); // System.err.flush(); // Make the distances row and initialize it. if (distances[from_node_index] == null) { distances[from_node_index] = new int[nodes.length]; } Arrays.fill(distances[from_node_index], Integer.MAX_VALUE); distances[from_node_index][from_node_index] = 0; // Reset the completed nodes array. Arrays.fill(completed_nodes, false); // Add the start node to the queue. queue.add(integers[from_node_index]); while (!(queue.isEmpty())) { if (this.interrupted) { // The task was canceled this.distances = null; return this.distances; } int index = ((Integer) queue.removeFirst()).intValue(); if (completed_nodes[index]) { continue; } completed_nodes[index] = true; to_node = nodes[index]; to_node_distance = distances[from_node_index][index]; if (index < from_node_index) { // Oh boy. We've already got every distance from/to this node. int distance_through_to_node; for (int i = 0; i < nodes.length; i++) { if (distances[index][i] == Integer.MAX_VALUE) { continue; } distance_through_to_node = to_node_distance + distances[index][i]; if (distance_through_to_node <= distances[from_node_index][i]) { // Any immediate neighbor of a node that's already been // calculated for that does not already have a shorter path // calculated from from_node never will, and is thus complete. if (distances[index][i] == 1) { completed_nodes[i] = true; } distances[from_node_index][i] = distance_through_to_node; } } // End for every node, update the distance using the distance from // to_node. // So now we don't need to put any neighbors on the queue or // anything, since they've already been taken care of by the previous // calculation. continue; } // End if to_node has already had all of its distances calculated. neighbors = getNeighbors(to_node).iterator(); // neighbors = perspective.neighborsList(to_node).iterator(); while (neighbors.hasNext()) { if (this.interrupted) { this.distances = null; return this.distances; } neighbor = (CyNode) neighbors.next(); neighbor_index = ((Integer) nodeIndexToMatrixIndexMap.get(neighbor.getSUID())).intValue(); // neighbor_index = ((Integer) nodeIndexToMatrixIndexMap.get(new // Integer(neighbor.getRootGraphIndex()))).intValue(); // If this neighbor was not in the incoming List, we cannot include // it in any paths. if (nodes[neighbor_index] == null) { distances[from_node_index][neighbor_index] = Integer.MAX_VALUE; continue; } if (completed_nodes[neighbor_index]) { // We've already done everything we can here. continue; } neighbor_distance = distances[from_node_index][neighbor_index]; if ((to_node_distance != Integer.MAX_VALUE) && (neighbor_distance > (to_node_distance + 1))) { distances[from_node_index][neighbor_index] = (to_node_distance + 1); queue.addLast(integers[neighbor_index]); } // TODO: REMOVE // System.out.print( "." ); // System.out.flush(); } // For each of the next nodes' neighbors // TODO: REMOVE // System.out.print( "|" ); // System.out.flush(); } // For each to_node, in order of their (present) distances // TODO: REMOVE /* System.err.println( "done." ); */ // Calculate Percentage. This must be a value between 0..100. int percentComplete = (int) (((double) currentProgress / maxValue) * 100); // Estimate Time Remaining long timeRemaining = maxValue - currentProgress; // Update the Task Monitor. // This automatically updates the UI Component w/ progress bar. if (taskMonitor != null) { taskMonitor.setProgress(percentComplete); taskMonitor.setStatusMessage( "Calculating Node Distances: " + currentProgress + "of " + maxValue); // taskMonitor.setEstimatedTimeRemaining(timeRemaining); } currentProgress++; } // For each from_node // TODO: REMOVE // System.err.println( "..Done calculating all node distances." ); return distances; } // calculate