-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfilter_phylip.py
More file actions
executable file
·55 lines (39 loc) · 1.44 KB
/
filter_phylip.py
File metadata and controls
executable file
·55 lines (39 loc) · 1.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
#!/usr/bin/env python
"""Filters a phylip alignment; can accept *either* a set of names to accepted or a set to be excluded, and saves the output to another file"""
_title = 'Filter a phylip alignment'
if __name__ == "__main__":
import os
import sys
if len(sys.argv) < 4:
print "usage: filter_phylip.py <infile> [excluded=<excludedtaxafile> | accepted=<acceptednamesfile>] <outfile>"
sys.exit(0)
infile = open(sys.argv[1],"r")
outfile = open(sys.argv[3],"w")
listtype, namesfile = sys.argv[2].split("=")
if listtype == "excluded" or listtype == "accepted":
names = [n.strip() for n in open(namesfile,"r").readlines()]
else:
print "unrecognized type for names list; please use 'excluded' or 'accepted'"
sys.exit(0)
while True:
testline = infile.readline()
try:
ntax, nsites = [int(n) for n in testline.split()]
break
except IndexError:
continue
saved = list()
ntax = 0
for name, seq in [line.split() for line in infile]:
if listtype == "excluded":
if name in names:
continue
elif listtype == "accepted":
if name not in names:
continue
saved.append(name + " " + seq + "\n")
ntax += 1
outfile.write(str(ntax) + " " + str(nsites) + "\n")
for line in saved:
outfile.write(line);
outfile.close()