Debian Codegraph @

README.md

raw
1 cyvcf2
2 ======
3
4 Note: cyvcf2 versions < 0.20.0 require htslib < 1.10. cyvcf2 versions >= 0.20.0 require htslib >= 1.10
5
6 <!-- ghp-import -p docs/build/html/ -->
7 The latest documentation for cyvcf2 can be found here:
8
9 [![Docs](https://img.shields.io/badge/docs-latest-blue.svg)](http://brentp.github.io/cyvcf2/)
10
11 If you use cyvcf2, please cite the [paper](https://academic.oup.com/bioinformatics/article/2971439/cyvcf2)
12
13
14 Fast Python **(3.9+)** parsing of VCF and BCF including region-queries.
15
16
17 [![Build](https://github.com/brentp/cyvcf2/actions/workflows/build.yml/badge.svg)](https://github.com/brentp/cyvcf2/actions/workflows/build.yml)
18
19 cyvcf2 is a cython wrapper around [htslib](https://github.com/samtools/htslib) built for fast parsing of [Variant Call Format](https://en.m.wikipedia.org/wiki/Variant_Call_Format) (VCF) files.
20
21 Attributes like `variant.gt_ref_depths` work for diploid samples and return a numpy array directly so they are immediately ready for downstream use.
22 **note** that the array is backed by the underlying C data, so, once `variant` goes out of scope. The array will contain nonsense.
23 To persist a copy, use: `cpy = np.array(variant.gt_ref_depths)` instead of just `arr = variant.gt_ref_depths`.
24
25 Example
26 =======
27
28 The example below shows much of the use of cyvcf2.
29
30 ```Python
31 from cyvcf2 import VCF
32
33 for variant in VCF('some.vcf.gz'): # or VCF('some.bcf')
34 variant.REF, variant.ALT # e.g. REF='A', ALT=['C', 'T']
35
36 variant.CHROM, variant.start, variant.end, variant.ID, \
37 variant.FILTER, variant.QUAL
38
39 # numpy arrays of specific things we pull from the sample fields.
40 # gt_types is array of 0,1,2,3==HOM_REF, HET, UNKNOWN, HOM_ALT
41 variant.gt_types, variant.gt_ref_depths, variant.gt_alt_depths # numpy arrays
42 variant.gt_phases, variant.gt_quals, variant.gt_bases # numpy array
43
44 ## INFO Field.
45 ## extract from the info field by it's name:
46 variant.INFO.get('DP') # int
47 variant.INFO.get('FS') # float
48 variant.INFO.get('AC') # float
49
50 # convert back to a string.
51 str(variant)
52
53
54 ## sample info...
55
56 # Get a numpy array of the depth per sample:
57 dp = variant.format('DP')
58 # or of any other format field:
59 sb = variant.format('SB')
60 assert sb.shape == (n_samples, 4) # 4-values per
61
62 # to do a region-query:
63
64 vcf = VCF('some.vcf.gz')
65 for v in vcf('11:435345-556565'):
66 if v.INFO["AF"] > 0.1: continue
67 print(str(v))
68
69 # to query "all records" via __call__:
70 # this uses the index (HTS_IDX_START), so an index is required.
71 # if no index is available, this yields zero records.
72 all_vars = list(vcf())
73 ```
74
75 Installation
76 ============
77
78 ## pip or uv with bundled htslib
79
80 If a binary wheel is available for your platform, pip and uv will install it
81 without building from source.
82
83 ```
84 pip install cyvcf2
85 uv pip install cyvcf2
86 ```
87
88 ## pip or uv with system htslib
89
90 Assuming you have already built and installed htslib version 1.12 or higher.
91 ```
92 CYVCF2_HTSLIB_MODE=EXTERNAL pip install --no-binary cyvcf2 cyvcf2
93 CYVCF2_HTSLIB_MODE=EXTERNAL uv pip install --no-binary cyvcf2 cyvcf2
94 ```
95
96 ## windows (experimental, only tested on MSYS2)
97
98 Assuming you have already built and installed htslib.
99 ```
100 CYVCF2_HTSLIB_MODE=EXTERNAL pip install cyvcf2
101 ```
102
103 ## github (building htslib and cyvcf2 from source)
104
105 ```
106 git clone --recursive https://github.com/brentp/cyvcf2
107 cd cyvcf2
108 CYVCF2_HTSLIB_MODE=BUILTIN python -m pip install .
109 CYVCF2_HTSLIB_MODE=BUILTIN uv pip install .
110 # or to use a system htslib.so
111 CYVCF2_HTSLIB_MODE=EXTERNAL python -m pip install .
112 CYVCF2_HTSLIB_MODE=EXTERNAL uv pip install .
113 ```
114
115 Source builds use scikit-build-core and CMake. Python build dependencies such
116 as Cython, NumPy, CMake, and Ninja are installed by PEP 517 build isolation, but
117 you still need a C compiler plus the htslib native dependencies for your
118 platform.
119
120 Most users can leave `CYVCF2_CYTHONIZE` unset. It is mainly for developers
121 building from a Git checkout who need to control when CMake regenerates
122 `cyvcf2.c` from `cyvcf2/cyvcf2.pyx`:
123
124 - `AUTO` (the default) uses an existing `cyvcf2/cyvcf2.c` when present and runs
125 Cython only when the file is missing.
126 - `ON` regenerates the C file after edits to `cyvcf2.pyx` or `cyvcf2.pxd`, or
127 when testing Cython-generated output.
128 - `OFF` requires an existing C file.
129
130 For example:
131
132 ```
133 CYVCF2_CYTHONIZE=ON python -m pip install .
134 python -m pip install . --config-settings=cmake.define.CYVCF2_CYTHONIZE=ON
135 ```
136
137 The legacy `CYTHONIZE=1` environment variable is still accepted.
138
139 To build source distributions and wheels locally:
140
141 ```
142 python -m build
143 uv build
144 ```
145
146 On **OSX**, using brew, you may have to set the following as indicated by the brew install:
147
148 ```
149 For compilers to find openssl you may need to set:
150 export LDFLAGS="-L/usr/local/opt/openssl/lib"
151 export CPPFLAGS="-I/usr/local/opt/openssl/include"
152
153 For pkg-config to find openssl you may need to set:
154 export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"
155 ```
156
157 Testing
158 =======
159
160 Install `pytest`, then tests can be run with:
161
162 ```
163 pytest
164 uv run --with pytest pytest
165 ```
166
167 CLI
168 =======
169 Run with `cyvcf2 path_to_vcf`
170
171 ```
172 $ cyvcf2 --help
173 Usage: cyvcf2 [OPTIONS] <vcf_file> or -
174
175 fast vcf parsing with cython + htslib
176
177 Options:
178 -c, --chrom TEXT Specify what chromosome to include.
179 -s, --start INTEGER Specify the start of region.
180 -e, --end INTEGER Specify the end of the region.
181 --include TEXT Specify what info field to include.
182 --exclude TEXT Specify what info field to exclude.
183 --loglevel [DEBUG|INFO|WARNING|ERROR|CRITICAL]
184 Set the level of log output. [default:
185 INFO]
186 --silent Skip printing of vcf.
187 --help Show this message and exit.
188 ```
189
190
191 See Also
192 ========
193
194 Pysam also [has a cython wrapper to htslib](https://github.com/pysam-developers/pysam/blob/master/pysam/libcbcf.pyx) and one block of code here is taken directly from that library. But, the optimizations that we want for gemini are very specific so we have chosen to create a separate project.
195
196 Performance
197 ===========
198
199 For the performance comparison in the paper, we used [thousand genomes chromosome 22](ftp://ftp.1000genomes.ebi.ac.uk/vol1/ftp/release/20130502/ALL.chr22.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz)
200 With the full comparison runner [here](https://github.com/brentp/cyvcf2/blob/main/scripts/compare.sh).