-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_vcf.py
More file actions
executable file
·28 lines (24 loc) · 1.28 KB
/
Copy pathsplit_vcf.py
File metadata and controls
executable file
·28 lines (24 loc) · 1.28 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
#!/usr/bin/env python3
import vcf
import sys
#Convert alt allele(s) to ambiguity code
IUPAC = {
'R': {'A', 'G'},
'Y': {'C', 'T'},
'S': {'G', 'C'},
'W': {'A', 'T'},
'K': {'G', 'T'},
'M': {'A', 'C'},
}
vcf_in = vcf.Reader(open(sys.argv[1], 'r'))
vcf_amb = vcf.Writer(open(sys.argv[2], 'w'), template=vcf_in)
vcf_unamb = vcf.Writer(open(sys.argv[3], 'w'), template=vcf_in)
for record in vcf_in:
if len(record.REF) == 1 and len(record.ALT[0]) == 1:
amb = [k for k in IUPAC.keys() if IUPAC[k] == set(list(record.REF) + list(map(str, record.ALT)))]
if amb:
vcf_amb.write_record(vcf.model._Record(CHROM=record.CHROM, POS=record.POS, ID=record.ID, REF=record.REF, ALT=record.ALT, QUAL=None, FILTER=None, INFO={'IUPAC': amb[0]}, FORMAT=record.FORMAT, sample_indexes=[], samples=record.samples))
else:
vcf_unamb.write_record(vcf.model._Record(CHROM=record.CHROM, POS=record.POS, ID=record.ID, REF=record.REF, ALT=record.ALT, QUAL=None, FILTER=None, INFO=None, FORMAT=record.FORMAT, sample_indexes=[], samples=record.samples))
else:
vcf_unamb.write_record(vcf.model._Record(CHROM=record.CHROM, POS=record.POS, ID=record.ID, REF=record.REF, ALT=record.ALT, QUAL=None, FILTER=None, INFO=None, FORMAT=record.FORMAT, sample_indexes=[], samples=record.samples))