Skip to content

Instantly share code, notes, and snippets.

@ryananeff
Created November 19, 2021 02:40
Show Gist options
  • Select an option

  • Save ryananeff/a82adb2d011b450b5784c77d0e8cdf9b to your computer and use it in GitHub Desktop.

Select an option

Save ryananeff/a82adb2d011b450b5784c77d0e8cdf9b to your computer and use it in GitHub Desktop.
Scrape networks from NDEx
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