-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1165 lines (995 loc) · 43.5 KB
/
index.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Quantification of phytochemicals in African nightshade leaves using UHPLC-QqQ-MS/MS</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
.tocify-subheader {
display: inline;
}
.tocify-subheader .tocify-item {
font-size: 0.95em;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">African Nightshades</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">R script</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Quantification of phytochemicals in African nightshade leaves using UHPLC-QqQ-MS/MS</h1>
</div>
<p><br></p>
<p><strong>Bo Yuan </strong> <em>Oct 2019 updated</em></p>
<br>
<p>The R code has been developed with reference to <a href="https://r4ds.hadley.nz/">R for Data Science (2e)</a>, and the
official documentation of <a href="https://www.tidyverse.org/">tidyverse</a>, and <a href="https://www.databrewer.co/"><strong>DataBrewer.co</strong></a>.
See breakdown of modules below:</p>
<ul>
<li><p><strong>Data visualization</strong> with <strong>ggplot2</strong> (<a href="https://www.databrewer.co/R/visualization/introduction">tutorial</a>
of the fundamentals; and <a href="https://www.databrewer.co/R/gallery">data
viz. gallery</a>).</p></li>
<li><p><a href="https://www.databrewer.co/R/data-wrangling"><strong>Data
wrangling</strong> </a> with the following packages: <a href="https://www.databrewer.co/R/data-wrangling/tidyr/introduction"><strong>tidyr</strong></a>,
transform (e.g., pivoting) the dataset into tidy structure; <a href="https://www.databrewer.co/R/data-wrangling/dplyr/0-introduction"><strong>dplyr</strong></a>,
the basic tools to work with data frames; <a href="https://www.databrewer.co/R/data-wrangling/stringr/0-introduction"><strong>stringr</strong></a>,
work with strings; <a href="https://www.databrewer.co/R/data-wrangling/regular-expression/0-introduction"><strong>regular
expression</strong></a>: search and match a string pattern; <a href="https://www.databrewer.co/R/data-wrangling/purrr/introduction"><strong>purrr</strong></a>,
functional programming (e.g., iterating functions across elements of
columns); and <a href="https://www.databrewer.co/R/data-wrangling/tibble/introduction"><strong>tibble</strong></a>,
work with data frames in the modern tibble structure.</p></li>
</ul>
<p><br></p>
<pre class="r"><code>library(readxl)
library(tidyr)
library(dplyr)
library(stringr)
library(rebus)
library(ggplot2)
library(ComplexHeatmap)
library(circlize)
library(RColorBrewer)
library(gridExtra)
library(cowplot)</code></pre>
<pre class="r"><code>path = "/Users/Boyuan/Desktop/Manuscript files/5th_AIV leaf QQQ_JFC/Oct 2019 JFC/NS_leaf_PhytochemQQQ dataset.xlsx"</code></pre>
<div id="contrast-analysis" class="section level1">
<h1><span class="header-section-number">1</span> Contrast analysis</h1>
<div id="tidy-up" class="section level2">
<h2><span class="header-section-number">1.1</span> Tidy up</h2>
<pre class="r"><code>d = read_excel(path, sheet = "Contrast.analysis")
# gather compounds
d = d %>%
gather(`chlorogenic acid`, quercetin, kaempferol, rhamnetin, isorhamnetin, solasodine, diosgenin, tigogenin,
key = compound, value = data)
# replace N.D. with zero values
d$data = d$data %>% str_replace(pattern = "N.D.", replacement = "0 ± 0")
# plit data into mean and standard deviation
d = d %>% separate(data, into = c("mean", "std"), sep = " ± ")
# convert mean and std to numeric value
d$mean = d$mean %>% as.numeric()
d$std = d$std %>% as.numeric()
# arrange order of display
d$compound = d$compound %>%
factor(levels = (c("chlorogenic acid", "quercetin", "kaempferol", "rhamnetin",
"isorhamnetin", "solasodine", "diosgenin", "tigogenin")),
ordered = T)
# NOTICE HERE!!
d = d %>% filter(ID != "PI 312110" & compound != "rhamnetin")
d = d %>% filter(compound != "rhamnetin")</code></pre>
</div>
<div id="visualization" class="section level2">
<h2><span class="header-section-number">1.2</span> Visualization</h2>
<div id="define-plotting-function" class="section level3">
<h3><span class="header-section-number">1.2.1</span> Define plotting function</h3>
<pre class="r"><code># define plot function
myplot = function(dataset, category){
dataset %>%
ggplot(aes_string(x = 1, y = "mean", color = category, fill = category), alpha = 0.2) +
geom_boxplot(alpha = .2, outlier.alpha = 0) +
facet_wrap(~compound, nrow = 1, strip.position = "bottom", scales = "free") +
# format
theme_classic() +
theme(axis.text = element_text(color = "black", size = 12),
axis.title = element_text(face = "bold"),
legend.title = element_blank(), legend.position = "right",
legend.text = element_text(colour = "black", size = 12),
strip.background = element_blank(), strip.text = element_text(face = "bold"),
# remove all y-axis related (not for plot, not for aesthetic)
axis.text.x = element_blank(), axis.line.x = element_blank(),
axis.ticks.x = element_blank(), axis.title.x = element_blank(),
axis.title.y = element_text(size = 12, face = "bold")) +
# add mean!
stat_summary(fun.y = mean, geom = "point", shape = 23,
position = position_jitterdodge(0),
color = "black", size = 3, stroke = 0.8)+
scale_y_continuous(breaks = scales::pretty_breaks(7),
limits = c(NA, NA)) + labs(y = "Content (mg/100g DW)") +
scale_color_brewer(palette = ifelse(category == "Species", "Set1", "Set2")) +
scale_fill_brewer(palette = ifelse(category == "Species", "Set1", "Set2")) +
geom_text(aes(label = `Sample No.`),
position = position_jitterdodge(0.25), size = 3.4, fontface = "bold")
}</code></pre>
</div>
<div id="plotting" class="section level3">
<h3><span class="header-section-number">1.2.2</span> Plotting</h3>
<pre class="r"><code># SPECIES CONTRAST
# There are four accessions of S. nigrum. Only one planted in Kenya, while all four planted in RU.
# THus compare those planted in RU.
# This dataframe contains species planted in RU: 3 nigrumw with the peculiar nigrum USDA 312110 excluded), & 8 scabrum
species.contrast.df = d %>% filter(`CultivationSite` == "RU" & Species != "N.D.")
plt.species = myplot(species.contrast.df, category = "Species")
# Cultivation site (environment) CONTRAST
# Only those IDs cultivated both in RU and Kenya are included for comparison.
# The comparison includes a total of 6 comparisions, all from from scabru
selected.ID = c("BG 29", "Ex Hai", "BG 16", "SS 49 (Olevolosi)", "SS 04.2", "SS 52")
cultivationSite.contrast.df = d %>% filter(ID %in% selected.ID)
plt.cultivationSite = myplot(cultivationSite.contrast.df, category = "CultivationSite")
# combining two plot together
grid.arrange(plt.species, plt.cultivationSite, nrow = 2)</code></pre>
<p><img src="index_files/figure-html/unnamed-chunk-5-1.png" width="1152" /></p>
</div>
</div>
<div id="scheffes-contrast" class="section level2">
<h2><span class="header-section-number">1.3</span> Scheffe’s contrast</h2>
<div id="define-contrast-function" class="section level3">
<h3><span class="header-section-number">1.3.1</span> Define contrast function</h3>
<pre class="r"><code># category = "Species", group1 = "S. nigrum", group2 = "S. scabrum", alpha = 0.5, etc.
# There should be only two groups in a given category
calculateP = function(loop.df, alpha, category, group1, group2){
n.group1 = ( loop.df[[category]] == group1 ) %>% sum()
n.group2 = ( loop.df[[category]] == group2) %>% sum()
# print contrasted groups of the given category
print(paste("Contrast =", group1, "-", group2))
# set up Ci and Ci-related terms
Ci = c()
for (i in 1:nrow(loop.df)){
if( loop.df[[category]] [i] == group1){Ci[i] = (1/n.group1)
} else { Ci[i] = - 1/ n.group2 }
}
Contrast = sum(Ci * loop.df$mean)
`Sum.Ci^2` = sum(Ci^2)
# MSE and critical value
n.treatment = nrow(loop.df)
df.error = (3-1) * n.treatment
SSE = ((loop.df$std)^2*(3-1)) %>% sum()
MSE = SSE / df.error
critic.S = sqrt( MSE/3 * `Sum.Ci^2` ) *
sqrt( (n.treatment - 1) * qf(p = 1- alpha, df1 = (n.treatment - 1), df2 = df.error) )
# notice alpha input
# Significant or not?
ifsignificant = ifelse(
abs(Contrast) >= critic.S, "Yes! Significant!", "NO...."
)
# print
cat(unique(loop.df$compound) %>%
as.character(), # factor as character, otherwise output level of factor
": Critical value = ", round(critic.S, 3),
", Contrast =", round(Contrast, 3), ifsignificant, "\n\n")
}</code></pre>
</div>
<div id="calculate-contrast-statistics" class="section level3">
<h3><span class="header-section-number">1.3.2</span> Calculate contrast statistics</h3>
<pre class="r"><code># compounds to loop through
myCompounds = d$compound %>% unique()</code></pre>
<div id="contrast-between-species" class="section level4">
<h4><span class="header-section-number">1.3.2.1</span> contrast between species</h4>
<pre class="r"><code>for (i in myCompounds){
loop.df = species.contrast.df %>% filter(compound == i)
calculateP(loop.df, alpha = 0.6,
category = "Species", group1 = "S. nigrum", group2 = "S. scabrum")
}</code></pre>
<pre><code>## [1] "Contrast = S. nigrum - S. scabrum"
## chlorogenic acid : Critical value = 31.932 , Contrast = 11.372 NO....
##
## [1] "Contrast = S. nigrum - S. scabrum"
## quercetin : Critical value = 26.757 , Contrast = 13.824 NO....
##
## [1] "Contrast = S. nigrum - S. scabrum"
## kaempferol : Critical value = 9.031 , Contrast = -10.293 Yes! Significant!
##
## [1] "Contrast = S. nigrum - S. scabrum"
## isorhamnetin : Critical value = 0.703 , Contrast = 0.869 Yes! Significant!
##
## [1] "Contrast = S. nigrum - S. scabrum"
## solasodine : Critical value = 1.667 , Contrast = 9.723 Yes! Significant!
##
## [1] "Contrast = S. nigrum - S. scabrum"
## diosgenin : Critical value = 5.541 , Contrast = 8.8 Yes! Significant!
##
## [1] "Contrast = S. nigrum - S. scabrum"
## tigogenin : Critical value = 32.492 , Contrast = 32.646 Yes! Significant!</code></pre>
</div>
<div id="contrast-between-cultivation-site" class="section level4">
<h4><span class="header-section-number">1.3.2.2</span> contrast between cultivation site</h4>
<pre class="r"><code>for (i in myCompounds){
loop.df = cultivationSite.contrast.df %>% filter(compound == i)
calculateP(loop.df, alpha = 0.001,
category = "CultivationSite", group1 = "RU", group2 = "Kenya")
}</code></pre>
<pre><code>## [1] "Contrast = RU - Kenya"
## chlorogenic acid : Critical value = 77.88 , Contrast = 58.585 NO....
##
## [1] "Contrast = RU - Kenya"
## quercetin : Critical value = 73.267 , Contrast = -62.11 NO....
##
## [1] "Contrast = RU - Kenya"
## kaempferol : Critical value = 17.43 , Contrast = -3.432 NO....
##
## [1] "Contrast = RU - Kenya"
## isorhamnetin : Critical value = 1.609 , Contrast = -0.672 NO....
##
## [1] "Contrast = RU - Kenya"
## solasodine : Critical value = 0.014 , Contrast = 0.018 Yes! Significant!
##
## [1] "Contrast = RU - Kenya"
## diosgenin : Critical value = 3.233 , Contrast = 9.95 Yes! Significant!
##
## [1] "Contrast = RU - Kenya"
## tigogenin : Critical value = 80.1 , Contrast = 141.198 Yes! Significant!</code></pre>
</div>
</div>
</div>
</div>
<div id="variance-partition" class="section level1">
<h1><span class="header-section-number">2</span> Variance partition</h1>
<div id="effect-of-plant-varieties" class="section level2">
<h2><span class="header-section-number">2.1</span> Effect of plant varieties</h2>
<pre class="r"><code># SS between accessions
SS.accessions = species.contrast.df %>%
group_by(compound, Species) %>%
mutate(Species.mean = mean(mean),
SS.accessions = (mean - Species.mean)^2*3) %>%
group_by(compound) %>%
summarise(SS.accessions = sum(SS.accessions))
# SS between species
species.count = (species.contrast.df %>%
filter(compound == "chlorogenic acid"))$Species %>%
table() %>% as.data.frame() %>%
rename(Species = ".", counts = Freq)
SS.species = species.contrast.df %>%
group_by(compound) %>% mutate(grandmean = mean(mean)) %>%
dplyr::group_by(compound, Species) %>%
summarise(Species.mean = mean(mean),
grandmean = unique(grandmean)) %>%
inner_join(species.count, by = "Species") %>%
mutate(SS = (Species.mean - grandmean)^2 * counts * 3) %>%
group_by(compound) %>% summarise(SS.species = sum(SS))</code></pre>
<pre><code>## Warning: Column `Species` joining character vector and factor, coercing
## into character vector</code></pre>
<pre class="r"><code>## SSE
SSE = species.contrast.df %>% mutate(diff.square = std^2 * (3-1)) %>%
group_by(compound) %>% summarise(SSE = sum(diff.square))
## combine SS together to get the SST
SS.df = cbind(SS.species, SS.accessions, SSE)
SS.df = SS.df[, !(SS.df %>% colnames() %>% duplicated())] %>%
mutate(SST = SS.species + SS.accessions + SSE)</code></pre>
<pre class="r"><code># import most original data (showing triplicated measurements)
raw = read_excel(path, sheet = "R_plant conc.mg 100g.DW") %>%
filter(ID != "PI 312110")
# Calculate the generic SST to confirm calculation correctness
raw.species = raw %>% filter(`CultivationSite` == "RU" & Species != "N.D." ) %>%
gather(quercetin, kaempferol, rhamnein, isorhamnetin, solasodine, diosgenin, tigogenin,
key = compound, value = content)
raw.species.SST = raw.species %>%
group_by(compound) %>%
mutate(SST = (content - mean(content))^2 ) %>%
summarise(SST.generic = sum(SST))
# This basically confirms the correctness of my calculation.
# The larger the content magnitude, the less susceptible the calculation is to rounding error
inner_join(SS.df, raw.species.SST) %>%
mutate(error.percent = (SST - SST.generic)/SST * 100)</code></pre>
<pre><code>## Joining, by = "compound"</code></pre>
<pre><code>## Warning: Column `compound` joining factor and character vector, coercing
## into character vector</code></pre>
<pre><code>## compound SS.species SS.accessions SSE SST
## 1 quercetin 1250.886005 429603.7033 12322.1750 443176.7643
## 2 kaempferol 693.396368 6155.0267 1403.8180 8252.2410
## 3 isorhamnetin 4.940028 405.0009 8.5116 418.4525
## 4 solasodine 618.775256 704.4920 47.8488 1371.1160
## 5 diosgenin 506.832001 8972.3888 528.4644 10007.6852
## 6 tigogenin 6975.821023 735140.5134 18170.2368 760286.5713
## SST.generic error.percent
## 1 443172.1504 0.001041076
## 2 9316.0197 -12.890784976
## 3 418.6207 -0.040209016
## 4 999.7155 27.087460461
## 5 10008.1637 -0.004781342
## 6 760276.0548 0.001383225</code></pre>
<pre class="r"><code># Now plot the partitioned variance for each compound!!!
SS.df = SS.df %>% gather(-1, key = Source, value = value)
SS.df$Source = SS.df$Source %>%
str_replace(pattern = "SS.species", replacement = "Species")
SS.df$Source = SS.df$Source %>%
str_replace(pattern = "SS.accessions", replacement = "Accessions")
SS.df$Source = SS.df$Source %>%
str_replace(pattern = "SSE", replacement = "Measurement")
source.levels = data.frame(Source = c("Species", "Accessions", "Measurement"),
level = c(1, 2, 3))
SS.df = inner_join(SS.df, source.levels)
SS.df$Source = SS.df$Source %>%
factor(levels = c("Species", "Accessions", "SST", "Measurement"))
SS.df$compound = SS.df$compound %>%
factor(levels = (c("chlorogenic acid", "quercetin", "kaempferol", "rhamnetin",
"isorhamnetin", "solasodine", "diosgenin", "tigogenin")),
ordered = T)
# add contribution percent
SS.df = SS.df %>% filter(Source != "SST") %>%
group_by(compound) %>%
mutate(percent = (value/sum(value)* 100) %>% round(1))
plt.species.vairance.partition = SS.df %>% filter(Source != "SST") %>%
ggplot(aes(x = compound, y = percent, fill = Source)) +
geom_bar(stat = "identity", position = "stack", alpha = 0.7,
color = "black", size = 0.1) +
theme_classic() +
theme(axis.text = element_text(color = "black", size = 12),
axis.title = element_blank(),
legend.title = element_blank(),
legend.text = element_text(size = 12)) +
scale_fill_brewer(palette = "OrRd") +
geom_text(aes(label = percent),
position = position_stack(0.5),
color = "black", size = 3.1)
plt.species.vairance.partition</code></pre>
<p><img src="index_files/figure-html/unnamed-chunk-12-1.png" width="960" /></p>
</div>
<div id="effect-of-cultivation-environment" class="section level2">
<h2><span class="header-section-number">2.2</span> Effect of cultivation environment</h2>
<pre class="r"><code># SS of accessions
cultivationSite.contrast.df = cultivationSite.contrast.df %>%
group_by(compound) %>%
mutate(grandmean = mean(mean)) %>%
group_by(compound, ID) %>%
mutate(accession.mean = mean(mean),
SS.accession = (accession.mean - grandmean)^2*3)
# SS of environment
cultivationSite.contrast.df = cultivationSite.contrast.df %>%
group_by(compound, CultivationSite) %>%
mutate(cultivationSite.mean = mean(mean),
SS.cultivationSite = (cultivationSite.mean - grandmean)^2*3)
# SS of accession & environment interaction
cultivationSite.contrast.df = cultivationSite.contrast.df %>%
group_by(compound, CultivationSite, ID) %>%
mutate(SS.interaction = (mean + grandmean - accession.mean - cultivationSite.mean)^2*3)
# SSE
cultivationSite.contrast.df = cultivationSite.contrast.df %>%
mutate(SSE = (std^2) * (3-1))
# SST
cultivationSite.contrast.df = cultivationSite.contrast.df %>%
mutate(SST = SSE + SS.cultivationSite + SS.accession + SS.interaction)
# Sum rows up for final SS
cultivationSite.SS.summary = cultivationSite.contrast.df %>%
group_by(compound) %>%
summarise(SSE = sum(SSE),
SS.cultivationSite = sum(SS.cultivationSite),
SS.accession = sum(SS.accession),
SS.interaction = sum(SS.interaction),
SST = sum(SST))
# check with generic SST to confirm correctness
# recall "selected.ID" variable storing selected IDs for cultivation/environment comparision
raw.CultivationSite = raw %>%
filter(ID %in% selected.ID) %>%
gather(quercetin, kaempferol, rhamnein, isorhamnetin, solasodine, diosgenin, tigogenin,
key = compound, value = content)
raw.CultivationSite.SST = raw.CultivationSite %>%
group_by(compound) %>% mutate(SST = (content - mean(content))^2 ) %>%
summarise(SST.generic = sum(SST))
# plot Variance partition !!!
cultivationSite.SS.summary = cultivationSite.SS.summary %>%
gather(-1, key = source, value = SS)
cultivationSite.SS.summary$source = cultivationSite.SS.summary$source %>%
str_replace(pattern = "SSE", replacement = "Measurement")
cultivationSite.SS.summary$source = cultivationSite.SS.summary$source %>%
str_replace(pattern = "SS.cultivationSite", replacement = "Environment")
cultivationSite.SS.summary$source = cultivationSite.SS.summary$source %>%
str_replace(pattern = "SS.accession", replacement = "Accession")
cultivationSite.SS.summary$source = cultivationSite.SS.summary$source %>%
str_replace(pattern = "SS.interaction", replacement = "Interaction")
# display order
cultivationSite.SS.summary$source = cultivationSite.SS.summary$source %>%
factor(levels = c("Environment", "Accession", "Interaction", "Measurement"))
cultivationSite.SS.summary$compound = cultivationSite.SS.summary$compound %>%
factor(levels = (c("chlorogenic acid", "quercetin", "kaempferol", "rhamnetin",
"isorhamnetin", "solasodine", "diosgenin", "tigogenin")), ordered = T)
# contribution percent
cultivationSite.SS.summary = cultivationSite.SS.summary %>%
filter(source != "SST") %>%
group_by(compound) %>%
mutate(percent = (SS/sum(SS)*100) %>% round(1))
# plotting
plt.cultivationSite.variance.partition = cultivationSite.SS.summary %>%
ggplot(aes(x = compound, y = percent, fill = source)) +
geom_bar(stat = "identity", position = "stack", alpha = 0.7,
color = "black", size = 0.1) +
theme_classic() +
theme(axis.text = element_text(color = "black", size = 12),
axis.title = element_blank(),
legend.title = element_blank(),
legend.text = element_text(size = 12))+
scale_fill_brewer(palette = "YlGn") +
geom_text(aes(label = percent), position = position_stack(0.5), color = "black", size = 3.1)
plt.cultivationSite.variance.partition</code></pre>
<p><img src="index_files/figure-html/unnamed-chunk-13-1.png" width="960" /></p>
</div>
</div>
<div id="content-profile-heatmap" class="section level1">
<h1><span class="header-section-number">3</span> Content profile heatmap</h1>
<div id="warm-up" class="section level2">
<h2><span class="header-section-number">3.1</span> Warm up</h2>
<pre class="r"><code># set uniform parameter for heatmap
bar_width = 3 # for row and column color side bar
# read data
path <- "/Users/Boyuan/Desktop/My publication/5th. N.S. leaf quant QqQ/R data analysis manuscript data.xlsx"
data <- lapply(excel_sheets(path), read_excel, path = path)
content_df <- data[[1]] %>% as.data.frame()
sample_df <- data[[2]] %>% as.data.frame()
# simplify sample ID name in sample_df
sample_df$ID[7:8] <- rep("SS 49", 2)
sample_df$ID <- str_replace_all(sample_df$ID, pattern = "-", replacement = "NA")
# remove deviation values in content_df
pattern = "±" %R% optional(SPC) %R% one_or_more(DGT) %R% DOT %R% one_or_more(DGT)
content_mat <- vapply(content_df, str_replace, pattern = pattern, replacement = "",
character(length = nrow(content_df))) # vapply convert dataframe to matrix
# order columns in alphabetical order
hm_mat <- content_mat[, -1][, colnames(content_df)[2: ncol(content_df)] %>% order()]
# add row names
# matrix allow identical row names but data frame does not allow identical row names
rownames(hm_mat) <- sample_df$ID
# add sample code number
rownames(hm_mat) = str_c(rownames(hm_mat), paste0("_", 1:20))
# type conversion: character to numeric
# set up matrix to be fill up
hm_mat_dbl <- matrix(1: (ncol(hm_mat) * nrow(hm_mat)), nrow = nrow(hm_mat))
rownames(hm_mat_dbl) <- rownames(hm_mat)
colnames(hm_mat_dbl) <- colnames(hm_mat)
for (i in 1: nrow(hm_mat)) {
for (j in 1: ncol(hm_mat)) {
hm_mat_dbl[i, j] <- as.numeric(hm_mat[i, j])
}
}
# check warning numbers equal to counts of undetected entries
# total number of "N.D." values for undetected levels
# sum(hm_mat == "N.D.")
# value transformation
minimum_value <- min(hm_mat_dbl, na.rm = T)
hm_mat_dbl_NAzero <- replace_na(hm_mat_dbl, replace = 0)
hm_mat_transformed <- log10(hm_mat_dbl_NAzero + minimum_value/2) %>% round(digits = 3)
# color of main plot
myblue <- colorRampPalette(c("white",brewer.pal(9, "Blues"), "black"))(50)
# pie(rep(1, length(myblue)), col = myblue)
myblue_ramp2 <- colorRamp2(seq(from = log10(minimum_value),
to = max(hm_mat_transformed),
length.out = 50), myblue)</code></pre>
</div>
<div id="column-annotation" class="section level2">
<h2><span class="header-section-number">3.2</span> column annotation</h2>
<pre class="r"><code># convert compound to class
cmpd_class_code <- c("chlorogenic acid" = "phenolic acid",
"diosgenin" = "saponin",
"isorhamnetin" = "flavonol",
"kaempferol" = "flavonol",
"quercetin" = "flavonol",
"rhamnetin" = "flavonol",
"solasodine" = "alkaloid",
"tigogenin" = "saponin")
cmpd_class <- cmpd_class_code[colnames(hm_mat_transformed)]
# convert class to color
Set1 <- brewer.pal(9, "Set1")
# pie(rep(1, length(Set1)), col = Set1) # 9 being the darkest color
class_color_code <- c("phenolic acid" = Set1[5],
"flavonol" = Set1[3],
"saponin" = "steelblue",
"alkaloid" = "firebrick")
class_color <- class_color_code[cmpd_class]
# make column sidebar
col_anno <- HeatmapAnnotation(
cmpd.class = cmpd_class,
col = list(cmpd_class = class_color_code),
annotation_legend_param = list(title = "compound category"),
# boxplot
boxplot = anno_boxplot(hm_mat_transformed,
gp = gpar(fill = class_color),axis = T),
annotation_height = unit.c(unit(bar_width, "mm"), unit(20, "mm")))</code></pre>
</div>
<div id="row-annotations" class="section level2">
<h2><span class="header-section-number">3.3</span> Row annotations</h2>
<div id="species" class="section level3">
<h3><span class="header-section-number">3.3.1</span> species</h3>
<pre class="r"><code>red <- brewer.pal(9, "YlOrRd")
# pie(rep(1, length(red)), col = red)
species_color_code <- c("S. scabrum" = "#800026",
"S. nigrum" = "#FEB24C",
"N.D." = "#FFFFCC")
species_anno <- rowAnnotation(species = sample_df$Species,
col = list(`sample_df$Species` = species_color_code),
width = unit(bar_width, "mm"),
annotation_legend_param = list(title = "species"))</code></pre>
</div>
<div id="institution-source" class="section level3">
<h3><span class="header-section-number">3.3.2</span> institution source</h3>
<pre class="r"><code>sample_df$Source[18:19] <- rep("Simlaw Kenya",2)
sample_df$Source[20] <- "Baker Creek Heirloom"
institute_color_code <- c(
"WorldVeg" = "#66C2A5",
"USDA" = "#FC8D62",
"Kenyan local market" = "#8DA0CB",
"Simlaw Kenya" = "#E78AC3",
"Baker Creek Heirloom" = "#A6D854")
institute_anno <- rowAnnotation(source = sample_df$Source,
col = list(`sample_df$Source` = institute_color_code),
width = unit(bar_width, "mm"),
annotation_legend_param = list(title ="seed source"))</code></pre>
</div>
<div id="cultivation-site" class="section level3">
<h3><span class="header-section-number">3.3.3</span> cultivation site</h3>
<pre class="r"><code>greys <- brewer.pal(9, "Greys")
# pie(rep(1, length(greys)), col = greys)
site_color_code <- c("Kenya" = greys[4], "RU" = greys[8])
cultivation_site_anno <-
rowAnnotation(sites = sample_df$`Cultivation site`,
col = list("sample_df$`Cultivation site`" = site_color_code),
width = unit(bar_width, "mm"),
annotation_legend_param = list(title = "cultivation site"))</code></pre>
</div>
<div id="cultivation-year" class="section level3">
<h3><span class="header-section-number">3.3.4</span> cultivation year</h3>
<pre class="r"><code>sample_df$`Harvest time` <- str_replace_all(sample_df$`Harvest time`,
pattern = exactly("2017-03-14"),
replacement = "2017 March")
sample_df$`Harvest time` <- str_replace_all(sample_df$`Harvest time`,
pattern = exactly("2016-07-08"),
replacement = "2016 July")
time <- sample_df$`Harvest time` %>% as.character()
paired <- brewer.pal(12, "Paired")
# pie(rep(1, length(paired)), col = paired)
time_color_code <- c("2017 March" = paired[5],
"2016 July" = paired[6])
harvest_time_anno <- rowAnnotation(time = time,
col = list(time = time_color_code),
width = unit(bar_width, "mm"),
annotation_legend_param = list(title = "harvest time"))</code></pre>
<pre class="r"><code># make row stacked bar plot
rowname <- rownames(hm_mat_dbl_NAzero)
rownames(hm_mat_dbl_NAzero) <- NULL
barplot_df <- hm_mat_dbl_NAzero %>% as.data.frame()
barplot_df$ID <- rowname
barplot_df <- cbind(barplot_df["ID"], barplot_df[1: (ncol(barplot_df) -1) ])
barplot_mat <- barplot_df %>%
mutate(`phenolic acid` = `chlorogenic acid`,
flavonol = isorhamnetin + kaempferol + quercetin + rhamnetin,
alkaloid = solasodine,
saponin = diosgenin + tigogenin) %>%
select(`phenolic acid` : `saponin`) %>%
data.matrix()
rownames(barplot_mat) <- rowname
barplot_anno <- rowAnnotation(
barplot = row_anno_barplot(
barplot_mat, axis = T,
gp = gpar(col = NA, fill = c("phenolic acid" = Set1[5],
"flavonol" = Set1[3],
"alkaloid" = "firebrick",