Search in sources :

Example 1 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class CyTransformerReaderImpl method readCompositeFilter.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void readCompositeFilter(JsonParser parser, CompositeFilter composite) throws IOException {
    JsonToken firstToken = parser.nextToken();
    if (firstToken == JsonToken.END_OBJECT) {
        return;
    }
    if (firstToken != JsonToken.FIELD_NAME) {
        throw new IOException("Expected: " + JsonToken.FIELD_NAME + ". Got: " + firstToken);
    }
    assertEquals(FilterIO.TRANSFORMERS_FIELD, parser.getCurrentName());
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    while (true) {
        Filter filter = (Filter) readTransformer(parser);
        if (filter == null) {
            break;
        }
        composite.append(filter);
    }
    assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
    assertNextToken(parser, JsonToken.END_OBJECT);
}
Also used : Filter(org.cytoscape.filter.model.Filter) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException)

Example 2 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class CyTransformerReaderImpl method readTransformer.

private Transformer<?, ?> readTransformer(JsonParser parser) throws IOException {
    JsonToken firstToken = parser.nextToken();
    if (firstToken == JsonToken.END_ARRAY) {
        return null;
    }
    if (firstToken != JsonToken.START_OBJECT) {
        throw new IOException("Expected: " + JsonToken.START_OBJECT + ". Got: " + firstToken);
    }
    assertField(parser, FilterIO.ID_FIELD);
    String id = parser.nextTextValue();
    assertField(parser, FilterIO.PARAMETERS_FIELD);
    assertNextToken(parser, JsonToken.START_OBJECT);
    Transformer<?, ?> transformer = transformerManager.createTransformer(id);
    if (transformer == null && !id.contains(".")) {
        transformer = transformerManager.createTransformer("org.cytoscape." + id);
    }
    if (transformer == null) {
        throw new IOException("Unrecognized id: '" + id + "'");
    }
    Map<String, Object> parameters = readParameters(parser);
    assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
    FilterIO.applyParameters(parameters, transformer);
    if (transformer instanceof CompositeFilter) {
        readCompositeFilter(parser, (CompositeFilter<?, ?>) transformer);
    } else if (transformer instanceof SubFilterTransformer) {
        SubFilterTransformer<?, ?> sft = (SubFilterTransformer<?, ?>) transformer;
        readCompositeFilter(parser, sft.getCompositeFilter());
    } else {
        assertNextToken(parser, JsonToken.END_OBJECT);
    }
    return transformer;
}
Also used : CompositeFilter(org.cytoscape.filter.model.CompositeFilter) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) SubFilterTransformer(org.cytoscape.filter.model.SubFilterTransformer)

Example 3 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class CreateFilterTaskTest method testCreateCompositeFilter.

@Test
public void testCreateCompositeFilter() {
    CreateFilterTask task = new CreateFilterTask(serviceRegistrar, "MyFilter");
    String subFilterJson = "{ \"id\" : \"ColumnFilter\", \"parameters\" : { \"criterion\" : \"1\", \"columnName\" : \"name\", \"predicate\" : \"CONTAINS\"} }";
    task.jsonTunable.json = "{ \"id\" : \"org.cytoscape.CompositeFilter\", \"parameters\" : { \"type\" : \"ALL\" }, \"transformers\" : [ " + subFilterJson + " ] }";
    TaskMonitor tm = mock(TaskMonitor.class);
    task.run(tm);
    verify(tm, times(0)).showMessage(any(), any());
    NamedTransformer<CyNetwork, CyIdentifiable> transformer = transformerContainer.getNamedTransformer(task.name);
    assertNotNull(transformer);
    @SuppressWarnings("rawtypes") CompositeFilter compositeFilter = (CompositeFilter) transformer.getTransformers().get(0);
    assertEquals(CompositeFilter.Type.ALL, compositeFilter.getType());
    assertEquals(1, compositeFilter.getLength());
    ColumnFilter columnFilter = (ColumnFilter) compositeFilter.get(0);
    assertEquals(Predicate.CONTAINS, columnFilter.getPredicate());
    assertEquals("1", columnFilter.getCriterion());
    assertEquals("name", columnFilter.getColumnName());
}
Also used : CompositeFilter(org.cytoscape.filter.model.CompositeFilter) TaskMonitor(org.cytoscape.work.TaskMonitor) CyNetwork(org.cytoscape.model.CyNetwork) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) CyIdentifiable(org.cytoscape.model.CyIdentifiable) Test(org.junit.Test)

Example 4 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter in project cytoscape-impl by cytoscape.

the class TransformerPanelController method createView.

@Override
public JComponent createView(TransformerPanel parent, Transformer<CyNetwork, CyIdentifiable> transformer, int depth) {
    // CompositeFilterImpl needs a CompositeFilterPanel but the top is blank so view will be null
    JComponent view = transformerViewManager.createView(transformer);
    if (transformer instanceof SubFilterTransformer || transformer instanceof CompositeFilter) {
        String addButtonTT = transformerViewManager.getAddButtonTooltip(transformer);
        CompositeFilterController controller = CompositeFilterController.createFor(view, addButtonTT);
        CompositeFilter<CyNetwork, CyIdentifiable> compositeFilter;
        if (transformer instanceof SubFilterTransformer)
            compositeFilter = ((SubFilterTransformer<CyNetwork, CyIdentifiable>) transformer).getCompositeFilter();
        else
            compositeFilter = (CompositeFilter<CyNetwork, CyIdentifiable>) transformer;
        return new CompositeFilterPanel<TransformerPanel>(parent, this, controller, compositeFilter, depth);
    }
    if (view == null)
        throw new IllegalArgumentException("view could not be created for: " + transformer.getId());
    return view;
}
Also used : CompositeFilter(org.cytoscape.filter.model.CompositeFilter) JComponent(javax.swing.JComponent) CyNetwork(org.cytoscape.model.CyNetwork) CompositeFilterController(org.cytoscape.filter.internal.filters.composite.CompositeFilterController) CompositeFilterPanel(org.cytoscape.filter.internal.filters.composite.CompositeFilterPanel) SubFilterTransformer(org.cytoscape.filter.model.SubFilterTransformer) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 5 with CompositeFilter

use of org.cytoscape.filter.model.CompositeFilter 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

CompositeFilter (org.cytoscape.filter.model.CompositeFilter)10 CyIdentifiable (org.cytoscape.model.CyIdentifiable)6 CyNetwork (org.cytoscape.model.CyNetwork)6 ColumnFilter (org.cytoscape.filter.internal.filters.column.ColumnFilter)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 TopologyFilter (org.cytoscape.filter.internal.filters.topology.TopologyFilter)3 AdjacencyTransformer (org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer)3 TransformerManagerImpl (org.cytoscape.filter.internal.work.TransformerManagerImpl)3 AbstractTransformer (org.cytoscape.filter.model.AbstractTransformer)3 Filter (org.cytoscape.filter.model.Filter)3 NamedTransformer (org.cytoscape.filter.model.NamedTransformer)3 SubFilterTransformer (org.cytoscape.filter.model.SubFilterTransformer)3 Transformer (org.cytoscape.filter.model.Transformer)3 CyTransformerReaderImpl (org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 JComponent (javax.swing.JComponent)2