~ubuntu-branches/ubuntu/trusty/qiime/trusty

« back to all changes in this revision

Viewing changes to .pc/fix_path_for_support_files/scripts/plot_taxa_summary.py

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2013-06-17 18:28:26 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130617182826-376az5ad080a0sfe
Tags: 1.7.0+dfsg-1
Upload preparations done for BioLinux to Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# File created on 19 Jan 2011
3
 
from __future__ import division
4
 
 
5
 
__author__ = "Jesse Stombaugh"
6
 
__copyright__ = "Copyright 2011, The QIIME project"
7
 
__credits__ = ["Jesse Stombaugh","Julia Goodrich", "Justin Kuczynski"]
8
 
__license__ = "GPL"
9
 
__version__ = "1.5.0"
10
 
__maintainer__ = "Jesse Stombaugh"
11
 
__email__ = "jesse.stombaugh@colorado.edu"
12
 
__status__ = "Release"
13
 
"""
14
 
This script generates taxonomy charts
15
 
"""
16
 
 
17
 
from qiime.util import parse_command_line_parameters, get_qiime_project_dir
18
 
from qiime.util import make_option
19
 
from qiime.plot_taxa_summary import make_all_charts
20
 
from cogent.util.misc import get_random_directory_name
21
 
from qiime.colors import taxonomy_color_prefs_and_map_data_from_options
22
 
import re
23
 
import matplotlib
24
 
import os
25
 
import shutil
26
 
 
27
 
plot_filetype_choices = ['pdf','svg','png']
28
 
 
29
 
script_info={}
30
 
script_info['brief_description']="""Make taxaonomy summary charts based on taxonomy assignment"""
31
 
script_info['script_description']="""This script automates the construction of pie, bar and area charts showing the breakdown of taxonomy by given levels. The script creates an html file for each chart type for easy visualization. It uses the taxonomy or category counts from summarize_taxa.py for combined samples by level (-i) and user specified labels for each file passed in (-l). Output will be written to the user specified folder (-o) the, where the default is the current working directory. The user can also specify the number of categories displayed for within a single pie chart, where the rest are grouped together as the 'other category' using the (-n) option, default is 20.
32
 
"""
33
 
script_info['script_usage']=[]
34
 
script_info['script_usage'].append(("""Examples:""","""If you wish to run the code using default parameters, you must supply a counts file (Class.txt) along with the taxon level label (Class) and the type(s) of chart, by using the following command:""","""plot_taxa_summary.py -i Class.txt -l Class -c pie,bar,area"""))
35
 
script_info['script_usage'].append(("""""","""If you want to make charts for multiple levels at a time (phylum.txt,class.txt,genus.txt) use the following command:""","""plot_taxa_summary.py -i phylum.txt,class.txt,genus.txt -l phylum,class,genus -c pie,bar,area"""))
36
 
script_info['script_usage'].append(("""""","""If you want specify an output directory (e.g. "output_charts/", regardless of whether the directory exists, use the following command:""","""plot_taxa_summary.py -i Class.txt -l Class -c pie,bar,area -o output_charts/"""))
37
 
script_info['script_usage'].append(("""""","""Additionally, if you would like to display on a set number of taxa ("-n 10") in the pie charts, you can use the following command:""","""plot_taxa_summary.py -i Class.txt -l Class -c pie -o pie_charts/ -n 10"""))
38
 
script_info['script_usage'].append(("""""","""If you would like to display generate pie charts for samples samples: 'sample1' and 'sample2' that are in the counts file header, you can use the following command:""","""plot_taxa_summary.py -i Class.txt -l Class -o pie_charts/ -b sample1,sample2"""))
39
 
script_info['output_description']="""The script generates an output folder, which contains several files. For each pie chart there is a png and a pdf file. The best way to view all of the pie charts is by opening up the file taxonomy_summary_pie_chart.html."""
40
 
 
41
 
