GraphQL example for PeptoidDB API
This file contains 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
#importing requests and json module | |
import requests | |
import json | |
#returns the set of residues in peptoids with author given by input lastName | |
def authorToResidues(lastName): | |
#GrapQL query for authors by lastName and their published peptoids' residues | |
query = """ | |
{ | |
authors { | |
edges { | |
node { | |
lastName | |
peptoids { | |
edges { | |
node { | |
peptoidResidue { | |
edges { | |
node { | |
longName}}}}}}}}}} | |
""" | |
#sending request to /graphql route | |
url = "https://databank.peptoids.org/graphql" | |
r = requests.post(url,json={'query':query}) | |
#parsing response for author given by last name and then the residues in their peptoids | |
rDict = json.loads(r.text) | |
authorNodes = rDict['data']['authors']['edges'] | |
for author in authorNodes: | |
if author['node']['lastName'] == lastName: | |
peptoids = author['node']['peptoids']['edges'] | |
residues = [p['node']['peptoidResidue']['edges'] for p in peptoids] | |
residues = set([item['node']['longName'] for sublist in residues for item in sublist]) | |
return residues #set object of peptoids found | |
print(authorToResidues('Kirshenbaum')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment