Mercurial > repos > boris > phylorelatives
comparison phylorelatives.py @ 31:75dcc9910f6d draft
Uploaded
author | boris |
---|---|
date | Mon, 10 Jun 2013 00:43:11 -0400 |
parents | 29ca7d28f6c7 |
children | 8d663949bdc8 |
comparison
equal
deleted
inserted
replaced
30:4d9f822d3488 | 31:75dcc9910f6d |
---|---|
2 # | 2 # |
3 # Boris Rebolledo-Jaramillo (boris-at-bx.psu.edu) | 3 # Boris Rebolledo-Jaramillo (boris-at-bx.psu.edu) |
4 #usage: phylorelatives.py [-h] [-i FASTA] [-b INT] [-p] [-r FASTA] | 4 #usage: phylorelatives.py [-h] [-i FASTA] [-b INT] [-p] [-r FASTA] |
5 # | 5 # |
6 #Constructs relatedness of a set of sequences based on the pairwise proportion | 6 #Constructs relatedness of a set of sequences based on the pairwise proportion |
7 #of different sites. It reports the test sequences relatives, tree plots and | 7 #of different sites. It reports the test sequences relatives, tree plot and |
8 #trees in Newick format. One or more test sequences are accepted as long as | 8 #tree in Newick format. One or more test sequences are accepted as long as |
9 #their name includes the strict suffix "_minor" or "_test" (i.e. >seq1_minor). | 9 #their name includes the strict suffix "_minor" or "_test" (i.e. >seq1_minor). |
10 #IMPORTANT: Sequences must have the same length! | 10 #IMPORTANT: Sequences must have the same length! |
11 # | 11 # |
12 #optional arguments: | 12 #optional arguments: |
13 # -h, --help show this help message and exit | 13 # -h, --help show this help message and exit |
55 B=iters, quiet=1, trees=1) | 55 B=iters, quiet=1, trees=1) |
56 bs_trees = bootstrap.rx('trees')[0] | 56 bs_trees = bootstrap.rx('trees')[0] |
57 cons_tree = ape.consensus(bs_trees,p=0.5) | 57 cons_tree = ape.consensus(bs_trees,p=0.5) |
58 return cons_tree | 58 return cons_tree |
59 | 59 |
60 #def parsimony_tree(tree,data): | |
61 # """Return Maximum Parsimony tree from supplied tree""" | |
62 # phangorn = importr('phangorn') | |
63 # mp_tree = phangorn.optim_parsimony(tree,phangorn.phyDat(data), trace=0) | |
64 # return mp_tree | |
65 | |
66 | 60 |
67 def dendro_relatives(tree,minor): | 61 def dendro_relatives(tree,minor): |
68 """Return minor allele sequence relatives in tree""" | 62 """Return minor allele sequence relatives in tree""" |
69 ape = importr('ape') | 63 ape = importr('ape') |
70 newick = list(ape.write_tree(tree))[0] | 64 newick = list(ape.write_tree(tree))[0] |
77 output = [relative.get_node_str() for relative in parent.leaf_nodes()] | 71 output = [relative.get_node_str() for relative in parent.leaf_nodes()] |
78 relatives = [relative for relative in output if not (relative.endswith('minor') or relative.endswith('test'))] | 72 relatives = [relative for relative in output if not (relative.endswith('minor') or relative.endswith('test'))] |
79 parent = parent.parent_node | 73 parent = parent.parent_node |
80 return output | 74 return output |
81 | 75 |
82 def plot_tree(outfile, tree1, tree2=None, root=False): | 76 |
83 """Generate tree(s) plot""" | 77 def dendro_plot(tree, root=False ): |
78 """Plot tree to file in ascii format""" | |
84 ape = importr('ape') | 79 ape = importr('ape') |
85 graphics = importr('graphics') | 80 if root: |
86 grdevices = importr('grDevices') | 81 newick = list(ape.write_tree(ape.root(tree,root)))[0] |
87 if tree2 is None: | 82 else: |
88 grdevices.png(file=outfile, width=1024, height=768) | 83 newick = list(ape.write_tree(tree))[0] |
89 if root: | 84 t = dendropy.Tree.get_from_string(newick,"newick") |
90 ape.plot_phylo(ape.root(tree1,root),edge_width=2,cex=1,underscore=1) | 85 ascii_tree = t.as_ascii_plot() |
91 else: | 86 return ascii_tree |
92 ape.plot_phylo(tree1,edge_width=2,cex=1,underscore=1) | 87 |
93 graphics.title(main='Neighbor Joining') | |
94 grdevices.dev_off() | |
95 elif tree2 is not None: | |
96 grdevices.png(file=outfile, width=1024, height=768) | |
97 graphics.par(mfcol=array.array('i',[1,2])) | |
98 if root: | |
99 ape.plot_phylo(ape.root(tree1,root),edge_width=2,cex=1,underscore=1) | |
100 else: | |
101 ape.plot_phylo(tree1,edge_width=2,cex=1,underscore=1) | |
102 graphics.title(main='Neighbor Joining', cex=1.5, font=2) | |
103 if root: | |
104 ape.plot_phylo(ape.root(tree2,root),edge_width=2,cex=1,underscore=1) | |
105 else: | |
106 ape.plot_phylo(tree2,edge_width=2,cex=1,underscore=1) | |
107 graphics.title(main='Maximum Parsimony',cex=1.5, font=2) | |
108 grdevices.dev_off() | |
109 return | |
110 | 88 |
111 def write_nwk(tree): | 89 def write_nwk(tree): |
112 "Write proper Newick string" | 90 "Write proper Newick string" |
113 ape = importr('ape') | 91 ape = importr('ape') |
114 nwk = list(ape.write_tree(tree))[0] | 92 nwk = list(ape.write_tree(tree))[0] |
123 parser.add_argument('-r', '--root', type=str, metavar='FASTA', default=False, help='Root trees using FASTA sequence as outgroup. (Default: Display unrooted trees)') | 101 parser.add_argument('-r', '--root', type=str, metavar='FASTA', default=False, help='Root trees using FASTA sequence as outgroup. (Default: Display unrooted trees)') |
124 parser.add_argument('--relatives-out', type=str, metavar='FILE', default=None, help=argparse.SUPPRESS) | 102 parser.add_argument('--relatives-out', type=str, metavar='FILE', default=None, help=argparse.SUPPRESS) |
125 parser.add_argument('--newick-out', type=str, metavar='FILE', default=None, help=argparse.SUPPRESS) | 103 parser.add_argument('--newick-out', type=str, metavar='FILE', default=None, help=argparse.SUPPRESS) |
126 parser.add_argument('--trees-out', type=str, metavar='FILE', default=None, help=argparse.SUPPRESS) | 104 parser.add_argument('--trees-out', type=str, metavar='FILE', default=None, help=argparse.SUPPRESS) |
127 parser.add_argument('-m', '--multi-fasta', type=str, default='multi-fasta.fa', help=argparse.SUPPRESS) | 105 parser.add_argument('-m', '--multi-fasta', type=str, default='multi-fasta.fa', help=argparse.SUPPRESS) |
128 args = parser.parse_args() | 106 args = parser.parse_args('-i multi.fa -b 0 -r RSRS.fasta'.split()) |
129 | 107 |
130 | 108 |
131 if args.input: | 109 if args.input: |
132 for fasta in args.input: | 110 for fasta in args.input: |
133 try: | 111 try: |
192 if args.newick_out is not None: | 170 if args.newick_out is not None: |
193 newick = open(args.newick_out,'w+') | 171 newick = open(args.newick_out,'w+') |
194 else: | 172 else: |
195 newick = open(args.multi_fasta+'-newick.txt','w+') | 173 newick = open(args.multi_fasta+'-newick.txt','w+') |
196 if args.trees_out is not None: | 174 if args.trees_out is not None: |
197 tree_plot_file = args.trees_out | 175 tree_plot_file = open(args.trees_out, 'w+') |
198 else: | 176 else: |
199 tree_plot_file = args.multi_fasta+'-tree.png' | 177 tree_plot_file = open(args.multi_fasta+'-tree-ascii.txt', 'w+') |
200 | 178 |
201 newick.write('>Neighbor_Joining\n%s\n' % (write_nwk(cons_tree))) | 179 newick.write('%s\n' % (write_nwk(cons_tree))) |
202 relatives.write('#source\tsample\trelatives\n') | 180 relatives.write('#source\tsample\trelatives\n') |
203 for node in minor_ids: | 181 for node in minor_ids: |
204 nj_relatives = [relative for relative in dendro_relatives(cons_tree,node) if relative != node] | 182 nj_relatives = [relative for relative in dendro_relatives(cons_tree,node) if relative != node] |
205 relatives.write( 'Neighbor_Joining_tree\t%s\t%s\n' % (node,','.join(sorted(nj_relatives))) ) | 183 relatives.write( 'Neighbor_Joining_tree\t%s\t%s\n' % (node,','.join(sorted(nj_relatives))) ) |
206 plot_tree(tree_plot_file,cons_tree,root=root_id) | |
207 | 184 |
208 | 185 tree_plot_file.write(dendro_plot(cons_tree,root=root_id)) |
209 # Parsimony requires X11 related dependencies. Tricky | |
210 # if args.max_parsimony: | |
211 # mp_tree = parsimony_tree(cons_tree,data) | |
212 # newick.write('>Maximum_Parsimony\n%s\n' % (write_nwk(mp_tree))) | |
213 # for node in minor_ids: | |
214 # mp_relatives = [relative for relative in dendro_leaves(mp_tree,node) if relative != node] | |
215 # relatives.write( 'Maximum_Parsimony_tree\t%s\t%s\n' % (node,','.join(sorted(mp_relatives))) ) | |
216 # plot_tree(tree_plot_file, tree1=cons_tree, tree2=mp_tree, root=args.root) | |
217 # else: | |
218 # plot_tree(tree_plot_file,cons_tree,root=args.root) | |
219 | 186 |
220 newick.close() | 187 newick.close() |
221 relatives.close() | 188 relatives.close() |
222 | 189 |
223 if __name__ == "__main__": main() | 190 if __name__ == "__main__": main() |