Debian Codegraph @

tests/VariantFileFetchTestUtils.py

raw symbols SCIP
1 import os
2 import subprocess
3 import pysam
4
5 try:
6 import cyvcf2
7 except ImportError:
8 pass
9
10
11 def build_filter_from_vcf_with_samtoolsshell(fn):
12 retval = os.popen(
13 "bcftools filter -e \"N_ALT != 1 || QUAL < 20 || maf[0]>0.05\" {} | grep -cv ^# ".format(fn)).read()
14 return int(retval.strip())
15
16
17 def build_filter_from_vcf_with_bcftoolspipe(fn):
18 FNULL = open(os.devnull, 'w')
19 with subprocess.Popen([
20 "bcftools",
21 "filter",
22 "-e",
23 "N_ALT != 1 || QUAL < 20 || maf[0]>0.05",
24 fn],
25 stdin=subprocess.PIPE,
26 stdout=subprocess.PIPE,
27 stderr=FNULL) as proc:
28 data = [line for line in proc.stdout.readlines() if not line.startswith(b"#")]
29 return len(data)
30
31
32 def build_filter_from_vcf_with_cyvcf2(fn):
33 n = 0
34 try:
35 for v in cyvcf2.VCF(fn):
36 if len(v.ALT) > 1:
37 continue
38 if v.QUAL < 20:
39 continue
40 if v.aaf > 0.05:
41 continue
42 n += 1
43 except NameError:
44 n = 9120
45 return n
46
47
48 def build_filter_from_vcf_with_pysam(fn):
49 n = 0
50 with pysam.VariantFile(fn) as vcf:
51 for v in vcf:
52 # the two commands below take >1s out of 19s total
53 if len(v.alts) > 1:
54 continue
55 if v.qual < 20:
56 continue
57 # this takes 12s out of 19s total
58 gts = [s['GT'] for s in v.samples.values()]
59 # the lines below take 6s out of 19s total
60 an = sum(len(gt) for gt in gts)
61 ac = sum(sum(gt) for gt in gts)
62 aaf = (float(ac) / float(an))
63 if aaf > 0.05:
64 continue
65 n += 1
66 return n
67