use of org.cytoscape.io.write.CyTransformerWriter in project cytoscape-impl by cytoscape.
the class FilterIO method writeFilters.
public void writeFilters(File file, NamedTransformer<CyNetwork, CyIdentifiable>[] namedTransformers) throws IOException {
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
CyTransformerWriter writer = serviceRegistrar.getService(CyTransformerWriter.class);
writer.write(stream, namedTransformers);
stream.close();
}
use of org.cytoscape.io.write.CyTransformerWriter in project cytoscape-impl by cytoscape.
the class FilterIOTest method testSerializationRoundTrip.
@Test
public void testSerializationRoundTrip() throws Exception {
DegreeFilter degreeFilter = new DegreeFilter();
degreeFilter.setCriterion(new Double[] { 0.2, 5.5 });
degreeFilter.setEdgeType(CyEdge.Type.ANY);
DummyFilter dummyFilter = new DummyFilter();
CyTransformerWriter writer = new CyTransformerWriterImpl();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
writer.write(stream, FilterIO.createNamedTransformer("filter1", degreeFilter), FilterIO.createNamedTransformer("filter2", dummyFilter));
String serialized = stream.toString("utf-8");
TransformerManagerImpl transformerManager = new TransformerManagerImpl();
Map<String, String> properties = Collections.emptyMap();
transformerManager.registerFilterFactory(new DegreeFilterFactory(), properties);
transformerManager.registerFilterFactory(new DummyFilterFactory(), properties);
CyTransformerReaderImpl reader = new CyTransformerReaderImpl();
reader.registerTransformerManager(transformerManager, null);
ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
NamedTransformer<?, ?>[] transformers = reader.read(inputStream);
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
writer.write(stream2, transformers);
String serialized2 = stream2.toString("utf-8");
Assert.assertEquals(serialized, serialized2);
}
use of org.cytoscape.io.write.CyTransformerWriter 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());
}
use of org.cytoscape.io.write.CyTransformerWriter 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