Read and Display JSON Network in Cytoscape Desktop

52 views
Skip to first unread message

Kyle Higgins

unread,
Feb 27, 2023, 11:43:39 AM2/27/23
to cytoscape-app-dev
Hello,

I am currently looking for some help for a relatively simple problem while getting started in app development.. I am interested in reading a JSON network file directly into Cytoscape Desktop from an app. 
I am interested in finding simple bundles or instructions for:
  1. Reading a JSON network file 
  2. Creating a Network View directly from this (without manually iterating over every node if possible, an existing bundle would be great)

In essence the app is intended to create a search bar which takes protein names from the user and contacts a web-based API (this I have working), then receives a JSON in response which I want to automatically load into Cytoscape.

I understand cytoscape.js has great capabilities for JSON but would really prefer to have something working in Cytoscape Desktop. I have so far made my way through the cytoscape web development ladder, reviewed Dr. Morris' slides, and taken a look at the Advanced cookbook but haven't been able to get this going, so wanted to see if anyone could help me get this figured out!

Thanks,
Kyle

Christopher Churas

unread,
Feb 27, 2023, 1:35:17 PM2/27/23
to cytoscap...@googlegroups.com
Hi,

If you have a custom JSON format you could implement your own reader:
http://wikiold.cytoscape.org/Cytoscape_3/AppDeveloper/Cytoscape_3_App_Cookbook#How_to_build_a_network_reader_to_support_my_own_format.3F

Otherwise the Cytoscape Desktop supports the CX JSON format: 

and the core app cx-support offers readers and writers for this format that are accessible via File > Import & File > Export menus
and can be used programmatically via getService() to get the following Reader Manager and then feeding it an InputStream of your CX:

hope this helps,

chris


--
You received this message because you are subscribed to the Google Groups "cytoscape-app-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cytoscape-app-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cytoscape-app-dev/ac6a83e6-3da0-41f4-b4ab-87621746e0a2n%40googlegroups.com.

Kyle Higgins

unread,
Mar 3, 2023, 9:08:57 AM3/3/23
to cytoscape-app-dev
Hi Chris!

Thank you very much. I think this information should do the trick once I have it organized properly, I can follow up if there are any other issues.

I am actually now having trouble launching cytoscape desktop from the command line (and therefore debug mode) which I will post as a separate message.

Thanks,
Kyle

Kyle Higgins

unread,
Apr 16, 2023, 3:41:19 PM4/16/23
to cytoscape-app-dev
Hello,

I wanted to follow up as I have made a few attempts to get this json reader/plotter up and running, but still am having some trouble. 