script_info['required_options']=[\
42
 
    ### dest should equal long-form parameter names! Can you clean this up?
43
 
    ### Also note that you don't need to pass type='string' - that's the default
44
 
    make_option('-i', '--counts_fname',
45
 
        help='Input comma-separated list of summarized taxa filepaths' +\
46
 
        ' (i.e results from summarize_taxa.py) [REQUIRED]',
47
 
        type='existing_filepaths'),
48
 
]
49
 
script_info['optional_options']=[\
50
 
    # changed this from type='string' (default) to type='int'
51
 
    make_option('-l', '--labels',
52
 
        help='Comma-separated list of taxonomic levels (e.g.' +\
53
 
        ' Phylum,Class,Order)  [default=%default]',default=None),
54
 
    make_option('-n', '--num_categories', dest='num_categories', \
55
 
        help='The maximum number of taxonomies to show in each pie chart.' +\
56
 
        ' All additional taxonomies are grouped into an "other" category.' +\
57
 
        ' NOTE: this functionality only applies to the pie charts.' +\
58
 
        ' [default: %default]', default=20,type='int'),
59
 
    make_option('-o', '--dir_path',
60
 
        help='Output directory',
61
 
        type='new_dirpath'),
62
 
    make_option('-b', '--colorby', dest='colorby',\
63
 
        help='This is the categories to color by in the plots from the' +\
64
 
        ' metadata mapping file. The categories must match the name of a ' +\
65
 
        ' column header in the mapping file exactly and multiple categories' +\
66
 
        ' can be list by comma separating them without spaces.' +\
67
 
        ' [default=%default]'),
68
 
    make_option('-p', '--prefs_path',
69
 
        help='Input user-generated preferences filepath. NOTE: This is a' +\
70
 
        ' file with a dictionary containing preferences for the analysis.' +\
71
 
        ' The key taxonomy_coloring is used for the coloring.' +\
72
 
        ' [default: %default]',
73
 
        type='existing_filepath'),
74
 
    make_option('-k', '--background_color',
75
 
        help='This is the background color to use in the plots' +\
76
 
        ' (black or white) [default: %default]',default='white',
77
 
        type='choice',choices=['black','white'],),
78
 
    make_option('-d', '--dpi',
79
 
        help='This is the resolution of the plot. [default: %default]', 
80
 
        type='int',default=80),
81
 
    make_option('-x', '--x_width',
82
 
        help='This is the width of the x-axis to use in the plots.' +\
83
 
        ' [default: %default]',default=12,type=int),
84
 
    make_option('-y', '--y_height',
85
 
        help='This is the height of the y-axis to use in the plots.' +\
86
 
        ' [default: %default]',default=6,type=int),
87
 
    make_option('-w', '--bar_width',
88
 
        help='This the width of the bars in the bar graph and should be a' +\
89
 
        ' number between 0 and 1. NOTE: this only applies to the bar charts.' +\
90
 
        ' [default: %default]', default=0.75,type='float'),
91
 
    make_option('-t', '--type_of_file',type='choice',
92
 
        help='This is the type of image to produce (i.e. ' +\
93
 
        ','.join(plot_filetype_choices) + '). [default: %default]',
94
 
        choices=plot_filetype_choices,default='pdf'),
95
 
    make_option('-c', '--chart_type',
96
 
         help='This is the type of chart to plot (i.e. pie, bar or area).' +\
97
 
         ' The user has the ability to plot multiple types, by using a' +\
98
 
         ' comma-separated list (e.g. area,pie) [default: %default]',
99
 
         default='area,bar'),
100
 
    make_option('-r', '--resize_nth_label', type='int',\
101
 
        help='Make every nth label larger than the other lables.' +\
102
 
        ' This is for large area and bar charts where the font on the x-axis' +\
103
 
        ' is small. This requires an integer value greater than 0.' +\
104
 
        ' [default: %default]',default=0),
105
 
    make_option('-s', '--include_html_legend', action='store_true',\
106
 
        dest='include_html_legend', default=False, \
107
 
        help='Include HTML legend. If present, the writing of the legend' +\
108
 
        ' in the html page is included. [default: %default]'),
109
 
    make_option('-m', '--include_html_counts', action='store_true',\
110
 
        dest='include_html_counts', default=False, \
111
 
        help='Include HTML counts. If present, the writing of the counts' +\
112
 
        ' in the html table is included [default: %default]'),
113
 
    make_option('-a', '--label_type',type='choice',
114
 
        help='Label type ("numeric" or "categorical"). '+\
115
 
        ' If the label type is defined as numeric, the x-axis will be' +\
116
 
        ' scaled accordingly. Otherwise the x-values will treated' +\
117
 
        ' categorically and be evenly spaced [default: %default].', 
118
 
        choices=['categorical','numeric'],default='categorical'),
119
 
]
120
 
