| 1 |
Modifying Existing VCFs |
| 2 |
======================= |
| 3 |
|
| 4 |
`cyvcf2` is optimized for fast reading and extraction from existing files. |
| 5 |
However, it also offers some means of modifying existing VCFs. |
| 6 |
|
| 7 |
Modifying the INFO field |
| 8 |
------------------------ |
| 9 |
|
| 10 |
.. this is the same example as on the index.rst page |
| 11 |
|
| 12 |
Here, we show an example of how to modify the INFO field at a locus to annotate |
| 13 |
variants with the genes that they overlap. |
| 14 |
|
| 15 |
.. code-block:: python |
| 16 |
|
| 17 |
from cyvcf2 import VCF, Writer |
| 18 |
vcf = VCF(VCF_PATH) |
| 19 |
# adjust the header to contain the new field |
| 20 |
# the keys 'ID', 'Description', 'Type', and 'Number' are required. |
| 21 |
vcf.add_info_to_header({'ID': 'gene', 'Description': 'overlapping gene', |
| 22 |
'Type':'Character', 'Number': '1'}) |
| 23 |
|
| 24 |
# create a new vcf Writer using the input vcf as a template. |
| 25 |
fname = "out.vcf" |
| 26 |
w = Writer(fname, vcf) |
| 27 |
|
| 28 |
for v in vcf: |
| 29 |
# The get_gene_intersections function is not shown. |
| 30 |
# This could be any operation to find intersections |
| 31 |
# or any manipulation required by the user. |
| 32 |
genes = get_gene_intersections(v) |
| 33 |
if genes is not None: |
| 34 |
v.INFO["gene"] = ",".join(genes) |
| 35 |
w.write_record(v) |
| 36 |
|
| 37 |
w.close(); vcf.close() |
| 38 |
|
| 39 |
Modifying genotypes and the FORMAT field |
| 40 |
---------------------------------------- |
| 41 |
|
| 42 |
Here is an example where we filter some calls from records in a VCF. This demonstrates |
| 43 |
how to add to the FORMAT field and how to modify genotypes at a locus. |
| 44 |
|
| 45 |
.. code-block:: python |
| 46 |
|
| 47 |
from cyvcf2 import VCF, Writer |
| 48 |
vcf = VCF(VCF_PATH) |
| 49 |
# adjust the header to contain the new field |
| 50 |
# the keys 'ID', 'Description', 'Type', and 'Number' are required. |
| 51 |
vcf.add_format_to_header({ |
| 52 |
'ID': 'FILTER_CODE', |
| 53 |
'Description': 'Numeric code for filtering reason', |
| 54 |
'Type': 'Integer', |
| 55 |
'Number': '1' |
| 56 |
}) |
| 57 |
|
| 58 |
# create a new vcf Writer using the input vcf as a template. |
| 59 |
fname = "out.vcf" |
| 60 |
w = Writer(fname, vcf) |
| 61 |
|
| 62 |
for v in vcf: |
| 63 |
# The filter_samples function is not shown. |
| 64 |
# This could be any manipulation required by the user. |
| 65 |
# Since we specified the FILTER_CODE format field is an Integer |
| 66 |
# with Number=1, reasons must be an n x 1 numpy array of integers |
| 67 |
# where n is the number of samples. |
| 68 |
indicies, reasons = filter_samples(v) |
| 69 |
if indicies: |
| 70 |
# add the reasons array to the format dictionary at this locus |
| 71 |
v.set_format('FILTER_CODE', reasons) |
| 72 |
for index in indicies: |
| 73 |
# overwrite the genotypes of each filtered locus to be nocalls |
| 74 |
# Note: until the reassignment to v.genotypes below, this |
| 75 |
# leaves the v Variant object in an inconsistent state |
| 76 |
v.genotypes[index] = [-1]*v.ploidy + [False] |
| 77 |
# it is necessary to reassign the genotypes field |
| 78 |
# so that the v Variant object reprocess it and future calls |
| 79 |
# to functions like v.genotype.array() properly reflect |
| 80 |
# the changed genotypes |
| 81 |
v.genotypes = v.genotypes |
| 82 |
|
| 83 |
w.write_record(v) |
| 84 |
|
| 85 |
w.close(); vcf.close() |
| 86 |
|
| 87 |
.. _Limitations with writing: |
| 88 |
|
| 89 |
Known Limitations with Writing VCFs |
| 90 |
----------------------------------- |
| 91 |
* `cyvcf2` currently does not support writing VCFs encoded with UTF-8 with non-ASCII characters in the contents of string-typed FORMAT fields. |
| 92 |
* `cyvcf2` currently does not support writing string type format fields with number>1. |
| 93 |
|