Skip to content

Instantly share code, notes, and snippets.

@etweisberg
Last active April 5, 2021 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etweisberg/2ed84cae6fe86989dabd6822c2ccf285 to your computer and use it in GitHub Desktop.
Save etweisberg/2ed84cae6fe86989dabd6822c2ccf285 to your computer and use it in GitHub Desktop.
GraphQL example for PeptoidDB API
#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