Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 91 lines (74 sloc) 3.255 kB
bdd950d point to maintained fork
James Casbon authored
1 **Development repository for PyVCF is at https://github.com/jamescasbon/PyVCF **
2
eb9c321 @jdoughertyii Adding module docstring as README
authored
3 A VCFv4.0 parser for Python.
4
5 The intent of this module is to mimic the ``csv`` module in the Python stdlib,
6 as opposed to more flexible serialization formats like JSON or YAML. ``vcf``
7 will attempt to parse the content of each record based on the data types
8 specified in the meta-information lines -- specifically the ##INFO and
9 ##FORMAT lines. If these lines are missing or incomplete, it will check
10 against the reserved types mentioned in the spec. Failing that, it will just
11 return strings.
12
13 There is currently one piece of interface: ``VCFReader``. It takes a file-like
14 object and acts as a reader::
15
16 >>> import vcf
17 >>> vcf_reader = vcf.VCFReader(open('example.vcf', 'rb'))
18 >>> for record in vcf_reader:
19 ... print record
20 Record(CHROM='20', POS=14370, ID='rs6054257', REF='G', ALT=['A'], QUAL=29,
21 FILTER='PASS', INFO={'H2': True, 'NS': 3, 'DB': True, 'DP': 14, 'AF': [0.5]
22 }, FORMAT='GT:GQ:DP:HQ', samples=[{'GT': '0', 'HQ': [58, 50], 'DP': 3, 'GQ'
23 : 49, 'name': 'NA00001'}, {'GT': '0', 'HQ': [65, 3], 'DP': 5, 'GQ': 3, 'nam
24 e' : 'NA00002'}, {'GT': '0', 'DP': 3, 'GQ': 41, 'name': 'NA00003'}])
25
26 This produces a great deal of information, but it is conveniently accessed.
27 The attributes of a Record are the 8 fixed fields from the VCF spec plus two
28 more. That is:
29
30 * ``Record.CHROM``
31 * ``Record.POS``
32 * ``Record.ID``
33 * ``Record.REF``
34 * ``Record.ALT``
35 * ``Record.QUAL``
36 * ``Record.FILTER``
37 * ``Record.INFO``
38
39 plus two more attributes to handle genotype information:
40
41 * ``Record.FORMAT``
42 * ``Record.samples``
43
44 ``samples``, not being the title of any column, is left lowercase. The format
45 of the fixed fields is from the spec. Comma-separated lists in the VCF are
46 converted to lists. In particular, one-entry VCF lists are converted to
47 one-entry Python lists (see, e.g., ``Record.ALT``). Semicolon-delimited lists
48 of key=value pairs are converted to Python dictionaries, with flags being given
49 a ``True`` value. Integers and floats are handled exactly as you'd expect::
50
51 >>> record = vcf_reader.next()
52 >>> print record.POS
53 17330
54 >>> print record.ALT
55 ['A']
56 >>> print record.INFO['AF']
57 [0.017]
58
59 ``record.FORMAT`` will be a string specifying the format of the genotype
60 fields. In case the FORMAT column does not exist, ``record.FORMAT`` is
61 ``None``. Finally, ``record.samples`` is a list of dictionaries containing the
62 parsed sample column::
63
64 >>> record = vcf_reader.next()
65 >>> for sample in record.samples:
66 ... print sample['GT']
67 '1|2'
68 '2|1'
69 '2/2'
70
71 Metadata regarding the VCF file itself can be investigated through the
72 following attributes:
73
74 * ``VCFReader.metadata``
75 * ``VCFReader.infos``
76 * ``VCFReader.filters``
77 * ``VCFReader.formats``
78 * ``VCFReader.samples``
79
80 For example::
81
82 >>> vcf_reader.metadata['fileDate']
83 20090805
84 >>> vcf_reader.samples
85 ['NA00001', 'NA00002', 'NA00003']
86 >>> vcf_reader.filters
87 {'q10': Filter(id='q10', desc='Quality below 10'),
88 's50': Filter(id='s50', desc='Less than 50% of samples have data')}
89 >>> vcf_reader.infos['AA'].desc
90 Ancestral Allele
Something went wrong with that request. Please try again.