cytoscape / py2cytoscape

Python utilities for Cytoscape and Cytoscape.js
https://py2cytoscape.readthedocs.io
MIT License
178 stars 45 forks source link

Problem in selecting nodes in CyNetwork/CyRestClient, loading network in cyrest.client() #90

Closed sayonidas03 closed 5 years ago

sayonidas03 commented 5 years ago
System: Ubuntu 18.04
Python environment: Python 3.5.5, py2cytoscape=0.7.0

Cytoscape: 3.7.1

CyRest: apiVersion v1

Java: 1.8.0_201

Code:

The following code works for loading network in Cytoscape from a dataframe created from a tab-separated file. But I cannot find functions to select nodes based on different criteria to generate different visualizations. Additionally, if it possible to fetch selected nodes from the Cytoscape Desktop GUI, that would be a plus!

from py2cytoscape.data.cynetwork import CyNetwork
from py2cytoscape.data.cyrest_client import CyRestClient
import py2cytoscape.util.util_dataframe as df_util

cy = CyRestClient()
df = pd.read_csv(
        'test_network.txt',
        sep='\t',
        header='infer',
      )

net = df_util.from_dataframe(df, 
                             source_col = 'col1', 
                             target_col = 'col2', 
                             interaction_col = 'interacts', 
                             name='From DataFrame')

auto_net = cy.network.create(data=net, name='cytoscape-automated network')
print('New network created with py2cytoscape.  Its SUID is ' + str(auto_net.get_id()))

Since I couldnt select nodes using CyNetwork/CyRestClient, I tried loading the network using cyrest.client() which seems to have function for selecting nodes using the code below, but I am getting an error (error message shown in the section below).

I would appreciate any help or advice on how to go about doing this. Any pointers or suggestions are welcome. Thanks in advance!


from py2cytoscape import cyrest
cytoscape=cyrest.cyclient()

test_net = cytoscape.network.import_file(afile = 'test_network.txt', 
                                         delimiters='\t',
                                         firstRowAsColumnNames = True,
                                         indexColumnSourceInteraction = 0,
                                         indexColumnTypeInteraction = 1,
                                         indexColumnTargetInteraction = 2,
                                        )

error:

<Response [500]> 500
b'{\n "data": {},\n "errors":[{"status":500,"type":"urn:cytoscape:ci:cyrest-core:v1:handle-json-command:errors:2","message":"Error processing arguments: Couldn\\u0027t parse value from: 2.0 for setting: indexColumnTargetInteraction","link":"file:/home/sayoni/CytoscapeConfiguration/3/framework-cytoscape.log"}]\n}'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-d6a0de7f7950> in <module>()
      4                                          indexColumnSourceInteraction = 0,
      5                                          indexColumnTypeInteraction = 1,
----> 6                                          indexColumnTargetInteraction = 2,
      7                                         )

~/anaconda3/envs/cytoscape_env/lib/python3.5/site-packages/py2cytoscape/cyrest/network.py in import_file(self, dataTypeList, defaultInteraction, delimiters, delimitersForDataList, afile, firstRowAsColumnNames, indexColumnSourceInteraction, indexColumnTargetInteraction, indexColumnTypeInteraction, NetworkViewRendererList, RootNetworkList, startLoadRow, TargetColumnList, verbose)
    464         afile,firstRowAsColumnNames,indexColumnSourceInteraction,indexColumnTargetInteraction,\
    465         indexColumnTypeInteraction,NetworkViewRendererList,RootNetworkList,startLoadRow,TargetColumnList])
--> 466         response=api(url=self.__url+"/import file", PARAMS=PARAMS, method="POST", verbose=verbose)
    467         return response
    468 

~/anaconda3/envs/cytoscape_env/lib/python3.5/site-packages/py2cytoscape/cyrest/base.py in api(namespace, command, PARAMS, host, port, version, method, verbose, url)
    144             print(r.content)
    145             sys.stdout.flush()
--> 146         res=json.loads(r.content)
    147         if "errors" in res.keys():
    148             if len(res["errors"]) > 0:

~/anaconda3/envs/cytoscape_env/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    310     if not isinstance(s, str):
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):
    314         raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",

TypeError: the JSON object must be str, not 'bytes'
zachary822 commented 5 years ago

Can you update to 0.7.1 and try again?

sayonidas commented 5 years ago

Thanks, I will and let you know.