Search in sources :

Example 1 with MemoizableTransformer

use of org.cytoscape.filter.internal.MemoizableTransformer in project cytoscape-impl by cytoscape.

the class TransformerWorker method doWork.

@Override
public void doWork(ProgressMonitor monitor) {
    if (controller == null) {
        return;
    }
    final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
    CyNetworkView networkView = applicationManager.getCurrentNetworkView();
    CyNetwork network;
    if (networkView != null) {
        network = networkView.getModel();
    } else {
        network = applicationManager.getCurrentNetwork();
    }
    if (network == null) {
        return;
    }
    ProgressMonitor filterMonitor = new SubProgressMonitor(monitor, 0.0, 0.4);
    ProgressMonitor chainMonitor = new SubProgressMonitor(monitor, 0.4, 0.98);
    monitor.setProgress(0.0);
    monitor.setStatusMessage(null);
    Sink sink = new Sink(network);
    long startTime = System.currentTimeMillis();
    try {
        List<Transformer<CyNetwork, CyIdentifiable>> transformers = controller.getTransformers(view);
        FilterElement selected = (FilterElement) controller.getStartWithComboBoxModel().getSelectedItem();
        TransformerSource<CyNetwork, CyIdentifiable> source = createSource(network, selected, filterMonitor);
        for (Transformer<?, ?> transformer : transformers) {
            if (transformer instanceof MemoizableTransformer) {
                ((MemoizableTransformer) transformer).startCaching();
            }
        }
        try {
            transformerManager.execute(network, source, transformers, sink, chainMonitor);
        } finally {
            for (Transformer<?, ?> transformer : transformers) {
                if (transformer instanceof MemoizableTransformer) {
                    ((MemoizableTransformer) transformer).clearCache();
                }
            }
        }
        if (networkView != null) {
            networkView.updateView();
        }
    } finally {
        long duration = System.currentTimeMillis() - startTime;
        monitor.setProgress(1.0);
        monitor.setStatusMessage(String.format("Selected %d %s and %d %s in %dms", sink.nodeCount, sink.nodeCount == 1 ? "node" : "nodes", sink.edgeCount, sink.edgeCount == 1 ? "edge" : "edges", duration));
    }
}
Also used : Transformer(org.cytoscape.filter.model.Transformer) MemoizableTransformer(org.cytoscape.filter.internal.MemoizableTransformer) CyNetwork(org.cytoscape.model.CyNetwork) MemoizableTransformer(org.cytoscape.filter.internal.MemoizableTransformer) CyApplicationManager(org.cytoscape.application.CyApplicationManager) FilterElement(org.cytoscape.filter.internal.view.FilterElement) TransformerSink(org.cytoscape.filter.model.TransformerSink) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 2 with MemoizableTransformer

use of org.cytoscape.filter.internal.MemoizableTransformer in project cytoscape-impl by cytoscape.

the class FilterWorker method doWork.

@Override
public void doWork(ProgressMonitor monitor) {
    if (controller == null) {
        return;
    }
    final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
    CyNetworkView networkView = applicationManager.getCurrentNetworkView();
    CyNetwork network;
    if (networkView != null) {
        network = networkView.getModel();
    } else {
        network = applicationManager.getCurrentNetwork();
    }
    if (network == null) {
        return;
    }
    monitor.setProgress(0);
    monitor.setStatusMessage(null);
    int nodeCount = 0;
    int edgeCount = 0;
    int counter = 0;
    long startTime = System.currentTimeMillis();
    try {
        Filter<CyNetwork, CyIdentifiable> filter = controller.getFilter();
        if (filter instanceof CompositeFilter) {
            // If we have an empty CompositeFilter, bail out.
            CompositeFilter<CyNetwork, CyIdentifiable> composite = (CompositeFilter<CyNetwork, CyIdentifiable>) filter;
            if (composite.getLength() == 0) {
                return;
            }
        }
        if (filter instanceof MemoizableTransformer) {
            ((MemoizableTransformer) filter).startCaching();
        }
        try {
            List<CyNode> nodeList = network.getNodeList();
            List<CyEdge> edgeList = network.getEdgeList();
            double total = nodeList.size() + edgeList.size();
            List<CyIdentifiable> selected = new ArrayList<>();
            List<CyIdentifiable> unselected = new ArrayList<>();
            for (CyNode node : nodeList) {
                if (monitor.isCancelled()) {
                    return;
                }
                boolean accepted = filter.accepts(network, node);
                if (accepted) {
                    selected.add(node);
                    nodeCount++;
                } else {
                    unselected.add(node);
                }
                monitor.setProgress(++counter / total);
            }
            for (CyEdge edge : edgeList) {
                if (monitor.isCancelled()) {
                    return;
                }
                boolean accepted = filter.accepts(network, edge);
                if (accepted) {
                    selected.add(edge);
                    edgeCount++;
                } else {
                    unselected.add(edge);
                }
                monitor.setProgress(++counter / total);
            }
            // now do the selection
            for (CyIdentifiable element : unselected) {
                CyRow row = network.getRow(element);
                if (row.get(CyNetwork.SELECTED, Boolean.class)) {
                    row.set(CyNetwork.SELECTED, Boolean.FALSE);
                }
            }
            for (CyIdentifiable element : selected) {
                CyRow row = network.getRow(element);
                if (!row.get(CyNetwork.SELECTED, Boolean.class)) {
                    row.set(CyNetwork.SELECTED, Boolean.TRUE);
                }
            }
        } finally {
            if (filter instanceof MemoizableTransformer) {
                ((MemoizableTransformer) filter).clearCache();
            }
        }
        if (networkView != null) {
            networkView.updateView();
        }
    } finally {
        long duration = System.currentTimeMillis() - startTime;
        monitor.setProgress(1.0);
        monitor.setStatusMessage(String.format("Selected %d %s and %d %s in %dms", nodeCount, nodeCount == 1 ? "node" : "nodes", edgeCount, edgeCount == 1 ? "edge" : "edges", duration));
    }
}
Also used : CompositeFilter(org.cytoscape.filter.model.CompositeFilter) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) MemoizableTransformer(org.cytoscape.filter.internal.MemoizableTransformer) CyApplicationManager(org.cytoscape.application.CyApplicationManager) CyNode(org.cytoscape.model.CyNode) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Aggregations

CyApplicationManager (org.cytoscape.application.CyApplicationManager)2 MemoizableTransformer (org.cytoscape.filter.internal.MemoizableTransformer)2 CyIdentifiable (org.cytoscape.model.CyIdentifiable)2 CyNetwork (org.cytoscape.model.CyNetwork)2 CyNetworkView (org.cytoscape.view.model.CyNetworkView)2 ArrayList (java.util.ArrayList)1 FilterElement (org.cytoscape.filter.internal.view.FilterElement)1 CompositeFilter (org.cytoscape.filter.model.CompositeFilter)1 Transformer (org.cytoscape.filter.model.Transformer)1 TransformerSink (org.cytoscape.filter.model.TransformerSink)1 CyEdge (org.cytoscape.model.CyEdge)1 CyNode (org.cytoscape.model.CyNode)1 CyRow (org.cytoscape.model.CyRow)1