I will include the simplest attempt below, where I adapted sample-create-network-view from (https://github.com/cytoscape/cytoscape-app-samples/tree/master/sample-create-network-view), which works for me in its original form. In this case, I attempted to use the CyNetworkReader you mentioned along with a sample cx format file from the link you listed (available on https://github.com/cytoscape/cx/blob/CYTOSCAPE-13006/examples/DLoc%20Hierarchy%20demo.cx2) Note: I was not able to import this cx2 file in my cytoscape desktop app using the file>import dropdown menu, so the file itself may also be problematic, or I am not reading it properly. When I try to activate the app in cytoscape the app manager simply reads "Failed to start".

Am I missing something here vital to displaying the network view? I believe it is quite likely I'm misinterpreting how to correctly pass the output of the cynetworkreader to a network view so please let me know if I am doing that incorrectly. 

Below is my code:

//File 1 - ReadAndPlotJSONTask.java

package your.org.myapp.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

import org.cytoscape.io.read.CyNetworkReader;
import org.cytoscape.io.read.CyNetworkReaderManager;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNetworkFactory;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.session.CyNetworkNaming;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewFactory;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.work.AbstractTask;
import org.cytoscape.work.TaskMonitor;

public class ReadAndPlotJSONTask extends AbstractTask {

private final CyNetworkFactory cnf;
private final CyNetworkReaderManager manager;
private final CyNetworkViewFactory cnvf;
private final CyNetworkViewManager networkViewManager;
private final CyNetworkManager networkManager;

public ReadAndPlotJSONTask (CyNetworkFactory cnf, CyNetworkReaderManager manager,
 final CyNetworkViewManager networkViewManager, CyNetworkViewFactory cnvf, CyNetworkManager networkManager) throws IOException {
this.cnf = cnf;
this.manager = manager;
this.networkViewManager = networkViewManager;
this.cnvf = cnvf;
this.networkManager = networkManager;


}


public void run (TaskMonitor monitor) throws IOException {

//Read in file
File initialFile = new File(<File from github, linked above>);
InputStream targetStream = new FileInputStream(initialFile);
   
CyNetworkReader cyReader = manager.getReader(targetStream, "cyReader");

final CyNetwork[] networks = cyReader.getNetworks();

final CyNetwork cyNet = networks[0];

if (cyNet == null)
return;
this.networkManager.addNetwork(cyNet);

final Collection<CyNetworkView> views = networkViewManager.getNetworkViews(cyNet);
CyNetworkView myView = null;
if(views.size() != 0)
myView = views.iterator().next();

if (myView == null) {
// create a new view for my network
myView = cnvf.createNetworkView(cyNet);
networkViewManager.addNetworkView(myView);
} else {
System.out.println("networkView already existed.");
}

boolean destroyView = false;
if (destroyView) {
networkViewManager.destroyNetworkView(myView);
}
}

}

//File 2 - ReadAndPlotJSONTaskFactory.java

package your.org.myapp.internal;

import java.io.IOException;

import org.cytoscape.io.read.CyNetworkReaderManager;
import org.cytoscape.model.CyNetworkFactory;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.view.model.CyNetworkViewFactory;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.work.AbstractTaskFactory;
import org.cytoscape.work.TaskIterator;

public class ReadAndPlotJSONTaskFactory extends AbstractTaskFactory {

private final CyNetworkFactory cnf;
private final CyNetworkReaderManager manager;
private final CyNetworkViewManager networkViewManager;
private final CyNetworkViewFactory cnvf;
private final CyNetworkManager networkManager;

public ReadAndPlotJSONTaskFactory(CyNetworkFactory cnf, CyNetworkReaderManager manager, final CyNetworkViewManager networkViewManager, CyNetworkViewFactory cnvf, CyNetworkManager networkManager) {

this.cnf = cnf;
this.manager = manager;
this.networkViewManager = networkViewManager;
this.cnvf = cnvf;
this.networkManager = networkManager;
}

public TaskIterator createTaskIterator(){
try {
return new TaskIterator(new ReadAndPlotJSONTask(cnf, manager, networkViewManager, cnvf, networkManager));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;

}
}

//File 3 - CyActivator.java

package your.org.myapp.internal;

import org.cytoscape.service.util.AbstractCyActivator;
import org.osgi.framework.BundleContext;
import org.cytoscape.application.swing.CyAction;
import org.cytoscape.application.swing.CySwingApplication;
import org.cytoscape.application.swing.CytoPanelComponent;

import java.util.Properties;


class CyActivator extends AbstractCyActivator {

public CyActivator() {
super();
}

@Override
public void start(BundleContext bc) {
CySwingApplication cytoscapeDesktopService = getService(bc, CySwingApplication.class);
MyPanelTask myPanel = new MyPanelTask();
MyPanelAction panelAction = new MyPanelAction(cytoscapeDesktopService,myPanel);

registerService(bc,myPanel,CytoPanelComponent.class, new Properties());
registerService(bc,panelAction,CyAction.class, new Properties());

}
}

Scooter Morris

unread,
Apr 19, 2023, 11:35:30 PM4/19/23
to cytoscape-app-dev
Hi Kyle,

First, you don't say what actually happens.  Does the network show up in the network panel?  If not, you didn't actually read it in.  It also looks like you never actually registered your ReadAndPlotJSONTaskFactory.  You need to create that task factory and register it in your CyActivator before anything can happen.  All I see in your CyActivator is the creation of a CytoPanel.  In any case, this is a rally good start!!  Bravo!

-- scooter

Kyle Higgins

unread,
Apr 20, 2023, 12:55:16 PM4/20/23
to cytoscape-app-dev
Hi Scooter,

Thanks for your help. After installation, the app has the 'Failed to Start' status from the App Manager dropdown menu. No network or side panels were displayed. This is unlike the similar sample-create-network-view app which works like a charm once installed. Based on my (very novice) understanding of app design I'm guessing I'm either not accessing the CyNetwork properly from cyReader or not communicating to my network manager properly in  ReadAndPlotJSONTask.

My apologies for posting the incomplete CyActivtor class! I had some lines for that I commented out for some reason while debugging. Here is the CyActivator method now (below), hopefully registering the service properly. 

And just wanted to say thanks for all of the resources you have made for learning the basics of cytoscape app design. Your developer's tutorials made it very easy to get started and I am sincerely grateful for that. 

Best wishes,
Kyle

package your.org.myapp.internal;

import org.cytoscape.service.util.AbstractCyActivator;
import org.osgi.framework.BundleContext;
import org.cytoscape.application.swing.CyAction;
import org.cytoscape.application.swing.CySwingApplication;
import org.cytoscape.application.swing.CytoPanelComponent;

import java.util.Properties;


public class CyActivator extends AbstractCyActivator {



public CyActivator() {
super();
}

@Override
public void start(BundleContext bc) {
CySwingApplication cytoscapeDesktopService = getService(bc, CySwingApplication.class);
MyPanelTask myPanel = new MyPanelTask();
MyPanelAction panelAction = new MyPanelAction(cytoscapeDesktopService,myPanel);

registerService(bc,myPanel,CytoPanelComponent.class, new Properties());
registerService(bc,panelAction,CyAction.class, new Properties());

CyNetworkFactory cyNetworkFactoryServiceRef = getService(bc,CyNetworkFactory.class);
CyNetworkReaderManager cyNetworkReaderManagerServiceRef = getService(bc, CyNetworkReaderManager.class);
CyNetworkViewManager cyNetworkViewManagerServiceRef = getService(bc,CyNetworkViewManager.class);
CyNetworkManager cyNetworkManagerServiceRef = getService(bc,CyNetworkManager.class);
CyNetworkViewFactory cyNetworkViewFactoryServiceRef = getService(bc,CyNetworkViewFactory.class);

ReadAndPlotJSONTaskFactory readAndPlotJSONTaskFactory = new ReadAndPlotJSONTaskFactory(cyNetworkFactoryServiceRef, cyNetworkReaderManagerServiceRef, cyNetworkViewManagerServiceRef, cyNetworkViewFactoryServiceRef, cyNetworkManagerServiceRef);

Properties readAndPlotJSONTaskFactoryProps = new Properties();
readAndPlotJSONTaskFactoryProps.setProperty("preferredMenu","Apps.Samples");
readAndPlotJSONTaskFactoryProps.setProperty("title","Read and Plot JSON Network");
registerService(bc,readAndPlotJSONTaskFactory,TaskFactory.class, readAndPlotJSONTaskFactoryProps);
}
}

Scooter Morris

unread,
Apr 26, 2023, 11:36:03 PM4/26/23
to cytoscape-app-dev
Hi Kyle,

If it actually fails to start, then something is throwing an error or exception.  I would suggest running this from the command line and see what kind of exception or error gets thrown at startup.  No other debugging will be effective until we can get the app to start.   Once we know what exception is getting thrown, we can figure out what's going on.

-- scooter

Kyle Higgins

unread,
May 15, 2023, 11:17:08 AM5/15/23
to cytoscape-app-dev
Hi Scooter,

Thanks for the suggestion! Since debugging from the command line, I now have it much further along. I am able to startup an app, but it looks to me like the I'm either not accessing the network from my reader properly or my cx files are faulty. I am able to see that a cx format file is recognized and my cyReader is initialized without error. However, when I move in the next step to access the networks from this object, trouble arises. For a brief summary:

Working bit:
App:
File initialFile = new File("cytoscape_cx_format_version3_draft.cx");

InputStream targetStream = new FileInputStream(initialFile);
System.out.println("File Stream loads");
CyNetworkReader cyReader = manager.getReader(targetStream, "cyReader");
System.out.println("CyNetworkReader loads");
Terminal:
>File Stream loads
>niceCX              14      : 16 ms
>CyNetworkReader loads

Trouble:
App:
System.out.println(cyReader);
System.out.println(cyReader.getNetworks());

Terminal:
>org.cytoscape.io.internal.cx_reader.CytoscapeCxNetworkReader@6d6234f2
>null

So it appears to me that I am either misunderstanding how to properly access the network from this object or my cx files are faulty. Now, the only cx file I have been able to get recognized by cyreader was taken from the RCX project (https://github.com/frankkramer-lab/RCX/blob/master/inst/extdata/RCX_Data_Structure.cx), which I believe is actually the outdated cx version (version 1). Otherwise the other examples on github I mentioned (linked by https://cytoscape.org/cx/) didn't work for me.

Could you possibly help me to check that I am setting up my CyReader object properly, and would be accessing my network properly? If so, are there any cx files you are aware of that you are sure would work using the reader? Help with either task would be very much appreciated!

A longer record of my code below:
 


App:
public class CreateNetworkViewTask extends AbstractTask {


private final CyNetworkFactory cnf;
private final CyNetworkReaderManager manager;
private final CyNetworkViewFactory cnvf;
private final CyNetworkViewManager networkViewManager;
private final CyNetworkManager networkManager;
private final CyNetworkNaming cyNetworkNaming;

public CreateNetworkViewTask(CyNetworkReaderManager manager, CyNetworkNaming cyNetworkNaming, CyNetworkFactory cnf, CyNetworkManager networkManager,
CyNetworkViewFactory cnvf, final CyNetworkViewManager networkViewManager) {
this.cnf = cnf;
this.cnvf = cnvf;

this.manager = manager;
this.networkViewManager = networkViewManager;
this.networkManager = networkManager;
this.cyNetworkNaming = cyNetworkNaming;
}

public void run(TaskMonitor monitor) throws URISyntaxException, FileNotFoundException {


File initialFile = new File("cytoscape_cx_format_version3_draft.json");


InputStream targetStream = new FileInputStream(initialFile);

System.out.println("File Stream loads");


CyNetworkReader cyReader = manager.getReader(targetStream, "cyReader");

System.out.println("CyNetworkReader loads");

//Note this now returns a network, so cyReader isnt empty
System.out.println(cyReader);

//This returns null, so networks are not loaded properly or at least not accesses properly
System.out.println("cyReader.getNetworks()");
System.out.println(cyReader.getNetworks());

//Attempt 1 to load network into view: buildCyNetworkView using getNetworks
CyNetworkView myView = cyReader.buildCyNetworkView(cyReader.getNetworks()[0]);

//Attempt 2 to load network into view: use cyreader on blank net
//CyNetwork myNet = this.cnf.createNetwork();
//CyNetworkView myView = cyReader.buildCyNetworkView(myNet);

System.out.println("BuildCyNetworkView works");

networkViewManager.addNetworkView(myView);

System.out.println("Networkview added to manager");


boolean destroyView = false;
if (destroyView) {
networkViewManager.destroyNetworkView(myView);
}
}

}

Terminal:
File Stream loads
niceCX              13      : 4 ms
CyNetworkReader loads
org.cytoscape.io.internal.cx_reader.CytoscapeCxNetworkReader@1b9962ac
cyReader.getNetworks()
null
java.lang.NullPointerException: null
        at org.cytoscape.sample.internal.CreateNetworkViewTask.run(CreateNetworkViewTask.java:110) ~[?:?]
        at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.innerRun(JDialogTaskManager.java:321) ~[!/:?]
        at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.run(JDialogTaskManager.java:352) [!/:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
        at java.lang.Thread.run(Thread.java:834) [?:?]
java.lang.NullPointerException
        at org.cytoscape.sample.internal.CreateNetworkViewTask.run(CreateNetworkViewTask.java:110)
        at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.innerRun(JDialogTaskManager.java:321)
        at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.run(JDialogTaskManager.java:352)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)






Thanks so much as always!
Kyle

Scooter Morris

unread,
May 20, 2023, 4:16:50 PM5/20/23
to cytoscape-app-dev
Hi Kyle,
   You never called the run method of the network reader, so it never read anything.  You need to call cyReader.run(monitor) before you call getNetworks()

-- scooter
Reply all
Reply to author
Forward
0 new messages