This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 268
/
make_rarefaction_plots.py
1171 lines (999 loc) · 47.3 KB
/
make_rarefaction_plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#file make_rarefaction_plots.py
from __future__ import division
__author__ = "Meg Pirrung"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["Meg Pirrung", "Jesse Stombaugh", "Antonio Gonzalez Pena", "Will Van Treuren"]
__license__ = "GPL"
__version__ = "1.6.0"
__maintainer__ = "Jesse Stombaugh"
__email__ = "jesse.stombaugh@colorado.edu"
__status__ = "Release"
from matplotlib import use
use('Agg',warn=False)
from sys import exit
from qiime.parse import parse_rarefaction_data
from matplotlib.pylab import savefig,clf,gca,gcf,errorbar
import matplotlib.pyplot as plt
import os.path
from os.path import splitext, split
from qiime.colors import iter_color_groups
from qiime.sort import natsort
from qiime.util import create_dir,stderr
from numpy import isnan,nan,array,transpose,mean,std,arange
from StringIO import StringIO
import urllib, base64
def save_ave_rarefaction_plots(xaxis, yvals, err, xmax, ymax, ops, \
mapping_category, imagetype, res, data_colors, colors, fpath,\
background_color,label_color,metric_name, output_type="file_creation"):
'''This function creates the images, using matplotlib.'''
#Create the plot image
plt.clf()
plt.title(metric_name + ": " + mapping_category)
fig = plt.gcf()
#Add the lines to the plot
for o in ops:
l = o
plt.errorbar(xaxis[:len(yvals[o])], yvals[o], \
yerr=err[o][:len(yvals[o])], label = l, color = \
data_colors[colors[o]].toHex(),elinewidth=1,lw=2,capsize=4)
#get the plot axis
ax = plt.gca()
ax.set_axis_bgcolor(background_color)
#ax.set_yscale('log',basey=2,basex=2)
#set tick colors and width
for line in ax.yaxis.get_ticklines():
# line is a matplotlib.lines.Line2D instance
line.set_color(label_color)
line.set_markeredgewidth(1)
for line in ax.xaxis.get_ticklines():
# line is a matplotlib.lines.Line2D instance
line.set_color(label_color)
line.set_markeredgewidth(1)
#set x/y limits and labels for plot
ax.set_axisbelow(True)
ax.set_xlim(0,xmax)
ax.set_ylim(0,ymax)
ax.set_xlabel('Sequences Per Sample')
ax.set_ylabel("Rarefaction Measure: " + metric_name)
imgpath = fpath+mapping_category+ '.'+imagetype
if output_type=="file_creation":
#Save the image
plt.savefig(imgpath, format=imagetype, dpi=res)
#Get the image name for the saved image relative to the main directory
image_loc = imgpath
plt.close()
return
elif (output_type=="memory"):
imgdata = StringIO()
plt.savefig(imgdata, format='png', dpi=res, transparent=True)
imgdata.seek(0)
plt.close()
return {imgpath: imgdata}
else:
return None
def save_single_ave_rarefaction_plots(xaxis, yvals, err, xmax, ymax, ops, \
mapping_category, imagetype, res, data_colors, colors, fpath,\
background_color,label_color,rarefaction_legend_mat,metric_name,
mapping_lookup, output_type="file_creation"):
'''This function creates the images, using matplotlib.'''
avg_plots = {}
#Add the lines to the plot
for o in ops:
#Create the plot image
plt.clf()
plt.title(metric_name + ": " + mapping_category,weight='regular')
fig = plt.gcf()
l = o
plt.errorbar(xaxis[:len(yvals[o])], yvals[o], \
yerr=err[o][:len(yvals[o])], label = l, color = \
data_colors[colors[o]].toHex(),elinewidth=1,lw=2,capsize=4
)
plt.alpha=(0)
#get the plot axis
ax = plt.gca()
#ax.set_axis_bgcolor(background_color)
#set tick colors and width
for line in ax.yaxis.get_ticklines():
# line is a matplotlib.lines.Line2D instance
line.set_color('black')
line.set_markeredgewidth(1)
for line in ax.xaxis.get_ticklines():
# line is a matplotlib.lines.Line2D instance
line.set_color('black')
line.set_markeredgewidth(1)
#set x/y limits and labels for plot
ax.set_axisbelow(True)
ax.set_xlim((0,xmax))
ax.set_ylim((0,ymax))
ax.set_xlabel('Sequences Per Sample')
ax.set_ylabel("Rarefaction Measure: " + metric_name)
x=ax.xaxis.get_label()
x.set_weight('regular')
#x.set_name('Arial')
y=ax.yaxis.get_label()
y.set_weight('regular')
#y.set_name('Arial')
imgpath = fpath+mapping_lookup[mapping_category+'-'+o]+ '_ave.'+imagetype
if output_type=="file_creation":
#Save the image
plt.savefig(imgpath, format=imagetype, dpi=res,transparent=True)
#Get the image name for the saved image relative to the main directory
image_loc = imgpath
plt.close()
elif (output_type=="memory"):
imgdata = StringIO()
plt.savefig(imgdata, format='png', dpi=res, transparent=True)
imgdata.seek(0)
avg_plots[imgpath] = imgdata
plt.close()
rarefaction_legend_mat[metric_name]['groups'][mapping_category][o]['ave_link']= \
os.path.join('html_plots', \
metric_name+mapping_lookup[mapping_category+'-'+o] + '_ave.'+imagetype)
if output_type=="file_creation":
return rarefaction_legend_mat
elif output_type=="memory":
return rarefaction_legend_mat, avg_plots
def save_single_rarefaction_plots(sample_dict,imagetype, metric_name,
data_colors, colors,fpath,
background_color,label_color,res,ymax,xmax,
rarefaction_legend_mat,groups,
mapping_category,group_id,mapping_lookup, output_type="file_creation"):
'''This function creates the images, using matplotlib.'''
#Create the plot image
plt.clf()
#plt.title(str(metric_name))
fig = plt.gcf()
ax = fig.add_subplot(111)
for o in groups:
for i in sample_dict[o]:
xaxis=[]
#this creates duplicates of the xval, since there are several
#iterations
for t in range(len(sample_dict[o][i])):
xaxis.append(i)
#If all the yvals are nan at a particular xval, skip adding
#it to the plot
if not isnan(sample_dict[o][i])[0]:
scplot=ax.scatter(xaxis, sample_dict[o][i],
c=data_colors[colors[o]].toHex(),
marker='s',edgecolors='none')
#get the plot axis
ax = plt.gca()
ax.set_axis_bgcolor(background_color)
#set tick colors and width
for line in ax.yaxis.get_ticklines():
# line is a matplotlib.lines.Line2D instance
line.set_color(label_color)
line.set_markeredgewidth(1)
for line in ax.xaxis.get_ticklines():
# line is a matplotlib.lines.Line2D instance
line.set_color(label_color)
line.set_markeredgewidth(1)
#set x/y limits and labels for plot
ax.set_axisbelow(False)
ax.set_xlim((0,xmax))
ax.set_ylim((0,ymax))
ax.set_xlabel('Sequences Per Sample')
ax.set_ylabel("Rarefaction Measure: " + str(metric_name))
x=ax.xaxis.get_label()
x.set_weight('regular')
#x.set_name('Arial')
y=ax.yaxis.get_label()
y.set_weight('regular')
#y.set_name('Arial')
#Create file for image
imgpath = os.path.join(fpath,metric_name+mapping_lookup[mapping_category+'-'+group_id]+'_raw.'+imagetype)
#Since both the average and raw are saved the same way we will save the
#raw link as well
rarefaction_legend_mat[metric_name]['groups'][mapping_category][group_id]['raw_link']= \
os.path.join('html_plots', \
metric_name+mapping_lookup[mapping_category+'-'+group_id] + '_raw.'+imagetype)
if output_type=="file_creation":
#Save the image
plt.savefig(imgpath, format=imagetype, dpi=res,transparent=True)
#Get the image name for the saved image relative to the main directory
image_loc = imgpath
plt.close()
return rarefaction_legend_mat
elif (output_type=="memory"):
imgdata = StringIO()
plt.savefig(imgdata, format='png', dpi=res, transparent=True)
imgdata.seek(0)
plt.close()
return [ rarefaction_legend_mat, {imgpath: imgdata} ]
def get_rarefaction_data(rarefaction_data, col_headers):
'''This function takes a rarefaction file and converts it into an array'''
rare_mat_raw = array(rarefaction_data)
rare_mat_min = [rare_mat_raw[x][2:] for x in range(0,len(rare_mat_raw))]
seqs_per_samp = [rare_mat_raw[x][0] for x in range(0,len(rare_mat_raw))]
sampleIDs = col_headers[3:]
#Need to transpose the array to be used in averaging
rare_mat_trans = transpose(array(rare_mat_min)).tolist()
return rare_mat_trans, seqs_per_samp, sampleIDs
def ave_seqs_per_sample(matrix, seqs_per_samp, sampleIDs):
"""Calculate the average for each sampleID across each number of \
seqs/sample"""
ave_ser = {}
temp_dict = {}
#Iterate through the samples id's and create a dictionary
for i,sid in enumerate(sampleIDs):
temp_dict[sid] = {}
for j,seq in enumerate(seqs_per_samp):
try:
temp_dict[sid][seq].append(matrix[i][j])
except(KeyError):
temp_dict[sid][seq] = []
temp_dict[sid][seq].append(matrix[i][j])
#create a dictionary for average data
for sid in sampleIDs:
ave_ser[sid] = []
keys = temp_dict[sid].keys()
keys.sort()
for k in keys:
ave_ser[sid].append(mean(array(temp_dict[sid][k]),0))
return ave_ser
'''
#This function is currently not being used
def is_max_category_ops(mapping, mapping_category):
"""Count how many unique values there are for the supplied mapping \
category and return true if all values are unique"""
header = mapping[1]
map_min = mapping[0]
num_samples = len(map_min)
index = header.index(mapping_category)
seen = set()
for m in map_min:
seen.update([m[index]])
return (len(seen) == num_samples), len(seen)
'''
def make_error_series(rare_mat, groups, std_type):
"""Create mean and error bar series for the supplied mapping category"""
err_ser = dict()
collapsed_ser = dict()
seen = set()
pre_err = {}
ops = [k for k in groups]
notfound = []
#Iterate through the groups
for o in ops:
pre_err[o] = []
#For each sample in group, create a row in a list
for samID in groups[o]:
pre_err[o].append(rare_mat[samID])
'''
try:
pre_err[o].append(rare_mat[samID])
except(KeyError):
notfound.append(samID)
'''
min_len = 1000 #1e10000
#Iterate through the series data and convert it to float
for series in pre_err[o]:
series = [float(v) for v in series if v != nan]
#determine the minimum length of a series
if len(series) < min_len:
min_len = len(series)
pre_err[o] = [x[:min_len] for x in pre_err[o]]
#iterate through the groups and calculate std deviations and error
for o in ops:
opsarray = array(pre_err[o])
mn = mean(opsarray, 0)
collapsed_ser[o] = mn.tolist()
if std_type=='stderr':
# this calculates the standard error
# (using sample standard deviation)
stderr_result = stderr(opsarray, 0)
err_ser[o] = stderr_result.tolist()
else:
# this calculates the population standard deviation
stddev = std(opsarray, 0)
err_ser[o] = stddev.tolist()
return collapsed_ser, err_ser, ops
'''
function is not used
def get_overall_averages(rare_mat, sampleIDs):
"""Make series of averages of all values of seqs/sample for each \
sampleID"""
overall_ave = dict();
for s in sampleIDs:
overall_ave[s] = mean(array(rare_mat[s]))
return overall_ave
'''
def save_rarefaction_data(rare_mat,xaxis, xmax, \
mapping_category, colors, rare_type, data_colors, groups,std_type):
'''This function formats the average data and writes it to the output
directory'''
#get the error data
yaxis, err, ops = make_error_series(rare_mat,groups,std_type)
lines = []
lines.append("# "+rare_type+'\n')
lines.append("# "+mapping_category+'\n')
line = ''
line += 'xaxis: '
for v in xaxis:
line += str(v) + '\t'
line += '\n'
lines.append(line)
lines.append('xmax: '+str(xmax)+'\n')
for o in colors.keys():
lines.append(">> " + o + '\n')
#write the color lines
if colors != None:
try:
lines.append("color " + data_colors[colors[o]].toHex() + '\n')
except(KeyError):
print 'Color reference is missing!'
#write the rarefection series lines
lines.append('series ')
line = ''
try:
for v in yaxis[o]:
line += str(v) + '\t'
except(TypeError):
line += str(yaxis[o])
line += '\n'
lines.append(line)
#write the rarefaction error lines
lines.append('error ')
line = ''
try:
for e in err[o]:
if e == 0:
line += str(nan) + '\t'
else:
line += str(e) + '\t'
except(TypeError):
line += str(err[o])
line += '\n'
lines.append(line)
return lines
def make_averages(color_prefs, data, background_color, label_color, rares, \
output_dir,resolution,imagetype,ymax,suppress_webpage,
std_type, output_type="file_creation"):
'''This is the main function, which takes the rarefaction files, calls the
functions to make plots and formatting the output html.'''
rarelines = []
rarefaction_legend_mat={}
if ymax:
user_ymax=True
else:
user_ymax=False
if not suppress_webpage and output_type=="file_creation":
# in this option the path must include the output directory
all_output_dir = os.path.join(output_dir, 'html_plots')
ave_output_dir = os.path.join(output_dir, 'average_plots')
#Create the directories, where plots and data will be written
create_dir(all_output_dir)
elif output_type == 'memory':
# this is rather an artificial path to work with the javascript code
all_output_dir = 'plot/html_plots'
ave_output_dir = 'plot/average_plots'
ave_data_file_path=os.path.join(output_dir,'average_tables')
if output_type=="file_creation":
create_dir(ave_output_dir)
create_dir(ave_data_file_path,False)
metric_num=0
rarefaction_legend_mat={}
rarefaction_data_mat={}
rare_num=0
# this is a fix for the issue of writing field values as the filenames
mapping_lookup={}
for i,column in enumerate(data['map'][0]):
for j,row in enumerate(data['map'][1:]):
mapping_lookup['%s-%s' % (column,row[i])]='col_%s_row_%s' % \
(str(i),str(j))
all_plots = []
#Iterate through the rarefaction files
for r in natsort(rares):
raredata = rares[r]
metric_name=r.split('.')[0]
#convert the rarefaction data into variables
col_headers,comments,rarefaction_fn,rarefaction_data=rares[r]
#Here we only need to perform these steps once, since the data is
#the same for all rarefaction files
if rare_num==0:
#Remove samples from the mapping file, which contain no data after
#rarefaction
updated_mapping=[]
for j in data['map']:
#Add the mapping header
if j[0]=='SampleID':
updated_mapping.append(j)
#Determine if the sample exists in the rarefaction file
for i in col_headers[3:]:
if j[0]==i:
updated_mapping.append(j)
#Get the groups and colors for the updated mapping file
groups_and_colors=iter_color_groups(updated_mapping,color_prefs)
groups_and_colors=list(groups_and_colors)
#parse the rarefaction data
rare_mat_trans, seqs_per_samp, sampleIDs = \
get_rarefaction_data(rarefaction_data, col_headers)
rarefaction_legend_mat[metric_name]={}
#Create dictionary variables and get the colors for each Sample
sample_colors=None
rarefaction_legend_mat[metric_name]['groups']={}
for i in range(len(groups_and_colors)):
labelname=groups_and_colors[i][0]
#Create a legend dictionary for html output
rarefaction_legend_mat[metric_name]['groups'][labelname]={}
#If this is the first time iterating through the rarefaction data
#create a data dictionary for html output
if rare_num==0:
rarefaction_data_mat[labelname]={}
#If the labelname is SampleID, use the colors assigned
if labelname=='SampleID':
sample_colors=groups_and_colors[i][2]
sample_data_colors=groups_and_colors[i][3]
rare_num=1
#If sample colors were not assigned, create a list of sample colors
if not sample_colors:
samples_and_colors=iter_color_groups(updated_mapping, \
{'SampleID': {'column': 'SampleID', 'colors': \
(('red', (0, 100, 100)), ('blue', (240, 100, 100)))}})
samples_and_colors=list(samples_and_colors)
sample_colors=samples_and_colors[0][2]
sample_data_colors=samples_and_colors[0][3]
sample_dict = {}
#Create a dictionary containing the samples
for i,sid in enumerate(sampleIDs):
if sid in (i[0] for i in updated_mapping):
sample_dict[sid] = {}
for j,seq in enumerate(seqs_per_samp):
try:
sample_dict[sid][seq].append(rare_mat_trans[i][j])
except(KeyError):
sample_dict[sid][seq] = []
sample_dict[sid][seq].append(rare_mat_trans[i][j])
#convert xvals to float
xaxisvals = [float(x) for x in set(seqs_per_samp)]
xaxisvals.sort()
#get the rarefaction averages
rare_mat_ave = ave_seqs_per_sample(rare_mat_trans, seqs_per_samp, \
sampleIDs)
#calculate the max xval
xmax = max(xaxisvals) + (xaxisvals[len(xaxisvals)-1] - \
xaxisvals[len(xaxisvals)-2])
'''
#get the overall average
#overall_average = get_overall_averages(rare_mat_ave, sampleIDs)
rarelines.append("#" + r + '\n')
for s in sampleIDs:
rarelines.append('%f'%overall_average[s] + '\n')
'''
if not user_ymax:
ymax=0
for i in range(len(groups_and_colors)):
labelname=groups_and_colors[i][0]
groups=groups_and_colors[i][1]
colors=groups_and_colors[i][2]
data_colors=groups_and_colors[i][3]
ave_file_path=os.path.join(ave_data_file_path,metric_name)
#save the rarefaction averages
rare_lines=save_rarefaction_data(rare_mat_ave, xaxisvals, xmax,\
labelname, colors, r, data_colors, groups,
std_type)
#write out the rarefaction average data
if output_type=="file_creation":
open(ave_file_path+labelname+'.txt','w').writelines(rare_lines)
#take the formatted rarefaction averages and format the results
rares_data = parse_rarefaction_data( \
''.join(rare_lines[:]).split('\n'))
#determine the ymax based on the average data
#multiple the ymax, since the dots can end up on the border
new_ymax=(max([max(v) for v in rares_data['series'].values()])+\
max([max(e) for e in rares_data['error'].values()])) * 1.15
if isnan(new_ymax):
new_ymax=(max([max(v) for v in \
rares_data['series'].values()])) * 1.15
if new_ymax>ymax:
ymax=new_ymax
iterator_num=0
#iterate through the groups
for i in range(len(groups_and_colors)):
labelname=groups_and_colors[i][0]
groups=groups_and_colors[i][1]
colors=groups_and_colors[i][2]
data_colors=groups_and_colors[i][3]
data_color_order=groups_and_colors[i][4]
#save the rarefaction averages
rare_lines=save_rarefaction_data(rare_mat_ave, xaxisvals, xmax, \
labelname, colors, r, data_colors, groups,
std_type)
#take the formatted rarefaction averages and format the results
rares_data = parse_rarefaction_data( \
''.join(rare_lines[:]).split('\n'))
if not suppress_webpage:
if iterator_num==0:
rarefaction_legend_mat[metric_name]['samples']={}
for o in sample_dict:
rarefaction_legend_mat[metric_name]['samples'][o]={}
#Add values to the legend dictionary
rarefaction_legend_mat[metric_name]['samples'][o]['color']=sample_data_colors[sample_colors[o]].toHex()
iterator_num=1
#Iterate through the groups and create the legend dictionary
for g in groups:
#generate the filepath for the image file
file_path = os.path.join(all_output_dir, \
metric_name+labelname+g)
#create a dictionary of samples and their colors
rarefaction_legend_mat[metric_name]['groups'][labelname][g]={}
rarefaction_legend_mat[metric_name]['groups'][labelname][g]['groupsamples']=groups[g]
rarefaction_legend_mat[metric_name]['groups'][labelname][g]['groupcolor']=\
data_colors[colors[g]].toHex()
#Create the individual category average plots
if output_type=="file_creation":
rarefaction_data_mat,rarefaction_legend_mat=make_plots(\
background_color, label_color, \
rares_data, ymax, xmax,all_output_dir, \
resolution, imagetype,groups, colors, \
data_colors,metric_name,labelname, \
rarefaction_data_mat,rarefaction_legend_mat,
sample_dict,sample_data_colors,
sample_colors,mapping_lookup,output_type)
elif output_type=="memory":
rarefaction_data_mat, rarefaction_legend_mat, all_plots_single, \
all_plots_ave = make_plots(\
background_color, label_color, \
rares_data, ymax, xmax,all_output_dir, \
resolution, imagetype,groups, colors, \
data_colors,metric_name,labelname, \
rarefaction_data_mat,rarefaction_legend_mat,
sample_dict,sample_data_colors,
sample_colors,mapping_lookup,output_type)
#generate the filepath for the image file
file_path = os.path.join(ave_output_dir, \
splitext(split(rares_data['headers'][0])[1])[0])
#Create the average plots
categories = [k for k in groups]
all_plots_rare = save_ave_rarefaction_plots(rares_data['xaxis'], rares_data['series'], \
rares_data['error'], xmax, ymax, categories, \
labelname, imagetype, resolution, data_colors, \
colors, file_path, background_color, label_color, \
metric_name, output_type)
if output_type == "memory":
all_plots.append(all_plots_rare)
all_plots.extend(all_plots_single)
all_plots.append(all_plots_ave)
else:
#generate the filepath for the image file
file_path = os.path.join(ave_output_dir, \
splitext(split(rares_data['headers'][0])[1])[0])
categories = [k for k in groups]
all_plots_rare = save_ave_rarefaction_plots(rares_data['xaxis'], rares_data['series'], \
rares_data['error'], xmax, ymax, categories, \
labelname, imagetype, resolution, data_colors, \
colors, file_path, background_color, label_color, \
metric_name, output_type)
if not suppress_webpage:
#format the html output
html_output=make_html(rarefaction_legend_mat, \
rarefaction_data_mat,xaxisvals,imagetype,mapping_lookup, output_type, all_plots)
else:
html_output=None
return html_output
def make_html(rarefaction_legend_mat, rarefaction_data_mat, xaxisvals, \
imagetype,mapping_lookup, output_type="file_creation", all_plots=None):
rarefaction_legend_mat
legend_td=['<b>Legend</b><div STYLE="border: thin black solid; height: 300px; width: 200px; font-size: 12px; overflow: auto;"><table>']
summarized_table=[]
metric_select_html=[]
category_select_html=[]
data_table_html=[]
metrics=[]
category_colors={}
cat_iter=0
#iterate the legend dictionary
for m in natsort(rarefaction_legend_mat):
#Create the metric select box options
metric_select_html.append('<option value="%s">%s</option>' % (m,m))
metrics.append(m)
#iterate through the categories in the legend dictionary
for category in natsort(rarefaction_legend_mat[m]['groups']):
#Create the select box options
if cat_iter==0:
cat_links=[]
for i in rarefaction_legend_mat[m]['groups'][category]:
cat_links.append(mapping_lookup[category+'-'+i])
category_select_html.append('<option value="%s">%s</option>' % \
(category+'$#!'+'$#!'.join(cat_links),category))
plot_iterator=0
#iterate through the groups in the legend dictionary and create
#the html formatted rows for each category and group
for group in natsort(rarefaction_legend_mat[m]['groups'][category]):
sample_list=[]
category_colors[group]=\
rarefaction_legend_mat[m]['groups'][category][group]['groupcolor']
for sample in natsort(rarefaction_legend_mat[m]['groups'][category][group]['groupsamples']):
sample_list.append('\''+sample+'\'')
plot_iterator=plot_iterator+1
legend_td.append('<tr id="%s" name="%s" style="display: none;"><td class="data" onmouseover="document.body.style.cursor=\'pointer\'" onmouseout="document.body.style.cursor=\'default\'" onclick="toggle(%s)" id="%s" name="%s">▶</td><td><input name="%s" type="checkbox" checked="True" onclick="show_hide_category(this)"></td><td style="color:%s">■ </td><td class="data"><b>%s</b></td></tr>' % (m+category,m+category,"'"+m+mapping_lookup[category+'-'+group]+"'",m+mapping_lookup[category+'-'+group],','.join(sample_list),m+mapping_lookup[category+'-'+group]+'_raw.'+imagetype,rarefaction_legend_mat[m]['groups'][category][group]['groupcolor'], group))
for sample in natsort(rarefaction_legend_mat[m]['groups'][category][group]['groupsamples']):
sample=str(sample)
legend_td.append('<tr id="%s" name="%s" style="display: none;"><td class="data" align="right">∟</td><td></td><td style="color:%s">◆</td><td class="data" align="left"><b>%s</b></td></tr>' % (m+mapping_lookup[category+'-'+group]+'_raw',m+mapping_lookup[category+'-'+group],rarefaction_legend_mat[m]['samples'][sample]['color'], sample))
cat_iter=1
#iterate through the data dictionary and format the rows for the html
#data table
for category in rarefaction_data_mat:
data_table_html.append('<tr name="%s" style="display: none;"><td class="headers">%s</td><td class="headers">Seqs/Sample</td>' % (category,category))
for j in metrics:
data_table_html.append('<td class="headers">%s Ave.</td><td class="headers">%s Err.</td>' % (j,j))
data_table_html.append('</tr>')
#data_table_html.append('<tr name="%s" style="display: none;"></tr>' % (category))
for g in natsort(rarefaction_data_mat[category]):
for i in range(len(xaxisvals)):
data_table_html.append('<tr name="%s" style="display: none;">' % (category))
data_table_html.append('<td class="data" bgcolor="%s">%s</td><td class="data">%s</td>' % (category_colors[g],g,xaxisvals[i]))
for m in metrics: #bugfix, was rarefaction_data_mat[category][g]
data_table_html.append('<td class="data">%s</td><td class="data">%s</td>' % (rarefaction_data_mat[category][g][m]['ave'][i],rarefaction_data_mat[category][g][m]['err'][i]))
data_table_html.append('</tr>')
legend_td.append('</table></div></div>')
#Create the table that contains the plots and table
plot_html='%s' % ('\n'.join(legend_td))
if output_type=="file_creation":
#insert the formatted rows into the html string at the bottom of this file
html_output=HTML % ('',
"img.setAttribute('src',\"./html_plots/\"+SelObject.value+array[i]+'_ave'+imagetype)",
"img.setAttribute('src',\"./html_plots/\"+metric+array[i]+'_ave'+imagetype)",
"img.setAttribute('src',\"./html_plots/\"+arguments[0]+'_raw'+imagetype)",
'.'+imagetype,
'\n'.join(metric_select_html), \
'\n'.join(category_select_html), \
plot_html, \
'\n'.join(data_table_html))
elif output_type=="memory":
plots_html = ['all_plots = {}']
for elements in all_plots:
for k,v in elements.items():
# the path is compatible with the javascript, see make_averages
plots_html.append('all_plots["%s"] = "%s"' % (k, \
"data:image/png;base64," + urllib.quote(base64.b64encode(v.buf))))
#insert the formatted rows into the html string at the bottom of this file
html_output=HTML % ('\n'.join(plots_html),
"img.setAttribute('src',all_plots[\"plot/html_plots/\"+SelObject.value+array[i]+'_ave'+imagetype])",
"img.setAttribute('src',all_plots[\"plot/html_plots/\"+metric+array[i]+'_ave'+imagetype])",
"img.setAttribute('src',all_plots[\"plot/html_plots/\"+arguments[0]+'_raw'+imagetype])",
'.'+imagetype,
'\n'.join(metric_select_html), \
'\n'.join(category_select_html), \
plot_html, \
'\n'.join(data_table_html))
return html_output
def make_plots(background_color, label_color, rares, ymax, xmax,\
output_dir, resolution, imagetype,groups,colors,data_colors, \
metric_name,labelname,rarefaction_data_mat,\
rarefaction_legend_mat,sample_dict,sample_data_colors,
sample_colors,mapping_lookup, output_type="file_creation"):
'''This is the main function for generating the rarefaction plots and html
file.'''
#Get the alpha rare data
raredata = rares
#generate the filepath for the image file
file_path = os.path.join(output_dir, \
splitext(split(raredata['headers'][0])[1])[0])
all_plots_single = []
#Sort and iterate through the groups
for i in natsort(groups):
#for k in groups[i]:
for j in range(len(raredata['xaxis'])):
group_field=i
seq_per_sample_field=int(raredata['xaxis'][j])
color_field=data_colors[colors[group_field]].toHex()
#If a field is missing, then it means that one of the
#samples did not contain enough sequences.
#For this case, we will assign the value as n.a.
try:
average_field=raredata['series'][i][j]
error_field=raredata['error'][i][j]
if isnan(average_field):
error_field=nan
except:
average_field=nan
error_field=nan
#Add context to the data dictionary, which will be used in the html
if rarefaction_data_mat[labelname].has_key(i):
if rarefaction_data_mat[labelname][i].has_key(metric_name):
rarefaction_data_mat[labelname][i][metric_name]['ave'].append(''.join('%10.3f' % ((raredata['series'][i][j]))))
rarefaction_data_mat[labelname][i][metric_name]['err'].append(''.join('%10.3f' % ((raredata['error'][i][j]))))
else:
rarefaction_data_mat[labelname][i][metric_name]={}
rarefaction_data_mat[labelname][i][metric_name]['ave']=[]
rarefaction_data_mat[labelname][i][metric_name]['err']=[]
rarefaction_data_mat[labelname][i][metric_name]['ave'].append(''.join('%10.3f' % ((raredata['series'][i][j]))))
rarefaction_data_mat[labelname][i][metric_name]['err'].append(''.join('%10.3f' % ((raredata['error'][i][j]))))
else:
rarefaction_data_mat[labelname][i]={}
rarefaction_data_mat[labelname][i][metric_name]={}
rarefaction_data_mat[labelname][i][metric_name]['ave']=[]
rarefaction_data_mat[labelname][i][metric_name]['err']=[]
rarefaction_data_mat[labelname][i][metric_name]['ave'].append(''.join('%10.3f' % ((raredata['series'][i][j]))))
rarefaction_data_mat[labelname][i][metric_name]['err'].append(''.join('%10.3f' % ((raredata['error'][i][j]))))
#Create raw plots for each group in a category
fpath = output_dir
if output_type=="file_creation":
rarefaction_legend_mat = save_single_rarefaction_plots( \
sample_dict, \
imagetype,metric_name, \
sample_data_colors,sample_colors, \
fpath,background_color, \
label_color,resolution,ymax,xmax,
rarefaction_legend_mat,groups[i],
labelname,i,mapping_lookup, output_type)
elif output_type == "memory":
rarefaction_legend_mat, rare_plot_for_all = save_single_rarefaction_plots( \
sample_dict, \
imagetype,metric_name, \
sample_data_colors,sample_colors, \
fpath,background_color, \
label_color,resolution,ymax,xmax,
rarefaction_legend_mat,groups[i],
labelname,i,mapping_lookup, output_type)
all_plots_single.append(rare_plot_for_all)
categories = [k for k in groups]
#Create the rarefaction average plot and get updated legend information
#
if output_type=="file_creation":
rarefaction_legend_mat = save_single_ave_rarefaction_plots(raredata['xaxis'], \
raredata['series'], raredata['error'], xmax, ymax, categories, \
labelname, imagetype, resolution, data_colors, \
colors, file_path, background_color, label_color, \
rarefaction_legend_mat, metric_name,mapping_lookup, output_type)
return rarefaction_data_mat, rarefaction_legend_mat
elif output_type == "memory":
rarefaction_legend_mat, all_plots_ave = save_single_ave_rarefaction_plots(raredata['xaxis'], \
raredata['series'], raredata['error'], xmax, ymax, categories, \
labelname, imagetype, resolution, data_colors, \
colors, file_path, background_color, label_color, \
rarefaction_legend_mat, metric_name,mapping_lookup, output_type)
return rarefaction_data_mat, rarefaction_legend_mat, all_plots_single, all_plots_ave
HTML='''
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;">
<title>Rarefaction Curves</title>
<style type="text/css">
td.data{font-size:10px;border-spacing:0px 10px;text-align:center;}
td.headers{font-size:12px;font-weight:bold;text-align:center;}
table{border-spacing:0px;}
.removed{display:none;}
.expands{cursor:pointer; cursor:hand;}
.child1 td:first-child{padding-left: 3px;}
</style>
<script language="javascript" type="text/javascript">
%s
function show_hide_category(checkobject){
var imagetype=document.getElementById('imagetype').value;
img=document.getElementById(checkobject.name.replace('_raw'+imagetype,'_ave'+imagetype))
if (checkobject.checked==false){
img.style.display='none';
}else{
img.style.display='';
}
}
function reset_tree(){
var category=document.getElementById('category').value;
var metric=document.getElementById('metric').value;
var old_all_categories=document.getElementById('all_categories');
var imagetype=document.getElementById('imagetype').value;
cat_list=old_all_categories.value.split('$#!')
if (metric!='' && category != ''){
for (var i=1, il=cat_list.length; i<il; i++){
group=metric+category+cat_list[i]
main_class=metric+category
var exp_item=document.getElementById(group);
if (exp_item!=null){
if (exp_item.innerHTML=='\u25BC'){
exp_item.innerHTML='\u25B6'
var rows=document.getElementsByName(group);
for (var j=0, jl=rows.length; j<jl; j++){
rows[j].style.display="none";
}
}
var rows=document.getElementsByName(group+'_raw'+imagetype);
for (var j=0, jl=rows.length; j<jl; j++){
if (rows[j].checked==false){
rows[j].checked=true;
}
}
}
}
}
}
function changeMetric(SelObject){
var category=document.getElementById('category');
var old_metric=document.getElementById('metric');
var imagetype=document.getElementById('imagetype').value;
var legend=document.getElementById('legend');
var array=document.getElementById('all_categories').value.split('$#!')
var plots=document.getElementById('plots');
plots.style.display='none'
reset_tree();
if (category.value != ''){
legend.style.display="";
cat=SelObject.value+category.value
data_display=document.getElementsByName(cat)
for (var i=0, il=data_display.length; i<il; i++){
data_display[i].style.display="";
}
cat=old_metric.value+category.value
data_hide=document.getElementsByName(cat)
for (var i=0, il=data_hide.length; i<il; i++){
data_hide[i].style.display="none";
}
data_display=document.getElementsByName(category.value)
for (var i=0, il=data_display.length; i<il; i++){
data_display[i].style.display="";
}
new_cat=SelObject.value+category.value
plots.innerHTML=''
for (var i=1, il=array.length; i<il; i++){
img=document.createElement('img')
img.setAttribute('width',"600px")
img.setAttribute('id',array[i]+'_ave'+imagetype)
img.setAttribute('style','position:absolute;z-index:0')
%s
plots.appendChild(img)