-
Notifications
You must be signed in to change notification settings - Fork 2
/
vcf.py
executable file
·53 lines (47 loc) · 1.19 KB
/
vcf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module with functions for dealing with .vcf files
"""
import csv
import itertools
# ~~~~~ FUNCTIONS ~~~~~ #
def header_skip_num(vcf_file):
"""
Counts the number of rows in the header that should be skipped when reading in the file. Header rows in a .vcf start with ``##``
Parameters
----------
vcf_file: str
the path to a .vcf file
Returns
-------
int
the number of rows of header at the beginning of the file
"""
skip_rows = 0
with open(vcf_file, 'r') as f:
for line in f:
if line.startswith('##'):
skip_rows += 1
else:
break
return(skip_rows)
def num_entries(vcf_file):
"""
Counts the number of entries in a .vcf file
Parameters
----------
vcf_file: str
the path to a .vcf file
Returns
-------
int
the number of entries in the file
"""
num = 0
skip_rows = header_skip_num(vcf_file = vcf_file)
with open(vcf_file) as f:
reader = csv.DictReader(itertools.islice(f, skip_rows, None), delimiter = '\t')
for row in reader:
num += 1
return(num)