Skip to content

Commit

Permalink
Support for hidden nodes. Requires cytoscape 3.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekucera committed Aug 21, 2017
1 parent 64c5654 commit f3bb784
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 37 deletions.
2 changes: 1 addition & 1 deletion AutoAnnotate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<project.name>AutoAnnotate</project.name>
<bundle.symbolicName>org.baderlab.autoannotate</bundle.symbolicName>
<bundle.namespace>org.baderlab.autoannotate</bundle.namespace>
<cytoscape.version>3.3.0</cytoscape.version>
<cytoscape.version>3.4.0</cytoscape.version>
</properties>

<name>AutoAnnotate</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.baderlab.autoannotate.internal.task.CollapseAllTaskFactory;
import org.baderlab.autoannotate.internal.task.CollapseTask;
import org.baderlab.autoannotate.internal.task.CreateAnnotationSetTask;
import org.baderlab.autoannotate.internal.task.CreateSubnetworkTask;
import org.baderlab.autoannotate.internal.task.LayoutAnnotationSetTaskFactory;
import org.baderlab.autoannotate.internal.task.LayoutClustersTaskFactory;
import org.baderlab.autoannotate.internal.task.RunClusterMakerTaskFactory;
Expand Down Expand Up @@ -87,6 +88,7 @@ protected void configure() {
installFactory(RemoveAllAnnotationsTask.Factory.class);
installFactory(CreateAnnotationSetTask.Factory.class);
installFactory(CreateClusterTask.Factory.class);
installFactory(CreateSubnetworkTask.Factory.class);
}

