use of org.cytoscape.filter.model.NamedTransformer 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());
}
use of org.cytoscape.filter.model.NamedTransformer in project cytoscape-impl by cytoscape.
the class FilterIOTest method testNestedFilterRoundTrip.
@SuppressWarnings("rawtypes")
@Test
public void testNestedFilterRoundTrip() throws Exception {
DegreeFilter degreeFilter = new DegreeFilter();
degreeFilter.setCriterion(new Double[] { 0.2, 5.5 });
degreeFilter.setEdgeType(CyEdge.Type.ANY);
CompositeFilter<CyNetwork, CyIdentifiable> composite = new CompositeFilterImpl<CyNetwork, CyIdentifiable>(CyNetwork.class, CyIdentifiable.class);
composite.setType(Type.ANY);
composite.append(degreeFilter);
CyTransformerWriter writer = new CyTransformerWriterImpl();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.write(stream, FilterIO.createNamedTransformer("filter1", composite));
String serialized = stream.toString("utf-8");
TransformerManagerImpl transformerManager = new TransformerManagerImpl();
Map<String, String> properties = Collections.emptyMap();
transformerManager.registerFilterFactory(new DegreeFilterFactory(), properties);
transformerManager.registerFilterFactory(new CompositeFilterFactory<CyNetwork, CyIdentifiable>(CyNetwork.class, CyIdentifiable.class), 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("filter1", 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 composite2 = (CompositeFilter) child;
Assert.assertEquals(composite.getType(), composite2.getType());
Assert.assertEquals(composite.getLength(), composite2.getLength());
Filter filter = composite2.get(0);
Assert.assertTrue(filter instanceof DegreeFilter);
DegreeFilter degreeFilter2 = (DegreeFilter) filter;
Assert.assertArrayEquals((Number[]) degreeFilter.getCriterion(), (Number[]) degreeFilter2.getCriterion());
Assert.assertEquals(degreeFilter.getEdgeType(), degreeFilter2.getEdgeType());
}
Aggregations