Skip to content

Commit 6641a79

Browse files
committed
Added VariantContig and VariantMetadata documentation
1 parent 1c682c8 commit 6641a79

2 files changed

Lines changed: 99 additions & 22 deletions

File tree

doc/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ VCF/BCF files
210210
.. autoclass:: pysam.VariantFile
211211
:members:
212212

213+
.. autoclass:: pysam.VariantMetadata
214+
:members:
215+
216+
.. autoclass:: pysam.VariantContig
217+
:members:
218+
213219
.. autoclass:: pysam.VariantHeader
214220
:members:
215221

pysam/libcbcf.pyx

Lines changed: 93 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,29 +1468,53 @@ cdef VariantHeaderRecords makeVariantHeaderRecords(VariantHeader header):
14681468

14691469

14701470
cdef class VariantMetadata(object):
1471-
"""filter, info or format metadata record from a :class:`VariantHeader` object"""
1471+
"""A FILTER, INFO or FORMAT metadata line from a :class:`VariantHeader` object.
1472+
1473+
Example:
1474+
Here is an example :class:`VariantMetadata` header line::
1475+
1476+
##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data">
1477+
1478+
In this example, :attr:`name` = ``"NS"``, :attr:`number` = ``1``, :attr:`type` = ``"Integer"``,
1479+
and :attr:`description` = ``"Number of Samples With Data"``.
1480+
"""
14721481
def __init__(self, *args, **kwargs):
1473-
raise TypeError('this class cannot be instantiated from Python')
1482+
raise TypeError('This class cannot be instantiated from Python')
14741483

14751484
@property
14761485
def name(self):
1477-
"""metadata name"""
1486+
"""The metadata field name (e.g. "AD")."""
14781487
cdef bcf_hdr_t *hdr = self.header.ptr
14791488
return bcf_str_cache_get_charptr(hdr.id[BCF_DT_ID][self.id].key)
14801489

14811490
# Q: Should this be exposed?
14821491
@property
14831492
def id(self):
1484-
"""metadata internal header id number"""
1493+
"""The metadata **internal** header ID number.
1494+
To access the ``ID`` field of a header metadata line, use :attr:`name` instead.
1495+
"""
14851496
return self.id
14861497

14871498
@property
14881499
def number(self):
1489-
"""metadata number (i.e. cardinality)"""
1500+
"""The number of values present for this metadata field (i.e. cardinality).
1501+
1502+
Raises:
1503+
ValueError
1504+
If the header ID number is invalid.
1505+
1506+
Returns:
1507+
* None if the header line is not a FILTER, INFO, or FORMAT metadata line.
1508+
* ``"."`` if there are a variable number of values.
1509+
* ``n`` if there are a fixed number of exactly ``n`` values.
1510+
* ``"A"`` if there is one value per alternate allele.
1511+
* ``"R"`` if there is one value per allele (including the reference).
1512+
* ``"G"`` if there is one value per genotype (usually for FORMAT tags).
1513+
"""
14901514
cdef bcf_hdr_t *hdr = self.header.ptr
14911515

14921516
if not check_header_id(hdr, self.type, self.id):
1493-
raise ValueError('Invalid header id')
1517+
raise ValueError('Invalid header ID')
14941518

14951519
if self.type == BCF_HL_FLT:
14961520
return None
@@ -1505,49 +1529,76 @@ cdef class VariantMetadata(object):
15051529

15061530
@property
15071531
def type(self):
1508-
"""metadata value type"""
1532+
"""The metadata value type.
1533+
1534+
Raises:
1535+
ValueError
1536+
If the header ID number is invalid.
1537+
1538+
Returns:
1539+
None if the header line is not a FILTER, INFO, or FORMAT metadata line.
1540+
Otherwise, one of the following strings representing the possible value types:
1541+
``"Flag"``, ``"Integer"``, ``"Float"``, or ``"String"``.
1542+
"""
15091543
cdef bcf_hdr_t *hdr = self.header.ptr
15101544
if not check_header_id(hdr, self.type, self.id):
1511-
raise ValueError('Invalid header id')
1545+
raise ValueError('Invalid header ID')
15121546

15131547
if self.type == BCF_HL_FLT:
15141548
return None
15151549
return VALUE_TYPES[bcf_hdr_id2type(hdr, self.type, self.id)]
15161550

15171551
@property
15181552
def description(self):
1519-
"""metadata description (or None if not set)"""
1553+
"""The metadata description.
1554+
1555+
Returns:
1556+
None if the description is unset, otherwise a string description of the metadata line.
1557+
"""
15201558
descr = self.record.get('Description')
15211559
if descr:
15221560
descr = descr.strip('"')
15231561
return force_str(descr)
15241562

15251563
@property
15261564
def record(self):
1527-
""":class:`VariantHeaderRecord` associated with this :class:`VariantMetadata` object"""
1565+
"""The :class:`VariantHeaderRecord` associated with this :class:`VariantMetadata` object.
1566+
1567+
Raises:
1568+
ValueError
1569+
If the header ID number is invalid.
1570+
1571+
Returns:
1572+
None if there is no associated record, otherwise the :class:`VariantHeaderRecord`.
1573+
"""
15281574
cdef bcf_hdr_t *hdr = self.header.ptr
15291575
if not check_header_id(hdr, self.type, self.id):
1530-
raise ValueError('Invalid header id')
1576+
raise ValueError('Invalid header ID')
15311577
cdef bcf_hrec_t *hrec = hdr.id[BCF_DT_ID][self.id].val.hrec[self.type]
15321578
if not hrec:
15331579
return None
15341580
return makeVariantHeaderRecord(self.header, hrec)
15351581

