| 1 |
.. cyvcf2 documentation master file, created by |
| 2 |
sphinx-quickstart on Mon Nov 14 08:40:54 2016. |
| 3 |
You can adapt this file completely to your liking, but it should at least |
| 4 |
contain the root `toctree` directive. |
| 5 |
|
| 6 |
cyvcf2 |
| 7 |
====== |
| 8 |
|
| 9 |
cyvcf2 is a fast VCF parser for python (2 and 3) released under the MIT license. |
| 10 |
It is a `cython` wrapper for `htslib <https://github.com/samtools/htslib/>`_ developed in |
| 11 |
the `Quinlan lab <http://quinlanlab.org/>`_. |
| 12 |
|
| 13 |
See the :ref:`api` for detailed documentation, but the most common usage is summarized in the snippet below: |
| 14 |
|
| 15 |
.. code-block:: python |
| 16 |
|
| 17 |
from cyvcf2 import VCF |
| 18 |
|
| 19 |
for variant in VCF('some.vcf.gz'): # or VCF('some.bcf') |
| 20 |
variant.REF, variant.ALT # e.g. REF='A', ALT=['C', 'T'] |
| 21 |
|
| 22 |
variant.CHROM, variant.start, variant.end, variant.ID, \ |
| 23 |
variant.FILTER, variant.QUAL |
| 24 |
|
| 25 |
# numpy arrays of specific things we pull from the sample fields. |
| 26 |
# gt_types is array of 0,1,2,3==HOM_REF, HET, UNKNOWN, HOM_ALT |
| 27 |
variant.gt_types, variant.gt_ref_depths, variant.gt_alt_depths # numpy arrays |
| 28 |
variant.gt_phases, variant.gt_quals, variant.gt_bases # numpy array |
| 29 |
|
| 30 |
|
| 31 |
## INFO Field. |
| 32 |
## extract from the info field by it's name: |
| 33 |
variant.INFO.get('DP') # int |
| 34 |
variant.INFO.get('FS') # float |
| 35 |
variant.INFO.get('AC') # float |
| 36 |
|
| 37 |
# convert back to a string. |
| 38 |
str(variant) |
| 39 |
|
| 40 |
|
| 41 |
## per-sample info... |
| 42 |
# Get a numpy array of the depth per sample: |
| 43 |
dp = variant.format('DP') |
| 44 |
# or of any other format field: |
| 45 |
sb = variant.format('SB') |
| 46 |
assert sb.shape == (n_samples, 4) # 4-values per |
| 47 |
|
| 48 |
# to do a region-query: |
| 49 |
|
| 50 |
vcf = VCF('some.vcf.gz') |
| 51 |
for v in vcf('11:435345-556565'): |
| 52 |
if v.INFO["AF"] > 0.1: continue |
| 53 |
print(str(v)) |
| 54 |
|
| 55 |
# to query "all records" via __call__: |
| 56 |
# this uses the index (HTS_IDX_START), so an index is required. |
| 57 |
# if no index is available, this yields zero records. |
| 58 |
all_vars = list(vcf()) |
| 59 |
|
| 60 |
# single sample of 0|1 in vcf becomes [[0, 1, True]] |
| 61 |
# 2 samples of 0/0 and 1|1 would be [[0, 0, False], [1, 1, True]] |
| 62 |
print v.genotypes |
| 63 |
|
| 64 |
Modifying Existing Records |
| 65 |
========================== |
| 66 |
|
| 67 |
.. this example is also in the writing.rst page |
| 68 |
|
| 69 |
`cyvcf2` is optimized for fast reading and extraction from existing files. |
| 70 |
However, it also offers some means of modifying existing VCFs. Here, we |
| 71 |
show an example of how to modify the INFO field at a locus to annotate |
| 72 |
variants with the genes that they overlap. |
| 73 |
|
| 74 |
.. code-block:: python |
| 75 |
|
| 76 |
from cyvcf2 import VCF, Writer |
| 77 |
vcf = VCF(VCF_PATH) |
| 78 |
# adjust the header to contain the new field |
| 79 |
# the keys 'ID', 'Description', 'Type', and 'Number' are required. |
| 80 |
vcf.add_info_to_header({'ID': 'gene', 'Description': 'overlapping gene', |
| 81 |
'Type':'Character', 'Number': '1'}) |
| 82 |
|
| 83 |
# create a new vcf Writer using the input vcf as a template. |
| 84 |
fname = "out.vcf" |
| 85 |
w = Writer(fname, vcf) |
| 86 |
|
| 87 |
for v in vcf: |
| 88 |
# The get_gene_intersections function is not shown. |
| 89 |
# This could be any operation to find intersections |
| 90 |
# or any manipulation required by the user. |
| 91 |
genes = get_gene_intersections(v) |
| 92 |
if genes is not None: |
| 93 |
v.INFO["gene"] = ",".join(genes) |
| 94 |
w.write_record(v) |
| 95 |
|
| 96 |
w.close(); vcf.close() |
| 97 |
|
| 98 |
More info on writing vcfs can be found :doc:`here <writing>` |
| 99 |
|
| 100 |
Setting Genotyping Strictness |
| 101 |
============================= |
| 102 |
|
| 103 |
By default, genotypes containing a single missing allele (e.g. genotypes such |
| 104 |
as ``0/.``, ``./0``, ``1/.``, or ``./1``) are classified as heterozygous ("HET" |
| 105 |
or id: 1) instead of "UNKNOWN" (or id: 2). A case can be made for either |
| 106 |
classification on these partially missing genotypes depending on the situation. |
| 107 |
|
| 108 |
A better way to explicitly control the genotype classification for these cases, |
| 109 |
is to use the "strict genotype", or ``strict_gt``, feature. Here's an example |
| 110 |
usage: |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
from cyvcf2 import VCF |
| 115 |
vcf = VCF("/path/to/vcf/file", strict_gt=True) |
| 116 |
for variant in vcf: |
| 117 |
# do something |
| 118 |
|
| 119 |
When the ``strict_gt`` flag is enabled, `cyvcf2` will treat any genotype |
| 120 |
containing a missing allele (containing a '.') as an UNKNOWN genotype; |
| 121 |
otherwise, genotypes like ``0/.``, ``./0``, ``1/.``, or ``./1`` will be |
| 122 |
classified as heterozygous ("HET"). |
| 123 |
|
| 124 |
The "strict genotype" feature is not enabled by default (i.e. ``strict_gt`` is |
| 125 |
set to `False` by default) to preserve backwards compatibility. |
| 126 |
|
| 127 |
Installation |
| 128 |
============ |
| 129 |
|
| 130 |
.. code-block:: bash |
| 131 |
|
| 132 |
pip install cyvcf2 |
| 133 |
|
| 134 |
or via bioconda. |
| 135 |
|
| 136 |
Testing |
| 137 |
======= |
| 138 |
|
| 139 |
Install `pytest`, then tests can be run with: |
| 140 |
|
| 141 |
.. code-block:: bash |
| 142 |
|
| 143 |
pytest |
| 144 |
|
| 145 |
Known Limitations |
| 146 |
================= |
| 147 |
* `cyvcf2` currently does not support reading VCFs encoded with UTF-8 with non-ASCII characters in the contents of string-typed FORMAT fields. |
| 148 |
|
| 149 |
For limitations on writing VCFs, see :ref:`here <Limitations with writing>` |
| 150 |
|
| 151 |
See Also |
| 152 |
======== |
| 153 |
|
| 154 |
Pysam also `has a cython wrapper to htslib <https://github.com/pysam-developers/pysam/blob/master/pysam/cbcf.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. |
| 155 |
|
| 156 |
.. toctree:: |
| 157 |
:maxdepth: 1 |
| 158 |
|
| 159 |
docstrings |
| 160 |
writing |