Upload imputation helper run_beagle.py
Browse files- preprocess/run_beagle.py +102 -0
preprocess/run_beagle.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
import re
|
3 |
+
import click
|
4 |
+
import os
|
5 |
+
import glob
|
6 |
+
"""
|
7 |
+
script for running Beagle 5.4
|
8 |
+
All kind of data for this script like human reference panel, genetic maps
|
9 |
+
and executable files for Beagle and Conform-gt can be found on the Beagle website
|
10 |
+
http://faculty.washington.edu/browning/beagle/beagle.html
|
11 |
+
"""
|
12 |
+
|
13 |
+
|
14 |
+
def bgzip_and_index(vcf, samples):
|
15 |
+
os.system(f'bcftools view {vcf} -Oz -o {vcf}.gz')
|
16 |
+
os.system(f'bcftools index {vcf}.gz')
|
17 |
+
if samples:
|
18 |
+
# for VCF with other samples
|
19 |
+
os.system(f'bcftools view {samples} -Oz -o {samples}.gz')
|
20 |
+
os.system(f'bcftools index {samples}.gz')
|
21 |
+
print('bgzip_and_index: done')
|
22 |
+
|
23 |
+
|
24 |
+
def merge(vcf, samples):
|
25 |
+
os.system(f'bcftools merge {vcf}.gz {samples}.gz -o merged.vcf')
|
26 |
+
bgzip_and_index('merged.vcf', False)
|
27 |
+
print('merge: done')
|
28 |
+
|
29 |
+
|
30 |
+
def clean_and_gzip(vcf, samples):
|
31 |
+
if samples:
|
32 |
+
vcf = 'merged.vcf'
|
33 |
+
result_file = f'{vcf.split(".vcf")[0]}_clean.vcf'
|
34 |
+
os.system(f'bcftools view -e \'ALT =="." | REF=="."\' {vcf}.gz -Oz -o temp.vcf.gz') # remove unknown alleles
|
35 |
+
os.system(f'bcftools norm -d none temp.vcf.gz >> {result_file}') # remove duplicates
|
36 |
+
os.system(f'gzip {result_file}')
|
37 |
+
os.remove('temp.vcf.gz') # remove temporal file
|
38 |
+
print('clean_and_gzip: done')
|
39 |
+
return f'{result_file}.gz'
|
40 |
+
|
41 |
+
|
42 |
+
def run_conform(conform, vcf_gz_file, ref_folder):
|
43 |
+
"""output files: checked_{chr_type}.vcf.gz
|
44 |
+
docs: http://faculty.washington.edu/browning/conform-gt.html
|
45 |
+
reference: files was downloaded from from Beagle human reference link
|
46 |
+
https://bochet.gcc.biostat.washington.edu/beagle/1000_Genomes_phase3_v5a/b37.vcf/"""
|
47 |
+
for ref_file in glob.glob(f'{ref_folder}/**/chr*.vcf.gz', recursive=True):
|
48 |
+
print('conform ', ref_file)
|
49 |
+
if re.search("chr(\d+)", ref_file):
|
50 |
+
chr_type = (re.search("chr(\d+)", ref_file))[1]
|
51 |
+
elif re.search("chrX", ref_file):
|
52 |
+
chr_type = (re.search("chrX", ref_file))[0].split('chr')[1]
|
53 |
+
os.system(f'java -jar {conform} ref={ref_file} gt={vcf_gz_file} chrom={chr_type} '
|
54 |
+
f'out=checked_{chr_type}')
|
55 |
+
print('run_conform: done')
|
56 |
+
|
57 |
+
|
58 |
+
def ensure_biallelic_ref(ref_dir):
|
59 |
+
for ref_file in glob.glob(f'{ref_dir}/chr*.v5a.vcf.gz'):
|
60 |
+
ref_biall_path = os.path.join(ref_dir, f'{ref_file.split("vcf")[0]}biallelic.vcf.gz')
|
61 |
+
print('ensure ', ref_file, ref_biall_path)
|
62 |
+
os.system(f'bcftools view -m2 -M2 -v snps -Oz -o {ref_biall_path} {ref_file}')
|
63 |
+
os.system(f'bcftools index {ref_biall_path}.gz')
|
64 |
+
os.remove(ref_file) # remove initial ref file
|
65 |
+
|
66 |
+
|
67 |
+
def run_beagle(beagle, gb, map_dir, ref_dir):
|
68 |
+
"""output files: checked_{chr_type}.vcf.gz
|
69 |
+
docs: http://faculty.washington.edu/browning/conform-gt.html"""
|
70 |
+
for checked_file in glob.glob(f'{os.getcwd()}/checked_*.vcf.gz'):
|
71 |
+
if re.search("checked_(\d+)", checked_file):
|
72 |
+
chr_type = (re.search("checked_(\d+)", checked_file))[1]
|
73 |
+
elif re.search("checked_X", checked_file):
|
74 |
+
chr_type = (re.search("checked_X", checked_file))[0].split('checked_')[1]
|
75 |
+
for ref_file in glob.glob(f'{ref_dir}/chr{chr_type}.*biallelic.vcf.gz'):
|
76 |
+
for map_file in glob.glob(f'{map_dir}/plink.chr{chr_type}.*.map'):
|
77 |
+
os.system(f'java -Xmx{gb}g -jar {beagle} gt={checked_file} ref={ref_file}'
|
78 |
+
f' out=imputed_{chr_type} map={map_file}')
|
79 |
+
|
80 |
+
|
81 |
+
@click.command()
|
82 |
+
@click.option('--vcf', help='Path to the target vcf file')
|
83 |
+
@click.option('--samples', help='Path to VCF with other samples for conform checks, not required if target VCF'
|
84 |
+
'contains data for at least 20 individuals', required=False)
|
85 |
+
@click.option('--conform', help='Path to conform .jar file')
|
86 |
+
@click.option('--beagle', help='Path to beagle .jar file')
|
87 |
+
@click.option('--ref', help='Path to folder with reference genome:'
|
88 |
+
' .vcf.gz files are expected to start with "chr1."..."chr22.", "chrX."')
|
89 |
+
@click.option('--map', help='Path to folder with PLINK format genetic maps, files are expected to start with'
|
90 |
+
'"plink.chr1.", ..."plink.chr22.", "plink.chrX."')
|
91 |
+
@click.option('--gb', help='Number of gigabytes for running beagle', default=10, show_default=True)
|
92 |
+
def main(vcf, samples, conform, beagle, ref, map, gb):
|
93 |
+
bgzip_and_index(vcf, samples)
|
94 |
+
if samples:
|
95 |
+
merge(vcf, samples)
|
96 |
+
cleaned_file = clean_and_gzip(vcf, samples) # returned cleaned file in .vcf.gz (gzip) format
|
97 |
+
ensure_biallelic_ref(ref)
|
98 |
+
run_conform(conform, cleaned_file, ref)
|
99 |
+
run_beagle(beagle, gb, map, ref)
|
100 |
+
|
101 |
+
|
102 |
+
main()
|