-
Notifications
You must be signed in to change notification settings - Fork 15
/
loci2phy.py
143 lines (110 loc) · 4.23 KB
/
loci2phy.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
#!/usr/bin/python3
# Copyright 2015 Francisco Pina Martins <f.pinamartins@gmail.com>
# This file is part of loci2phy.
# loci2phy 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.
# loci2phy 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 loci2phy. If not, see <http://www.gnu.org/licenses/>.
# Usage: python3 loci2phy.py file.vcf file.loci file.phy
def vcf_parser(vcf_filename):
"""Parses a VCF file and returns a dict with loci names and a sortd list
with taxa names."""
vcf = open(vcf_filename, 'r')
loci = []
seqnames = {}
for line in vcf:
if line.startswith("##"):
pass
elif line.startswith("#CHROM"):
for names in line.split()[9:]:
seqnames[names] = ""
else:
loci.append(line.split()[0])
vcf.close()
loci = sorted(list(set(loci)))
return loci, seqnames
def loci_parser(loci_filename, loci, seqnames):
"""Gets a loci list, a .loci file and sequence names and filters the .loci
file according to the loci list. Returns a dict {seqname: sequence}"""
loci_file = open(loci_filename, 'r')
if loci[0] == "1":
gather_stuff = 1
else:
gather_stuff = 0
seqlen = 0
totlen = 0
locus_number = 1
vcfseqs = set(seqnames.keys())
taxaset = set()
seqlines= " "
seqlens = [0]
for lines in loci_file:
if gather_stuff == 1 and lines.startswith("//") == False:
seqlines = lines.strip(">\n").split()
if seqlines[0] in seqnames:
seqnames[seqlines[0]] += seqlines[1]
taxaset.add(seqlines[0])
elif lines.startswith("//") and gather_stuff == 1:
seqlen = len(seqlines[1])
totlen += seqlen
seqlens.append(totlen)
difset = vcfseqs.difference(taxaset)
for t in difset:
seqnames[t] += "N" * seqlen
taxaset = set()
gather_stuff = 0
try:
if str(int(lines[lines.find("|"):]) + 1) in loci:
gather_stuff = 1
except:
locus_number += 1
if str(locus_number) in loci:
gather_stuff = 1
elif gather_stuff == 0 and lines.startswith("//"):
try:
if str(int(lines[lines.find("|"):]) + 1) in loci:
gather_stuff = 1
except:
locus_number += 1
if str(locus_number) in loci:
gather_stuff = 1
loci_file.close()
return seqnames, seqlens
def phy_writer(phy_filename, seqnames, seqlens):
"""Writes the output ready to submit to RAxML or other phylogeny
programs, along with a partitions file. Based on seqnames dict
{seqname: sequence} and on [seqlens]"""
phy = open(phy_filename, 'w')
seqnum = len(seqnames)
bpnum = len(list(seqnames.values())[0])
phy.write(str(seqnum) + " " + str(bpnum) + "\n")
for k, v in seqnames.items():
phy.write(k + "\t" + v + "\n")
phy.close()
part = open(phy_filename[:-4] + ".part", 'w')
for counts in list(range(len(seqlens)))[:-1]:
part.write("DNA, p" + str(counts + 1) + "=" + str(seqlens[counts] + 1) + "-" + str(seqlens[counts + 1]) + "\n")
part.close()
def fas_writer(fas_filename, seqnames):
"""
Writes the output in fasta format. Arguments as in phy_writer function
"""
fh = open(fas_filename, "w")
for k, v in seqnames.items():
fh.write(">{}\n{}\n".format(k, v))
fh.close()
if __name__ == "__main__":
from sys import argv
loci, seqnames = vcf_parser(argv[1])
seqnames, seqlens = loci_parser(argv[2], loci, seqnames)
try:
if argv[4] == "-f":
fas_writer(argv[3], seqnames)
except IndexError:
phy_writer(argv[3], seqnames, seqlens)