-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsubsample_alignment_phylogenetic.py
More file actions
executable file
·58 lines (38 loc) · 2.34 KB
/
subsample_alignment_phylogenetic.py
File metadata and controls
executable file
·58 lines (38 loc) · 2.34 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
#!/usr/bin/env python
'''Subsample an alignment using a tree.'''
from phyaln import Alignment, PhylogeneticSubsampler
def read_rate_file(f):
with open(f, 'r') as infile:
rates = {}
for parts in (l.split('=') for l in infile):
if len(parts) == 2:
name, rate = (p.strip() for p in parts)
rates[name] = float(rate)
return rates
if __name__ == '__main__':
import argparse, newick3
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-a', '--alignment', type=open, required=True, \
help='the location of the extended phylip alignment file to be subsampled.')
parser.add_argument('-r', '--rates', type=read_rate_file, required=True, \
help='the evolutionary rates (substitution/time) underlying the models for the partitions.')
parser.add_argument('-t', '--tree', type=open, required=True, \
help='the tree.')
parser.add_argument('-q', '--partitions', type=open, required=True, \
help='the location of the raxml partitions file corresponding to the alignment to be subsampled.')
parser.add_argument('-x', '--random-seed', type=int, required=False, \
help='an integer seed for the random number generator function')
parser.add_argument('-f', '--reduction-factor', type=float, required=False, \
help='a decimal value specifying how sparse to make the subsampling. the number of taxa that will be subsampled will be reduced proportionally to this value, thus a value of f 0.5 leads to a reduction by (approximately) half')
parser.add_argument('-n', '--output-label', required=False, default='', \
help='a label to be attached to output files')
args = parser.parse_args()
a = Alignment(args.alignment, args.partitions)
t = newick3.parse(args.tree)
f = args.reduction_factor if 'reduction_factor' in args else 1
s = PhylogeneticSubsampler(alignment=a, tree=t, rates=args.rates, reduction_factor=f)
s.subsample()
s.write_subsampled_output(args.output_label)
s.report_sampled_partitions()
print('files have been written to:\n' + s.output_label + '.sampling_matrix.txt\n' + s.output_label + '.phy\n' + s.output_label + '.partitions.txt\n' \
'sampling proportion is ' + str(s.get_sampling_proportion()))