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

« back to all changes in this revision

Viewing changes to 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:
4
4
 
5
5
__author__ = "Jesse Stombaugh"
6
6
__copyright__ = "Copyright 2011, The QIIME project"
7
 
__credits__ = ["Jesse Stombaugh","Julia Goodrich", "Justin Kuczynski"]
 
7
__credits__ = ["Jesse Stombaugh","Julia Goodrich", "Justin Kuczynski",
 
8
                 "John Chase", "Jose Antonio Navas Molina"]
8
9
__license__ = "GPL"
9
 
__version__ = "1.5.0"
 
10
__version__ = "1.7.0"
10
11
__maintainer__ = "Jesse Stombaugh"
11
12
__email__ = "jesse.stombaugh@colorado.edu"
12
13
__status__ = "Release"
16
17
 
17
18
from qiime.util import parse_command_line_parameters, get_qiime_project_dir
18
19
from qiime.util import make_option
 
20
from qiime.util import create_dir
19
21
from qiime.plot_taxa_summary import make_all_charts
20
22
from cogent.util.misc import get_random_directory_name
21
23
from qiime.colors import taxonomy_color_prefs_and_map_data_from_options
27
29
plot_filetype_choices = ['pdf','svg','png']
28
30
 
29
31
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
script_info['brief_description']="""Make taxaonomy summary charts based on\
 
33
 taxonomy assignment"""
 
34
script_info['script_description']="""This script automates the construction\
 
35
 of pie, bar and area charts showing the breakdown of taxonomy by given levels.\
 
36
 The script creates an html file for each chart type for easy visualization. It\
 
37
 uses the taxonomy or category counts from summarize_taxa.py for combined\
 
38
 samples by level (-i) and user specified labels for each file passed in (-l).\
 
39
 Output will be written to the user specified folder (-o) the, where the\
 
40
 default is the current working directory. The user can also specify the number\
 
41
 of categories displayed for within a single pie chart, where the rest are\
 
42
 grouped together as the 'other category' using the (-n) option, default is 20.
32
43
"""
33
44
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."""
 
45
script_info['script_usage'].append(("""Examples:""",
 
46
"""If you wish to run the code using default parameters, you must supply a\
 
47
 counts file (phylum.txt) along with the taxon level label (Phylum), the\
 
48
 type(s) of charts to produce, and an output directory, by using the following\
 
49
 command:""",
 
50
"""%prog -i phylum.txt -l phylum -c pie,bar,area -o phylum_charts/"""))
 
51
 
 
52
script_info['script_usage'].append(("""""",
 
53
"""If you want to make charts for multiple levels at a time\
 
54
 (phylum.txt,class.txt,genus.txt) use the following command:""",
 
55
"""%prog -i phylum.txt,class.txt,genus.txt -l Phylum,Class,Genus\
 
56
 -c pie,bar,area -o phylum_class_genus_charts/"""))
 
57
 
 
58
script_info['script_usage'].append(("""""",
 
59
"""Additionally, if you would like to display on a set number of taxa ("-n 10")\
 
60
 in the pie charts, you can use the following command:""",
 
61
 """%prog -i class.txt -l Class -c pie -n 10 -o class_pie_n10_charts/"""))
 
62
 
 
63
script_info['script_usage'].append(("""""",
 
64
"""If you would like to display generate pie charts for specific samples, i.e.\
 
65
 sample 'PC.636' and sample 'PC.635' that are in the counts file header, you\
 
66
 can use the following command:""",
 
67
"""%prog -i class.txt -l Class -b PC.636,PC.635 -o sample_charts/"""))
 
68
 
 
69
script_info['output_description']="""The script generates an output folder,\
 
70
 which contains several files. For each pie chart there is a png and a pdf\
 
71
 file. The best way to view all of the pie charts is by opening up the file\
 
