Repository navigation
Expand file tree
/
Copy pathvcf2fas.py
More file actions
120 lines (83 loc) · 3.32 KB
/
Copy pathvcf2fas.py
File metadata and controls
120 lines (83 loc) · 3.32 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
#!/usr/bin/python
# vcf2fas.py converts a vcf file with multiple samples into a fasta sequence for ech locus based on SAM files for each sample in the vcf file
import argparse
parser = argparse.ArgumentParser(description="Converts VCF files into Fasta files from SAM")
parser.add_argument("-vcf", dest="vcf_infile", help="Provide the VCF file.", required=True)
arg = parser.parse_args()
def get_sam_sequences(fname, chrom, pos):
"""
Gets a list of sequences from a SAM file according to a chromosome name and position. It also assumes that reads are 90bp long
"""
ref_seqs = []
sam_fh = open(fname)
saved = False
for line in sam_fh:
if line.startswith("@"):
pass
else:
fields = line.split()
ref_chrom = fields[2]
ref_pos = fields[3]
seq = fields[0]
if ref_chrom == chrom:
save = True
#Check if sequence overlaps position
if pos in range(ref_pos, ref_pos + len(seq)):
ref_seqs.append(seq)
elif ref_chrom != chrom and not saved:
pass
elif ref_chrom != chrom and saved:
return ref_seqs
def vcf2fasta(vcf_file, mode):
"""
This checks the coverage of each SNP in the VCF file according to the SAM files
"""
vcf_fh = open(vcf_file)
line = next(vcf_fh)
if mode == "check_coverage":
bad_snps = open("bad_coverage_loci.csv", "w")
bad_counts = 0
total_counts = 0
taxa_pos = {}
# Skip VCF header
for line in vcf_file:
if line.startswith("#"):
pass
elif line.startswith("CHOM"):
fields = line.split()
taxa_names = [x.strip() for x in fields[9:]]
for sample in taxa_names:
taxa_pos[sample] = fields.index(sample)
else:
# Iterate over every SNP
fields = line.split()
chrom = fields[0]
pos = fields[1]
genotypes = [x.strip().split(":")[0] for x in fields[9:]]
coverage = [x.strip().split(":")[-1] for x in fields[9:]]
ref_coverage = []
# Check SNP coverage for each sample
for taxon, gen in zip(taxa_names, genotypes):
# Only do this for present genotypes
if gen != "./.":
sequences = get_sam_sequences("{}.sam".format(taxon), chrom, pos)
ref_coverage.append(len(sequences))
if mode == "check_coverage":
total_counts += 1
else:
if mode == "check_coverage":
ref_coverage.append(None)
if mode == "convert":
if mode == "check_coverage":
# Check coverage
for ref_cov, cov, taxon in zip(ref_coverage, coverage, taxa_names):
if ref_cov:
if ref_cov != cov:
bad_snps.write("{}; {}; {}\n".format(ref_cov, cov, taxon))
bad_counts += 1
if mode == "check_coverage":
print("There were %s bad SNP coverage counts (%s%%)" % (bad_counts, round(float(bad_counts)/float(total_counts), 2)))
def main():
# Arguments
vcf_file = arg.vcf_infile
main()