15361582
def remove_header(self):
1583+
"""Mark the :class:`VariantHeaderRecord` associated with this :class:`VariantMetadata` object for deletion.
1584+
`
1585+
Warning:
1586+
Due to HTSlib limitations, the record is not immediately removed from the header.
1587+
"""
15371588
cdef bcf_hdr_t *hdr = self.header.ptr
15381589
cdef const char *key = hdr.id[BCF_DT_ID][self.id].key
15391590
bcf_hdr_remove(hdr, self.type, key)
15401591

15411592

15421593
cdef VariantMetadata makeVariantMetadata(VariantHeader header, int type, int id):
15431594
if not header:
1544-
raise ValueError('invalid VariantHeader')
1595+
raise ValueError('Invalid VariantHeader')
15451596

15461597
if type != BCF_HL_FLT and type != BCF_HL_INFO and type != BCF_HL_FMT:
1547-
raise ValueError('invalid metadata type')
1598+
raise ValueError('Invalid metadata type')
15481599

15491600
if id < 0 or id >= header.ptr.n[BCF_DT_ID]:
1550-
raise ValueError('invalid metadata id')
1601+
raise ValueError('Invalid metadata ID')
15511602

15521603
cdef VariantMetadata meta = VariantMetadata.__new__(VariantMetadata)
15531604
meta.header = header
@@ -1709,47 +1760,67 @@ cdef VariantHeaderMetadata makeVariantHeaderMetadata(VariantHeader header, int32
17091760

17101761

17111762
cdef class VariantContig(object):
1712-
"""contig metadata from a :class:`VariantHeader`"""
1763+
"""Contig metadata from a :class:`VariantHeader` metadata header line.
1764+
1765+
Example:
1766+
Here is an example :class:`VariantContig` metadata header line::
1767+
1768+
##contig=<ID=20,length=62435964,assembly=B36,species="Homo sapiens">
1769+
1770+
In this example, :attr:`name` = ``"20"`` and :attr:`length` = ``62435964``.
1771+
"""
17131772
def __init__(self, *args, **kwargs):
1714-
raise TypeError('this class cannot be instantiated from Python')
1773+
raise TypeError("This class cannot be instantiated from Python")
17151774

17161775
@property
17171776
def name(self):
1718-
"""contig name"""
1777+
"""The contig name."""
17191778
cdef bcf_hdr_t *hdr = self.header.ptr
17201779
return bcf_str_cache_get_charptr(hdr.id[BCF_DT_CTG][self.id].key)
17211780

17221781
@property
17231782
def id(self):
1724-
"""contig internal id number"""
1783+
"""The contig internal ID number.
1784+
1785+
To access the "ID" field of a header contig line, use :attr:`name` instead.
1786+
"""
17251787
return self.id
17261788

17271789
@property
17281790
def length(self):
1729-
"""contig length or None if not available"""
1791+
"""The contig length.
1792+
1793+
Returns:
1794+
The contig length, or None if not available.
1795+
"""
17301796
cdef bcf_hdr_t *hdr = self.header.ptr
17311797
cdef uint32_t length = hdr.id[BCF_DT_CTG][self.id].val.info[0]
17321798
return length if length else None
17331799

17341800
@property
17351801
def header_record(self):
1736-
""":class:`VariantHeaderRecord` associated with this :class:`VariantContig` object"""
1802+
"""The :class:`VariantHeaderRecord` associated with this :class:`VariantContig` object."""
17371803
cdef bcf_hdr_t *hdr = self.header.ptr
17381804
cdef bcf_hrec_t *hrec = hdr.id[BCF_DT_CTG][self.id].val.hrec[0]
17391805
return makeVariantHeaderRecord(self.header, hrec)
17401806

17411807
def remove_header(self):
1808+
"""Mark the :class:`VariantHeaderRecord` associated with this :class:`VariantContig` object for deletion.
1809+
1810+
Warning:
1811+
Due to HTSlib limitations, the record is not immediately removed from the header.
1812+
"""
17421813
cdef bcf_hdr_t *hdr = self.header.ptr
17431814
cdef const char *key = hdr.id[BCF_DT_CTG][self.id].key
17441815
bcf_hdr_remove(hdr, BCF_HL_CTG, key)
17451816

17461817

17471818
cdef VariantContig makeVariantContig(VariantHeader header, int id):
17481819
if not header:
1749-
raise ValueError('invalid VariantHeader')
1820+
raise ValueError('Invalid VariantHeader')
17501821

17511822
if id < 0 or id >= header.ptr.n[BCF_DT_CTG]:
1752-
raise ValueError('invalid contig id')
1823+
raise ValueError('Invalid contig ID')
17531824

17541825
cdef VariantContig contig = VariantContig.__new__(VariantContig)
17551826
contig.header = header

0 commit comments

Comments
 (0)