Search in sources :

Example 1 with Transformer

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

the class TransformerPanelController method handleDrop.

@Override
public void handleDrop(TransformerPanel parent, JComponent source, List<Integer> sourcePath, JComponent target, List<Integer> targetPath) {
    CompositeTransformerPanel root = parent.getRootPanel();
    try {
        if (sourcePath.size() == 1 && targetPath.size() == 1) {
            int sourceIndex = sourcePath.get(0);
            List<Transformer<CyNetwork, CyIdentifiable>> model = root.getModel();
            Transformer<CyNetwork, CyIdentifiable> transformer = model.remove(sourceIndex);
            int targetIndex = targetPath.get(targetPath.size() - 1) + 1;
            if (sourceIndex < targetIndex) {
                targetIndex--;
            }
            model.add(targetIndex, transformer);
        } else {
            super.handleFilterDrop(parent, source, sourcePath, target, targetPath);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        root.updateLayout();
    }
}
Also used : CompositeTransformerPanel(org.cytoscape.filter.internal.filters.composite.CompositeTransformerPanel) Transformer(org.cytoscape.filter.model.Transformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) SubFilterTransformer(org.cytoscape.filter.model.SubFilterTransformer) CyNetwork(org.cytoscape.model.CyNetwork) InvocationTargetException(java.lang.reflect.InvocationTargetException) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 2 with Transformer

use of org.cytoscape.filter.model.Transformer 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 3 with Transformer

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

the class FilterIOTest method testSubFilterTransformerRoundTrip.

@SuppressWarnings("rawtypes")
@Test
public void testSubFilterTransformerRoundTrip() throws Exception {
    ColumnFilter columnFilter = new ColumnFilter();
    columnFilter.setColumnName("MyColName");
    columnFilter.setPredicateAndCriterion(Predicate.CONTAINS, "blah");
    AdjacencyTransformer adjacency = new AdjacencyTransformer();
    adjacency.setAction(Action.REPLACE);
    adjacency.setEdgesAre(EdgesAre.INCOMING);
    adjacency.setFilterTarget(What.EDGES);
    adjacency.setOutput(What.EDGES);
    adjacency.getCompositeFilter().append(columnFilter);
    CyTransformerWriter writer = new CyTransformerWriterImpl();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.write(stream, FilterIO.createNamedTransformer("transformer1", adjacency));
    // String serialized = stream.toString("utf-8");
    TransformerManagerImpl transformerManager = new TransformerManagerImpl();
    Map<String, String> properties = Collections.emptyMap();
    transformerManager.registerFilterFactory(new ColumnFilterFactory(), properties);
    transformerManager.registerElementTransformerFactory(new AdjacencyTransformerFactory(), properties);
    CyTransformerReaderImpl reader = new CyTransformerReaderImpl();
    reader.registerTransformerManager(transformerManager, null);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
    NamedTransformer<?, ?>[] transformers = reader.read(inputStream);
    Assert.assertNotNull(transformers);
    Assert.assertEquals(1, transformers.length);
    NamedTransformer<?, ?> namedTransformer = transformers[0];
    Assert.assertNotNull(namedTransformer);
    Assert.assertEquals("transformer1", namedTransformer.getName());
    List children = namedTransformer.getTransformers();
    Assert.assertEquals(1, children.size());
    Transformer child = (Transformer) children.get(0);
    Assert.assertNotNull(child);
    Assert.assertTrue(child instanceof AdjacencyTransformer);
    AdjacencyTransformer adjacency2 = (AdjacencyTransformer) child;
    Assert.assertEquals(adjacency.getAction(), adjacency2.getAction());
    Assert.assertEquals(adjacency.getEdgesAre(), adjacency2.getEdgesAre());
    Assert.assertEquals(adjacency.getFilterTarget(), adjacency2.getFilterTarget());
    Assert.assertEquals(adjacency.getOutput(), adjacency2.getOutput());
    CompositeFilter<?, ?> composite = adjacency2.getCompositeFilter();
    Assert.assertEquals(1, composite.getLength());
    Filter filter = composite.get(0);
    Assert.assertTrue(filter instanceof ColumnFilter);
    ColumnFilter columnFilter2 = (ColumnFilter) filter;
    Assert.assertEquals(columnFilter.getColumnName(), columnFilter2.getColumnName());
    Assert.assertEquals(columnFilter.getPredicate(), columnFilter2.getPredicate());
    Assert.assertEquals(columnFilter.getCriterion(), columnFilter2.getCriterion());
}
Also used : CyTransformerReaderImpl(org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl) Transformer(org.cytoscape.filter.model.Transformer) AdjacencyTransformer(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) AbstractTransformer(org.cytoscape.filter.model.AbstractTransformer) CyTransformerWriter(org.cytoscape.io.write.CyTransformerWriter) CyTransformerWriterImpl(org.cytoscape.io.internal.write.transformer.CyTransformerWriterImpl) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) AdjacencyTransformerFactory(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformerFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) TopologyFilter(org.cytoscape.filter.internal.filters.topology.TopologyFilter) DegreeFilter(org.cytoscape.filter.internal.filters.degree.DegreeFilter) Filter(org.cytoscape.filter.model.Filter) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) List(java.util.List) TransformerManagerImpl(org.cytoscape.filter.internal.work.TransformerManagerImpl) ColumnFilterFactory(org.cytoscape.filter.internal.filters.column.ColumnFilterFactory) AdjacencyTransformer(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer) Test(org.junit.Test)

Example 4 with Transformer

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

the class CyTransformerReaderImpl method read.

