Skip to content

Commit 5a9f158

Browse files
authored
Merge pull request #63 from ScilifelabDataCentre/docs-on-queries
Add drafts for the user guides on queries
2 parents f2d70d2 + e00042a commit 5a9f158

14 files changed

Lines changed: 526 additions & 110 deletions

docs/development/deployment.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ We have accounts on both [PyPI](https://pypi.org/) and [TestPyPI](https://test.p
2121

2222
(Login credentials are in bitwarden, but if you use the GH actions below you don't need to use them.)
2323

24+
## Considerations for Kubernetes deployment
25+
26+
TODO: add some lessons learned from the k8s deployment, e.g. on resources needed for the celery worker.
27+
2428
### Good to know
2529

2630
1. You cannot upload the same version of a package to PyPI or TestPyPI if that version already exists (even if you delete the existing package). So in such cases you need to bump the version number in the packages before uploading new versions.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# How to create efficient DivBase queries
2+
3+
Split large VCF files into smaller files. For instance by chromosome. If the assembly does not have chromosome level contiguity, we suggest to store a range of scaffolds per file.
4+
5+
divbase bcftools "pipes"
6+
7+
If subsets based on variant range are included, run then first before any sample subsets. subsetting on samples requres inspecting and potentially updating every row in the VCF (N+1 problem); subsetting on samples will be faster on smaller datasets and therefore better to run after all row-based subsets have been done.

docs/user-guides/query-syntax.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DivBase Query Syntax for VCF data
2+
3+
TODO

docs/user-guides/quick-start.md

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Add your project(s) to the configuration file:
5353
divbase-cli config add PROJECT_NAME --default
5454
```
5555

56-
By setting this as the default project, you won't need to specify the project name in future commands. To select a different project in any future command, you can add the `--project` flag to any command.
56+
By setting this as the default project, you won't need to specify the project name in future commands. To select a different project in any future command, you can add the `--project` flag to any command. Note that the rest of the example commands in this quick start guide will omit the `--project` flag for brevity.
5757

5858
!!! note
5959
On the divbase website you can see your project's name(s) under the "Projects" tab after logging in.
@@ -89,26 +89,37 @@ divbase-cli files list
8989

9090
## Step 7: Upload sample metadata
9191

92+
TODO It might make more sense to have run the dimensions update job before this if we are to use a pre-populated template file
93+
9294
Sample metadata must be uploaded as follows:
9395

9496
- In TSV format and be named "sample_metadata.tsv"
9597
- Must contain a column named "sample_id" which matches the sample IDs in your VCF files
9698
- The names and values of all other columns are optional.
9799

98-
TODO - do we support numerical columns, including queries on them, i.e in range 31 - 50. etc...
100+
TODO update this after `sidecar-metadata.md` docs are done, there are changes planned for some details.
99101

100102
```bash
101103
divbase-cli files upload path/to/your/sample_metadata.tsv
102104
```
103105

104106
## Step 8: Dimensions update
105107

106-
Update the project dimensions after uploading your files:
108+
For DivBase to be able to efficiently handle the VCF files in the the project, some key information about each VCF files is fetched from the files. In DivBase, this is refered to as "VCF dimensions". These include for instance which samples and scaffolds that a VCF file contains.
109+
110+
Update the project dimensions after uploading your files.
107111

108112
```bash
109113
divbase-cli dimensions update
110114
```
111115

116+
This submits a task to the DivBase task management system. The task will wait in a queue until the system is ready to work on it. Depending on the size of the VCF files, this make take a couple of minutes.
117+
118+
!!! notes
119+
1. Please note that it is not possible to run VCF queries in DivBase until the dimensions update task has finished. The reason for this is that the VCF queries use the dimensions data ensure that the queries are feasible and to know which VCF files from the project to process.
120+
121+
2. Please also note that the `divbase-cli dimensions update` command needs to be done every time a new VCF or a new version of a VCF file is uploaded.
122+
112123
## Step 9: Confirm dimensions update job completion
113124

114125
Check the task history to confirm the dimensions update job has completed:
@@ -119,16 +130,52 @@ divbase-cli task-history user
119130

120131
Once complete, you can run any queries on the uploaded data.
121132

133+
It is possible to inspect the cached VCF dimensions data for the project at any time with the command:
134+
135+
```bash
136+
divbase-cli dimensions show
137+
```
138+
122139
## Step 10: Run your queries
123140

124-
Query your VCF data:
141+
There are three types of queries in DivBase:
142+
143+
- Sample metadata query
144+
- VCF data query
145+
- Combined sample metadata and VCF data query
146+
147+
!!! note
148+
Queries are one of the more complex aspects of DivBase and therefore the user is encouraged to read the section on [Running Queries](running-queries.md) after reading this quick start.
149+
150+
### Running sample metadata queries
151+
152+
As an example, let's assume that user-defined sidecar sample metadata TSV file contains a custom column name `Area` and that `Northern Portugal` is one of the values in the column. To filter on all samples on that column and value:
153+
154+
```bash
155+
divbase-cli query tsv "Area:Northern Portugal"
156+
```
157+
158+
!!! note
159+
Please see [Sidecar Metadata TSV files: creating and querying sample metadata files](sidecar-metadata.md) for more details on the syntax for writing sample metadata queries.
160+
161+
### Running VCF data queries
162+
163+
DivBase uses [`bcftools`](https://github.com/samtools/bcftools) to subset VCF data and therefore the query syntax is based on `bcftools view` syntax (as described in the [bcftools manual](https://samtools.github.io/bcftools/bcftools.html#view)).
164+
165+
For instance, to subset all VCF files in the project on a chromosomal region in a scaffold named `21`:
166+
167+
```bash
168+
divbase-cli query bcftools-pipe --command "view -r 21:15000000-25000000"
169+
```
170+
171+
The VCF queries can be combined with sidecar sample metadata queries with `--tsv-filter` and the fixed expression `view -s SAMPLES` (where `SAMPLES` tells DivBase to use the results from the sidecar filtering as input for `bcftools view -s`). In this way, only the VCF files that fulfil the sample metadata query will be used in the `bcftools` subset commands. An example:
125172

126173
```bash
127-
# TODO - some simple examples to show what kind of queries can be run
174+
divbase-cli query bcftools-pipe --tsv-filter "Area:Northern Portugal" --command "view -s SAMPLES; view -r 21:15000000-25000000"
128175
```
129176

130177
!!! note
131-
For more detailed information on running queries, refer to the [Running Queries Guide](running-queries.md).
178+
DivBase only allows `bcftools view` in its query syntax and no other `bcftools` commands. The `merge`, `concat`, and `annotate` commands are used when processing a query, but should not be defined by the user.
132179

133180
## Step 11: Download any results files
134181

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
1-
# Running Queries
1+
# Running Queries: Overview
2+
3+
DivBase allows users to submit queries to checkout sample metadata and VCF data from the DivBase project data store. There types of queries in DivBase are:
4+
5+
- Sample metadata query
6+
- VCF data query
7+
- Combined sample metadata and VCF data query
8+
9+
The sample metadata is a user defined, sidecar TSV (tab separated variables) file where users can add any custom metadata as columns. The VCF queries use `bcftools` to subset data from the files in the data store based on user defined filters. The VCF queries can be combined with a sample metadata query to allow users to filter both on data in the sidecar metadata and VCF files in a single query.
210

311
TODO
12+
13+
- [TO BE IMPLEMENTED] think about the `divbase-cli query` commands. it would make sense to use `sample-metadata` (perhaps with `tsv` as a short form) and `vcf`
14+
15+
The system will use the latest version the the files for all queries.
16+
17+
TODO COPIED OVER FROM QUICKSTART REIMPLEMENT
18+
19+
DivBase only allows `bcftools view` in its query syntax and no other `bcftools` commands. The `merge`, `concat`, and `annotate` commands are used when processing a query, but should not be defined by the user.
20+
21+
## Side car sample metadata queries
22+
23+
For more details, see [Sidecar Metadata TSV files: creating and querying sample metadata files](sidecar-metadata.md).
24+
25+
how to write the sample metadata TSV (+template)
26+
27+
how to use dimensions show to get all samples in the project
28+
29+
how to write query
30+
31+
## Before any VCF queries: run the VCF dimensions command
32+
33+
For more details see [VCF Dimensions caching](vcf-dimensions.md)
34+
35+
For performance reasons and to ensure query feasibility, key metadata from the VCF files must first be cached in DivBase.
36+
For each VCF in the DivBase project, the system extracts file name, number and name of all samples, number and name of all scaffolds, number of variants,
37+
38+
DivBase will use this whenever a user submits a query to the project. For instance, the user might make a sample metadata filtering that results in only certain samples. The system knows which file names each requested sample are located in, and will ensure that only those files will be transferred to the worker.
39+
40+
example
41+
dimensions show
42+
43+
## VCF queries
44+
45+
See also [DivBase Query Syntax for VCF data](query-syntax.md), [How to create efficient DivBase queries](how-to-create-efficient-divbase-queries.md), and [Tutorial: Running a query on a public dataset](tutorial-query-on-public-data.md).
46+
47+
can be run with or without sample metadata filtering
48+
49+
for sample metadata linked VCF queries, it can be good to do a dry run first [TO BE IMPLEMENTED, at the moment it needs to be run seperatelly]
50+
51+
how to write a query
52+
53+
how to optimize a query (see separate markdown)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Sidecar Metadata TSV files: creating and querying sample metadata files
2+
3+
TODO
4+
5+
- rationale: what is this and how can it be used
6+
7+
## Creating a sidecar TSV for a DivBase project
8+
9+
Note! there can be multiple TSVs in the same project and it is possible to call them for the queries with the `--metadata-tsv-name` flag.
10+
11+
TODOs:
12+
13+
- [TO BE IMPLEMENTED] consider changing the mandatory column name from `Sample_ID` to `Sample`
14+
- [TO BE IMPLEMENTED] CLI command to generate template (empty template and template with the samples from the DivBase project pre-filled). Pre-filling the template will require that dimensions update has been run
15+
- [TO BE IMPLEMENTED] what happens if a TSV does not contain all the samples in the DivBase project? There should probably be a warning, but not an error?
16+
- [TO BE IMPLEMENTED] what happens if a sample name is misspelled in the TSV? a warning? can this be checked against the dimensions show?
17+
18+
- [TO BE IMPLEMENTED] what happens if a sample is duplicated in the file. what happens if the sample name is duplicated but not the values (diverging duplicate)?
19+
20+
1 mandatory column: sample name.
21+
22+
any other columns are optional and user defined
23+
24+
it is possible to have more than one sidecar sample metadata file in each DivBase project.
25+
26+
example
27+
28+
```
29+
TODO add example here
30+
```
31+
32+
## Query Syntax for sidecar metadata
33+
34+
from `divbase-cli query tsv -h` docstring:
35+
String consisting of keys:values in the tsv file to filter on. The syntax is 'Key1:Value1,Value2;Key2:Value3,Value4', where the key are the column header names in the tsv, and values are the column values. Multiple values for a key are separated by commas, and multiple keys are separated by semicolons. When multple keys are provided, an intersect query will be performed. E.g. 'Area:West of Ireland,Northern Portugal;Sex:F'.
36+
37+
- [TO BE IMPLEMENTED] filtering based on ranges (ints and maybe floats) and not just on strings, e.g. in range 31 - 50. etc...
38+
- [TO BE IMPLEMENTED] add more Set Operations: union, intersection, difference, symmetric difference. Be clear on the default behaviour
39+
40+
## Trying out a query
41+
42+
```bash
43+
divbase-cli query tsv "Area:Northern Portugal"
44+
```
45+
46+
- [TO BE IMPLEMENTED] what to do if a query references a column that does not exist. E.g. `divbase-cli query tsv "Area:Northern Portugal"` when Area does not exist? This should probably give a warning and not just return nothing
47+
48+
- [TO BE IMPLEMENTED] what to do if a query references a column value. E.g. `divbase-cli query tsv "Area:Northern Portugal"` when Northern Portugal does not exist in the column? This should probably also give a warning and not just return nothing, but nothing is a result here and not a syntax problem...
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Tutorial: Running a DivBase query on a public dataset
2+
3+
This tutorial assumes that you have an account on DivBase and have a membership in a DivBase project with at least an EDIT role (i.e. can upload files and run queries).
4+
5+
We will use a mouse (_Mus musculus_) data set availalbe on the European Nucleotide Archive: <https://www.ebi.ac.uk/ena/browser/view/ERZ022025>. It a 5.5 Gb VCF.gz file that contains 18 samples and 66,007,044 variants.
6+
7+
## 1. Obtain the data and upload it to your DivBase project
8+
9+
First we need download the files from EVA to our local computer. We can do that from the ENA homepage with a web browser, or from the terminal with a tool like `curl` or `wget`:
10+
11+
```bash
12+
curl -o mgp.v3.snps.rsIDdbSNPv137.vcf.gz ftp://ftp.sra.ebi.ac.uk/vol1/analysis/ERZ022/ERZ022025/mgp.v3.snps.rsIDdbSNPv137.vcf.gz
13+
```
14+
15+
On the terminal, log in to DivBase:
16+
17+
```bash
18+
divbase-cli auth login <USER_EMAIL>
19+
```
20+
21+
We then need to upload the file to the DivBase project. If the project you want to upload to is your default project, you can skip `--project <YOUR_DIVBASE_PROJECT_NAME>`, but otherwise you will need to specify that.
22+
23+
```bash
24+
divbase-cli files upload mgp.v3.snps.rsIDdbSNPv137.vcf.gz --project <YOUR_DIVBASE_PROJECT_NAME>
25+
```
26+
27+
For the sake of demonstration, we can create a sidecar metadata file for the 18 samples in this dataset so that we can use that for the query. In fact, a file prepared for this demo can be downloaded from the DivBase repo with:
28+
29+
```bash
30+
curl -o tutorial_mock_metadata_mgpv3snps.tsv https://raw.githubusercontent.com/ScilifelabDataCentre/divbase/refs/heads/docs-on-queries/tests/fixtures/tutorial_mock_metadata_mgpv3snps.tsv
31+
```
32+
33+
TODO update the TSV url when it has been merged to main.
34+
35+
And then upload it to the DivBase project with:
36+
37+
```bash
38+
divbase-cli files upload tutorial_mock_metadata_mgpv3snps.tsv --project <YOUR_DIVBASE_PROJECT_NAME>
39+
```
40+
41+
With this, we are set in terms of the files we need in the DivBase project to be able to run the query.
42+
43+
### A comment on the mock sidecar metadata TSV
44+
45+
If we look at the first few lines of this TSV, we see that the first column has the SAMPLE IDs from the VCF. There is a column for population grouping, and a column for the geographical area the sample comes from. All of this is made up for the tutorial, just to illustrate what a sidecar metadata file can look like. We will use the Area column later on to subset on samples.
46+
47+
```bash
48+
head tests/fixtures/tutorial_mock_metadata_mgpv3snps.tsv
49+
50+
#Sample_ID Population Area
51+
129P2 1 North
52+
129S1 2 East
53+
129S5 3 South
54+
AJ 4 West
55+
```
56+
57+
For comparison, we can inspect all the samples in the VCF with:
58+
59+
```bash
60+
# On MacOS, use gzcat instead of zcat
61+
zcat mgp.v3.snps.rsIDdbSNPv137.vcf.gz | grep "#CHROM"
62+
63+
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT 129P2 129S1 129S5 AJ AKRJ BALBcJ C3HHeJ C57BL6NJ CASTEiJ CBAJ DBA2J FVBNJ LPJ NODShiLtJ NZOHlLtJ PWKPhJ SPRETEiJ WSBEiJ
64+
```
65+
66+
## 2. Submit a job to cache the VCF dimensions in the DivBase backend
67+
68+
DivBase needs this to make calculations. It can be seen as a pre-processing step that needs to be run every time a new version of a VCF is uploaded to DivBase.
69+
70+
```bash
71+
divbase-cli dimensions update --project <YOUR_DIVBASE_PROJECT_NAME>
72+
```
73+
74+
The task should now have been submitted. The terminal prints a DivBase Task ID with a message like this:
75+
76+
```
77+
# Example with Task ID 102
78+
Job submitted successfully with task id: 102
79+
```
80+
81+
Make note of the Task ID integer for for now; we will use it later in the tutorial for a variable named `<THE_JOB_ID_OF_THE_QUERY>`.
82+
83+
We need to wait until this has finished before we can send the actual DivBase query.
84+
85+
```bash
86+
divbase-cli task-history user
87+
```
88+
89+
## 3. Submit a query job
90+
91+
```bash
92+
divbase-cli query bcftools-pipe --tsv-filter 'Area:North,East' --command 'view -s SAMPLES; view -r 1:15000000-25000000' --metadata-tsv-name tutorial_mock_metadata_mgpv3snps.tsv --project <YOUR_DIVBASE_PROJECT_NAME>
93+
```
94+
95+
note that we are specifically using the `tutorial_mock_metadata_mgpv3snps.tsv` sample metadata. If this is not specified, the query will default to `sample_metadata.tsv`.
96+
97+
Depending on queue length, this job will take some time to run. Check that it has started, and then leave it running for half an hour.
98+
99+
```bash
100+
divbase-cli task-history user
101+
```
102+
103+
## 4. Download the results file
104+
105+
```bash
106+
divbase-cli file download result_of_job_<THE_JOB_ID_OF_THE_QUERY>.vcf.gz--project <YOUR_DIVBASE_PROJECT_NAME>
107+
```
108+
109+
We can now run some quick sanity-checks on the result file.
110+
111+
```bash
112+
# On MacOS, use gzcat instead of zcat
113+
zcat result_of_job_<THE_JOB_ID_OF_THE_QUERY>.vcf.gz | grep -v "^#" |wc -l
114+
115+
# Expected terminal output:
116+
297415
117+
```
118+
119+
If you want, you can compare this to the original file:
120+
121+
```bash
122+
# Note! This will take a little time since this file has many rows
123+
zzcat mgp.v3.snps.rsIDdbSNPv137.vcf.gz | grep -v "^#" |wc -l
124+
125+
# Expected terminal output:
126+
66007044
127+
```
128+
129+
As for the samples, the expected result is the following:
130+
131+
```bash
132+
zcat mgp.v3.snps.rsIDdbSNPv137.vcf.gz | grep "#CHROM"
133+
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT 129P2 129S1 129S5 AJ AKRJ BALBcJ C3HHeJ C57BL6NJ CASTEiJ CBAJ DBA2J FVBNJ LPJ NODShiLtJ NZOHlLtJ PWKPhJ SPRETEiJ WSBEiJ
134+
135+
zcat result_of_job_<THE_JOB_ID_OF_THE_QUERY>.vcf.gz | grep "#CHROM"
136+
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT 129P2 129S1 AKRJ BALBcJ CASTEiJ CBAJ LPJ NODShiLtJ SPRETEiJ WSBEiJ
137+
```

docs/user-guides/vcf-dimensions.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# VCF Dimensions caching
2+
3+
TODO
4+
5+
This a prerequisite step before running any queries
6+
7+
The dimensions update job will:
8+
9+
- For each VCF files in the project data store:
10+
- store filename and data store file version ID
11+
- store timestamp of last update [TODO IS THIS LAST TIME DIMENSIONS WAS RUN OR LAST TIME FILE WAS UPDATED?]
12+
- Fetche and store all sample names from the VCF file
13+
- Fetch and store all chromosome/scaffold/contig names
14+
- count the number of samples and variants in the VCF file
15+
- not consider results files produced by DivBase
16+
- by filename (`result_of_job_` prefix)
17+
- as an additional check, looks for bcftools annotate stamp in header (all DivBase results files are branded like this)
18+
19+
Run `divbase-cli dimensions update` whenever:
20+
21+
- A new VCF file is uploaded to the project
22+
- A new version of an existing VCF file is uploaded to the project

0 commit comments

Comments
 (0)