-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsegenotypes.py
194 lines (173 loc) · 4.92 KB
/
parsegenotypes.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
from pygr import worldbase
import glob
import os
import math
def filterSNPs(name):
reffile = name +'Ref'
altfile = name + 'Alt'
[ref, alt] = map(lambda x: glob.json(x, ''), [reffile, altfile])
print "Loaded Ref {0}, Alt {1}".format(len(ref),len(alt))
keys = ref.keys()
complsnps = []
for snppos in keys:
if glob.compl[ref[snppos].upper()] == alt[snppos].upper() or ref[snppos].upper() == alt[snppos].upper():
complsnps.append(snppos)
del ref[snppos]
del alt[snppos]
print len(ref)
print len(alt)
glob.dump(ref, reffile+'T')
glob.dump(alt, altfile+'T')
return complsnps
def checkRef(name):
reffile = name +'RefT'
altfile = name + 'AltT'
hg19 = worldbase.Bio.Seq.Genome.HUMAN.hg19(download = True)
print "Loaded hg19"
ref = glob.json(reffile)
print "Loaded Ref"
alt = glob.json(altfile)
print "Loaded Alt"
flip = []
errors = []
keys = ref.keys()
for snppos in keys:
print snppos + '\t' + name
t = snppos.split('pos')
hg19snp = str(hg19[t[0]][int(t[1])-1]).upper()
refsnp = ref[snppos].upper()
altsnp = alt[snppos].upper()
if hg19snp == refsnp:
continue
elif hg19snp == altsnp:
flip.append(snppos)
else:
print "Error: Neither Ref nor Alt of SNP corresponds to hg19 sequence"
errors.append(snppos)
glob.dump(flip, name+'flips')
glob.dump(errors, name+'errors')
return [flip, errors]
def corrRef(flip, name):
reffile = name +'RefT'
altfile = name + 'AltT'
for snp in flip:
t = ref[snp]
ref[snp] = alt[snp]
alt[snp] = t
glob.dump(ref, reffile+'flipped')
glob.dump(alt, altfile+'flipped')
def flipGeno(genofile, flip, errors):
"""flip genotypes based on new ref and alt,
the new genotype allele frequencies are
flipped 0->2, 2->0 based on list of snp positions
inputed as flip list
"""
file = open(genofile)
lines = file.readlines()
newgeno = open(genofile+'flippedfast', 'w')
newgeno.write(lines[0])
#newlines = []
i=0
flipset = set(flip)
for l in lines[1:]:
t = l.split('\t')
i+=1
print i
if t[0] in flipset:
newg = map(lambda x: 2-int(x), t[1].strip('\n').strip(',').split(','))
newl = t[0] +'\t'
newl = reduce(lambda x,y: x+str(y) + ',', [newl]+newg)
newgeno.write(newl.strip(',') + '\n')
#newlines.append(newl)
elif t[0] not in errors:
#newlines.append(l)
newgeno.write(l.strip('\n').strip(',') + '\n')
#for l in newlines:
# newgeno.write(l)
def flipArray(arrayname, flip, error):
"""flip array snp frequencies (hash)
1-freq for those in snp list inputed as flip
input is constructed in the original getarraysnps() function
"""
try:
arrayfreq = glob.json(arrayname+'freq')
except:
"No array snp frequency file"
for snp in flip:
arrayfreq[snp] = 1 - arrayfreq[snp]
for snp in error:
del arrayfreq[snp]
glob.dump(arrayfreq, arrayname+'freq')
def filterzeros(arrayname):
"""take out those that are 0
"""
freq = glob.json(arrayname+'freq')
for snp in freq.keys():
if freq[snp] == 0 or math.isnan(freq[snp]):
del freq[snp]
glob.dump(freq, arrayname+'freq')
def printtabarray(arrayname):
"""output will be analyzed by R to find cell line frequencies
"""
output = open(arrayname+'Rinput', 'w')
freq = glob.json(arrayname+'freq')
for snp in freq.keys():
output.write(snp + '\t')
output.write(str(freq[snp]) + '\n')
#modified to the 19 version
def parse1KGvcf(vcffile, outputname):
file = open(vcffile)
outputfile = open(outputname+'Geno', 'w')
ref = {}
alt = {}
lines = file.readlines(1000000)
while(lines != []):
for l in lines:
if l.startswith('#CHROM'):
g = reduce(lambda x,y: x+','+y, l.strip('\n').split('\t')[9:])
outputfile.write('\t'+str(g) +'\n')
if not l.startswith('#'):
tokens = l.strip('\n').split('\t')
f = filter(lambda x: 'GP' in x, tokens[7].split(';'))
if f != []:
pos = 'chr'+f[0].split('=')[1].split(':')[0]+'pos'+f[0].split('=')[1].split(':')[1]
ref[pos] = tokens[3]
alt[pos] = tokens[4]
m=pos +'\t'
for t in tokens[9:]:
m = m + str(int(t[0]) + int(t[2])) + ','
outputfile.write(m.strip(',')+'\n')
lines = file.readlines(1000000)
glob.dump(ref, outputname+'Ref')
glob.dump(alt, outputname+'Alt')
def parsehapmap():
import parsehapmapgenotypes
ref = {}
alt = {}
genotype = open('hapmapGeno','w')
for c in range(1,23):
"here"
[r,a] = parsehapmapgenotypes.parsehapmapchrom(c)
ref.update(r)
alt.update(a)
with open('../genotypes/hapmapchr'+str(c)+'genotype') as g:
lines = g.readlines()
map(lambda l: genotype.write(l), lines)
genotype.close()
glob.dump(ref, '../genotypes/hapmapRef')
glob.dump(alt, '../genotypes/hapmapAlt')
# Tests
def test():
hg19 = worldbase.Bio.Seq.Genome.HUMAN.hg19(download = True)
for i in range(1,1000000):
b = str(hg19['chr1'][i])
def testRef():
reffile = '../genotypes/CEUlowcovRef'
altfile = '../genotypes/CEUlowcovAlt'
ref = simplejson.load(open(reffile))
print "loaded ref"
alt = simplejson.load(open(altfile))
print "all loaded"
keys = ref.keys()
for snppos in keys:
continue