script_info['option_label']={'counts_fname':'Summarized taxa filepaths',
121
 
                             'labels':'Taxonomic levels',
122
 
                             'num_categories': '# of categories to retain',
123
 
                             'map_fname':'QIIME-formatted mapping filepath',
124
 
                             'colorby': 'Colorby category',
125
 
                             'dir-prefix': 'Output directory',
126
 
                             'prefs_path': 'Preferences filepath',
127
 
                             'background_color': 'Background color',
128
 
                             'type_of_file': 'Image type',
129
 
                             'dpi':'Image resolution',
130
 
                             'x_width': 'X-axis width',
131
 
                             'y_height': 'Y-axis height',
132
 
                             'bar_width':'Bar width',
133
 
                             'chart_type': 'Chart type(s)',
134
 
                             'resize_nth_label':'Resize nth label',
135
 
                             'include_html_legend':'Include HTML legend',
136
 
                             'include_html_counts':'Include HTML counts',
137
 
                             'label_type': 'X-axis label type'}
138
 
 
139
 
script_info['version']=__version__
140
 
 
141
 
def main():
142
 
    option_parser, opts, args = parse_command_line_parameters(**script_info)
143
 
    
144
 
    #Check the version of Matplotlib
145
 
    matplotlib_version = re.split("[^\d]", matplotlib.__version__)
146
 
    matplotlib_version_info = tuple([int(i) for i in matplotlib_version if \
147
 
                            i.isdigit()])
148
 
 
149
 
    if matplotlib_version_info != (1,1,0):
150
 
        print "This code was only tested with Matplotlib-1.1.0"
151
 
 
152
 
    #get QIIME directory
153
 
    qiime_dir=get_qiime_project_dir()
154
 
 
155
 
    if not opts.counts_fname:
156
 
        option_parser.error("A list of input files must be specified")
157
 
 
158
 
    #get color preferences
159
 
    color_prefs, color_data, background_color, label_color= \
160
 
                   taxonomy_color_prefs_and_map_data_from_options(opts)
161
 
    
162
 
    colorby = opts.colorby
163
 
    if colorby==None:
164
 
        colorby=[]
165
 
        for c in color_data['counts'].values():
166
 
            colorby.extend(c[0])
167
 
    else:
168
 
        colorby=colorby.strip().strip("'").split(',')
169
 
    
170
 
    counts_fname = opts.counts_fname
171
 
 
172
 
    #Define labels to use
173
 
    labels = opts.labels
174
 
    
175
 
    if not opts.labels:
176
 
        new_labels=[]
177
 
        #create an empty list since the user didn't specify labels
178
 
        for i in counts_fname:
179
 
            new_labels.append("")
180
 
        labels=','.join(new_labels)
181
 
        
182
 
    data = [(label,f.strip()) \
183
 
        for f,label in zip(counts_fname,labels.split(","))]
184
 
    filepath=data[0][1]
185
 
    
186
 
    filename=filepath.strip().rpartition('/')[0]
187
 
    num_categories = int(opts.num_categories)
188
 
    if num_categories<=0:
189
 
        raise ValueError, 'The number of categories has to be greater than 0!'
190
 
 
191
 
    #create directory path
192
 
    if opts.dir_path:
193
 
        if os.path.exists(opts.dir_path):