@Override
public NamedTransformer<?, ?>[] read(InputStream stream) throws IOException {
    JsonParser parser = factory.createParser(stream);
    try {
        assertNextToken(parser, JsonToken.START_ARRAY);
        List<NamedTransformer<?, ?>> namedTransformers = new ArrayList<NamedTransformer<?, ?>>();
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            assertEquals(JsonToken.START_OBJECT, parser.getCurrentToken());
            assertField(parser, FilterIO.NAME_FIELD);
            String name = parser.nextTextValue();
            assertField(parser, FilterIO.TRANSFORMERS_FIELD);
            assertNextToken(parser, JsonToken.START_ARRAY);
            List<Transformer<?, ?>> transformers = new ArrayList<Transformer<?, ?>>();
            while (true) {
                Transformer<?, ?> transformer = readTransformer(parser);
                if (transformer == null) {
                    break;
                }
                transformers.add(transformer);
            }
            assertNextToken(parser, JsonToken.END_OBJECT);
            namedTransformers.add(FilterIO.createNamedTransformer(name, transformers));
        }
        return namedTransformers.toArray(new NamedTransformer[namedTransformers.size()]);
    } finally {
        parser.close();
    }
}
Also used : Transformer(org.cytoscape.filter.model.Transformer) SubFilterTransformer(org.cytoscape.filter.model.SubFilterTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) ArrayList(java.util.ArrayList) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 5 with Transformer

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

the class FilterIOTest method testLoadOldTopologyFilter.

/**
 * The topology filter was made a composite filter in 3.3. Older versions of cytoscape
 * will export a topology filter without the "transformers" field that is part of
 * composite filters. The filter json parser was enhanced to support this.
 */
@SuppressWarnings("rawtypes")
@Test
public void testLoadOldTopologyFilter() throws IOException {
    TransformerManagerImpl transformerManager = new TransformerManagerImpl();
    Map<String, String> properties = Collections.emptyMap();
    transformerManager.registerFilterFactory(new CompositeFilterFactory<CyNetwork, CyIdentifiable>(CyNetwork.class, CyIdentifiable.class), properties);
    transformerManager.registerFilterFactory(new TopologyFilterFactory(), properties);
    transformerManager.registerFilterFactory(new ColumnFilterFactory(), properties);
    String path = "./src/test/resources/testData/filter/topology-filter-3.2.json";
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    // String contents = new String(encoded);
    // System.out.println(contents);
    CyTransformerReaderImpl reader = new CyTransformerReaderImpl();
    reader.registerTransformerManager(transformerManager, null);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(encoded);
    NamedTransformer<?, ?>[] transformers = reader.read(inputStream);
    Assert.assertNotNull(transformers);
    Assert.assertEquals(1, transformers.length);
    NamedTransformer<?, ?> namedTransformer = transformers[0];
    Assert.assertNotNull(namedTransformer);
    Assert.assertEquals("Default filter", namedTransformer.getName());
    List children = namedTransformer.getTransformers();
    Assert.assertEquals(1, children.size());
    Transformer child = (Transformer) children.get(0);
    Assert.assertNotNull(child);
    Assert.assertTrue(child instanceof CompositeFilter);
    CompositeFilter composite = (CompositeFilter) child;
    Assert.assertEquals(2, composite.getLength());
    Assert.assertTrue(composite.get(0) instanceof TopologyFilter);
    Assert.assertTrue(composite.get(1) instanceof ColumnFilter);
    TopologyFilter topology = (TopologyFilter) composite.get(0);
    Assert.assertEquals(0, topology.getLength());
    Assert.assertEquals(Integer.valueOf(4), topology.getDistance());
    Assert.assertEquals(Integer.valueOf(5), topology.getThreshold());
    ColumnFilter column = (ColumnFilter) composite.get(1);
    Assert.assertEquals("name", column.getColumnName());
}
Also used : CyTransformerReaderImpl(org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) Transformer(org.cytoscape.filter.model.Transformer) AdjacencyTransformer(org.cytoscape.filter.internal.transformers.adjacency.AdjacencyTransformer) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) AbstractTransformer(org.cytoscape.filter.model.AbstractTransformer) CyNetwork(org.cytoscape.model.CyNetwork) TopologyFilterFactory(org.cytoscape.filter.internal.filters.topology.TopologyFilterFactory) ColumnFilter(org.cytoscape.filter.internal.filters.column.ColumnFilter) NamedTransformer(org.cytoscape.filter.model.NamedTransformer) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) TransformerManagerImpl(org.cytoscape.filter.internal.work.TransformerManagerImpl) ColumnFilterFactory(org.cytoscape.filter.internal.filters.column.ColumnFilterFactory) CyIdentifiable(org.cytoscape.model.CyIdentifiable) TopologyFilter(org.cytoscape.filter.internal.filters.topology.TopologyFilter) Test(org.junit.Test)

Aggregations

Transformer (org.cytoscape.filter.model.Transformer)6 NamedTransformer (org.cytoscape.filter.model.NamedTransformer)5 CyIdentifiable (org.cytoscape.model.CyIdentifiable)4 CyNetwork (org.cytoscape.model.CyNetwork)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 List (java.util.List)3 ColumnFilter (org.cytoscape.filter.internal.filters.column.ColumnFilter)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 CompositeFilter (org.cytoscape.filter.model.CompositeFilter)3 CyTransformerReaderImpl (org.cytoscape.io.internal.read.transformer.CyTransformerReaderImpl)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ColumnFilterFactory (org.cytoscape.filter.internal.filters.column.ColumnFilterFactory)2 DegreeFilter (org.cytoscape.filter.internal.filters.degree.DegreeFilter)2 Filter (org.cytoscape.filter.model.Filter)2 SubFilterTransformer (org.cytoscape.filter.model.SubFilterTransformer)2 CyTransformerWriterImpl (org.cytoscape.io.internal.write.transformer.CyTransformerWriterImpl)2