-
Notifications
You must be signed in to change notification settings - Fork 0
/
snp_plotter.py
executable file
·166 lines (148 loc) · 6.46 KB
/
snp_plotter.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
#!usr/bin/env python
import sys
import csv
import sqlite3
import os
import argparse
import dhsquery
def resolve_output_fp(input_fp):
output_fp = input_fp
if ('/' in output_fp):
while not (output_fp.endswith('/')):
output_fp = output_fp[:len(output_fp)-1]
output_fp = output_fp[:len(output_fp)-1]
else:
output_fp = ''
return output_fp
def prep_snps(input_fp):
snp_list = []
snp_done = []
if os.path.isfile(input_fp):
with open (input_fp, 'rb') as infile:
reader = csv.reader(infile, delimiter = '\t')
next(reader, None)
for line in reader:
snp = line[0]
if snp not in snp_done:
snp_done.append(snp)
snp_list.append(line)
return snp_list
def find_dhs(snp_list, dhsDB):
conn = sqlite3.connect(dhsDB)
conn.text_factory = str
cur = conn.cursor()
dhs_list = []
for snp_rec in snp_list:
snp = snp_rec[0]
snp_chr = 'chr'+snp_rec[1]
snp_pos = int(snp_rec[2])
dhs_data = []
print '\t Checking if '+ snp + ' lies in a DHS'
cur.execute("SELECT rowid, chr, start, end FROM dhs112 WHERE chr = ? \
AND start <= ? AND end >= ?", \
(snp_chr, snp_pos, snp_pos))
query_data = cur.fetchone()
if query_data is not None:
dhs_id = query_data[0]
dhs_start = int(query_data[2])
dhs_end = int(query_data[3])
dhs_center = dhs_start + ((dhs_end - dhs_start)/2)
snp_distance = snp_pos - dhs_center
dhs_list.append([snp, snp_chr, snp_pos, dhs_id, dhs_start, dhs_end, \
dhs_center, snp_distance])
else:
start = snp_pos
stop = snp_pos
for i in xrange(0, 400000):
start -= 250
stop += 250
cur.execute("SELECT rowid, chr, start, end FROM dhs112 \
WHERE chr = ? AND start >= ? AND end <= ?; ", \
(snp_chr, start, stop))
q_data = cur.fetchall()
if q_data is not None:
for dhs in q_data:
dhs_id = dhs[0]
dhs_start = int(dhs[2])
dhs_end = int(dhs[3])
dhs_center = dhs_start + ((dhs_end - dhs_start)/2)
snp_distance = snp_pos - dhs_center
dhs_list.append([snp, snp_chr, snp_pos, dhs_id, \
dhs_start, dhs_end, dhs_center, \
snp_distance])
break
with open(output_fp + '/snp_dhs_map.txt', 'wb') as out:
writer = csv.writer(out, delimiter = '\t')
writer.writerow(['SNP', 'SNP_CHR', 'SNP_POS', 'DHS_ID', 'DHS_START', \
'DHS_END', 'DHS_CENTER', 'SNP_DHS_DISTANCE'])
writer.writerows(dhs_list)
def get_dhs(snp_list, dhsDB):
conn = sqlite3.connect(dhsDB)
conn.text_factory = str
cur = conn.cursor()
dhs_list = []
for snp_rec in snp_list:
snp = snp_rec[0]
snp_chr = 'chr'+snp_rec[1]
snp_pos = int(snp_rec[2])
dhs_data = []
print '\t Checking if '+ snp + ' lies in a DHS'
dhs = list(dhsquery.get_snpDHS(snp, snp_chr, snp_pos, dhsDB))
if dhs[3] != 'NA':
dhs_start = int(dhs[4])
dhs_end = int(dhs[5])
dhs_center = dhs_start + ((dhs_end - dhs_start)/2)
snp_distance = snp_pos - dhs_center
dhs.append(snp_distance)
dhs_list.append(dhs)
else:
start = snp_pos
stop = snp_pos
for i in xrange(0, 400000):
start -= 250
stop += 250
cur.execute("SELECT rowid, chr, start, end FROM dhs112 \
WHERE chr = ? AND start >= ? AND end <= ?; ", \
(snp_chr, start, stop))
q_data = cur.fetchall()
if q_data is not None:
for dhs in q_data:
dhs_id = dhs[0]
dhs_start = int(dhs[2])
dhs_end = int(dhs[3])
dhs_center = dhs_start + ((dhs_end - dhs_start)/2)
snp_distance = snp_pos - dhs_center
cur.execute("SELECT chr, start, end, refined_cluster \
FROM dhsCluster LIMIT 1 OFFSET " \
+ str(dhs_id) + ";")
data = cur.fetchone()
cluster_id = data[3]
open_Samples = dhsquery.get_openSamples(cluster_id, dhsDB)
overlaps = dhsquery.get_overlaps(cluster_id, dhsDB)
motifs = dhsquery.get_motifs(cluster_id, dhsDB)
sig_dhsSamples = dhsquery.get_sampleDHS_signal(dhs_id, dhsDB)
snp_dhs = (snp, snp_chr, snp_pos, dhs_id, dhs_start, \
dhs_end, sig_dhsSamples[1], cluster_id, \
motifs[1], open_Samples[9], \
open_Samples[10], open_Samples[11], \
snp_distance)
dhs_list.append(snp_dhs)
break
with open(output_fp + '/snp_dhs.txt', 'wb') as out:
writer = csv.writer(out, delimiter = '\t')
writer.writerow(['SNP', 'SNP_CHR', 'SNP_POS', 'DHS_ID', 'DHS_START',\
'DHS_END', 'DHS_CELLTYPES', 'CLUSTER_ID', \
'TFs', 'MAX_SAMPLE', 'MAX_CELLTYPE', \
'MAX_TISSUE', 'SNP_DHS_DISTANCE'])
writer.writerows(dhs_list)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', required = True, \
help = 'Significant SNP-eQTL pairs from CoDeS3D')
parser.add_argument('-d', '--dhsDB', default = '/mnt/3dgenome/projects/' + \
'tfad334/dhs/dhs-mining/dhs.db')
args = parser.parse_args()
snps = prep_snps(args.input)
output_fp = resolve_output_fp(args.input)
get_dhs(snps, args.dhsDB)
find_dhs(snps, args.dhsDB)