-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmake_map.py
More file actions
executable file
·124 lines (99 loc) · 3.28 KB
/
Copy pathmake_map.py
File metadata and controls
executable file
·124 lines (99 loc) · 3.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
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
#!/usr/bin/python3
#
# make_map.py - Creates filters for use with make_template.py
# Copyright (C) 2017 Philip Baltar (psbaltar@gmail.com)
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# make_map.py [outputMapFile] [filterFile] [snpListVCF]
import csv
import re
import sys
from collections import defaultdict
import tabix
import gzip
from vcflib import *
mapfilename = sys.argv[1]
infilename = sys.argv[2]
reffilename = sys.argv[3]
infile = open(infilename, 'r')
infilereader = csv.reader(infile, delimiter='\t')
tb = tabix.open(reffilename)
mapfile = open(mapfilename, 'w')
mapfile.write("#CHROM\tPOS\tRSID\tREFCHROM\tREFPOS\tREFRSID\n")
for line in infilereader:
if re.search("^#", line[0]):
continue
chrom = line[0]
pos = line[1]
rsid = line[2]
inputline = '\t'.join([chrom, pos, rsid])
query = chrom + ":" + pos + "-" + pos
recorditer = tb.querys(query)
# convert iterator to actual array of records
records = []
for record in recorditer:
records.append(record)
refline = '\t'.join(['.','.','.'])
if len(records) == 1:
#do stuff for single match
record = records[0]
qchrom = record[0]
qpos = record[1]
qrsid = record[2]
if (rsid == qrsid) or (getRSPOS(record) == pos):
refline = '\t'.join([qchrom, qpos, qrsid])
if '.' in refline:
lowerpos = str(int(pos) - 100)
upperpos = str(int(pos) + 100)
query = chrom + ':' + lowerpos + '-' + upperpos
recorditer = tb.querys(query)
# convert iterator to actual array of records
records = []
for record in recorditer:
records.append(record)
# look for matching RSID and RSPOS
filteredRecords = records
if len(filteredRecords) > 1 and re.search("^rs", rsid):
rsidMatched = getIDmatches(rsid, filteredRecords)
if len(rsidMatched) > 0:
filteredRecords = rsidMatched
if len(filteredRecords) > 1:
filteredRecords = getRSPOSmatches(pos, filteredRecords)
if len(filteredRecords) > 1 and not re.search("^rs", rsid):
# find lowest rsid in filteredRecords
lrsidrecord = []
for record in filteredRecords:
qrsidnum = int(record[2][2:])
if len(lrsidrecord) == 0:
lrsidnum = sys.maxsize
else:
lrsidnum = int(lrsidrecord[2][2:])
if qrsidnum < lrsidnum:
lrsidrecord = record
filteredRecords = [lrsidrecord]
if len(filteredRecords) == 1:
record = filteredRecords[0]
qchrom = record[0]
qpos = record[1]
qrsid = record[2]
refline = '\t'.join([qchrom, qpos, qrsid])
else: # not a match, so skip
mapfile.write(inputline + '\t' + refline + '\n')
continue
mapfile.write(inputline + '\t' + refline + '\n')
infile.close()
mapfile.close()