~jon-hill/supertree-toolkit/visualisations

« back to all changes in this revision

Viewing changes to stk/scripts/plot_character_taxa_matrix.py

  • Committer: Jon Hill
  • Date: 2013-09-24 16:34:30 UTC
  • mfrom: (108.1.16 bug_fixes)
  • Revision ID: jon.hill@imperial.ac.uk-20130924163430-jcgij3mc90g66hnh
Fixing a number of bugs and adding comments to a lot of the schema

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import argparse
 
3
import os
 
4
import sys
 
5
stk_path = os.path.join( os.path.realpath(os.path.dirname(__file__)), os.pardir )
 
6
sys.path.insert(0, stk_path)
 
7
import supertree_toolkit as stk
 
8
from lxml import etree
 
9
from pylab import *
 
10
 
 
11
params = {
 
12
          'legend.fontsize': 25,
 
13
          'xtick.labelsize': 25,
 
14
          'ytick.labelsize': 25,
 
15
          'font.size' : 28,
 
16
          'axes.labelsize' : 32,
 
17
          'font.size' : 25,
 
18
          'text.fontsize' : 25,
 
19
          # need to do these to make room for labels
 
20
          # no labels, then comment these out
 
21
          'figure.subplot.top' : 0.75,
 
22
          'figure.subplot.right' : 0.75,
 
23
}
 
24
rcParams.update(params)
 
25
 
 
26
 
 
27
def main():
 
28
 
 
29
    # do stuff
 
30
    parser = argparse.ArgumentParser(
 
31
         prog="plot chracter taxa matrix",
 
32
         description="""Plot a matrix of character availability against taxa""",
 
33
         )
 
34
    parser.add_argument(
 
35
            '-v', 
 
36
            '--verbose', 
 
37
            action='store_true', 
 
38
            help="Verbose output: mainly progress reports.",
 
39
            default=False
 
40
            )
 
41
    parser.add_argument(
 
42
            'input_file', 
 
43
            metavar='input_file',
 
44
            nargs=1,
 
45
            help="Your pyhml"
 
46
            )
 
47
    parser.add_argument(
 
48
            'output_file', 
 
49
            metavar='output_file',
 
50
            nargs=1,
 
51
            help="The output graphics. .png, .pdf, or .svg"
 
52
            )
 
53
 
 
54
 
 
55
    args = parser.parse_args()
 
56
    verbose = args.verbose
 
57
    input_file = args.input_file[0]
 
58
    output_file = args.output_file[0]
 
59
 
 
60
    XML = stk.load_phyml(input_file)
 
61
    all_taxa = stk.get_all_taxa(XML)
 
62
    all_chars_d = stk.get_all_characters(XML)
 
63
    all_chars = []
 
64
    for c in all_chars_d:
 
65
        all_chars.extend(all_chars_d[c])
 
66
 
 
67
    taxa_character_matrix = {}
 
68
    for t in all_taxa:
 
69
        taxa_character_matrix[t] = []
 
70
 
 
71
    trees = stk.obtain_trees(XML)
 
72
    for t in trees:
 
73
        chars = stk.get_characters_from_tree(XML,t,sort=True)
 
74
        taxa = stk.get_taxa_from_tree(XML,t, sort=True)
 
75
        for taxon in taxa:
 
76
            taxa_character_matrix[taxon].extend(chars)
 
77
    
 
78
    for t in taxa_character_matrix:
 
79
        array = taxa_character_matrix[t]
 
80
        taxa_character_matrix[t] = list(set(array))
 
81
 
 
82
    # create a map
 
83
    x = []
 
84
    y = []
 
85
    for i in range(0,len(all_taxa)):
 
86
        for j in range(0,len(all_chars)):
 
87
            if (all_chars[j] in taxa_character_matrix[all_taxa[i]]):
 
88
                x.append(i)
 
89
                y.append(j)
 
90
 
 
91
    fig=figure(figsize=(22,17),dpi=90)
 
92
    fig.subplots_adjust(left=0.3)
 
93
    ax = fig.add_subplot(1,1,1)
 
94
    ax.scatter(x,y,50,marker='o',c='r',lw=0)
 
95
    yticks(range(0,len(all_chars)), all_chars)    
 
96
    ax.set_xlim(0,len(all_taxa))
 
97
    ax.set_ylim(0,len(all_chars))
 
98
    xlabel('Taxa')
 
99
    ylabel('Characters')
 
100
    savefig(output_file, dpi=90)
 
101
 
 
102
if __name__ == "__main__":
 
103
    main()