-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate012AllParents.py
executable file
·206 lines (170 loc) · 5.87 KB
/
create012AllParents.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
import sys, os, gzip
import numpy as np
from collections import Counter,defaultdict
from subprocess import Popen, PIPE
from time import time
from collections import Counter
if len(sys.argv) < 5:
print("Usage: create012AllParents.py <HDB> <out-012-file> phased=1 or 0 proband=1 or 0 ")
exit(1)
hdb = sys.argv[1]
out = sys.argv[2]
phased = int(sys.argv[3])
proband = int(sys.argv[4])
#faiFn = sys.argv[5]
#th = float(sys.argv[6])
#hdb = "/mnt/wigclust8/data/safe/autism/HT/SSC510/SSC510-22"
#out = "a.vcf"
#phased = False
print(hdb, out, phased, file=sys.stderr)
badGens = set(list('oucd'))
ambiguous = {
'AC':'M',
'AG':'R',
'AT':'W',
'CG':'S',
'CT':'Y',
'GT':'K'
}
ramb = {v:k for k,v in list(ambiguous.items())}
for t,c in list(ambiguous.items()):
ambiguous[t[1] + t[0]] = c
class P:
def __init__(p,cs):
p.ch,p.pos,p.refA,p.A,p.C,p.G,p.T = cs
p.pos,p.A,p.C,p.G,p.T = list(map(int,[p.pos,p.A,p.C,p.G,p.T]))
aad = {a:n for a,n in zip("ACGT",[p.A,p.C,p.G,p.T])}
ad = {a:n for a,n in list(aad.items()) if a!=p.refA and n>0}
p.AI = {p.refA:0}
for ai,(a,n) in enumerate(sorted(list(ad.items()),key=lambda x: (-x[1],x[0]))):
p.AI[a] = ai+1
p.unphasedD = {}
p.phasedD = {}
for a,ai in list(p.AI.items()):
p.unphasedD[a + a] = "%d/%d" % (ai,ai)
p.phasedD[a + a] = "%d|%d" % (ai,ai)
for b,bi in list(p.AI.items()):
if a==b: continue
fi,si = sorted([ai,bi])
p.unphasedD[a + b] = "%d/%d" % (fi,si)
p.phasedD[a + b] = "%d|%d" % (ai,bi)
p.unphasedD[ambiguous[a + b]*2] = "%d/%d" % (fi,si)
p.phasedD[ambiguous[a + b]*2] = "%d/%d" % (fi,si)
# unphasedD {"AA": "0/0", "AC": 0/1, "CA": 0/1, "RR": "0/1"}
# phasedD {"AA": "0|0", "AC": 0|1, "CA": 1|0, "RR": "0/1"}
def vcfGen(p,AT,ANT, phased,fmId):
if AT in badGens:
return '-1'
D = p.phasedD if phased else p.unphasedD
#return D[AT+ANT]
try:
return sum(map(int,[D[AT+ANT][0], D[AT+ANT][2]]))
except KeyError:
print(p.ch, p.pos, p.AI, p.A, p.C, p.G, p.T, AT, ANT, fmId, file=sys.stderr)
# if phased==True: 0|0 1|0 0|1, ...
# else 0/0, 0/1, 0/2
# * if AT or ANT are small letters: ./. or .|.
# * if AT is a two-nucleotide code, assert that AT == ANT, and
# output unphased regardless of phased parameter
POS = []
posFn = hdb + '-pos.txt'
with open(posFn, 'r') as f:
f.readline()
for l in f:
POS.append(P(l.strip('\n\r').split('\t')))
FM_CH = {}
FM_HPTH = defaultdict(dict)
hpthFn = hdb + '-hpth.txt'
if os.path.isfile(hpthFn):
f = open(hpthFn)
elif os.path.isfile(hpthFn + '.gz'):
f = gzip.open(hpthFn + '.gz')
else:
print("no file ", hpthFn + '[.gz]')
exit(1)
f.readline()
for li,l in enumerate(f):
if li % 100 == 0:
print(li, "...")
fmId,perId,thrT,thr = l.strip('\n\r').split('\t')
#if fmId in FM_CH and perId != FM_CH[fmId]:
#if fmId in FM_CH and ((proband and 's1' in perId) or (not proband and 'p1' in perId)):
# continue
#FM_CH[fmId] = perId # my addition
FM_HPTH[fmId][thrT] = thr
#if li == 50000:
# break
f.close()
NH ,= set(len(x) for x in list(FM_HPTH.values()))
assert NH == 4
fmIdOrder = sorted(FM_HPTH.keys())
### TODO: get that somehow!!!!
"""
with open(faiFn, 'r') as f:
chrLen = {'chr'+str(x):0 for x in range(1,23)}
for l in f:
cs = l.strip('\n\r').split('\t')
if cs[0] in chrLen:
chrLen[cs[0]] = int(cs[1])
"""
OF = open(out,"w")
# OF = sys.stdout
#OF.write('##fileformat=VCFv4.2\n')
#OF.write('##fileDate=20171012\n')
#OF.write('##source=createVcfAllParents.py\n')
#for ch in sorted({p.ch for p in POS}):
# OF.write('##contig=<ID=%s,length=%d>\n' % (ch,chrLen[ch]))
#OF.write('##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">\n')
OF.write("\t".join('#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT'.split(' ') +
["%s.%s" % (fmId,rl) for fmId in fmIdOrder for rl in ["mo", "fa"]]) + "\n")
filterStats = defaultdict(int)
for pi,p in enumerate(POS):
if (pi % 1000) == 0:
print("writing out position", pi, "...", file=sys.stderr)
"""
# no laternative alleles
if len(p.AI) == 1:
filterStats['no_alt'] += 1
print >>sys.stderr, 'no_alt', p.ch, p.pos
continue
# more than one alternative
if len(p.AI) > 2:
filterStats['more_alt'] += 1
print >>sys.stderr, 'more_alt', p.ch, p.pos
continue
# refA not seen
cntsA = dict(zip('ACGT',[p.A,p.C,p.G,p.T]));
if cntsA[p.refA] == 0:
filterStats['no_ref'] += 1
print >>sys.stderr, 'no_ref', p.ch, p.pos
continue
# less than 90% genotyped
NG = sum(cntsA.values())
if NG < 0.9 * len(FM_HPTH) * 2 * 2:
filterStats['less_then_95%'] += 1
print >>sys.stderr, 'less_then_90%', p.ch, p.pos, NG, len(FM_HPTH)
continue
"""
#if 100.*(NG - cntsA[p.refA])/NG < th:
# continue
cs = [p.ch,
str(p.pos),
"%s:%d" % (p.ch,p.pos),
p.refA,
",".join([a for a,ai in sorted(list(p.AI.items()),key=lambda x: -x[1]) if ai>0]),
".",
".",
".",
"GT"]
for fmId in fmIdOrder:
fmD = FM_HPTH[fmId]
cs += [p.vcfGen(fmD['MT'][pi],fmD['MNT'][pi],phased,fmId),
p.vcfGen(fmD['FT'][pi],fmD['FNT'][pi],phased,fmId)]
OF.write("\t".join(map(str, cs)) + "\n")
#if pi > 10000:
# break
OF.close()
with open(hdb+'-filterStats.txt', 'w') as f:
f.write('\t'.join(map(str,sorted(filterStats)))+'\n')
f.write('\t'.join(map(str,[filterStats[k] for k in sorted(filterStats)]))+'\n')