private void installFactory(Class<?> factoryInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.cytoscape.model.CyNetworkTableManager;
import org.cytoscape.model.CyTableFactory;
import org.cytoscape.model.CyTableManager;
import org.cytoscape.model.subnetwork.CyRootNetworkManager;
import org.cytoscape.service.util.CyServiceRegistrar;
import org.cytoscape.session.CySessionManager;
import org.cytoscape.util.swing.FileUtil;
Expand Down Expand Up @@ -75,6 +76,7 @@ protected void configure() {
bindService(CyTableManager.class);
bindService(CyTableFactory.class);
bindService(FileUtil.class);
bindService(CyRootNetworkManager.class);

bindService(DialogTaskManager.class);
TypeLiteral<SynchronousTaskManager<?>> synchronousManager = new TypeLiteral<SynchronousTaskManager<?>>(){};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import org.baderlab.autoannotate.internal.model.io.CreationParameter;
import org.baderlab.autoannotate.internal.util.ResultObserver;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.CyNode;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.View;
import org.cytoscape.view.presentation.property.BasicVisualLexicon;
import org.cytoscape.work.AbstractTask;
import org.cytoscape.work.TaskIterator;
import org.cytoscape.work.TaskManager;
Expand All @@ -41,8 +44,10 @@
public class CreateAnnotationSetTask extends AbstractTask {

@Inject private RunClusterMakerTaskFactory.Factory clusterMakerFactoryFactory;
@Inject private CreateSubnetworkTask.Factory subnetworkTaskFactory;
@Inject private Provider<LabelMakerManager> labelManagerProvider;
@Inject private LayoutClustersTaskFactory.Factory layoutTaskFactoryFactory;
@Inject private CyNetworkManager networkManager;

@Inject private @Named("sync") TaskManager<?,?> syncTaskManager;
@Inject private ModelManager modelManager;
Expand Down Expand Up @@ -170,11 +175,30 @@ private boolean needClusterEdgeAttribute() {


private Map<String,Collection<CyNode>> runClusterMaker(Optional<Double> cutoff) {
RunClusterMakerTaskFactory clusterMakerTaskFactory = clusterMakerFactoryFactory.create(params, cutoff.orElse(null));
CyNetworkView networkView = params.getNetworkView();
CyNetwork network = networkView.getModel();
boolean hasHidden = hasHiddenNodes(networkView);
if(hasHidden) {
Collection<CyNode> visibleNodes = getVisibleNodes(networkView);
CreateSubnetworkTask subnetworkTask = subnetworkTaskFactory.create(network, visibleNodes);
ResultObserver<CyNetwork> observer = new ResultObserver<>(subnetworkTask, CyNetwork.class);
syncTaskManager.execute(new TaskIterator(subnetworkTask), observer);
network = observer.getResults().get();
networkManager.addNetwork(network, false);
}

RunClusterMakerTaskFactory clusterMakerTaskFactory =
clusterMakerFactoryFactory.create(network, params.getClusterAlgorithm(), params.getClusterMakerEdgeAttribute(), cutoff.orElse(null));
RunClusterMakerResultObserver clusterResultObserver = new RunClusterMakerResultObserver();
TaskIterator tasks = clusterMakerTaskFactory.createTaskIterator(clusterResultObserver);
syncTaskManager.execute(tasks);
return clusterResultObserver.getResult();
Map<String,Collection<CyNode>> result = clusterResultObserver.getResult();

if(hasHidden) {
networkManager.destroyNetwork(network);
}

return result;
}


Expand Down Expand Up @@ -215,7 +239,8 @@ private String createName(NetworkViewSet networkViewSet) {

private Map<String,Collection<CyNode>> computeClustersFromColumn() {
String attribute = params.getClusterDataColumn();
CyNetwork network = params.getNetworkView().getModel();
CyNetworkView networkView = params.getNetworkView();
CyNetwork network = networkView.getModel();

Map<String,Collection<CyNode>> clusters = new HashMap<>();

Expand All @@ -227,25 +252,22 @@ private Map<String,Collection<CyNode>> computeClustersFromColumn() {
}

for(CyNode node : network.getNodeList()) {
if(isHidden(node, networkView))
continue;

List<?> list;
if(isList)
list = network.getRow(node).getList(attribute, type);
else
list = Collections.singletonList(network.getRow(node).get(attribute, type));

for(Object o : list) {
if(o == null)
continue;
String key = String.valueOf(o).trim();
if(key.isEmpty())
continue;

Collection<CyNode> cluster = clusters.get(key);
if(cluster == null) {
cluster = new HashSet<>();
clusters.put(key, cluster);
if(o != null) {
String key = String.valueOf(o).trim();
if(!key.isEmpty()) {
clusters.computeIfAbsent(key, k -> new HashSet<>()).add(node);
}
}
cluster.add(node);
}
}
return clusters;
Expand Down Expand Up @@ -274,12 +296,38 @@ private void limitClusters(Map<String,Collection<CyNode>> clusters, int maxClust
}

private Collection<CyNode> getUnclusteredNodes(Map<String,Collection<CyNode>> clusters) {
CyNetwork network = params.getNetworkView().getModel();
CyNetworkView networkView = params.getNetworkView();
CyNetwork network = networkView.getModel();
Set<CyNode> nodes = new HashSet<>(network.getNodeList());
for(Collection<CyNode> cluster : clusters.values()) {
nodes.removeAll(cluster);
}

// filter out hidden nodes
nodes.removeIf(node -> isHidden(node, networkView));
return nodes;
}


private static boolean isHidden(View<CyNode> nodeView) {
if(nodeView == null)
return false;
return nodeView.getVisualProperty(BasicVisualLexicon.NODE_VISIBLE) == Boolean.FALSE;
}

private static boolean isHidden(CyNode node, CyNetworkView networkView) {
View<CyNode> nodeView = networkView.getNodeView(node);
return isHidden(nodeView);
}

private static boolean hasHiddenNodes(CyNetworkView view) {
return view.getNodeViews().stream().anyMatch(CreateAnnotationSetTask::isHidden);
}

private static Collection<CyNode> getVisibleNodes(CyNetworkView view) {
return view.getNodeViews().stream()
.filter(nv -> !isHidden(nv))
.map(nv -> nv.getModel())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package org.baderlab.autoannotate.internal.task;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.model.VirtualColumnInfo;
import org.cytoscape.model.subnetwork.CyRootNetworkManager;
import org.cytoscape.model.subnetwork.CySubNetwork;
import org.cytoscape.work.AbstractTask;
import org.cytoscape.work.ObservableTask;
import org.cytoscape.work.TaskMonitor;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

public class CreateSubnetworkTask extends AbstractTask implements ObservableTask {

@Inject private CyRootNetworkManager rootNetMgr;

private final CyNetwork parentNetwork;
private final Collection<CyNode> nodes;
private CySubNetwork resultNetwork;


public interface Factory {
CreateSubnetworkTask create(CyNetwork parentNetwork, Collection<CyNode> nodes);
}

@Inject
public CreateSubnetworkTask(@Assisted CyNetwork parentNetwork, @Assisted Collection<CyNode> nodes) {
this.parentNetwork = parentNetwork;
this.nodes = nodes;
}

@Override
public void run(TaskMonitor tm) {
if (parentNetwork == null) {
tm.showMessage(TaskMonitor.Level.ERROR, "Source network must be specified.");
return;
}

tm.setProgress(0.2);

// create subnetwork and add selected nodes and appropriate edges
final CySubNetwork newNet = rootNetMgr.getRootNetwork(parentNetwork).addSubNetwork();

//We need to cpy the columns to local tables, since copying them to default table will duplicate the virtual columns.
addColumns(parentNetwork.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS), newNet.getTable(CyNode.class, CyNetwork.LOCAL_ATTRS));
addColumns(parentNetwork.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS), newNet.getTable(CyEdge.class, CyNetwork.LOCAL_ATTRS) );
addColumns(parentNetwork.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS), newNet.getTable(CyNetwork.class, CyNetwork.LOCAL_ATTRS));

tm.setProgress(0.3);

for(CyNode node : nodes) {
newNet.addNode(node);
cloneRow(parentNetwork.getRow(node), newNet.getRow(node));
//Set rows and edges to not selected state to avoid conflicts with table browser
newNet.getRow(node).set(CyNetwork.SELECTED, false);
}

tm.setProgress(0.4);

for (final CyEdge edge : getEdges(parentNetwork, nodes)) {
newNet.addEdge(edge);
cloneRow(parentNetwork.getRow(edge), newNet.getRow(edge));
//Set rows and edges to not selected state to avoid conflicts with table browser
newNet.getRow(edge).set(CyNetwork.SELECTED, false);
}

tm.setProgress(0.5);

newNet.getRow(newNet).set(CyNetwork.NAME, getNetworkName());

resultNetwork = newNet;
tm.setProgress(1.0);
}

private void addColumns(CyTable parentTable, CyTable subTable) {
List<CyColumn> colsToAdd = new ArrayList<CyColumn>();

for (CyColumn col: parentTable.getColumns())
if (subTable.getColumn(col.getName()) == null)
colsToAdd.add( col );

for (CyColumn col: colsToAdd) {
VirtualColumnInfo colInfo = col.getVirtualColumnInfo();
if (colInfo.isVirtual())
addVirtualColumn(col, subTable);
else
copyColumn(col, subTable);
}
}

private void addVirtualColumn (CyColumn col, CyTable subTable){
VirtualColumnInfo colInfo = col.getVirtualColumnInfo();
CyColumn checkCol= subTable.getColumn(col.getName());

if (checkCol == null)
subTable.addVirtualColumn(col.getName(), colInfo.getSourceColumn(), colInfo.getSourceTable(),
colInfo.getTargetJoinKey(), col.isImmutable());

else
if (!checkCol.getVirtualColumnInfo().isVirtual() ||
!checkCol.getVirtualColumnInfo().getSourceTable().equals(colInfo.getSourceTable()) ||
!checkCol.getVirtualColumnInfo().getSourceColumn().equals(colInfo.getSourceColumn()))
subTable.addVirtualColumn(col.getName(), colInfo.getSourceColumn(), colInfo.getSourceTable(),
colInfo.getTargetJoinKey(), col.isImmutable());
}

private void copyColumn(CyColumn col, CyTable subTable) {
if (List.class.isAssignableFrom(col.getType()))
subTable.createListColumn(col.getName(), col.getListElementType(), false);
else
subTable.createColumn(col.getName(), col.getType(), false);
}

private void cloneRow(final CyRow from, final CyRow to) {
for (final CyColumn column : from.getTable().getColumns()){
if (!column.getVirtualColumnInfo().isVirtual())
to.set(column.getName(), from.getRaw(column.getName()));
}
}

private String getNetworkName() {
return "AutoAnnotate_Temp";
}

/**
* Returns all edges that connect the selected nodes.
*/
private static Set<CyEdge> getEdges(CyNetwork net, Collection<CyNode> nodes) {
Set<CyEdge> edges = new HashSet<CyEdge>();

for (final CyNode n1 : nodes) {
for (final CyNode n2 : nodes)
edges.addAll(net.getConnectingEdgeList(n1, n2, CyEdge.Type.ANY));
}

return edges;
}

@Override
public <R> R getResults(Class<? extends R> type) {
if(CyNetwork.class.equals(type)) {
return type.cast(resultNetwork);
}
return null;
}

}
Loading

0 comments on commit f3bb784

Please sign in to comment.