194
 
            dir_path=opts.dir_path
195
 
        else:
196
 
            try:
197
 
                os.mkdir(opts.dir_path)
198
 
                dir_path=opts.dir_path
199
 
            except OSError:
200
 
                pass
201
 
    else:
202
 
        dir_path='./'
203
 
        
204
 
    if dir_path == './':
205
 
        dir_path = os.getcwd()
206
 
 
207
 
    #make javascript output directory
208
 
    javascript_path = os.path.join(dir_path,'js')
209
 
    try:
210
 
        os.mkdir(javascript_path)
211
 
    except OSError: #raised if dir exists
212
 
        pass
213
 
        
214
 
    #make raw_data output directory
215
 
    raw_data_path = os.path.join(dir_path,'raw_data')
216
 
    try:
217
 
        os.mkdir(raw_data_path)
218
 
    except OSError: #raised if dir exists
219
 
        pass
220
 
        
221
 
    # move javascript file to javascript output directory
222
 
    shutil.copyfile(os.path.join(qiime_dir,'qiime','support_files',\
223
 
                    'js/overlib.js'),\
224
 
                    os.path.join(javascript_path,'overlib.js'))
225
 
 
226
 
    #make css output directory
227
 
    css_path = os.path.join(dir_path,'css')
228
 
    try:
229
 
        os.mkdir(css_path)
230
 
    except OSError: #raised if dir exists
231
 
        pass
232
 
        
233
 
    # move css file to css output directory
234
 
    shutil.copyfile(os.path.join(qiime_dir,'qiime','support_files',\
235
 
                    'css/qiime_style.css'),\
236
 
                    os.path.join(css_path,'qiime_style.css'))
237
 
 
238
 
    # verify all parameters are valid
239
 
    plot_width=float(opts.x_width)
240
 
    if plot_width<=0:
241
 
        raise ValueError, 'The width of the plot has to be greater than 0!'
242
 
    
243
 
    plot_height=float(opts.y_height)
244
 
    if plot_height<=0:
245
 
        raise ValueError, 'The height of the plot has to be greater than 0!'
246
 
    
247
 
    bar_width=float(opts.bar_width)
248
 
    if bar_width<=0 or bar_width>1:
249
 
        raise ValueError, 'The bar width of the plot has to be between 0 and 1!'
250
 
 
251
 
    dpi=float(opts.dpi)    
252
 
    if dpi<=0:
253
 
        raise ValueError, 'The dpi of the plot has to be greater than 0!'
254
 
    
255
 
    resize_nth_label=int(opts.resize_nth_label)
256
 
    if resize_nth_label<0:
257
 
        raise ValueError, 'The resize_nth_label of the plot has to be greater than 0!'
258
 
    
259
 
    generate_image_type=opts.type_of_file
260
 
    label_type=opts.label_type
261
 
    include_html_legend=opts.include_html_legend
262
 
    include_html_counts=opts.include_html_counts  
263
 
    plots_to_make=opts.chart_type.split(',')
264
 
    chart_types=['area','pie','bar']
265
 
    for i in plots_to_make:
266
 
        chart_type=i.lower().strip()
267
 
        if chart_type not in chart_types:
268
 
            raise ValueError, 'Please type in one of the appropriate chart types (i.e. %s)!' % ','.join(chart_types) 
269
 
            
270
 
        #make pie chart output path
271
 
        charts_path = os.path.join(dir_path,'charts')
272
 
        try:
273
 
            os.mkdir(charts_path)
274
 
        except OSError: #raised if dir exists
275
 
            pass
276
 
        
277
 
        make_all_charts(data,dir_path,filename,num_categories, \
278
 
        colorby,args,color_data, color_prefs,background_color,label_color,\
279
 
        chart_type,generate_image_type,plot_width,plot_height,bar_width,dpi,\
280
 
        resize_nth_label,label_type,include_html_legend,include_html_counts)
281
 
        
282
 
    
283
 
if __name__ == "__main__":
284
 
    main()