A package to facilitate making API requests to the IMPC Solr API
Project description
IMPC_API
impc_api
is a Python package which provides several helper functions that wrap around the IMPC SOLR API.
The functions in this package are intended for use in a Jupyter Notebook.
Installation Instructions
- Create a virtual environment (optional but recommended): On Mac:
python3 -m venv .venv
source .venv/bin/activate
- Install the package:
pip install impc_api
- Install Jupyter:
pip install jupyter
- Run the Jupyter Notebook:
jupyter notebook
After executing the command, the Jupyter interface should open in your browser. If it does not, follow the instructions provided in the terminal.
- Try it out:
Create a Jupyter Notebook and try some of the examples below:
Available functions
The available functions can be imported as:
from impc_api import solr_request, batch_solr_request
1. Solr request
The most basic request to the IMPC solr API
num_found, df = solr_request(
core='genotype-phenotype',
params={
'q': '*:*',
'rows': 10,
'fl': 'marker_symbol,allele_symbol,parameter_stable_id'
}
)
a. Facet request
solr_request
allows facet requests
num_found, df = solr_request(
core="genotype-phenotype",
params={
"q": "*:*",
"rows": 0,
"facet": "on",
"facet.field": "zygosity",
"facet.limit": 15,
"facet.mincount": 1,
}
)
b. Solr request validation
A common pitfall when writing a query is the misspelling of core
and fields
arguments. For this, we have included a validate
argument that raises a warning when these values are not as expected. Note this does not prevent you from executing a query; it just alerts you to a potential issue.
Core validation
num_found, df = solr_request(
core='invalid_core',
params={
'q': '*:*',
'rows': 10
},
validate=True
)
> InvalidCoreWarning: Invalid core: "genotype-phenotyp", select from the available cores:
> dict_keys(['experiment', 'genotype-phenotype', 'impc_images', 'phenodigm', 'statistical-result'])
Field list validation
num_found, df = solr_request(
core='genotype-phenotype',
params={
'q': '*:*',
'rows': 10,
'fl': 'invalid_field,marker_symbol,allele_symbol'
},
validate=True
)
> InvalidFieldWarning: Unexpected field name: "invalid_field". Check the spelling of fields.
> To see expected fields check the documentation at: https://www.ebi.ac.uk/mi/impc/solrdoc/
2. Batch Solr Request
batch_solr_request
is available for large queries. This solves issues where a request is too large to fit into memory or where it puts a lot of strain on the API.
Use batch_solr_request
for:
- Large queries (>1,000,000)
- Querying multiple items in a list
- Downloading data in
json
orcsv
format.
Large queries
For large queries you can choose between seeing them in a DataFrame or downloading them in json
or csv
format.
a. Large query - see in DataFrame
This will fetch your data using the API responsibly and return a Pandas DataFrame
When your request is larger than recommended and you have not opted for downloading the data, a warning will be presented and you should follow the instructions to proceed.
df = batch_solr_request(
core='genotype-phenotype',
params={
'q':'*:*'
},
download=False,
batch_size=30000
)
print(df.head())
b. Large query - Download
When using the download=True
option, a file with the requested information will be saved as filename
. The format is selected based on the wt
parameter.
A DataFrame may be returned, provided it does not exceed the memory available on your laptop. If the DataFrame is too large, an error will be raised. For these cases, we recommend you read the downloaded file in batches/chunks.
df = batch_solr_request(
core='genotype-phenotype',
params={
'q':'*:*',
'wt':'csv'
},
download=True,
filename='geno_pheno_query',
batch_size=100000
)
print(df.head())
c. Query by multiple values
batch_solr_request
also allows to search multiple items in a list provided they belong to them same field.
Pass the list to the field_list
param and specify the type of fl
in field_type
.
# List of gene symbols
genes = ["Zfp580", "Firrm", "Gpld1", "Mbip"]
df = batch_solr_request(
core='genotype-phenotype',
params={
'q':'*:*',
'fl': 'marker_symbol,mp_term_name,p_value',
'field_list': genes,
'field_type': 'marker_symbol'
},
download = False
)
print(df.head())
This can be downloaded too:
# List of gene symbols
genes = ["Zfp580", "Firrm", "Gpld1", "Mbip"]
df = batch_solr_request(
core='genotype-phenotype',
params={
'q':'*:*',
'fl': 'marker_symbol,mp_term_name,p_value',
'field_list': genes,
'field_type': 'marker_symbol'
},
download = True,
filename='gene_list_query'
)
print(df.head())
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file impc_api-1.0.6.tar.gz
.
File metadata
- Download URL: impc_api-1.0.6.tar.gz
- Upload date:
- Size: 25.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a14e635eeb4b1c8218837768fe4a701adf76c9a9a717b11a15cd5eab71a5d2d |
|
MD5 | 3ba1253ad07839e700f523f51080b2aa |
|
BLAKE2b-256 | 3aa957de3edea3fb3e851dddd361e1fcf6571c3100996e7f9c0822de818a680d |
Provenance
The following attestation bundles were made for impc_api-1.0.6.tar.gz
:
Publisher:
publish.yml
on mpi2/impc-api
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
impc_api-1.0.6.tar.gz
- Subject digest:
1a14e635eeb4b1c8218837768fe4a701adf76c9a9a717b11a15cd5eab71a5d2d
- Sigstore transparency entry: 145143806
- Sigstore integration time:
- Predicate type:
File details
Details for the file impc_api-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: impc_api-1.0.6-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d437c9b4994b407e739d18ff4339e4f29631529ff3719c3ad1b14cf62a323db |
|
MD5 | b156db7e5233dd33dc2ae123b41ca50b |
|
BLAKE2b-256 | 84f332918de92451be75adee3932a1fc45136976fc463f65d81fe1abacae9a96 |
Provenance
The following attestation bundles were made for impc_api-1.0.6-py3-none-any.whl
:
Publisher:
publish.yml
on mpi2/impc-api
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
impc_api-1.0.6-py3-none-any.whl
- Subject digest:
7d437c9b4994b407e739d18ff4339e4f29631529ff3719c3ad1b14cf62a323db
- Sigstore transparency entry: 145143817
- Sigstore integration time:
- Predicate type: