forked from immunogenomics/goshifter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
executable file
·190 lines (163 loc) · 5.85 KB
/
data.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
#!/usr/bin/env python
"""
:File: data.py
:Author: Gosia Trynka <[email protected]>
:Last updated: 2014-07-23
Copyright (C) 2014 Gosia Trynka
This file is part of GoShifter package.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
BY USING THE SOFTWARE YOU ACKNOWLEDGE THAT YOU HAVE READ AND UNDERSTAND THE
TERMS OF USE BELOW.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THIS SOFTWARE IS TO BE USED AS A RESEARCH TOOL ONLY. THE SOFTWARE TOOL SHALL
NOT BE USED AS A DIAGNOSTIC DECISION MAKING SYSTEM AND MUST NOT BE USED TO
MAKE A CLINICAL DIAGNOSIS OR REPLACE OR OVERRULE A LICENSED HEALTH CARE
PROFESSIONAL'S JUDGMENT OR CLINICAL DIAGNOSIS. ANY ACTION THE RESEARCHER TAKES
IN RESPONSE TO THE INFORMATION CONTAINED WITHIN IS AT THE RESEARCHER'S
DISCRETION ONLY.
"""
import gzip
import io
import sys
import os
import re
def readSnpMapByChr(file,maf=None):
"""
Reads in file with SNP mappings.
Returns dict, key:chrom, value:snp, bp.
"""
maf = maf if maf is not None else 0
print "Filtering SNPs if MAF < {}".format(maf)
snpMap = {}
i = 0
with open(file,'rU') as f:
snpNu = 0
header = next(f).rstrip().split("\t")
chr_i = getCol('Chrom',header,file)
snp_i = getCol('SNP',header,file)
bp_i = getCol('BP',header,file)
#check for freq in the header only if maf is specified
if maf != 0:
freq_i = getCol('Freq',header,file)
for line in f:
i += 1
line = line.rstrip().split("\t")
checkMappingsFormat(file,line,i)
chr = line[chr_i]
snp = line[snp_i]
if snp == 'excluded': continue
bp = int(line[bp_i])
if maf != 0:
freq = float(line[freq_i])
if freq > 0.5:
freq = 1-freq
if freq < maf: continue
snpMap.setdefault(chr,{})
snpMap[chr].setdefault(snp,{})
snpMap[chr][snp]['bp'] = bp
snpNu += 1
print 'Read {snpNu} SNPs from {file}'.format(**locals())
return snpMap
def readProxyMap(file,maf=None):
"""
Reads in file with SNP mappings.
Returns dict, key:chrom, value:snp, bp.
"""
maf = maf if maf is not None else 0
print "Filtering SNPs if MAF < {}".format(maf)
snpMap = {}
i = 0
with open(file,'rU') as f:
snpNu = 0
header = next(f).rstrip().split("\t")
chr_i = getCol('Chrom',header,file)
snp_i = getCol('SNP',header,file)
bp_i = getCol('BP',header,file)
#check for freq in the header only if maf is specified
if maf != 0:
freq_i = getCol('Freq',header,file)
for line in f:
i += 1
line = line.rstrip().split("\t")
checkMappingsFormat(file,line,i)
chr = line[chr_i]
snp = line[snp_i]
if snp == 'excluded': continue
bp = int(line[bp_i])
if maf != 0:
freq = float(line[freq_i])
if freq > 0.5:
freq = 1-freq
if freq < maf: continue
snpMap.setdefault(chr,{})
snpMap[chr].setdefault(snp,{})
snpMap[chr][snp]['bp'] = bp
snpNu += 1
print 'Read {snpNu} SNPs from {file}'.format(**locals())
return snpMap
def getCol(name,line,file):
"""
Retrives the index of the column that contains queried term
"""
try:
col = line.index(name)
except ValueError:
sys.exit("File: {file} missing {name} value in the header line. STOPPING".format(**locals()))
return col
def checkMappingsFormat(file, line, i):
"""
Check if SNP mappings file is in the correct format.
"""
try:
snp = line[0]
chr = line[1]
bp = line[2]
if not isSnp(snp):
sys.exit('{file} wrong format, {snp} does not look like a SNP.'.format(**locals()))
elif not isChr(chr):
sys.exit("Line {i}, {file}: wrong format, {chr} does not match"\
" 'chrN' format. SNPs on chrom X cannot be tested."\
.format(**locals()))
elif not isBp(bp):
sys.exit("Line {i}, {file}: wrong format, {bp} does not look"\
" like a genomic position.".format(**locals()))
except IndexError:
print line
sys.exit('{file} missing value at line {i}, is file tab delimited?'.format(**locals()))
def find(pat, text):
foundMatch = []
match = re.search(pat, text)
if match:
foundMatch = match.group()
return foundMatch
def isHeader(snp):
snp = find('SNP',snp)
return snp
def isSnp(snp):
snp = find('[\w:-]+', snp)
return snp
def isChr(chr):
chr = find('chr\w+',chr) ##modif to include chrX
return chr
def isBp(bp):
bp = find('\d+',bp)
return bp
def isLine(line,file):
yey = find('^chr',line)
print yey, file
def startsChr(line):
yay = find('^chr\w+',line)
return yay