72
 taxonomy_summary_pie_chart.html."""
40
73
 
41
74
script_info['required_options']=[\
42
75
    ### dest should equal long-form parameter names! Can you clean this up?
59
92
    make_option('-o', '--dir_path',
60
93
        help='Output directory',
61
94
        type='new_dirpath'),
62
 
    make_option('-b', '--colorby', dest='colorby',\
 
95
    make_option('-b', '--colorby', dest='colorby',type='string',\
63
96
        help='This is the categories to color by in the plots from the' +\
64
97
        ' metadata mapping file. The categories must match the name of a ' +\
65
98
        ' column header in the mapping file exactly and multiple categories' +\
80
113
        type='int',default=80),
81
114
    make_option('-x', '--x_width',
82
115
        help='This is the width of the x-axis to use in the plots.' +\
83
 
        ' [default: %default]',default=12,type=int),
 
116
        ' [default: %default]',default=12,type='int'),
84
117
    make_option('-y', '--y_height',
85
118
        help='This is the height of the y-axis to use in the plots.' +\
86
 
        ' [default: %default]',default=6,type=int),
 
119
        ' [default: %default]',default=6,type='int'),
87
120
    make_option('-w', '--bar_width',
88
121
        help='This the width of the bars in the bar graph and should be a' +\
89
122
        ' number between 0 and 1. NOTE: this only applies to the bar charts.' +\
92
125
        help='This is the type of image to produce (i.e. ' +\
93
126
        ','.join(plot_filetype_choices) + '). [default: %default]',
94
127
        choices=plot_filetype_choices,default='pdf'),
95
 
    make_option('-c', '--chart_type',
 
128
    make_option('-c', '--chart_type', type='multiple_choice',
 
129
         mchoices=['pie', 'bar', 'area'],
96
130
         help='This is the type of chart to plot (i.e. pie, bar or area).' +\
97
131
         ' The user has the ability to plot multiple types, by using a' +\
98
132
         ' comma-separated list (e.g. area,pie) [default: %default]',
146
180
    matplotlib_version_info = tuple([int(i) for i in matplotlib_version if \
147
181
                            i.isdigit()])
148
182
 
149
 
    if matplotlib_version_info != (1,1,0):
150
 
        print "This code was only tested with Matplotlib-1.1.0"
 
183
    if matplotlib_version_info[0:2] != (1,1):
 
184
        print "This code was only tested with Matplotlib-1.1.x"
151
185
 
152
186
    #get QIIME directory
153
187
    qiime_dir=get_qiime_project_dir()
189
223
        raise ValueError, 'The number of categories has to be greater than 0!'
190
224
 
191
225
    #create directory path
 
226
    dir_path = os.getcwd()
192
227
    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()
 
228
        dir_path = opts.dir_path
 
229
        try:
 
230
            create_dir(opts.dir_path)
 
231
        except OSError:
 
232
            pass
206
233
 
207
234
    #make javascript output directory
208
235
    javascript_path = os.path.join(dir_path,'js')
209
236
    try:
210
 
        os.mkdir(javascript_path)
 
237
        create_dir(javascript_path)
211
238
    except OSError: #raised if dir exists
212
239
        pass
213
240
        
214
241
    #make raw_data output directory
215
242
    raw_data_path = os.path.join(dir_path,'raw_data')
216
243
    try:
217
 
        os.mkdir(raw_data_path)
 
244
        create_dir(raw_data_path)
218
245
    except OSError: #raised if dir exists
219
246
        pass
220
247
        
221
248
    # move javascript file to javascript output directory
222
 
    shutil.copyfile('/usr/lib/qiime/support_files/js/overlib.js',\
 
249
    shutil.copyfile(os.path.join(qiime_dir,'qiime','support_files',\
 
250
                    'js/overlib.js'),\
223
251
                    os.path.join(javascript_path,'overlib.js'))
224
252
 
225
253
    #make css output directory
226
254
    css_path = os.path.join(dir_path,'css')
227
255
    try:
228
 
        os.mkdir(css_path)
 
256
        create_dir(css_path)
229
257
    except OSError: #raised if dir exists
230
258
        pass
231
259
        
232
260
    # move css file to css output directory
233
 
    shutil.copyfile('/usr/lib/qiime/support_files/css/qiime_style.css',\
 
261
    shutil.copyfile(os.path.join(qiime_dir,'qiime','support_files',\
 
262
                    'css/qiime_style.css'),\
234
263
                    os.path.join(css_path,'qiime_style.css'))
235
264
 
236
265
    # verify all parameters are valid
252
281
    
253
282
    resize_nth_label=int(opts.resize_nth_label)
254
283
    if resize_nth_label<0:
255
 
        raise ValueError, 'The resize_nth_label of the plot has to be greater than 0!'
 
284
        raise ValueError, 'The resize_nth_label of the plot has to be greater\
 
285
 than 0!'
256
286
    
257
287
    generate_image_type=opts.type_of_file
258
288
    label_type=opts.label_type
259
289
    include_html_legend=opts.include_html_legend
260
290
    include_html_counts=opts.include_html_counts  
261
 
    plots_to_make=opts.chart_type.split(',')
262
 
    chart_types=['area','pie','bar']
263
 
    for i in plots_to_make:
264
 
        chart_type=i.lower().strip()
265
 
        if chart_type not in chart_types:
266
 
            raise ValueError, 'Please type in one of the appropriate chart types (i.e. %s)!' % ','.join(chart_types) 
 
291
    plots_to_make=opts.chart_type
 
292
    for chart_type in plots_to_make:
267
293
            
268
294
        #make pie chart output path
269
295
        charts_path = os.path.join(dir_path,'charts')
270
296
        try:
271
 
            os.mkdir(charts_path)
 
297
            create_dir(charts_path)
272
298
        except OSError: #raised if dir exists
273
299
            pass
274
300