use of org.cytoscape.view.model.View in project cytoscape-impl by cytoscape.
the class EdgeBundlerTask method run.
@Override
public void run(TaskMonitor tm) {
// Check tunables
if (numNubs < 1)
numNubs = 1;
if (numNubs > 50) {
logger.warn("Maximum handles is 50.");
numNubs = 50;
}
tm.setTitle("Edge Bundle Layout");
// Pre-cache data structures
tm.setStatusMessage("Caching network data");
Collection<View<CyEdge>> edges = null;
// Get selection
if (// Use all edges
selection == 0) {
edges = this.view.getEdgeViews();
} else if (// Use selected nodes only
selection == 1) {
Collection<View<CyEdge>> edgeView = this.view.getEdgeViews();
edges = new ArrayList<View<CyEdge>>(edgeView.size());
for (View<CyEdge> e : edgeView) {
boolean n1 = view.getNodeView(e.getModel().getSource()).getVisualProperty(EDGE_SELECTED);
boolean n2 = view.getNodeView(e.getModel().getTarget()).getVisualProperty(EDGE_SELECTED);
if (n1 && n2)
edges.add(e);
}
} else if (// Use selected edges only
selection == 2) {
Collection<View<CyEdge>> edgeView = this.view.getEdgeViews();
edges = new ArrayList<View<CyEdge>>(edgeView.size());
for (View<CyEdge> e : edgeView) if (e.getVisualProperty(EDGE_SELECTED))
edges.add(e);
}
int ei = 0;
for (View<CyEdge> e : edges) {
View<CyNode> eSource = view.getNodeView(e.getModel().getSource());
View<CyNode> eTarget = view.getNodeView(e.getModel().getTarget());
if (eSource.getSUID().equals(eTarget.getSUID()))
continue;
ei++;
}
numEdges = ei;
if (numEdges < 2) {
logger.warn("Less than two edges found.");
return;
}
edgePos = new double[2][2][numEdges];
nubs = new double[numNubs][2][numEdges];
edgeLength = new double[numEdges];
ei = 0;
for (final View<CyEdge> e : edges) {
// System.out.println("SUID: "+e.getModel().getSUID());
View<CyNode> eSource = view.getNodeView(e.getModel().getSource());
View<CyNode> eTarget = view.getNodeView(e.getModel().getTarget());
if (eSource.getSUID().equals(eTarget.getSUID()))
continue;
edgePos[0][0][ei] = eSource.getVisualProperty(NODE_X_LOCATION);
edgePos[0][1][ei] = eSource.getVisualProperty(NODE_Y_LOCATION);
edgePos[1][0][ei] = eTarget.getVisualProperty(NODE_X_LOCATION);
edgePos[1][1][ei] = eTarget.getVisualProperty(NODE_Y_LOCATION);
double diffx = edgePos[1][0][ei] - edgePos[0][0][ei];
double diffy = edgePos[1][1][ei] - edgePos[0][1][ei];
for (int ni = 0; ni < numNubs; ni++) {
nubs[ni][0][ei] = (diffx) * (ni + 1) / (numNubs + 1) + edgePos[0][0][ei];
nubs[ni][1][ei] = (diffy) * (ni + 1) / (numNubs + 1) + edgePos[0][1][ei];
}
edgeLength[ei] = Math.sqrt(diffx * diffx + diffy * diffy);
ei++;
}
computeEdgeCompatability();
// Simulating physics
tm.setStatusMessage("Simulating physics");
double time = System.nanoTime();
final double maxItrDouble = Double.valueOf(maxIterations);
// Nub, X/Y, edgeIndex
final double[][][] forces = new double[numNubs][2][numEdges];
final HandleFactory handleFactory = serviceRegistrar.getService(HandleFactory.class);
final BendFactory bendFactory = serviceRegistrar.getService(BendFactory.class);
final VisualMappingManager visualMappingManager = serviceRegistrar.getService(VisualMappingManager.class);
final VisualMappingFunctionFactory discreteFactory = serviceRegistrar.getService(VisualMappingFunctionFactory.class, "(mapping.type=discrete)");
// Repeat the simulation [maxIterations] times.
for (int iteri = 0; iteri < maxIterations; iteri++) {
if (this.cancelled) {
logger.info("Edge bundling cancelled: iter=" + iteri);
break;
}
tm.setProgress(iteri / maxItrDouble);
updateForces(forces);
updateNubs(forces);
// Check convergence once in awhile
if (iteri % 1000 == 0 && isConverged(forces, .01)) {
logger.info("Edge bundling converged: iter=" + iteri);
break;
}
if (iteri == maxIterations - 1) {
logger.info("Edge bundling did not converge: iter=" + iteri);
break;
}
if (animate && System.nanoTime() - time > 3) {
render(edges, handleFactory, bendFactory, visualMappingManager, discreteFactory);
time = System.nanoTime();
}
}
render(edges, handleFactory, bendFactory, visualMappingManager, discreteFactory);
}
use of org.cytoscape.view.model.View in project cytoscape-impl by cytoscape.
the class NodeChangeListener method updateGroupLocation.
private void updateGroupLocation(CyNetworkView networkView, View<CyNode> nodeView) {
double groupX = nodeView.getVisualProperty(BasicVisualLexicon.NODE_X_LOCATION);
double groupY = nodeView.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION);
CyGroup group = cyGroupManager.getGroup(nodeView.getModel(), networkView.getModel());
if (group == null)
return;
// System.out.println("Updating group "+group+" location");
Dimension lastPosition = ViewUtils.getLocation(networkView.getModel(), group);
double xOffset = lastPosition.getWidth() - groupX;
double yOffset = lastPosition.getHeight() - groupY;
// System.out.println("Group node offset = "+xOffset+","+yOffset);
List<View<CyNode>> groupNodeList = new ArrayList<>();
boolean lastIgnoreChanges = ignoreChanges;
ignoreChanges = true;
// OK, move all of our nodes
for (CyNode node : group.getNodeList()) {
View<CyNode> nv = networkView.getNodeView(node);
if (nv == null)
continue;
double x = nv.getVisualProperty(BasicVisualLexicon.NODE_X_LOCATION);
double y = nv.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION);
// System.out.println("Node location = "+x+","+y);
Dimension d = new Dimension();
// System.out.println("Moving node to "+(x-xOffset)+","+(y-yOffset));
d.setSize(x - xOffset, y - yOffset);
ViewUtils.moveNode(networkView, node, d);
if (cyGroupManager.isGroup(node, networkView.getModel()))
groupNodeList.add(nv);
}
final CyEventHelper cyEventHelper = cyGroupManager.getService(CyEventHelper.class);
cyEventHelper.flushPayloadEvents();
ViewUtils.updateGroupLocation(networkView.getModel(), group, groupX, groupY);
if (groupNodeList.size() > 0) {
for (View<CyNode> nv : groupNodeList) {
updateGroupLocation(networkView, nv);
}
}
ignoreChanges = lastIgnoreChanges;
cyEventHelper.flushPayloadEvents();
}
use of org.cytoscape.view.model.View in project cytoscape-impl by cytoscape.
the class GMLNetworkReader method layoutGraphics.
/**
* Assigns visual properties to node and edge views, based on the values in
* the GML "graphics" list.
*/
@SuppressWarnings("unchecked")
private void layoutGraphics(final CyNetworkView netView, final List<KeyValue> list, final View<? extends CyIdentifiable> view) {
final CyIdentifiable model = view.getModel();
Class<? extends CyIdentifiable> type = CyNetwork.class;
if (model instanceof CyNode)
type = CyNode.class;
else if (model instanceof CyEdge)
type = CyEdge.class;
boolean srcArrowParsed = false;
boolean tgtArrowParsed = false;
for (final KeyValue keyVal : list) {
String key = keyVal.key;
Object value = keyVal.value;
Object vpValue = null;
if (type == CyNode.class) {
if (key.equals(OUTLINE_WIDTH))
key = WIDTH;
} else if (type == CyEdge.class) {
if (key.equals(LINE)) {
// This is a polyline obj. However, it will be translated
// into straight line.
layoutEdgeGraphicsLine(netView, (List<KeyValue>) value, (View<CyEdge>) view);
continue;
}
if (key.equals(YED_ARROW) && !(srcArrowParsed || tgtArrowParsed)) {
if (value.toString().equals(ARROW_NONE))
continue;
if (value.toString().equals(ARROW_FIRST))
key = YED_SOURCE_ARROW;
else if (value.toString().equals(ARROW_LAST))
key = YED_TARGET_ARROW;
vpValue = ArrowShapeVisualProperty.ARROW;
} else {
if (key.equals(YED_SOURCE_ARROW) || key.equals(YED_TARGET_ARROW)) {
vpValue = yedArrowShapes.get(value.toString());
} else if (key.equals(SOURCE_ARROW) || key.equals(TARGET_ARROW)) {
key = key.replace("_arrow", "Arrow");
vpValue = legacyArrowShapes.get(value.toString());
}
if (key.equals(YED_SOURCE_ARROW))
srcArrowParsed = true;
if (key.equals(YED_TARGET_ARROW))
tgtArrowParsed = true;
}
}
final Set<VisualProperty<?>> vpSet = getVisualProperties(type, key);
if (vpSet.isEmpty()) {
unrecognizedVisualPropertyMgr.addUnrecognizedVisualProperty(netView, view, key, value.toString());
} else {
for (final VisualProperty<?> vp : vpSet) {
if (vpValue == null)
vpValue = vp.parseSerializableString(value.toString());
if (vpValue != null) {
if (isLockedVisualProperty(model, key)) {
// so we should avoid bypass.
if (vp != BasicVisualLexicon.NODE_LABEL) {
view.setLockedValue(vp, vpValue);
}
} else
view.setVisualProperty(vp, vpValue);
}
}
}
}
}
use of org.cytoscape.view.model.View in project cytoscape-impl by cytoscape.
the class GenericXGMMLReader method buildCyNetworkView.
@Override
public CyNetworkView buildCyNetworkView(final CyNetwork network) {
NetworkViewRenderer networkViewRenderer = null;
if (readDataMgr.getRendererId() != null)
networkViewRenderer = cyApplicationManager.getNetworkViewRenderer(readDataMgr.getRendererId());
// If the XGMML has no renderer ID info or a NetworkViewRenderer with that ID can't be found,
// just use the passed CyNetworkViewFactory.
// Otherwise use the factory provided by the specified NetworkViewRenderer.
final CyNetworkViewFactory netViewFactory = networkViewRenderer != null ? networkViewRenderer.getNetworkViewFactory() : getNetworkViewFactory();
final CyNetworkView netView = netViewFactory.createNetworkView(network);
setNetworkViewProperties(netView);
if (netView.getModel().getNodeCount() > 0) {
final Collection<View<CyNode>> nodes = netView.getNodeViews();
final Collection<View<CyEdge>> edges = netView.getEdgeViews();
for (final View<CyNode> nodeView : nodes) setNodeViewProperties(netView, nodeView);
for (final View<CyEdge> edgeView : edges) setEdgeViewProperties(netView, edgeView);
}
if (!readDataMgr.isSessionFormat())
readDataMgr.updateGroupNodes(netView);
return netView;
}
use of org.cytoscape.view.model.View in project PhenomeScape by soulj.
the class PhenomeExpress method run.
@Override
public void run(TaskMonitor taskMonitor) throws Exception {
taskMonitor.setTitle("Running PhenomeExpress");
taskMonitor.setStatusMessage("Preparing Networks");
long time = System.currentTimeMillis();
inputCheck();
setUpNetworks();
double[] relativeWeighting = { 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 };
double[] alphas = { 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 };
double[] piValues = proteinNetwork.getPivalues();
Map<String, Integer> phenotypeMap = phenomeNetwork.getName2IndexMap();
double[] phenoArray = new double[phenomeNetwork.getName2IndexMap().size()];
selectedPhenotypes = new ArrayList<Phenotype>();
boolean first = true;
for (Phenotype phenotype : phenotypes) {
if (phenotype.getSelected()) {
if (first) {
phenotypeSelected = phenotype.getID();
phenotypeNamesSelected = phenotype.getName();
selectedPhenotypes.add(phenotype);
phenoArray[phenotypeMap.get(phenotype.getID())] = 1.0;
first = false;
} else {
phenotypeSelected = phenotypeSelected + " ," + phenotype.getID();
phenotypeNamesSelected = phenotypeNamesSelected + " ," + phenotype.getName();
phenoArray[phenotypeMap.get(phenotype.getID())] = 1.0;
selectedPhenotypes.add(phenotype);
}
}
}
Map<CyNode, Integer> map = proteinNetwork.getNode2IndexMap();
int size = proteinNetwork.getNetwork().getNodeCount();
int[] occuranceArray = new int[size];
HashMap<CyNode, Protein> node2Protein = new HashMap<CyNode, Protein>();
if (cancelled) {
return;
}
taskMonitor.setStatusMessage("Calculating Activity Scores");
taskMonitor.setProgress(0.0);
double progress = 0.0;
SparseRowMatrix adjMatrix = SparseRowMatrix.create(size, size);
double[] transitionProbs = { 0.5, 0.6, 0.7, 0.8 };
for (double transitionProb : transitionProbs) {
TransitionMatrix transitionMatrix = new TransitionMatrix(phenomeNetwork.normalise(transitionProb, phenoGeneNetwork), proteinNetwork.normalise(transitionProb, phenoGeneNetwork), phenoGeneNetwork.normaliseT(transitionProb), phenoGeneNetwork.normalise(transitionProb));
for (double weighting : relativeWeighting) {
if (cancelled) {
return;
}
transitionMatrix.prepareVector(piValues, phenoArray, weighting);
for (double alpha : alphas) {
progress += 0.0051;
taskMonitor.setProgress(progress);
double[] pageRankResults = transitionMatrix.pageRank(alpha);
HashMap<Integer, Protein> index2ProteinMap = proteinNetwork.getIndex2ProteinMap();
for (int i = 0; i < index2ProteinMap.size(); i++) {
Protein protein = index2ProteinMap.get(i);
protein.setValue(pageRankResults[i]);
}
List<Protein> proteinList = new ArrayList<Protein>(index2ProteinMap.values());
Collections.sort(proteinList, new DescendingScoreComparator());
int rank = 0;
for (Protein protein : proteinList) {
rank++;
protein.setRank(rank);
node2Protein.put(protein.getNode(), protein);
}
Collections.sort(proteinList, new DescendingScoreComparator());
GIGA giga = new GIGA(proteinList, node2Protein, proteinNetwork, maxNetworkSize);
giga.compute();
for (GIGACluster cluster : giga.getClusters()) {
for (CyNode node : cluster.getCluster()) {
Integer index = map.get(node);
AVector row = adjMatrix.getRow(index);
if (!row.isMutable()) {
row = row.mutable();
}
for (CyNode node2 : cluster.getCluster()) {
Integer index2 = map.get(node2);
row.set(index2, row.get(index2) + 1.0);
occuranceArray[index2]++;
}
adjMatrix.replaceRow(index, row);
}
}
}
}
}
if (cancelled) {
return;
}
taskMonitor.setProgress(1.0);
taskMonitor.setStatusMessage("Creating Consensus Subnetworks");
AMatrix cooccuranceMat = adjMatrix.transposeInnerProduct(adjMatrix);
for (int i = 0; i < size; i++) {
AVector row = cooccuranceMat.getRow(i);
if (row.elementSum() > 0) {
row.divide(occuranceArray[i]);
Index nonSparse = row.nonSparseIndex();
for (int j = 0; j < nonSparse.length(); j++) {
if (row.get(nonSparse.get(j)) < 1.0) {
row.set(nonSparse.get(j), 0.0);
}
}
cooccuranceMat.replaceRow(i, row.sparse());
}
}
ConnectedComponentAnalyser CCA = new ConnectedComponentAnalyser(node2Protein);
ArrayList<PhenomeExpressSubnetwork> subnetworks = CCA.ccFromAdjMatrix(adjMatrix, proteinNetwork);
Iterator<PhenomeExpressSubnetwork> it = subnetworks.iterator();
while (it.hasNext()) {
PhenomeExpressSubnetwork subnetwork = it.next();
if (subnetwork.getNodeList().size() < this.minNetSize) {
it.remove();
}
}
if (cancelled) {
return;
}
taskMonitor.setStatusMessage("Calculating Subnetwork Significance");
subnetworksSignificance(subnetworks);
taskMonitor.setStatusMessage("Visualising Subnetworks");
List<Double> foldChangeValues = proteinNetwork.getNodeTable().getColumn(foldChange).getValues(Double.class);
double fcMax = Collections.max(foldChangeValues);
double fcMin = Collections.min(foldChangeValues);
// Apply the visual style to a NetworkView
VizStyle vizStyle = new VizStyle();
VisualStyle vs = vizStyle.createVizStyle(cyServiceRegistrar, geneName, foldChange, fcMax, fcMin);
this.property = getNodeLabelPositionProperty();
vs.setDefaultValue(property, property.parseSerializableString("N,S,c,0.0,0.0"));
VisualMappingManager visualMappingManager = cyServiceRegistrar.getService(VisualMappingManager.class);
visualMappingManager.setCurrentVisualStyle(vs);
ArrayList<CyNetworkView> networkViewList = new ArrayList<CyNetworkView>();
Iterator<PhenomeExpressSubnetwork> it2 = subnetworks.iterator();
CyNetworkManager networkManager = cyServiceRegistrar.getService(CyNetworkManager.class);
CyNetworkViewFactory networkViewFactory = cyServiceRegistrar.getService(CyNetworkViewFactory.class);
CyLayoutAlgorithmManager layoutManager = cyServiceRegistrar.getService(CyLayoutAlgorithmManager.class);
CyApplicationManager cyApplicationManager = cyServiceRegistrar.getService(CyApplicationManager.class);
CyLayoutAlgorithm layout = layoutManager.getLayout("force-directed");
Map<String, Object> settings = new HashMap<String, Object>();
settings.put("defaultSpringLength", 5.0);
settings.put("defaultSpringNodeMass", 2.0);
TunableSetter setter = cyServiceRegistrar.getService(TunableSetter.class);
Object context = layout.createLayoutContext();
setter.applyTunables(context, settings);
GOTermAnalyser2 goTermAnalyser = new GOTermAnalyser2(proteinNetwork, species);
while (it2.hasNext()) {
PhenomeExpressSubnetwork subnet = it2.next();
if (subnet.getPvalue() > this.threshold) {
it2.remove();
} else {
goTermAnalyser.calculateGOTermPValues(subnet, proteinNetwork);
String subnetworkName = NetworkUtils.getUniqueNetworkName(cyServiceRegistrar, networkName + "_" + subnet.getBestGOTerm());
subnet.setName(subnetworkName);
CySubNetwork subnetwork = NetworkUtils.createSubNetwork(((CySubNetwork) proteinNetwork.getNetwork()).getRootNetwork(), subnet.getNodeList());
subnetwork.getRow(subnetwork).set(CyNetwork.NAME, subnetworkName);
addSeedPhenotypes(subnetwork);
networkManager.addNetwork(subnetwork);
CyNetworkViewManager viewManager = cyServiceRegistrar.getService(CyNetworkViewManager.class);
CyNetworkView nv = networkViewFactory.createNetworkView(subnetwork);
viewManager.addNetworkView(nv);
cyApplicationManager.setCurrentNetworkView(nv);
for (CyNode phenoNode : phenotypesAdded) {
View<CyNode> nodeView = nv.getNodeView(phenoNode);
nodeView.setLockedValue(BasicVisualLexicon.NODE_SHAPE, NodeShapeVisualProperty.RECTANGLE);
nodeView.setLockedValue(BasicVisualLexicon.NODE_FILL_COLOR, Color.BLUE);
}
for (CyEdge phenoEdge : phenoEdges) {
View<CyEdge> edgeView = nv.getEdgeView(phenoEdge);
edgeView.setLockedValue(BasicVisualLexicon.EDGE_LINE_TYPE, LineTypeVisualProperty.DOT);
edgeView.setLockedValue(BasicVisualLexicon.EDGE_PAINT, Color.BLUE);
}
networkViewList.add(nv);
Set<View<CyNode>> nodeSet = Collections.emptySet();
cyServiceRegistrar.getService(TaskManager.class).execute(layout.createTaskIterator(nv, context, nodeSet, null));
}
}
CyEventHelper cyEventHelper = cyServiceRegistrar.getService(CyEventHelper.class);
for (CyNetworkView nv : networkViewList) {
visualMappingManager.setVisualStyle(vs, nv);
vs.apply(nv);
cyEventHelper.flushPayloadEvents();
cyApplicationManager.setCurrentNetworkView(nv);
nv.updateView();
}
phenomeNetwork = null;
proteinNetwork = null;
ResultsPanel resultsPanel = (ResultsPanel) CytoPanelUtils.getCytoPanel(cyServiceRegistrar, ResultsPanel.class, CytoPanelName.EAST);
resultsPanel.newTableData(getResultsSummary(subnetworks), getParameterList());
CytoPanelUtils.showCytoPanel(cyServiceRegistrar, CytoPanelName.EAST);
}
Aggregations