-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnps2phylip.py
67 lines (38 loc) · 1.91 KB
/
snps2phylip.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
#!/usr/bin/env python
import re
import argparse
import sys
def Get_Arguments():
parser = argparse.ArgumentParser(description="Converts pyRAD .snps file to phylip format")
parser.add_argument("-f", "--file", type=str, required=True, help="Input filename (.snps)")
parser.add_argument("-o", "--outfile", type=str, required=False,
help="Output filename; Default = out.phy", nargs="?", default="out.phy")
args = parser.parse_args()
return args
def read_snpsfile(infile):
check_if_exists(infile)
with open(infile, "r") as fin:
fin.readline()
samples = dict(line.rstrip().split(None, 1) for line in fin if not line.isspace())
return samples
def check_if_exists(filename):
try:
file = open(filename, "r")
except IOError:
print("\nError: The file " + filename + " does not exist.\n")
sys.exit(1)
def write_to_file(dictionary, file, val_length):
with open(file, "w") as fout:
fout.write(str(len(dictionary.keys())) + " " + str(val_length) + "\n")
for key, val in dictionary.iteritems():
fout.write(key + "\t" + val + "\n")
fout.write("\n")
################################################################################################################################
#####################################################MAIN#######################################################################
################################################################################################################################
args = Get_Arguments()
data = read_snpsfile(args.file)
for id, seq in data.iteritems():
data[id] = "".join(seq.split(" _ "))
seq_length = len(data[id])
write_to_file(data, args.outfile, seq_length)