Repository navigation
Expand file tree
/
Copy pathwrapper.py
More file actions
155 lines (134 loc) · 5.13 KB
/
Copy pathwrapper.py
File metadata and controls
155 lines (134 loc) · 5.13 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import glob
from snakemake.shell import shell
from tempfile import TemporaryDirectory
log = snakemake.log_fmt_shell(stdout=True, stderr=True)
extra = snakemake.params.get("extra", "")
with TemporaryDirectory() as tmpdirname:
### Run paraphase
shell(
f"""
(paraphase --bam {snakemake.input.bam} \
--reference {snakemake.input.fasta} \
--out {tmpdirname} \
{snakemake.params.genome} \
{extra}) {log}
""",
tmpdirname=tmpdirname, # Pass tmpdirname to the shell environment
)
shell(
"""
touch {snakemake.output}
"""
)
### Create a new VCF header
# Get the paths from Snakemake input and output objects
input_faidx = snakemake.input.faidx
output_vcf_header = snakemake.output.vcf_header
# Open the .fai index file and read lines
with open(input_faidx, "r") as fai_file:
lines = fai_file.readlines()
# Open the output file and write formatted header lines
with open(output_vcf_header, "w") as output:
for line in lines:
contig_id, length = line.split()[
:2
] # Assuming the first two elements are ID and length
output.write(f"##contig=<ID={contig_id},length={length}>\n")
### Concatenating, reheadering, and sorting the zipped and indexed VCF files, and copy the remapped reads
vcf_res = glob.glob(f"{tmpdirname}/*_vcfs/*vcf")
if vcf_res:
for vcf in vcf_res:
bgzip_cmd = f"bgzip -c {vcf} > {vcf}.gz"
shell(bgzip_cmd)
index_cmd = f"bcftools index {vcf}.gz"
shell(index_cmd)
print(f"Compressed and indexed: {vcf}.gz")
params_variant_files = " ".join([f"{vcf}.gz" for vcf in vcf_res])
shell(
f"bcftools concat -a -Oz {params_variant_files} | "
f"bcftools annotate --header-lines {snakemake.output.vcf_header} | "
f"bcftools sort -Oz -o {snakemake.output.merged_vcf}"
)
print(
f"Merged, reheadered, and sorted VCF file created: {snakemake.output.merged_vcf}"
)
# Copy out bam and bai files
bam_res = glob.glob(f"{tmpdirname}/*.bam")
bai_res = glob.glob(f"{tmpdirname}/*.bai")
# print("BAM RES: ", bam_res, bai_res)
shell(
f"""
cp -pr {' '.join(bam_res)} {snakemake.output.bam};
cp -pr {' '.join(bai_res)} {snakemake.output.bai}
"""
)
else:
print(
"No output VCF or BAM files were produced by paraphase, I hope this is what you were expecting, human?"
)
shell(f"touch {snakemake.output.merged_vcf}")
with TemporaryDirectory() as tmpdirname:
### Run paraphase
shell(
f"""
(paraphase --bam {snakemake.input.bam} \
--reference {snakemake.input.fasta} \
--out {tmpdirname} \
{snakemake.params.genome} \
{extra}) {log}
""",
tmpdirname=tmpdirname, # Pass tmpdirname to the shell environment
)
shell(
"""
touch {snakemake.output}
"""
)
### Create a new VCF header
# Get the paths from Snakemake input and output objects
input_faidx = snakemake.input.faidx
output_vcf_header = snakemake.output.vcf_header
# Open the .fai index file and read lines
with open(input_faidx, "r") as fai_file:
lines = fai_file.readlines()
# Open the output file and write formatted header lines
with open(output_vcf_header, "w") as output:
for line in lines:
contig_id, length = line.split()[
:2
] # Assuming the first two elements are ID and length
output.write(f"##contig=<ID={contig_id},length={length}>\n")
output.close()
### Concatenating, reheadering, and sorting the zipped and indexed VCF files, and copy the remapped reads
vcf_res = glob.glob(f"{tmpdirname}/*_vcfs/*vcf")
if vcf_res:
for vcf in vcf_res:
bgzip_cmd = f"bgzip -c {vcf} > {vcf}.gz"
shell(bgzip_cmd)
index_cmd = f"bcftools index {vcf}.gz"
shell(index_cmd)
print(f"Compressed and indexed: {vcf}.gz")
params_variant_files = " ".join([f"{vcf}.gz" for vcf in vcf_res])
shell(
f"bcftools concat -a -Oz {params_variant_files} | "
f"bcftools annotate --header-lines {snakemake.output.vcf_header} | "
f"bcftools sort -Oz -o {snakemake.output.merged_vcf}"
)
print(
f"Merged, reheadered, and sorted VCF file created: {snakemake.output.merged_vcf}"
)
# Copy out bam and bai files
bam_res = glob.glob(f"{tmpdirname}/*.bam")
bai_res = glob.glob(f"{tmpdirname}/*.bai")
# print("BAM RES: ", bam_res, bai_res)
shell(
f"""
cp -pr {' '.join(bam_res)} {snakemake.output.bam};
cp -pr {' '.join(bai_res)} {snakemake.output.bai}
"""
)
else:
print(
"No output VCF or BAM files were produced by paraphase, I hope this is what you were expecting, human?"
)
shell(f"touch {snakemake.output.merged_vcf}")