# Comments start with "#" and do not need to be run in the terminal. # This is an example to run in your bash shell terminal. # Commands start with "$" and the output is shown after the command, e.g. for the date command: $ date Mon Apr 1 11:11:14 PDT 2024 # use the curl command to do a simple call to the SMC API & return JSON $ curl https://smc-dev.jgi.lbl.gov/v2/msg/hello {"msg_time":"2024-04-03 11:35:56","msg":"hello","msg_ip":"172.71.154.171"}
>>> import requests >>> url="https://smc-dev.jgi.lbl.gov" >>> endpoint="/v2/msg" >>> param="hello" >>> r = requests.get(f"{url}/{endpoint}/{param}") >>> print(r.text) {"msg_time":"2024-10-01 15:26:35","msg":"hello","msg_ip":"172.71.155.71"}
# get source data from from the source_data API using id 58411 $ curl https://smc-dev.jgi.lbl.gov/v2/source-data/58411 {"source_data":{"source_id":3210,"size_bp":5892123,"gc_count":35.18,"source_data_type_id":1,"dna_sequence_file_id":22695,"gff_file_id":22696,"taxonomy_id":718224,"accession_id":"2537562224", ...}}
# set the SMC_USER_TOKEN variable to store the user token $ export SMC_USER_TOKEN="HHGTTG42" $ curl -X 'GET' \ "https://smc-dev.jgi.lbl.gov/v2/users/me" \ -H 'accept: application/json' \ -H "Authorization: Bearer $SMC_USER_TOKEN" | jq . { "dt_join": "Feb 03, 2022 02:22:36 pm", "dt_login": "Apr 03, 2024 11:44:20 am", "first_name": "Donnie", "last_name": "Baker", "email": "[email protected]", "role_name": "regular", "token_exp_sec": 21868.049319 }
#!/usr/bin/env python import requests import json # setup variables to use smc_url = "https://smc-dev.jgi.lbl.gov" # SMC website # smc user token from SMC website, need to refresh every 48 hours smc_user_token = "HHGTTG42" # normally longer # setup smc_header to send the smc_user_token smc_header = {"Authorization": f"Bearer {smc_user_token}", "Content-Type": "application/json"} # wait up to 30 seconds to get a response from the SMC backend API timeout_sec = 30 # put together the API call smc_api = f"{smc_url}/v2/users/me" print(f"GET {smc_api}") # use the requests library to call the API r = requests.get(smc_api, headers=smc_header, timeout=timeout_sec) # use the json library to load the response into the smc_data json variable smc_data = json.loads(r.text) # print results print("User email: {}, name: {} {}".format(smc_data['data'].get("email"), smc_data['data'].get("first_name"), smc_data['data'].get("last_name")))
# using the same token from previous example $ wget https://smc-dev.jgi.lbl.gov/v2/bgc/annotation/7417122
$ curl https://smc-dev.jgi.lbl.gov/v2/search/source-name \ -d '{"search": "Escherichia Coli", "page_size": 5}' \ -H 'Content-Type: application/json' | jq . { "total_count": 1000, "search_results": [ { "smc_id": 5713, "tax_name": "Escherichia coli BIDMC 20B", "bgc_count": 13 }, { "smc_id": 5636, "tax_name": "Escherichia coli BIDMC 20A", "bgc_count": 13 }, { "smc_id": 5618, "tax_name": "Escherichia coli O104:H4 str. Ec11-9450", "bgc_count": 14 }, { "smc_id": 5617, "tax_name": "Escherichia coli O104:H4 str. Ec11-9941", "bgc_count": 14 }, { "smc_id": 5514, "tax_name": "Escherichia coli BIDMC 38", "bgc_count": 13 } ], "token": "eyJzZWFyY2giOiAiRXNjaGVyaWNoaWEgQ29saSIsICJwYWdlX3NpemUiOiA1LCAic29ydCI6ICJkZXNjIiwgInNvcnRfZmllbGQiOiAic21jX2lkIiwgImxhc3RfaWQiOiA1NTE0fQ==" }
$ curl https://smc-dev.jgi.lbl.gov/v2/search/source-name \ -d '{"token": "eyJzZWFyY2giOiAiRXNjaGVyaWNoaWEgQ29saSIsICJwYWdlX3NpemUiOiA1LCAic29ydCI6ICJkZXNjIiwgInNvcnRfZmllbGQiOiAic21jX2lkIiwgImxhc3RfaWQiOiA1NTE0fQ=="}' \ -H 'Content-Type: application/json' | jq . { "total_count": 995, "search_results": [ { "smc_id": 5449, "tax_name": "Escherichia coli EC096/10", "bgc_count": 9 }, { "smc_id": 5426, "tax_name": "Escherichia coli UMEA 3489-1", "bgc_count": 11 }, { "smc_id": 5425, "tax_name": "Escherichia coli UMEA 3426-1", "bgc_count": 11 }, { "smc_id": 5424, "tax_name": "Escherichia coli UMEA 3693-1", "bgc_count": 15 }, { "smc_id": 5423, "tax_name": "Escherichia coli UMEA 3342-1", "bgc_count": 17 } ], "token": "eyJzZWFyY2giOiAiRXNjaGVyaWNoaWEgQ29saSIsICJwYWdlX3NpemUiOiA1LCAic29ydCI6ICJkZXNjIiwgInNvcnRfZmllbGQiOiAic21jX2lkIiwgImxhc3RfaWQiOiA1NDIzfQ==" }
#!/usr/bin/env python import requests import json # setup variables to use search = "Escherichia Coli" # our search string page_size = 5 # number of results to return from a request smc_url = "https://smc-dev.jgi.lbl.gov" # SMC Website smc_header = {"Content-Type": "application/json"} # header for getting the correct content-type timeout_sec = 30 # wait up to 30 seconds to get a response from the SMC API smc_api = f"{smc_url}/v2/search/source-name" # API to use # create json data with search parameters search_json = { "search": search, "page_size": page_size, "sort": "asc" # sort by SMC-ID ascending instead of the default descending } pagination_token = "" # returned from the API call done = False # have we gotten all of the results? request_cnt = 0 # number of requests to the API page_total = 0 # number of results returned while not done: request_cnt = request_cnt + 1 if pagination_token: search_json = { "token": pagination_token } # use the requests library to call the API print(f"POST {smc_api}") r = requests.post(smc_api, json=search_json, headers=smc_header, timeout=timeout_sec) # use the json library to load the response into the smc_data json variable smc_data = json.loads(r.text) # print results page_total = page_total + page_size print("Results: {page_total} / {total}".format(page_total=page_total, total=smc_data.get("total_count"))) for source_data in smc_data.get("search_results", []): print("SMC ID: {}, Tax Name: {}, BGC Count: {}".format(source_data.get("smc_id"), source_data.get("tax_name"), source_data.get("bgc_count"))) # get the token pagination_token = smc_data.get("token") print() if not pagination_token: done = True # just get the first 3 pages of results if request_cnt >= 3: done = TrueExample output:
POST https://smc-dev.jgi.lbl.gov/v2/search/source-name Results: 5 / 1000 SMC ID: 2, Tax Name: Escherichia coli str. K-12 substr. MG1655, BGC Count: 8 SMC ID: 65, Tax Name: Escherichia coli CFT073, BGC Count: 12 SMC ID: 112, Tax Name: Escherichia coli O157:H7 str. Sakai, BGC Count: 12 SMC ID: 140, Tax Name: Escherichia coli BL21(DE3), BGC Count: 10 SMC ID: 171, Tax Name: Escherichia coli str. K-12 substr. W3110, BGC Count: 8 POST https://smc-dev.jgi.lbl.gov/v2/search/source-name Results: 10 / 995 SMC ID: 177, Tax Name: Escherichia coli SE11, BGC Count: 10 SMC ID: 181, Tax Name: Escherichia coli SE15, BGC Count: 10 SMC ID: 192, Tax Name: Escherichia coli O103:H2 str. 12009, BGC Count: 11 SMC ID: 193, Tax Name: Escherichia coli O111:H- str. 11128, BGC Count: 14 SMC ID: 287, Tax Name: Escherichia coli UTI89, BGC Count: 14 POST https://smc-dev.jgi.lbl.gov/v2/search/source-name Results: 15 / 990 SMC ID: 289, Tax Name: Escherichia coli 536, BGC Count: 12 SMC ID: 348, Tax Name: Escherichia coli APEC O1, BGC Count: 11 SMC ID: 443, Tax Name: Escherichia coli O139:H28 str. E24377A, BGC Count: 10 SMC ID: 444, Tax Name: Escherichia coli HS, BGC Count: 10 SMC ID: 452, Tax Name: Escherichia coli B str. REL606, BGC Count: 11For APIs that need a user token, add the "Authorization" key to the smc_header dictionary.
smc_header = {"Authorization": f"Bearer {smc_user_token}", "Content-Type": "application/json"}