-
Notifications
You must be signed in to change notification settings - Fork 15
/
geste2baypass.py
64 lines (49 loc) · 1.94 KB
/
geste2baypass.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
#!/usr/bin/python3
# Copyright 2016 Francisco Pina Martins <[email protected]>
# This file is part of geste2baypass.
# geste2baypass 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.
# geste2baypass 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 geste2baypass. If not, see <http://www.gnu.org/licenses/>.
#Usage: python3 geste2baypass.py file.geste file.BAYPASS
# Shamelessly stole code from https://github.com/Telpidus/omics_tools/blob/master/GESTE2BayEnv.py
from collections import OrderedDict
def parse_geste(infile_name): # Parses the file (Bayscan input)
"""
Parses a GESTE file and retuns a dict with:
{"SNP_num":["FreqAp1\tFreqBp1","FreqAp2\tFreqBp2",...]}.
"""
infile = open(infile_name, "r")
snp = OrderedDict()
for line in infile:
line = line.split()
# Neat trick to ignore data that is not SNP info
try:
int(line[0])
except (ValueError, IndexError):
continue
try:
snp[line[0]].append("\t".join(line[3:]))
except KeyError:
snp[line[0]] = ["\t".join(line[3:])]
infile.close()
return snp
def write_baypass(snp, baypass_filename):
"""
Write a Baypass inpt file based on the GESTE dict.
"""
outfile = open(baypass_filename, "w")
for snps in snp.values():
line = "\t".join(snps) + "\n"
outfile.write(line)
outfile.close()
if __name__ == "__main__":
from sys import argv
snp_dict = parse_geste(argv[1])
write_baypass(snp_dict, argv[2])