27
29
plot_filetype_choices = ['pdf','svg','png']
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.
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\
50
"""%prog -i phylum.txt -l phylum -c pie,bar,area -o phylum_charts/"""))
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/"""))
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/"""))
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/"""))
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."""
41
74
script_info['required_options']=[\
42
75
### dest should equal long-form parameter names! Can you clean this up?
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]',
189
223
raise ValueError, 'The number of categories has to be greater than 0!'
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
197
os.mkdir(opts.dir_path)
198
dir_path=opts.dir_path
205
dir_path = os.getcwd()
228
dir_path = opts.dir_path
230
create_dir(opts.dir_path)
207
234
#make javascript output directory
208
235
javascript_path = os.path.join(dir_path,'js')
210
os.mkdir(javascript_path)
237
create_dir(javascript_path)
211
238
except OSError: #raised if dir exists
214
241
#make raw_data output directory
215
242
raw_data_path = os.path.join(dir_path,'raw_data')
217
os.mkdir(raw_data_path)
244
create_dir(raw_data_path)
218
245
except OSError: #raised if dir exists
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',\
223
251
os.path.join(javascript_path,'overlib.js'))
225
253
#make css output directory
226
254
css_path = os.path.join(dir_path,'css')
229
257
except OSError: #raised if dir exists
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'))
236
265
# verify all parameters are valid
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\
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:
268
294
#make pie chart output path
269
295
charts_path = os.path.join(dir_path,'charts')
271
os.mkdir(charts_path)
297
create_dir(charts_path)
272
298
except OSError: #raised if dir exists