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();
}
}
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));
}
}
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());
}
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();
}
}
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());
}
Aggregations