-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake_filter.py
More file actions
executable file
·98 lines (81 loc) · 2.44 KB
/
make_filter.py
File metadata and controls
executable file
·98 lines (81 loc) · 2.44 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
#!/usr/bin/python3
#
# make_filter.py - Creates filters for use with make_map.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_filter.py [Sample1] [Sample2] [Sample3] ... [SampleN]
import csv
import re
import sys
from collections import defaultdict
files = sys.argv[1:]
varlist = defaultdict(dict)
for filename in files:
infilename = filename
infile = open(infilename, 'r')
infilereader = csv.reader(infile, delimiter='\t')
for line in infilereader:
# skip comment lines
if re.search("^#", line[0]):
continue
rsid = line[0]
chrom = line[1]
pos = int(line[2])
genotype = line[3]
# for AncestryDNA, convert numbers to canonical:
# - 23==X, 24==Y, 25==Y, 26==MT
if chrom == "23":
chrom = "X"
elif (chrom == "24") or (chrom == "25"):
chrom = "Y"
elif chrom == "26":
chrom = "MT"
# replace chromosome id's with numbers for easy sorting
if chrom == "X":
chrom = 23
elif chrom == "Y":
chrom = 24
elif chrom == "MT":
chrom = 25
else:
chrom = int(chrom)
# if it's already on the list, skip
if (chrom in varlist) and (pos in varlist[chrom]):
continue
else:
varlist[chrom][pos] = [str(chrom), str(pos), rsid]
infile.close()
# output
print("#CHROM\tPOS\tRSID")
for chrom in sorted(varlist):
# chromosome 0 is showing up in output and don't know why
# probably some sort of array initialization issue
if chrom == 0:
continue
for pos in sorted(varlist[chrom]):
variation = varlist[chrom][pos]
# change the chromosome id's back to strings
chromnum = variation[0]
if chromnum == "23":
chromnum = "X"
elif chromnum == "24":
chromnum = "Y"
elif chromnum == "25":
chromnum = "MT"
variation[0] = chromnum
print('\t'.join(variation))