-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrections_clc.py
More file actions
1286 lines (1087 loc) · 54.4 KB
/
Copy pathcorrections_clc.py
File metadata and controls
1286 lines (1087 loc) · 54.4 KB
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
# This script will make corrected figures based on Howard's comments.
# First we will import the necessary modules.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans as KM
from sklearn.preprocessing import KBinsDiscretizer as Bin
import matplotlib.patches as patches
from statannotations.Annotator import Annotator
from statsmodels.stats.multicomp import pairwise_tukeyhsd
from scipy.stats import f_oneway
import scipy.stats as stat
from scikit_posthocs import posthoc_tukey
from io import StringIO
import os.path
# Define paths.
All = './All/'
No_1 = './No_1Mo/'
PK = './Only_PKO/'
syn = './prism/syn/'
wbh = './prism/wbh/'
# Turn off pandas warnings, not clear on why they appear sometimes and not
# others. In both cases, the desired result is the same. Will need to dig in on
# this.
pd.options.mode.chained_assignment = None
# We will read in the data
df = pd.read_excel('Concat_with_all.xlsx')
# We will rename the 'Frequency' column to 'Heteroplasmy' and the 'Reference
# Position' column to 'Mitochondrial Position as these are more appropriate
# names.
df = df.rename(columns={'Frequency' : 'Heteroplasmy', 'Reference Position' :
'Mitochondrial Position'})
# We will also replace 'Syn' with 'Synaptosome'
df = df.replace('Syn', 'Synaptosome')
df2 = df[df.Strain != 'Polg-W402A']
df1 = df[df.Strain != 'WT\n1Mo']
df1 = df[df.Strain != 'PKO\n1Mo']
# First we'll create a count column so that the number of samples can be
# counted and hence be compared to each other.
df['count'] = 0
df1['count'] = 0
# We now create variables only for the strains of interest (WT, Polg, and
# Polg-PKO) and then from these subsets create further subsets, syn and
# homogenate.
W = df1.loc[df1['Strain'] == 'WT']
WS = W.loc[W['Sample_type'] == 'Synaptosome']
WH = W.loc[W['Sample_type'] == 'Homogenate']
P = df1.loc[df1['Strain'] == 'Polg']
PS = P.loc[P['Sample_type'] == 'Synaptosome']
PH = P.loc[P['Sample_type'] == 'Homogenate']
K = df1.loc[df1['Strain'] == 'Polg-PKO']
KS = K.loc[K['Sample_type'] == 'Synaptosome']
KH = K.loc[K['Sample_type'] == 'Homogenate']
W4 = df1.loc[df1['Strain'] == 'Polg-W402A']
W4S = W4.loc[W4['Sample_type'] == 'Synaptosome']
W4H = W4.loc[W4['Sample_type'] == 'Homogenate']
# Perform elbow method on all of the dataframes above to on the Heteroplasmy
# column to see how many clusters they each have. To do so, assign a variable
# to the heteroplasmy column, connvert into an np array, and change the shape
# to (-1,1).
x = WS['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method WT Syn')
plt.savefig('WT_Syn_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
WS_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (WS_bins[:-1] + WS_bins[1:])/2
WS.loc[:,'Heteroplasmy Cluster'] = pd.cut(WS['Heteroplasmy'], WS_bins, labels=center)
# Repeat for Homogenate.
x = WH['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method WT Homogenate')
plt.savefig('WT_WBH_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
WH_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (WH_bins[:-1] + WH_bins[1:])/2
WH.loc[:,'Heteroplasmy Cluster'] = pd.cut(WH['Heteroplasmy'], WH_bins, labels=center)
# Repeat for Polg Syn
x = PS['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method Polg Syn')
plt.savefig('Polg_Syn_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
PS_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (PS_bins[:-1] + PS_bins[1:])/2
PS.loc[:,'Heteroplasmy Cluster'] = pd.cut(PS['Heteroplasmy'], PS_bins, labels=center)
# Repeat for Polg Homogenate
x = PH['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method Polg Homogenate')
plt.savefig('Polg_WBH_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
PH_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (PH_bins[:-1] + PH_bins[1:])/2
PH['Heteroplasmy Cluster'] = pd.cut(PH['Heteroplasmy'], PH_bins, labels=center)
# Repeat for Polg-PKO Syn.
x = KS['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method Polg-PKO Syn')
plt.savefig('Polg-PKO_Syn_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
KS_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (KS_bins[:-1] + KS_bins[1:])/2
KS['Heteroplasmy Cluster'] = pd.cut(KS['Heteroplasmy'], KS_bins, labels=center)
# Repeat for Polg-PKO Homogenate/
x = KH['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method Polg-PKO Homogenate')
plt.savefig('Polg-PKO_WBH_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
KH_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (KH_bins[:-1] + KH_bins[1:])/2
KH['Heteroplasmy Cluster'] = pd.cut(KH['Heteroplasmy'], KH_bins, labels=center)
# Repeat for Polg-W402A Syn.
x = W4S['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method Polg-W402A Syn')
plt.savefig('Polg-W402a_Syn_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
W4S_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (W4S_bins[:-1] + W4S_bins[1:])/2
W4S['Heteroplasmy Cluster'] = pd.cut(W4S['Heteroplasmy'], W4S_bins, labels=center)
# Repeat for Polg-W402A Homogenate/
x = W4H['Heteroplasmy']
x = np.array(x)
x = x.reshape(-1,1)
# Use a range of k to run the elbow method on.
K = range(1,7)
# Create a list of distortions, and use kmeans to fit the data to. Finally,
# append the distortion list with the kmeans data.
distortions = []
for k in K:
km = KM(n_clusters=k, random_state=42)
km.fit(x)
distortions.append(km.inertia_)
# Plot the elbow results and save the figure.
plt.figure(figsize=(16,8))
plt.plot(K, distortions, 'bx-')
plt.ylabel('Distortion')
plt.title('The Elbow Method Polg-W402A Homogenate')
plt.savefig('Polg-W402A_WBH_Het_Elbow.png')
# Each time the kmeans algo is run, an unsorted list of cluster centers
# are outputeed, are in the form of floats, and not sorted or rounded.
# Therefore, they will be sorted and rounded to the nearest integer
km.cluster_centers_ = np.sort(km.cluster_centers_,axis=None).round()
# Now we will create bins using the sorted, rounded kmeans cluseters
W4H_bins = km.cluster_centers_
# Bin centers are defined by taking half the sum of consecutive bins. The
# resulting bin centers will be put into a new column in the dataframe called
# 'Heteroplasmy Cluster'
center = (W4H_bins[:-1] + W4H_bins[1:])/2
W4H['Heteroplasmy Cluster'] = pd.cut(W4H['Heteroplasmy'], W4H_bins, labels=center)
# Create concatenated files Syn and WBH so that graphs can be made for the
# cluster centers.
Syn = pd.concat([WS, PS, KS, W4S], axis=0)
WBH = pd.concat([WH, PH, KH, W4H], axis=0)
com = pd.concat([Syn, WBH], axis=0)
order = ['Synaptosome', 'Homogenate']
hue_order = ['WT', 'Polg', 'Polg-PKO', 'Polg-W402A']
# Now we will plot out the Heteroplasmy Clusters vs Reference Position as well
# as vs gene. We will use the facetgrid from seabron so that the two (Syn and
# WBH) can be on the same figure.
x = sns.FacetGrid(data=com, col='Sample_type', col_wrap=1, despine=True,
hue='Strain',palette='colorblind', col_order=order, hue_order=hue_order, aspect=2)
# The graph title is adjusted to be on top of the graph, but not intruding on
# it, and it is emboldened.
x.fig.subplots_adjust(top=0.8)
x.fig.suptitle('Heteroplasmy Clusters', fontsize=28,weight='bold')
labels = hue_order
colors = sns.color_palette('colorblind').as_hex()[:len(labels)]
# Seaborn has an issue pulling the colors for the legend in the facetgrid.
# Therefore a patch from matplotlib.patches is called to correct for that.
handles = [patches.Patch(color=col, label=lab) for col, lab in zip(colors,
labels)]
# Next, seaborn is called to add the legened as outlined in the patch
x.add_legend(legend_data={lab: hand for lab, hand in zip(labels, handles)})
# A histplot will be inserted into the facetgrid. X will be Reference
# Position, Y will be Heteroplasmy Cluster. The alpha command represents
# transperancy of the colors. The higher it is, the less transparent.
x.map(sns.histplot, 'Mitochondrial Position', 'Heteroplasmy'
' Cluster',alpha=0.4)
#.set(yscale='log')
# Since facetgrids puts the title of each graph with '=', it is changes so that
# only the name without '=' is posted on the title of each graph.
x.set_titles(col_template='{col_name}',weight='bold')
# Set y limit from 0 to 100 so that 100% heteroplasmy is noticable.
x.set(ylim=(0,100))
plt.savefig(os.path.join(No_1,'Heteroplasmy_clusters.png'))
# Now we'll do it for the gene_name.
x = sns.FacetGrid(data=com, col='Sample_type', col_wrap=1, despine=True,
hue='Strain',palette='colorblind', col_order=order, hue_order=hue_order, aspect=4)
# The graph title is adjusted to be on top of the graph, but not intruding on
# it, and it is emboldened.
x.fig.subplots_adjust(top=0.8)
x.fig.suptitle('Heteroplasmy Clusters', fontsize=28,weight='bold')
labels = hue_order
colors = sns.color_palette('colorblind').as_hex()[:len(labels)]
# Seaborn has an issue pulling the colors for the legend in the facetgrid.
# Therefore a patch from matplotlib.patches is called to correct for that.
handles = [patches.Patch(color=col, label=lab) for col, lab in zip(colors,
labels)]
# Next, seaborn is called to add the legened as outlined in the patch
x.add_legend(legend_data={lab: hand for lab, hand in zip(labels, handles)})
# A scatterplot will be inserted into the facetgrid. X will be Reference
# Position, Y will be Heteroplasmy Cluster. The alpha command represents
# transperancy of the colors. The higher it is, the less transparent.
x.map(sns.histplot, 'Gene_name', 'Heteroplasmy'
' Cluster',alpha=0.4)
#.set(yscale='log')
# Since we want all gene names to be on the graph, the xticklables will be
# rotated 45 degress.
[plt.setp(ax.get_xticklabels(), rotation=45) for ax in x.axes.flat]
# Since facetgrids puts the title of each graph with '=', it is changes so that
# only the name without '=' is posted on the title of each graph.
x.set_titles(col_template='{col_name}',weight='bold')
# Set y limit from 0 to 100 so that 100% heteroplasmy is noticable.
x.set(ylim=(0,100))
plt.savefig(os.path.join(No_1,'Heteroplasmy_clusters_gene.png'), bbox_inches='tight')
Syn.to_excel(os.path.join(syn,'het_cluster_syn.xlsx'),index=False)
WBH.to_excel(os.path.join(wbh,'het_cluster_wbh.xlsx'),index=False)
# Now we'll do it for each of the strains separately, Syn and WBH will be two
# separate figures .
x = sns.FacetGrid(data=Syn, col='Strain', col_wrap=1, despine=True,
hue='Strain',palette='colorblind', col_order=hue_order, hue_order=hue_order, aspect=4)
# The graph title is adjusted to be on top of the graph, but not intruding on
# it, and it is emboldened.
x.fig.subplots_adjust(top=0.8)
x.fig.suptitle('Synaptosome - Heteroplasmy Clusters', fontsize=28,weight='bold')
# A histplot will be inserted into the facetgrid. X will be Reference
# Position, Y will be Heteroplasmy Cluster. The alpha command represents
# transperancy of the colors. The higher it is, the less transparent.
x.map(sns.histplot, 'Mitochondrial Position', 'Heteroplasmy'
' Cluster',alpha=1)
# Since facetgrids puts the title of each graph with '=', it is changes so that
# only the name without '=' is posted on the title of each graph.
x.set_titles(col_template='{col_name}',weight='bold')
# Set y limit from 0 to 100 so that 100% heteroplasmy is noticable.
x.set(ylim=(0,100))
plt.savefig(os.path.join(No_1,'Heteroplasmy_clusters_Syn_Strain.png'), bbox_inches='tight')
# Now we'll do it for gene name for Syn.
x = sns.FacetGrid(data=Syn, col='Strain', col_wrap=1, despine=True,
hue='Strain',palette='colorblind', col_order=hue_order, hue_order=hue_order, aspect=4)
# The graph title is adjusted to be on top of the graph, but not intruding on
# it, and it is emboldened.
x.fig.subplots_adjust(top=0.8)
x.fig.suptitle('Synaptosome - Heteroplasmy Clusters', fontsize=28,weight='bold')
# A histplot will be inserted into the facetgrid. X will be Reference
# Position, Y will be Heteroplasmy Cluster. The alpha command represents
# transperancy of the colors. The higher it is, the less transparent.
x.map(sns.histplot, 'Gene_name', 'Heteroplasmy'
' Cluster',alpha=1)
# Since we want all gene names to be on the graph, the xticklables will be
# rotated 45 degress.
[plt.setp(ax.get_xticklabels(), rotation=45) for ax in x.axes.flat]
# Since facetgrids puts the title of each graph with '=', it is changes so that
# only the name without '=' is posted on the title of each graph.
x.set_titles(col_template='{col_name}',weight='bold')
# Set y limit from 0 to 100 so that 100% heteroplasmy is noticable.
x.set(ylim=(0,100))
plt.savefig(os.path.join(No_1,'Heteroplasmy_clusters_Syn_Strain_Gene.png'), bbox_inches='tight')
# Now ww'll do it for WBH.
x = sns.FacetGrid(data=WBH, col='Strain', col_wrap=1, despine=True,
hue='Strain',palette='colorblind', col_order=hue_order, hue_order=hue_order, aspect=4)
# The graph title is adjusted to be on top of the graph, but not intruding on
# it, and it is emboldened.
x.fig.subplots_adjust(top=0.8)
x.fig.suptitle('Homogenate - Heteroplasmy Clusters', fontsize=28,weight='bold')
# A histplot will be inserted into the facetgrid. X will be Reference
# Position, Y will be Heteroplasmy Cluster. The alpha command represents
# transperancy of the colors. The higher it is, the less transparent.
x.map(sns.histplot, 'Mitochondrial Position', 'Heteroplasmy'
' Cluster',alpha=1)
# Since facetgrids puts the title of each graph with '=', it is changes so that
# only the name without '=' is posted on the title of each graph.
x.set_titles(col_template='{col_name}',weight='bold')
# Set y limit from 0 to 100 so that 100% heteroplasmy is noticable.
x.set(ylim=(0,100))
plt.savefig(os.path.join(No_1,'Heteroplasmy_clusters_WBH_Strain.png'), bbox_inches='tight')
# Now we'll do it for gene name for Syn.
x = sns.FacetGrid(data=WBH, col='Strain', col_wrap=1, despine=True,
hue='Strain',palette='colorblind', col_order=hue_order, hue_order=hue_order, aspect=4)
# The graph title is adjusted to be on top of the graph, but not intruding on
# it, and it is emboldened.
x.fig.subplots_adjust(top=0.8)
x.fig.suptitle('Homogenate - Heteroplasmy Clusters', fontsize=28,weight='bold')
# A histplot will be inserted into the facetgrid. X will be Reference
# Position, Y will be Heteroplasmy Cluster. The alpha command represents
# transperancy of the colors. The higher it is, the less transparent.
x.map(sns.histplot, 'Gene_name', 'Heteroplasmy'
' Cluster',alpha=1)
# Since we want all gene names to be on the graph, the xticklables will be
# rotated 45 degress.
[plt.setp(ax.get_xticklabels(), rotation=45) for ax in x.axes.flat]
# Since facetgrids puts the title of each graph with '=', it is changes so that
# only the name without '=' is posted on the title of each graph.
x.set_titles(col_template='{col_name}',weight='bold')
# Set y limit from 0 to 100 so that 100% heteroplasmy is noticable.
x.set(ylim=(0,100))
plt.savefig(os.path.join(No_1,'Heteroplasmy_clusters_WBH_Strain_Gene.png'), bbox_inches='tight')
# Now we will create separate dataframes with minimum heteroplasmies in order
# to calculate and graph if there are significant differences between the
# strains.
Syn10 = Syn[Syn['Heteroplasmy'] >= 10]
Syn20 = Syn[Syn['Heteroplasmy'] >= 20]
Syn30 = Syn[Syn['Heteroplasmy'] >= 30]
Syn50 = Syn[Syn['Heteroplasmy'] >= 50]
WBH10 = WBH[WBH['Heteroplasmy'] >= 10]
WBH20 = WBH[WBH['Heteroplasmy'] >= 20]
WBH30 = WBH[WBH['Heteroplasmy'] >= 30]
WBH50 = WBH[WBH['Heteroplasmy'] >= 50]
# Create a new column in both Syn and WBH named 'Heteroplasmic Burden'. This
# column is a multiple of the length of mutations per animal multiplied by the
# mean heteroplasmy.
# First we create a column 'Len of name' which give the length (i.e. the number
# of mutations) per animal. This will be done for each strain in a separate
# dataframe because otherwise the entire heteroplasmy gets taken into
# consideration.
WS['Len of name'] = WS.groupby(['Name'])['count'].transform('count')
PS['Len of name'] = PS.groupby(['Name'])['count'].transform('count')
KS['Len of name'] = KS.groupby(['Name'])['count'].transform('count')
W4S['Len of name'] = W4S.groupby(['Name'])['count'].transform('count')
WH['Len of name'] = WH.groupby(['Name'])['count'].transform('count')
PH['Len of name'] = PH.groupby(['Name'])['count'].transform('count')
KH['Len of name'] = KH.groupby(['Name'])['count'].transform('count')
W4H['Len of name'] = W4H.groupby(['Name'])['count'].transform('count')
# Next we create a column 'Heteroplasmic Burden' by multiplying 'Len of name by
# the mean heteroplasmy. * Let's figure out how to get mean of heteroplasmy per
# animal instead of mean of entire strain, probably won't be much different,
# but more precise.
WS['Heteroplasmic Burden'] = WS['Len of name'] * WS['Heteroplasmy'].mean()
PS['Heteroplasmic Burden'] = PS['Len of name'] * PS['Heteroplasmy'].mean()
KS['Heteroplasmic Burden'] = KS['Len of name'] * KS['Heteroplasmy'].mean()
W4S['Heteroplasmic Burden'] = W4S['Len of name'] * W4S['Heteroplasmy'].mean()
WH['Heteroplasmic Burden'] = WH['Len of name'] * WH['Heteroplasmy'].mean()
PH['Heteroplasmic Burden'] = PH['Len of name'] * PH['Heteroplasmy'].mean()
KH['Heteroplasmic Burden'] = KH['Len of name'] * KH['Heteroplasmy'].mean()
W4H['Heteroplasmic Burden'] = W4H['Len of name'] * W4H['Heteroplasmy'].mean()
NewSyn = pd.concat([WS, PS, KS, W4S], axis=0)
NewWBH = pd.concat([WH, PH, KH, W4H], axis=0)
NewSyn.to_excel(os.path.join(syn,'Heteroburdensyn.xlsx'),index=False)
NewWBH.to_excel(os.path.join(wbh,'Heteroburdenwbh.xlsx'),index=False)
# We will create StrainS and StrainW for unique strains in NewSyn and NewWBH
# respectively. Do stats on them and graph them.
StrainS = NewSyn.Strain.unique()
StrainW = NewWBH.Strain.unique()
df3 = []
for s in StrainS:
df3.append(NewSyn[NewSyn['Strain'] == s]['Heteroplasmic Burden'])
F = f_oneway(*df3)
# Cast the f_oneway statistics into a string so that it can then be saved as a
# dataframe. Use the StringIO to implement a file-like class on the string
# ('F') so that it can be read in as a CSV and hence become a dataframe.
F = str(F)
F = StringIO(F)
F = pd.read_csv(F, sep=';')
# Perform groupwise comparisons using tukey HSD
tukey = pairwise_tukeyhsd(endog=NewSyn['Heteroplasmic Burden'], groups=NewSyn['Strain'],
alpha=0.05)
# Save the tukey data as a dataframe and append the F stats from F dataframe
# into it. Finally, export the dataframe to csv as 'Syn_stats.csv'.
tukey = pd.DataFrame(data=tukey._results_table.data[1:],
columns=tukey._results_table.data[0])
tukey = pd.concat([tukey, F])
tukey.to_csv(os.path.join(No_1,'Syn_heteroburden_stats.csv'),index=False)
# In order to annotate the resulting graphs with stars for statistical
# significance, the tukey results need to be put into a simple matrix (just
# showing the p values, not other parameters such as meandiff, lower and upper
# etc).
tukey_df = posthoc_tukey(NewSyn, val_col="Heteroplasmic Burden",
group_col="Strain")
# The matrix needs to be converted to a non-redundant list of comparisons with
# the p-value. This is done by removing the lower half and diagonal of the
# matrix and turning the matrix format into a long dataframe using melt(). The
# code and resulting dataframe are shown below.
remove = np.tril(np.ones(tukey_df.shape), k=0).astype("bool")
tukey_df[remove] = np.nan
molten_df = tukey_df.melt(ignore_index=False).reset_index().dropna()
# x, y, and order are defined so that they can be used in the graphs below.
x = "Strain"
y = 'Heteroplasmic Burden'
order = ['WT', 'Polg', 'Polg-PKO', 'Polg-W402A']
sns.set_style("whitegrid", {'axes.grid' : False})
sns.set_palette('colorblind')
fig, axes = plt.subplots(1, 2,figsize=(10, 5))
fig.suptitle('Average Heteroplasmic Burden', weight='bold')
ax = sns.violinplot(ax=axes[0],data=NewSyn,x=x, y=y,order=order,color='red')
#ax = sns.swarmplot(ax=axes[0], data=NewSyn,x=x, y=y,order=order,color='black',
# size=3)
ax.set_title('Synaptosome')
ax.set_ylim(top=max(NewSyn['Heteroplasmic Burden']) + 2)
# In order to only annotate the graph where there are significant differences,
# the dataframe 'molten_df', which contains the p values, will be filtered so
# that only significant p values (<= 0.05) are in there. Note, if all notations
# are desireable, skip the filtering step.
molten_df = molten_df.loc[molten_df['value'] <= 0.05]
# The pairs for multiple comparisons is defined as all strains in the p value
# table.
pairs = [(i[1]["index"], i[1]["variable"]) for i in molten_df.iterrows()]
# A list of p values is generated from the molten_df dataframe. The annotator
# is then defnied and configured to annotate the graph with stars using the p
# values from the list.
p_values = [i[1]["value"] for i in molten_df.iterrows()]
annotator = Annotator(ax, pairs, data=NewSyn, x=x, y=y, order=order)
annotator.configure(text_format="star", loc="inside")
annotator.set_pvalues_and_annotate(p_values)
# Now for WBH.
df3 = []
for s in StrainW:
df3.append(NewWBH[NewWBH['Strain'] == s]['Heteroplasmic Burden'])
F = f_oneway(*df3)
# Cast the f_oneway statistics into a string so that it can then be saved as a
# dataframe. Use the StringIO to implement a file-like class on the string
# ('F') so that it can be read in as a CSV and hence become a dataframe.
F = str(F)
F = StringIO(F)
F = pd.read_csv(F, sep=';')
# Perform groupwise comparisons using tukey HSD
tukey = pairwise_tukeyhsd(endog=NewWBH['Heteroplasmic Burden'], groups=NewWBH['Strain'],
alpha=0.05)
# Save the tukey data as a dataframe and append the F stats from F dataframe
# into it. Finally, export the dataframe to csv as 'Syn_stats.csv'.
tukey = pd.DataFrame(data=tukey._results_table.data[1:],
columns=tukey._results_table.data[0])
tukey = pd.concat([tukey, F])
tukey.to_csv(os.path.join(No_1,'WBH_heteroburden_stats.csv'),index=False)
# In order to annotate the resulting graphs with stars for statistical
# significance, the tukey results need to be put into a simple matrix (just
# showing the p values, not other parameters such as meandiff, lower and upper
# etc).
tukey_df = posthoc_tukey(NewWBH, val_col="Heteroplasmic Burden",
group_col="Strain")
# The matrix needs to be converted to a non-redundant list of comparisons with
# the p-value. This is done by removing the lower half and diagonal of the
# matrix and turning the matrix format into a long dataframe using melt(). The
# code and resulting dataframe are shown below.
remove = np.tril(np.ones(tukey_df.shape), k=0).astype("bool")
tukey_df[remove] = np.nan
molten_df = tukey_df.melt(ignore_index=False).reset_index().dropna()
ax = sns.violinplot(ax=axes[1],data=NewWBH,x=x, y=y,order=order,color='blue')
#ax = sns.swarmplot(ax=axes[1], data=NewWBH,x=x, y=y,order=order,color='black',
# size=3)
ax.set_title('Homogenate')
ax.set_ylim(top=max(NewWBH['Heteroplasmic Burden']) + 2)
# In order to only annotate the graph where there are significant differences,
# the dataframe 'molten_df', which contains the p values, will be filtered so
# that only significant p values (<= 0.05) are in there. Note, if all notations
# are desireable, skip the filtering step.
molten_df = molten_df.loc[molten_df['value'] <= 0.05]
# The pairs for multiple comparisons is defined as all strains in the p value
# table.
pairs = [(i[1]["index"], i[1]["variable"]) for i in molten_df.iterrows()]
# A list of p values is generated from the molten_df dataframe. The annotator
# is then defnied and configured to annotate the graph with stars using the p
# values from the list.
p_values = [i[1]["value"] for i in molten_df.iterrows()]
annotator = Annotator(ax, pairs, data=NewWBH, x=x, y=y, order=order)
annotator.configure(text_format="star", loc="inside")
annotator.set_pvalues_and_annotate(p_values)
plt.savefig(os.path.join(No_1,'Het_burden.png'))
# Group the dataframes by Name and Strain and aggregate by count in order to be
# able to do stats on them. Reset the index so that the variable is available
# downstream
Syn = Syn.groupby(['Name', 'Strain'],observed=True).count().reset_index()
Syn10 = Syn10.groupby(['Name', 'Strain'],observed=True).count().reset_index()
Syn20 = Syn20.groupby(['Name', 'Strain'],observed=True).count().reset_index()
Syn30 = Syn30.groupby(['Name', 'Strain'],observed=True).count().reset_index()
Syn50 = Syn50.groupby(['Name', 'Strain'],observed=True).count().reset_index()
WBH = WBH.groupby(['Name', 'Strain'],observed=True).count().reset_index()
WBH10 = WBH10.groupby(['Name', 'Strain'],observed=True).count().reset_index()
WBH20 = WBH20.groupby(['Name', 'Strain'],observed=True).count().reset_index()
WBH30 = WBH30.groupby(['Name', 'Strain'],observed=True).count().reset_index()
WBH50 = WBH50.groupby(['Name', 'Strain'],observed=True).count().reset_index()
Syn10.to_excel(os.path.join(syn,'10%SYN.xlsx'),index=False)
Syn20.to_excel(os.path.join(syn,'20%SYN.xlsx'),index=False)
Syn30.to_excel(os.path.join(syn,'30%SYN.xlsx'),index=False)
Syn50.to_excel(os.path.join(syn,'50%SYN.xlsx'),index=False)
WBH10.to_excel(os.path.join(wbh,'10%WBH.xlsx'),index=False)
WBH20.to_excel(os.path.join(wbh,'20%WBH.xlsx'),index=False)
WBH30.to_excel(os.path.join(wbh,'30%WBH.xlsx'),index=False)
WBH50.to_excel(os.path.join(wbh,'50%WBH.xlsx'),index=False)
# Get all unique strains into a variable called 'Strain'. Then create an empty
# dataframe 'df3' and for each unique value in strain, append the df values
# from 'count'. Finally, perform f_oneway statistics on df3.
Strain10 = Syn10.Strain.unique()
Strain20 = Syn20.Strain.unique()
Strain30 = Syn30.Strain.unique()
Strain50 = Syn50.Strain.unique()
df3 = []
for s in Strain10:
df3.append(Syn10[Syn10['Strain'] == s]['count'])
F = f_oneway(*df3)
# Cast the f_oneway statistics into a string so that it can then be saved as a
# dataframe. Use the StringIO to implement a file-like class on the string
# ('F') so that it can be read in as a CSV and hence become a dataframe.
F = str(F)
F = StringIO(F)
F = pd.read_csv(F, sep=';')
# Perform groupwise comparisons using tukey HSD
tukey = pairwise_tukeyhsd(endog=Syn10['count'], groups=Syn10['Strain'],
alpha=0.05)
# Save the tukey data as a dataframe and append the F stats from F dataframe
# into it. Finally, export the dataframe to csv as 'Syn_stats.csv'.
tukey = pd.DataFrame(data=tukey._results_table.data[1:],
columns=tukey._results_table.data[0])
tukey = pd.concat([tukey, F])
tukey.to_csv(os.path.join(No_1,'Syn10_stats.csv'),index=False)
# In order to annotate the resulting graphs with stars for statistical
# significance, the tukey results need to be put into a simple matrix (just
# showing the p values, not other parameters such as meandiff, lower and upper
# etc).
tukey_df = posthoc_tukey(Syn10, val_col="count",
group_col="Strain")
# The matrix needs to be converted to a non-redundant list of comparisons with
# the p-value. This is done by removing the lower half and diagonal of the
# matrix and turning the matrix format into a long dataframe using melt(). The
# code and resulting dataframe are shown below.
remove = np.tril(np.ones(tukey_df.shape), k=0).astype("bool")
tukey_df[remove] = np.nan
molten_df = tukey_df.melt(ignore_index=False).reset_index().dropna()
# x, y, and order are defined so that they can be used in the graphs below.
x = "Strain"
y = 'count'
order = ['Polg', 'Polg-PKO', 'Polg-W402A']
sns.set_style("whitegrid", {'axes.grid' : False})
sns.set_palette('colorblind')
fig, axes = plt.subplots(2, 2,figsize=(17, 10))
fig.suptitle('Synaptosomes', weight='bold')
ax = sns.barplot(ax=axes[0,0],data=Syn10,x=x, y=y,order=order,
facecolor=('green'),edgecolor='.2')
ax.set_title('Minimum 10% Heteroplasmy')
# Add a swarmplot to visualize the individual datapoints on the barplot. Color
# it black so that the points are easy to spot.
ax = sns.swarmplot(ax=axes[0,0],data=Syn10,x=x, y=y,color='.2', order=order)
ax.set_ylabel('Number of Mutations')
ax.set_ylim(top=max(Syn10['count']) + 2)
# In order to only annotate the graph where there are significant differences,
# the dataframe 'molten_df', which contains the p values, will be filtered so
# that only significant p values (<= 0.05) are in there. Note, if all notations
# are desireable, skip the filtering step.
molten_df = molten_df.loc[molten_df['value'] <= 0.05]
# The pairs for multiple comparisons is defined as all strains in the p value
# table.
pairs = [(i[1]["index"], i[1]["variable"]) for i in molten_df.iterrows()]
# A list of p values is generated from the molten_df dataframe. The annotator
# is then defnied and configured to annotate the graph with stars using the p
# values from the list.
p_values = [i[1]["value"] for i in molten_df.iterrows()]
annotator = Annotator(ax, pairs, data=Syn10, x=x, y=y, order=order)
annotator.configure(text_format="star", loc="inside")
annotator.set_pvalues_and_annotate(p_values)
df3 = []
for s in Strain20:
df3.append(Syn20[Syn20['Strain'] == s]['count'])
F = f_oneway(*df3)
# Cast the f_oneway statistics into a string so that it can then be saved as a
# dataframe. Use the StringIO to implement a file-like class on the string
# ('F') so that it can be read in as a CSV and hence become a dataframe.
F = str(F)
F = StringIO(F)
F = pd.read_csv(F, sep=';')
# Perform groupwise comparisons using tukey HSD
tukey = pairwise_tukeyhsd(endog=Syn20['count'], groups=Syn20['Strain'],
alpha=0.05)
# Save the tukey data as a dataframe and append the F stats from F dataframe
# into it. Finally, export the dataframe to csv as 'Syn_stats.csv'.
tukey = pd.DataFrame(data=tukey._results_table.data[1:],
columns=tukey._results_table.data[0])
tukey = pd.concat([tukey, F])
tukey.to_csv(os.path.join(No_1,'Syn20_stats.csv'),index=False)
# In order to annotate the resulting graphs with stars for statistical
# significance, the tukey results need to be put into a simple matrix (just
# showing the p values, not other parameters such as meandiff, lower and upper
# etc).
tukey_df = posthoc_tukey(Syn20, val_col="count",
group_col="Strain")
# The matrix needs to be converted to a non-redundant list of comparisons with
# the p-value. This is done by removing the lower half and diagonal of the
# matrix and turning the matrix format into a long dataframe using melt(). The
# code and resulting dataframe are shown below.
remove = np.tril(np.ones(tukey_df.shape), k=0).astype("bool")
tukey_df[remove] = np.nan
molten_df = tukey_df.melt(ignore_index=False).reset_index().dropna()
# x, y, and order are defined so that they can be used in the graphs below.
x = "Strain"
y = 'count'
order = ['Polg', 'Polg-PKO', 'Polg-W402A']
sns.set_style("whitegrid", {'axes.grid' : False})
ax = sns.barplot(ax=axes[0,1],data=Syn20,x=x, y=y,order=order,
facecolor=('blue'),edgecolor='.2')
ax.set_title('Minimum 20% Heteroplasmy')
# Add a swarmplot to visualize the individual datapoints on the barplot. Color
# it black so that the points are easy to spot.
ax = sns.swarmplot(ax=axes[0,1],data=Syn20,x=x, y=y,color='.2', order=order)
ax.set_ylabel('Number of Mutations')
ax.set_ylim(top=max(Syn20['count']) + 2)
# In order to only annotate the graph where there are significant differences,
# the dataframe 'molten_df', which contains the p values, will be filtered so
# that only significant p values (<= 0.05) are in there. Note, if all notations
# are desireable, skip the filtering step.
molten_df = molten_df.loc[molten_df['value'] <= 0.05]
# The pairs for multiple comparisons is defined as all strains in the p value
# table.
pairs = [(i[1]["index"], i[1]["variable"]) for i in molten_df.iterrows()]
# A list of p values is generated from the molten_df dataframe. The annotator
# is then defnied and configured to annotate the graph with stars using the p
# values from the list.
p_values = [i[1]["value"] for i in molten_df.iterrows()]
annotator = Annotator(ax, pairs, data=Syn20, x=x, y=y, order=order)
annotator.configure(text_format="star", loc="inside")
annotator.set_pvalues_and_annotate(p_values)
df3 = []
for s in Strain30:
df3.append(Syn30[Syn30['Strain'] == s]['count'])
F = f_oneway(*df3)
# Cast the f_oneway statistics into a string so that it can then be saved as a
# dataframe. Use the StringIO to implement a file-like class on the string
# ('F') so that it can be read in as a CSV and hence become a dataframe.
F = str(F)
F = StringIO(F)
F = pd.read_csv(F, sep=';')
# Perform groupwise comparisons using tukey HSD
tukey = pairwise_tukeyhsd(endog=Syn30['count'], groups=Syn30['Strain'],
alpha=0.05)
# Save the tukey data as a dataframe and append the F stats from F dataframe
# into it. Finally, export the dataframe to csv as 'Syn_stats.csv'.
tukey = pd.DataFrame(data=tukey._results_table.data[1:],
columns=tukey._results_table.data[0])
tukey = pd.concat([tukey, F])
tukey.to_csv(os.path.join(No_1,'Syn30_stats.csv'),index=False)
# In order to annotate the resulting graphs with stars for statistical
# significance, the tukey results need to be put into a simple matrix (just
# showing the p values, not other parameters such as meandiff, lower and upper
# etc).
tukey_df = posthoc_tukey(Syn30, val_col="count",
group_col="Strain")
# The matrix needs to be converted to a non-redundant list of comparisons with
# the p-value. This is done by removing the lower half and diagonal of the
# matrix and turning the matrix format into a long dataframe using melt(). The
# code and resulting dataframe are shown below.
remove = np.tril(np.ones(tukey_df.shape), k=0).astype("bool")
tukey_df[remove] = np.nan
molten_df = tukey_df.melt(ignore_index=False).reset_index().dropna()
# x, y, and order are defined so that they can be used in the graphs below.
x = "Strain"
y = 'count'
order = ['Polg', 'Polg-PKO', 'Polg-W402A']
sns.set_style("whitegrid", {'axes.grid' : False})
ax = sns.barplot(ax=axes[1,0],data=Syn30,x=x, y=y,order=order,
facecolor=('purple'),edgecolor='.2')
ax.set_title('Minimum 30% Heteroplasmy')
# Add a swarmplot to visualize the individual datapoints on the barplot. Color
# it black so that the points are easy to spot.
ax = sns.swarmplot(ax=axes[1,0],data=Syn30,x=x, y=y,color='.2', order=order)
ax.set_ylabel('Number of Mutations')
ax.set_ylim(top=max(Syn30['count']) + 2)
# In order to only annotate the graph where there are significant differences,
# the dataframe 'molten_df', which contains the p values, will be filtered so
# that only significant p values (<= 0.05) are in there. Note, if all notations
# are desireable, skip the filtering step.
molten_df = molten_df.loc[molten_df['value'] <= 0.05]
# The pairs for multiple comparisons is defined as all strains in the p value
# table.
pairs = [(i[1]["index"], i[1]["variable"]) for i in molten_df.iterrows()]
# A list of p values is generated from the molten_df dataframe. The annotator
# is then defnied and configured to annotate the graph with stars using the p
# values from the list.
p_values = [i[1]["value"] for i in molten_df.iterrows()]
annotator = Annotator(ax, pairs, data=Syn30, x=x, y=y, order=order)
annotator.configure(text_format="star", loc="inside")
annotator.set_pvalues_and_annotate(p_values)
# x, y, and order are defined so that they can be used in the graphs below.
x = "Strain"
y = 'count'
order = ['Polg', 'Polg-PKO', 'Polg-W402A']
sns.set_style("whitegrid", {'axes.grid' : False})
ax = sns.barplot(ax=axes[1,1],data=Syn50,x=x, y=y,order=order,color='red')
# facecolor=('red'),edgecolor='.2')
ax.set_title('Minimum 50% Heteroplasmy')
# Add a swarmplot to visualize the individual datapoints on the barplot. Color
# it black so that the points are easy to spot.
ax = sns.swarmplot(ax=axes[1,1],data=Syn50,x=x, y=y,color='.2', order=order)
ax.set_ylabel('Number of Mutations')
ax.set_ylim(top=max(Syn50['count']) + 2)
# In order to only annotate the graph where there are significant differences,
# the dataframe 'molten_df', which contains the p values, will be filtered so
# that only significant p values (<= 0.05) are in there. Note, if all notations
# are desireable, skip the filtering step.
molten_df = molten_df.loc[molten_df['value'] <= 0.05]
# The pairs for multiple comparisons is defined as all strains in the p value
# table.
pairs = [(i[1]["index"], i[1]["variable"]) for i in molten_df.iterrows()]