Created
November 19, 2021 02:40
-
-
Save ryananeff/a82adb2d011b450b5784c77d0e8cdf9b to your computer and use it in GitHub Desktop.
Scrape networks from NDEx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas | |
| import requests, json | |
| req = input("Please enter a URL:") | |
| network_obj = json.loads(requests.get(req).text) | |
| node_names = {i["@id"]:i["n"] for i in network_obj[3]["nodes"]} | |
| edges = network_obj[4]["edges"] | |
| edgeattr = network_obj[7]["edgeAttributes"] | |
| citations = {i["po"]:i["v"] for i in edgeattr if i["n"] == "citation"} | |
| directed = {i["po"]:i["v"] for i in edgeattr if i["n"] == "directed"} | |
| table = [(node_names[int(i["s"])],i["i"],node_names[int(i["t"])], citations[i["@id"]],directed[i["@id"]]) for i in edges] | |
| output = input("Enter a name for the output file (output.tsv):") | |
| output = output if output!="" else "output.tsv" | |
| pandas.DataFrame(table,columns=["source","interaction","target","citation","directed"]).to_csv("output.tsv",sep="\t",index=None) | |
| print("Table saved at " + output + ".") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment