pyvcf

A Variant Call Format reader for Python.

https://github.com/jamescasbon/pyvcf

Science Score: 10.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    5 of 44 committers (11.4%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.4%) to scientific vocabulary

Keywords from Contributors

bioinformatics genomics dna phylogenetics biopython protein protein-structure sequence-alignment closember ipython
Last synced: 11 months ago · JSON representation

Repository

A Variant Call Format reader for Python.

Basic Info
Statistics
  • Stars: 415
  • Watchers: 27
  • Forks: 199
  • Open Issues: 100
  • Releases: 0
Fork of jdoughertyii/PyVCF
Created over 14 years ago · Last pushed almost 3 years ago
Metadata Files
Readme License

README.rst

A VCFv4.0 and 4.1 parser for Python.

Online version of PyVCF documentation is available at http://pyvcf.rtfd.org/

The intent of this module is to mimic the ``csv`` module in the Python stdlib,
as opposed to more flexible serialization formats like JSON or YAML.  ``vcf``
will attempt to parse the content of each record based on the data types
specified in the meta-information lines --  specifically the ##INFO and
##FORMAT lines.  If these lines are missing or incomplete, it will check
against the reserved types mentioned in the spec.  Failing that, it will just
return strings.

There main interface is the class: ``Reader``.  It takes a file-like
object and acts as a reader::

    >>> import vcf
    >>> vcf_reader = vcf.Reader(open('vcf/test/example-4.0.vcf', 'r'))
    >>> for record in vcf_reader:
    ...     print record
    Record(CHROM=20, POS=14370, REF=G, ALT=[A])
    Record(CHROM=20, POS=17330, REF=T, ALT=[A])
    Record(CHROM=20, POS=1110696, REF=A, ALT=[G, T])
    Record(CHROM=20, POS=1230237, REF=T, ALT=[None])
    Record(CHROM=20, POS=1234567, REF=GTCT, ALT=[G, GTACT])


This produces a great deal of information, but it is conveniently accessed.
The attributes of a Record are the 8 fixed fields from the VCF spec::

    * ``Record.CHROM``
    * ``Record.POS``
    * ``Record.ID``
    * ``Record.REF``
    * ``Record.ALT``
    * ``Record.QUAL``
    * ``Record.FILTER``
    * ``Record.INFO``

plus attributes to handle genotype information:

    * ``Record.FORMAT``
    * ``Record.samples``
    * ``Record.genotype``

``samples`` and ``genotype``, not being the title of any column, are left lowercase.  The format
of the fixed fields is from the spec.  Comma-separated lists in the VCF are
converted to lists.  In particular, one-entry VCF lists are converted to
one-entry Python lists (see, e.g., ``Record.ALT``).  Semicolon-delimited lists
of key=value pairs are converted to Python dictionaries, with flags being given
a ``True`` value. Integers and floats are handled exactly as you'd expect::

    >>> vcf_reader = vcf.Reader(open('vcf/test/example-4.0.vcf', 'r'))
    >>> record = next(vcf_reader)
    >>> print record.POS
    14370
    >>> print record.ALT
    [A]
    >>> print record.INFO['AF']
    [0.5]

There are a number of convenience methods and properties for each ``Record`` allowing you to
examine properties of interest::

    >>> print record.num_called, record.call_rate, record.num_unknown
    3 1.0 0
    >>> print record.num_hom_ref, record.num_het, record.num_hom_alt
    1 1 1
    >>> print record.nucl_diversity, record.aaf, record.heterozygosity
    0.6 [0.5] 0.5
    >>> print record.get_hets()
    [Call(sample=NA00002, CallData(GT=1|0, GQ=48, DP=8, HQ=[51, 51]))]
    >>> print record.is_snp, record.is_indel, record.is_transition, record.is_deletion
    True False True False
    >>> print record.var_type, record.var_subtype
    snp ts
    >>> print record.is_monomorphic
    False

``record.FORMAT`` will be a string specifying the format of the genotype
fields.  In case the FORMAT column does not exist, ``record.FORMAT`` is
``None``.  Finally, ``record.samples`` is a list of dictionaries containing the
parsed sample column and ``record.genotype`` is a way of looking up genotypes
by sample name::

    >>> record = next(vcf_reader)
    >>> for sample in record.samples:
    ...     print sample['GT']
    0|0
    0|1
    0/0
    >>> print record.genotype('NA00001')['GT']
    0|0

The genotypes are represented by ``Call`` objects, which have three attributes: the
corresponding Record ``site``, the sample name in ``sample`` and a dictionary of
call data in ``data``::

     >>> call = record.genotype('NA00001')
     >>> print call.site
     Record(CHROM=20, POS=17330, REF=T, ALT=[A])
     >>> print call.sample
     NA00001
     >>> print call.data
     CallData(GT=0|0, GQ=49, DP=3, HQ=[58, 50])

Please note that as of release 0.4.0, attributes known to have single values (such as
``DP`` and ``GQ`` above) are returned as values.  Other attributes are returned
as lists (such as ``HQ`` above).

There are also a number of methods::

    >>> print call.called, call.gt_type, call.gt_bases, call.phased
    True 0 T|T True

Metadata regarding the VCF file itself can be investigated through the
following attributes:

    * ``Reader.metadata``
    * ``Reader.infos``
    * ``Reader.filters``
    * ``Reader.formats``
    * ``Reader.samples``

For example::

    >>> vcf_reader.metadata['fileDate']
    '20090805'
    >>> vcf_reader.samples
    ['NA00001', 'NA00002', 'NA00003']
    >>> vcf_reader.filters
    OrderedDict([('q10', Filter(id='q10', desc='Quality below 10')), ('s50', Filter(id='s50', desc='Less than 50% of samples have data'))])
    >>> vcf_reader.infos['AA'].desc
    'Ancestral Allele'

ALT records are actually classes, so that you can interrogate them::

    >>> reader = vcf.Reader(open('vcf/test/example-4.1-bnd.vcf'))
    >>> _ = next(reader); row = next(reader)
    >>> print row
    Record(CHROM=1, POS=2, REF=T, ALT=[T[2:3[])
    >>> bnd = row.ALT[0]
    >>> print bnd.withinMainAssembly, bnd.orientation, bnd.remoteOrientation, bnd.connectingSequence
    True False True T

The Reader supports retrieval of records within designated regions for files
with tabix indexes via the fetch method. This requires the pysam module as a
dependency. Pass in a chromosome, and, optionally, start and end coordinates,
for the regions of interest::

    >>> vcf_reader = vcf.Reader(filename='vcf/test/tb.vcf.gz')
    >>> # fetch all records on chromosome 20 from base 1110696 through 1230237
    >>> for record in vcf_reader.fetch('20', 1110695, 1230237):  # doctest: +SKIP
    ...     print record
    Record(CHROM=20, POS=1110696, REF=A, ALT=[G, T])
    Record(CHROM=20, POS=1230237, REF=T, ALT=[None])

Note that the start and end coordinates are in the zero-based, half-open
coordinate system, similar to ``_Record.start`` and ``_Record.end``. The very
first base of a chromosome is index 0, and the the region includes bases up
to, but not including the base at the end coordinate. For example::

    >>> # fetch all records on chromosome 4 from base 11 through 20
    >>> vcf_reader.fetch('4', 10, 20)   # doctest: +SKIP

would include all records overlapping a 10 base pair region from the 11th base
of through the 20th base (which is at index 19) of chromosome 4. It would not
include the 21st base (at index 20). (See
http://genomewiki.ucsc.edu/index.php/Coordinate_Transforms for more
information on the zero-based, half-open coordinate system.)

The ``Writer`` class provides a way of writing a VCF file.  Currently, you must specify a
template ``Reader`` which provides the metadata::

    >>> vcf_reader = vcf.Reader(filename='vcf/test/tb.vcf.gz')
    >>> vcf_writer = vcf.Writer(open('/dev/null', 'w'), vcf_reader)
    >>> for record in vcf_reader:
    ...     vcf_writer.write_record(record)

An extensible script is available to filter vcf files in vcf_filter.py.  VCF filters
declared by other packages will be available for use in this script.  Please
see :doc:`FILTERS` for full description.

Owner

  • Name: James Casbon
  • Login: jamescasbon
  • Kind: user
  • Location: Brighton, UK

GitHub Events

Total
  • Issues event: 1
  • Watch event: 14
  • Issue comment event: 5
  • Pull request event: 1
  • Fork event: 1
Last Year
  • Issues event: 1
  • Watch event: 14
  • Issue comment event: 5
  • Pull request event: 1
  • Fork event: 1

Committers

Last synced: over 2 years ago

All Time
  • Total Commits: 414
  • Total Committers: 44
  • Avg Commits per committer: 9.409
  • Development Distribution Score (DDS): 0.698
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
James Casbon j****n@p****m 125
Martijn Vermaat m****n@v****e 55
Lenna Peterson a****a@g****m 33
arq5x a****n@g****m 26
dzerbino d****o@s****u 21
James Casbon c****n@g****m 18
Ian Roberts i****s@c****t 14
Chris Lasher c****r@g****m 13
mgymrek m****k@m****u 10
bow b****w@b****d 9
Sam Brightman s****n@g****m 7
Marcel Martin m****n@t****e 6
John j****i@g****m 6
Kaarel k****9@u****m 5
Libor Morkovsky l****k@g****m 5
datagram w****1@s****l 5
awenger a****r@g****m 4
Alistair Miles a****o@g****m 4
Redmar r****r@u****m 4
Marc Abramowitz m****c@m****m 4
Juan Medina j****r@g****m 3
Sean Davis s****i@g****m 3
chapmanb c****b@5****m 3
Cory McLean c****t@h****m 3
trijntje R****g@x****l 3
Nils Homer N****r@c****u 2
Eric e****3@g****m 2
alexjironkin a****n@g****m 2
Brent Pedersen b****e@g****m 2
redmar R****g@n****l 2
and 14 more...

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 74
  • Total pull requests: 30
  • Average time to close issues: 11 months
  • Average time to close pull requests: over 1 year
  • Total issue authors: 65
  • Total pull request authors: 23
  • Average comments per issue: 2.08
  • Average comments per pull request: 2.17
  • Merged pull requests: 3
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 2.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • dridk (4)
  • everestial (3)
  • myourshaw (3)
  • peterjc (2)
  • krlk89 (2)
  • jsmedmar (1)
  • freekvh (1)
  • kpsimonlin (1)
  • SantosJGND (1)
  • Redmar-van-den-Berg (1)
  • wcardoen (1)
  • CanadianGPD (1)
  • doctorasgr (1)
  • webmasterar (1)
  • nuin (1)
Pull Request Authors
  • peterjc (3)
  • sambrightman (3)
  • gitanoqevaporelmundoentero (2)
  • netsettler (2)
  • ericman93 (2)
  • everestial (1)
  • dridk (1)
  • jfjlaros (1)
  • jkoestinger (1)
  • sodre (1)
  • kevinjyee (1)
  • joaoe (1)
  • CholoTook (1)
  • dustinbleile (1)
  • Marcormio (1)
Top Labels
Issue Labels
Incompatible API change (1)
Pull Request Labels

Packages

  • Total packages: 4
  • Total downloads:
    • pypi 35,476 last-month
  • Total docker downloads: 257,677
  • Total dependent packages: 13
    (may contain duplicates)
  • Total dependent repositories: 164
    (may contain duplicates)
  • Total versions: 39
  • Total maintainers: 1
pypi.org: pyvcf

Variant Call Format (VCF) parser for Python

  • Versions: 22
  • Dependent Packages: 12
  • Dependent Repositories: 129
  • Downloads: 35,476 Last month
  • Docker Downloads: 257,677
Rankings
Dependent packages count: 1.0%
Dependent repos count: 1.3%
Docker downloads count: 1.4%
Downloads: 1.4%
Average: 2.0%
Stargazers count: 3.3%
Forks count: 3.7%
Maintainers (1)
Last synced: 11 months ago
proxy.golang.org: github.com/jamescasbon/PyVCF
  • Versions: 8
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 9.0%
Average: 9.6%
Dependent repos count: 10.2%
Last synced: 11 months ago
proxy.golang.org: github.com/jamescasbon/pyvcf
  • Versions: 8
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 9.0%
Average: 9.6%
Dependent repos count: 10.2%
Last synced: 11 months ago
conda-forge.org: pyvcf

The intent of this module is to mimic the csv module in the Python stdlib, as opposed to more flexible serialization formats like JSON or YAML. vcf will attempt to parse the content of each record based on the data types specified in the meta-information lines -- specifically the \##INFO and \##FORMAT lines. If these lines are missing or incomplete, it will check against the reserved types mentioned in the spec. Failing that, it will just return strings.

  • Versions: 1
  • Dependent Packages: 1
  • Dependent Repositories: 35
Rankings
Dependent repos count: 6.2%
Forks count: 12.8%
Average: 17.0%
Stargazers count: 20.0%
Dependent packages count: 29.0%
Last synced: 11 months ago