-
Notifications
You must be signed in to change notification settings - Fork 8
/
DEG_Anno_Plot.R
1508 lines (1398 loc) · 65.3 KB
/
DEG_Anno_Plot.R
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
args = commandArgs(trailingOnly=TRUE) # Passing arguments to an R script from bash/shell command lines
# make folder DEG to store outputs
setwd(dirname(args[1]))
#system("mkdir -p DEG") # make new a folder DEG in current working directory
library(DESeq2)
library(tibble)
# Read & preprocess the input file cts before run deseq2 analysis
filename<-basename(args[1])
if (filename %in% c("humann_genefamilies_Abundance_go_translated.tsv","humann_genefamilies_Abundance_kegg_translated.tsv")){
# read the data file
#translated file has additional quote symbol "\""
cts<-read.table(args[1],
row.names=1, sep="\t",header=T)
#round the values before as.integer
cts<-as.data.frame(lapply(lapply(cts,round),as.integer),row.names = row.names(cts))
# make a folder for outputs
if (filename == "humann_genefamilies_Abundance_go_translated.tsv"){
dir.create("Nonhost_hmn_DEG/GO",recursive = T)
setwd("Nonhost_hmn_DEG/GO")
}
if (filename == "humann_genefamilies_Abundance_kegg_translated.tsv"){
dir.create("Nonhost_hmn_DEG/KEGG",recursive = T)
setwd("Nonhost_hmn_DEG/KEGG")
}
}
if (filename %in% c("bracken_species_all","bracken_phylum_all","bracken_genus_all")){
# read the data file
#bracken file (eg. bracken_species_all) without quote symbol "\""; mark empty quote as quote=""
cts<-read.table(args[1],
row.names=1, sep="\t",header=T, quote="")
# Extract columns with count numbers
cts<-cts[,grepl("*_num", names(cts))]
# match column name to sample name
colnames(cts)<-gsub("^Report_|\\.species.bracken_num$|\\.phylum.bracken_num$|.genus.bracken_num$","",colnames(cts))
# decontamination step
# read a list of contamination organisms
# conta_ls<-read.table("~/Dual-seq/conta_ls.txt",sep="\t",colClasses = c("character","NULL"))
# conta_ls<-conta_ls[conta_ls != "",]
# remove the entries matching the contamination organisms
# for (c in conta_ls){
# cts<-cts[!grepl(c,row.names(cts)),]
# }
# make a folder for outputs
dir.create("Nonhost_DEG", recursive = T)
setwd("Nonhost_DEG")
# save the decontaminated result for later reference
# write.table(cts,paste0(filename,"_decontaminated"),sep="\t", quote = F, col.names = NA)
}
if (filename == "host_counts.txt"){
# read the data file
#featureCounts file (eg. host_counts.txt) without quote symbol "\""; mark empty quote as quote=""
cts<-read.table(args[1],
row.names=1, sep="\t",header=T, quote="")
# drop first 5 columns with information other than counts
cts<-cts[,-c(1:5)]
# drop rows of zero count
cts<-cts[rowSums(cts[-1])>0,]
# make a folder for outputs
dir.create("Host_DEG", recursive = T)
setwd("Host_DEG")
}
# read the original samplesheet
coldata0 <- read.csv(args[2], header = T, na.strings=c("","NA"))
# extract the contrast information for reference
coldata_vs<- coldata0[c("group1","group2")]
coldata_vs<-coldata_vs[rowSums(is.na(coldata_vs)) == 0,] #remove the NA rows
# read samplesheet as factors (as.is = F) for Deseq2 statistical analysis
coldata_factor <- read.csv(args[2], header = T, as.is = F)
coldata_factor[]<-lapply(coldata_factor, factor)
coldata<-coldata_factor[,1:2]
# update the coldata if metadata is provided
if (length(args) == 5){
coldata <- read.csv(args[5], header = T, as.is = F)
coldata[]<-lapply(coldata, factor)
}
if (filename == "host_counts.txt"){
# make cts(count matrix) has consistent order with samplesheet/metadata
cts<-cts[coldata0$sample_name]
# load the datastructure to DESeq
dds <- DESeqDataSetFromMatrix(countData = cts,
colData = coldata,
design= ~ group)
}
if (filename %in% c("bracken_species_all","bracken_phylum_all","bracken_genus_all",
"humann_genefamilies_Abundance_go_translated.tsv","humann_genefamilies_Abundance_kegg_translated.tsv")){
if (filename %in% c("bracken_species_all","bracken_phylum_all","bracken_genus_all")){
files_h <- list.files(path=paste0(dirname(args[1]),"/temp"), pattern="^Report_host_.*\\.txt$", full.names=TRUE, recursive=FALSE)
}
if (filename %in% c("humann_genefamilies_Abundance_go_translated.tsv","humann_genefamilies_Abundance_kegg_translated.tsv")){
humann_f <- gsub("/hmn_genefamily_abundance_files","",dirname(args[1]))
files_h <- list.files(path=paste0(humann_f,"/temp"), pattern="^Report_host_.*\\.txt$", full.names=TRUE, recursive=FALSE)
}
# to get a list for host
transcriptome_size<-c() #generate a empty list
for (i in files_h){
t<-read.table(i,sep="\t", quote= "")
total_reads<-t[1,2] + t[2,2] #get total reads abundance of a sample
fn<-gsub("^Report_host_|\\.txt$","",basename(i)) #grep the sample name
transcriptome_size<-c(transcriptome_size,setNames(total_reads,fn)) #add sample name with value to the list lh
}
transcriptome_size <- log2(transcriptome_size)-mean(log2(transcriptome_size))
coldata$order<-1:nrow(coldata)
coldata<-merge(coldata,as.data.frame(transcriptome_size), by.x="sample_name",by.y="row.names")
coldata<-coldata[order(coldata$order), ]
coldata<-subset(coldata, select = -c(order))
# make cts(count matrix) has consistent order with samplesheet/metadata
cts<-cts[coldata0$sample_name]
# load the datastructure to DESeq
dds <- DESeqDataSetFromMatrix(countData = cts,
colData = coldata,
design= ~ group + transcriptome_size)
}
# adjust the design if metadata is provided
funNew <- function(x){
as.formula(paste("~", paste(x, collapse = " + ")))
}
if (length(args) == 5){
design(dds)<-funNew(names(coldata)[2:ncol(coldata)])
}
# perform the DESeq analysis
dds <- DESeq(dds)
# Data transformation for visualization (normalization included)
#rld<-rlog(dds,blind=F) # regularized log transformation (log2 based)
if (dim(results(dds))[1] < 1000 || min(colSums(cts !=0)) < 1000){
vsd<-varianceStabilizingTransformation(dds,blind=F) # vatiance stabilizing transformation
} else {
vsd<-vst(dds,blind=F)
}
normtrans<-assay(vsd)
# save normalized & transformed data for visualization
write.csv(normtrans,file=paste0(sub(".tsv$|.txt$","",filename),"_normalized_transformed.csv"))
# save normalized (untransformed) data for reference
norm<-counts(dds,normalized=T)
write.csv(norm,file=paste0(sub(".tsv$|.txt$","",filename),"_normalized.csv"))
# merge and add suffixes; normalized and normalized&transformed
merge.nt<-merge(norm,normtrans,by="row.names", suffixes=c(".norm",".normtrans"))
if (filename == "host_counts.txt"){
library("biomaRt")
host_sp<-read.csv(args[4]) # read a list of supported host species
# add annotations; try up to 120 times/20 mins if biomaRt server not response
ensembl <- NULL
attempt <- 0
while ( is.null(ensembl) && attempt <= 120){
try(
ensembl <- useMart("ensembl",dataset=host_sp[host_sp$Taxon_ID==args[3],2])
)
attempt<-attempt+1
if (attempt > 1){
print(paste0("Retry to get gene annotations from ensembl: ",attempt," times"))
}
Sys.sleep(10)
}
if (is.null(ensembl)){
stop("Failed to get gene annotations from ensembl server, please try again later")
}
names(merge.nt)[1]<-"GeneID"
genes <- merge.nt$GeneID
gene_ID <- getBM(filters="ensembl_gene_id",
attributes=c("external_gene_name","ensembl_gene_id",
"chromosome_name","start_position","end_position",
"strand","gene_biotype","description"),
values=genes,mart=ensembl)
names(gene_ID)[names(gene_ID)=="external_gene_name"]<-"gene_name" #rename the first column
gene_len<-read.table(args[1], row.names=1, sep="\t",header=T, quote="")
gene_len<-gene_len["Length"]
colnames(gene_len)<-"gene_length"
gene_ID<-merge(gene_ID,gene_len,by.x="ensembl_gene_id", by.y="row.names")
}
# #to add entrezid separately; caution: may bring a issue of "duplicate" ENTREZID!!
# library(clusterProfiler)
# library(org.Mmu.eg.db)
# # may bring a issue of "duplicate" ENTREZID!!
# ENTREZID = bitr(gene_ID$external_gene_name,
# fromType="SYMBOL", toType="ENTREZID", OrgDb="org.Mmu.eg.db") #get entrezid with acceptable id duplicate
# gene_ID <- merge(gene_ID,ENTREZID,by.x="gene_name",by.y="SYMBOL",all.x = T)
# function for host
# function of contrast between groups, merge to annotation and count tables, and save to .csv files
comparison<-function(dds,coldata_vs,filename,gene_ID,cts,merge.nt){
for (i in 1:nrow(coldata_vs)){
group1<-coldata_vs$group1[i]
group2<-coldata_vs$group2[i]
comparison <- results(dds, contrast=c("group", group1, group2))
comparison_f<-as.data.frame(comparison)
comparison_f<-comparison_f[order(comparison_f$pvalue),]
system(paste0("mkdir -p"," ",group1,"_vs_",group2)) #for output file structure
setwd(paste0(group1,"_vs_",group2))
write.csv(comparison_f,file=paste0(sub(".tsv$|.txt$","",filename),"_",group1,"_vs_",group2,".csv"))
setwd("../")
colnames(comparison_f)<-paste0(colnames(comparison_f),".",group1,"_vs_",group2)
gene_ID<-merge(gene_ID,comparison_f,by.x="ensembl_gene_id", by.y="row.names") #merge each comparison to annotation
}
Anno.merge.all<-merge(cts,gene_ID,by.x="row.names",by.y="ensembl_gene_id")
Anno.merge.all<-merge(Anno.merge.all,merge.nt,by.x="Row.names",by.y="GeneID")
names(Anno.merge.all)[1]<-"gene_id" #rename the first column
Anno.merge.all[Anno.merge.all==""]<-"-" #replace the empty cells to "-"
Anno.merge.all<-Anno.merge.all[complete.cases(Anno.merge.all[,grep("pvalue|padj",names(Anno.merge.all))]),] # drop pvalue == NA rows; optional
hybrid_name<-Anno.merge.all$gene_name # add a column of gene_id/name hybrid
while ("-" %in% hybrid_name){
hybrid_name[match("-",hybrid_name)] <-
Anno.merge.all$gene_id[match("-",hybrid_name)]}
Anno.merge.all<-add_column(Anno.merge.all, hybrid_name, .after = "gene_name")
write.csv(Anno.merge.all,file=paste0(sub(".tsv$|.txt$","",filename),"_DEG.csv"),row.names=F)
}
#function for non-host
comparison_nonhost<-function(dds,coldata_vs,filename,cts,merge.nt){
gene_ID_temp<-list()
for (i in 1:nrow(coldata_vs)){
group1<-coldata_vs$group1[i]
group2<-coldata_vs$group2[i]
comparison <- results(dds, contrast=c("group", group1, group2))
comparison_f<-as.data.frame(comparison)
comparison_f<-comparison_f[order(comparison_f$pvalue),]
system(paste0("mkdir -p"," ",group1,"_vs_",group2)) #for output file structure
setwd(paste0(group1,"_vs_",group2))
write.csv(comparison_f,file=paste0(sub(".tsv$|.txt$","",filename),"_",group1,"_vs_",group2,".csv"))
setwd("../")
colnames(comparison_f)<-paste0(colnames(comparison_f),".",group1,"_vs_",group2)
comparison_f <- tibble::rownames_to_column(comparison_f, "NAME")
gene_ID_temp[[i]]<-comparison_f
}
library(dplyr)
gene_ID <- purrr::reduce(gene_ID_temp,full_join, by = "NAME") #reduce multiple data.frames in a list to a single data.frame
names(merge.nt)[1]<-"Name" #rename the first column
Anno.merge.all<-merge(cts,gene_ID,by.x="row.names", by.y="NAME")
Anno.merge.all<-merge(Anno.merge.all,merge.nt,by.x="Row.names",by.y="Name")
names(Anno.merge.all)[1]<-"Name" #rename the first column
Anno.merge.all[Anno.merge.all==""]<-"-" #replace the empty cells to "-"
Anno.merge.all<-Anno.merge.all[complete.cases(Anno.merge.all[,grep("pvalue|padj",names(Anno.merge.all))]),] # drop pvalue == NA rows
write.csv(Anno.merge.all,file=paste0(sub(".tsv$|.txt$","",filename),"_DEG.csv"),row.names=F)
}
# apply the comparison function
if (filename == "host_counts.txt"){
comparison(dds, coldata_vs, filename,gene_ID,cts,merge.nt)
} else {comparison_nonhost(dds, coldata_vs, filename,cts,merge.nt)}
#to match with sample name from DESeq2, which is not allow "-" in the name
coldata[,1] <-gsub("-",".",coldata[,1])
## MaAsLin2 ##
library("Maaslin2")
system("mkdir -p MaAsLin2_results")
features <- t(cts)
metadata <- coldata
rownames(metadata)<-metadata[,1]
metadata<-subset(metadata,select=-1)
covar<-toString(shQuote(names(coldata)[2:ncol(coldata)]))
for (r in 1:length(unique(coldata_vs[,2]))){
fit_data <- Maaslin2(features, metadata, paste0('MaAsLin2_results/ref_',unique(coldata_vs[,2])[r]),
fixed_effects = cat(covar, "\n"),
reference = paste0("group,",unique(coldata_vs[,2])[r]),
plot_heatmap = T, plot_scatter = T,
cores=4)
}
if (filename == "host_counts.txt"){ #prepare for gene symbol
system("mkdir -p MaAsLin2_results/gene_symbol")
DEG<-read.csv(paste0(sub(".tsv$|.txt$","",filename),"_DEG.csv"),header = T)
df.DEG<-DEG[!duplicated(DEG$hybrid_name),] #remove duplicate gene symbol
df4features <- data.frame(df.DEG$hybrid_name, df.DEG[names(cts)], row.names=1)
features.symbol <- t(df4features)
for (r in 1:length(unique(coldata_vs[,2]))){
fit_data <- Maaslin2(features.symbol, metadata, paste0('MaAsLin2_results/gene_symbol/ref_',unique(coldata_vs[,2])[r]),
fixed_effects = cat(covar, "\n"),
reference = paste0("group,",unique(coldata_vs[,2])[r]),
plot_heatmap = T, plot_scatter = T,
cores=4)
}
write("The number of genes may be less due to the duplicated gene symbols being removed.","MaAsLin2_results/gene_symbol/Readme.txt")
}
if (filename == "host_counts.txt"){
setwd(paste0(dirname(args[1]),"/Host_DEG")) # go back to the Host_DEG folder
}
### Plots ###
library(ggplot2)
library(ggrepel)
library(dplyr)
library(stringr) #for str_trunc function: restrict the showing character length
library(colorspace)
library(RColorBrewer)
# subset normtrans for visualization; consider all groups; to integrate in the comparison function?? DEG=Anno.merge.all
DEG<-read.csv(paste0(sub(".tsv$|.txt$","",filename),"_DEG.csv"),header = T)
# count gene TPM
if (filename == "host_counts.txt"){
RPK <- DEG[,as.character(coldata[,1])]/DEG$gene_length
TPM <- RPK*1000000/sum(RPK)
TPM<-cbind(DEG[,c("gene_name","hybrid_name")],TPM)
write.csv(TPM,file=paste0(sub(".tsv$|.txt$","",filename),"_TPM.csv"),row.names=F)
}
# filter by log2 <0.5 & >0.5 and p-value <0.05 for all groups
flt_groups_all<-data.frame()
for (i in 1:length(grep("pvalue",names(DEG)))){
p<-DEG[DEG[,grep("pvalue",names(DEG))[i]]<0.05 &
abs(DEG[,grep("log2FoldChange",names(DEG))[i]])>0.5,]
flt_groups_all<-rbind(flt_groups_all,p)
}
# flt_groups<-DEG[DEG[,grep("pvalue",names(DEG))[i]]<0.05 &
# abs(DEG[,grep("log2FoldChange",names(DEG))[i]])>0.5,]
#
# flt_groups<-flt_groups[complete.cases(flt_groups),]
# flt_groups_all <- DEG[(abs(DEG[,"log2FoldChange.ART_Young_vs_Ctl_Young"]) > 0.5 |
# abs(DEG[,"log2FoldChange.ART_Old_vs_ART_Young"]) > 0.5) &
# (DEG[,"pvalue.ART_Young_vs_Ctl_Young"]<0.05 |
# DEG[,"pvalue.ART_Old_vs_ART_Young"]<0.05),]
#subset the interested columnes
subset_normtrans<-flt_groups_all[,grep("normtrans|gene_name|gene_id|Name",names(flt_groups_all))]
if (filename == "host_counts.txt"){
# pass undefined gene name
while ("-" %in% subset_normtrans$gene_name){
subset_normtrans$gene_name[match("-",subset_normtrans$gene_name)] <-
subset_normtrans$gene_id[match("-",subset_normtrans$gene_name)]
}
##check the duplicated rows
#n_occur <- data.frame(table(subset_normtrans$gene_name))
#n_occur[n_occur$Freq > 1,]
##drop the duplicated rows
#flt_groups_all<-unique(flt_groups_all)
# drop the duplicate rows
subset_normtrans<-subset_normtrans[!duplicated(subset_normtrans$gene_name),]
#convert column name (gene_name) to row name
subset_normtrans2<-subset_normtrans[,-1:-2]
rownames(subset_normtrans2)<-subset_normtrans[,2]
} else {
# drop the duplicate rows
subset_normtrans<-subset_normtrans[!duplicated(subset_normtrans$Name),]
#convert column name (gene_name) to row name
subset_normtrans2<-subset_normtrans[,-1]
rownames(subset_normtrans2)<-subset_normtrans[,1]
}
#chop the sample column names
colnames(subset_normtrans2)<-gsub(".normtrans","",colnames(subset_normtrans2))
#heatmap require matrix
transdata <- as.matrix(subset_normtrans2)
# adjust the rowname length
for (i in 1:length(row.names(transdata))){
l<-nchar(row.names(transdata)[i])
if (l > 35){
row.names(transdata)[i]<-str_trunc(row.names(transdata)[i],35)
}
}
#read the column annotation information (derive from samplesheet.csv)
Anno_col<-as.data.frame(coldata[,2])
rownames(Anno_col)<-coldata[,1]
colnames(Anno_col)<-"Group"
# annotation color selection
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
n<-nrow(unique(Anno_col))
cols = gg_color_hue(n)
names(cols)<-as.character(unique(Anno_col)$Group)
anno_colors<-list(Group=cols)
# draw heat map
library(pheatmap)
hp_thumbnail<-pheatmap(transdata, cluster_cols = T, scale="row",
annotation_col =Anno_col, annotation_colors=anno_colors,
show_rownames=F,cex=1)
if (nrow(transdata)>100){
hp<-pheatmap(transdata, cluster_cols = T, scale="row",
fontsize_row=5, annotation_col =Anno_col,
annotation_colors=anno_colors)
} else {
hp<-pheatmap(transdata, cluster_cols = T, scale="row",
annotation_col =Anno_col,
annotation_colors=anno_colors)
}
# hp<-pheatmap(transdata, cluster_cols = T, scale="row",
# fontsize_row=5,cex=1, annotation_col =Anno_col, cex=0.9)
# save the plot
ggsave("heatmap_thumbnail.pdf",plot=hp_thumbnail)
if (nrow(transdata)>100){
ggsave("heatmap.pdf",plot=hp,
limitsize = F,
height = 0.1*nrow(transdata),
width = 0.8*ncol(transdata))
} else {
ggsave("heatmap.pdf",plot=hp,height = 0.2*nrow(transdata))
}
## draw PCA
if (filename %in%
c("humann_genefamilies_Abundance_go_translated.tsv",
"humann_genefamilies_Abundance_kegg_translated.tsv",
"host_counts.txt")){
pcadata<-plotPCA(vsd,intgroup=c("group"), returnData=T)
percentVar<-round(100*attr(pcadata,"percentVar"))
ggplot(pcadata, aes(PC1, PC2, shape=group)) +
geom_point(size=3) +
xlab(paste0("PC1: ",percentVar[1],"% variance")) +
ylab(paste0("PC2: ",percentVar[2],"% variance")) +
coord_fixed() +
theme_bw() +
ggtitle("PCA")
ggsave("PCA.pdf")
ggplot(pcadata, aes(PC1, PC2, color=group)) +
geom_point(size=3) +
xlab(paste0("PC1: ",percentVar[1],"% variance")) +
ylab(paste0("PC2: ",percentVar[2],"% variance")) +
coord_fixed() +
theme_bw() +
ggtitle("PCA")
ggsave("PCA_color.pdf")
ggplot(pcadata, aes(PC1, PC2, shape=group)) +
geom_point(size=3) +
xlab(paste0("PC1: ",percentVar[1],"% variance")) +
ylab(paste0("PC2: ",percentVar[2],"% variance")) +
geom_text_repel(aes(label=pcadata$name),size=3) +
coord_fixed() +
theme_bw() +
ggtitle("PCA")
ggsave("PCA_label.pdf")
ggplot(pcadata, aes(PC1, PC2, color=group)) +
geom_point(size=3) +
xlab(paste0("PC1: ",percentVar[1],"% variance")) +
ylab(paste0("PC2: ",percentVar[2],"% variance")) +
geom_text_repel(aes(label=pcadata$name),size=3) +
coord_fixed() +
theme_bw() +
ggtitle("PCA")
ggsave("PCA_label_color.pdf")
}
# draw PCoA, heatmap, diversity tests for microbiome
if (filename %in% c("bracken_species_all",
"bracken_phylum_all",
"bracken_genus_all")){
library(phyloseq)
library(vegan)
#Imported original biom file into phyloseq object
biomfilename=paste0(dirname(args[1]),"/temp/bracken_species_all0.biom")
data<-import_biom(biomfilename,parseFunction = parse_taxonomy_default)
colnames(tax_table(data)) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
#Imported host transcriptome size adjusted & normtrans biom file into phyloseq object
biomfilename.adj=paste0(dirname(args[1]),"/bracken_species_all.biom")
data.adj<-import_biom(biomfilename.adj,parseFunction = parse_taxonomy_default)
colnames(tax_table(data.adj)) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
#Estimated and exported alpha diversity
#The measures of diversity that aren't totally reliant on singletons, eg. Shannon/Simpson, are valid to use, and users can ignore the warning in phyloseq when calculating those measures.
data.alpha<-estimate_richness(data.adj)
write.csv(data.alpha, file="alpha-diversity.csv")
theme_set(theme_bw())
p.alpha <- plot_richness(data,measures = c("Shannon","Simpson"))
p.alpha
ggsave("Alpha_diversity_sample.pdf")
#Estimated and exported beta diversity (Bray-Curtis)
#to transformation data by vst before beta diversity analysis
if (length(args) == 5){
samples4phyloseq<-as.data.frame(coldata[2:ncol(coldata)], row.names = as.character(coldata$sample_name))
sampledata<-sample_data(samples4phyloseq)
data<-merge_phyloseq(data,sampledata)
dds1 <- phyloseq_to_deseq2(data, funNew(names(coldata)[2:ncol(coldata)]))
} else {
samples4phyloseq<-as.data.frame(coldata$group, row.names = as.character(coldata$sample_name))
colnames(samples4phyloseq)<-"group"
sampledata<-sample_data(samples4phyloseq)
data<-merge_phyloseq(data,sampledata)
dds1 <- phyloseq_to_deseq2(data, ~ group)
}
## ANCOMBC ##
library("ANCOMBC")
pseq <- phyloseq::tax_glom(data, taxrank = "Species") # species taxid
pseq1 <- microbiome::aggregate_taxa(data,"Species") # species name
ancombc_out <- function(pseq,formula){
ancombc(
phyloseq = pseq,
formula = formula,
p_adj_method = "fdr",
zero_cut = 0.90, # by default prevalence filter of 10% is applied
lib_cut = 0,
group = "group",
struc_zero = TRUE,
neg_lb = TRUE,
tol = 1e-5,
max_iter = 100,
conserve = TRUE,
alpha = 0.05,
global = TRUE
)
}
if (length(args) == 5){
out <- ancombc_out(pseq,paste(names(coldata)[2:(ncol(coldata)-1)], collapse = " + "))
out1 <- ancombc_out(pseq1,paste(names(coldata)[2:(ncol(coldata)-1)], collapse = " + "))
} else {
out <- ancombc_out(pseq,"group") # taxid
out1 <- ancombc_out(pseq1,"group") # species name
}
res <- out$res # taxid
res_global = out$res_global
res1 <- out1$res # species name
res_global1 = out1$res_global
system("mkdir -p ANCOMBC_results")
write.csv(res[["diff_abn"]], file="ANCOMBC_results/diff_abundance.csv")
write.csv(res[["W"]], file="ANCOMBC_results/Test_statistics.csv")
write.csv(res[["p_val"]], file="ANCOMBC_results/p_value.csv")
write.csv(res[["q_val"]], file="ANCOMBC_results/q_value.csv")
write.csv(res_global, file="ANCOMBC_results/Global_test.csv")
system("mkdir -p ANCOMBC_results/with_species_names")
write.csv(res1[["diff_abn"]], file="ANCOMBC_results/with_species_names/diff_abundance_name.csv")
write.csv(res1[["W"]], file="ANCOMBC_results/with_species_names/Test_statistics_name.csv")
write.csv(res1[["p_val"]], file="ANCOMBC_results/with_species_names/p_value_name.csv")
write.csv(res1[["q_val"]], file="ANCOMBC_results/with_species_names/q_value_name.csv")
write.csv(res_global1, file="ANCOMBC_results/with_species_names/Global_test_name.csv")
## heatmap for ANCOMBC results ##
ANCOMBC_plot <- function(res){
library(tidyr)
heat.mw <- res[["W"]] %>%
rownames_to_column("taxid") %>% #preserve rownames
gather(key, value, -taxid)
heat.md <- res[["diff_abn"]] %>%
rownames_to_column("taxid") %>% #preserve rownames
gather(key, value, -taxid)
heat.m<-merge(heat.mw,heat.md,by="taxid")
# Arrange the figure
p <- ggplot(heat.m, aes(x = key.x, y = taxid, fill = value.x))
p <- p + geom_tile()
p <- p + scale_fill_gradientn("value.x", name ="Test statistics",
breaks = seq(from = -2, to = 2, by = 0.5),
colours = c("darkblue", "blue", "white", "red", "darkred"),
limits = c(-2,2))
# Polish texts
p <- p + theme(axis.text.x=element_text(angle = 90, hjust=1, face = "italic"),
axis.text.y=element_text(size = 8))
p <- p + xlab("") + ylab("Taxonomy ID")
# Mark the most significant cells with stars
if (length(unique(heat.m$taxid))>100){
p <- p + geom_text(data = subset(heat.m, value.y == "TRUE"),
aes(x = key.x, y = taxid, label = "+"), col = "white", size = 2.5)
} else {
p <- p + geom_text(data = subset(heat.m, value.y == "TRUE"),
aes(x = key.x, y = taxid, label = "+"), col = "white", size = 3)
}
if (length(unique(heat.m$taxid))>100){
ggsave("heatmap_ANCOMBC.pdf",plot=p,
limitsize = F,
height = 0.1*length(unique(heat.m$taxid)),
width = 1.5+0.6*length(unique(heat.m$key.x)))
} else {
ggsave("heatmap_ANCOMBC.pdf",plot=p,height = 0.2*length(unique(heat.m$taxid)))
}
}
setwd("ANCOMBC_results")
ANCOMBC_plot(res)
ANCOMBC_plot_sp <- function(res1){
library(tidyr)
heat.mw <- res1[["W"]] %>%
rownames_to_column("name") %>% #preserve rownames
gather(key, value, -name)
heat.md <- res1[["diff_abn"]] %>%
rownames_to_column("name") %>% #preserve rownames
gather(key, value, -name)
heat.m<-merge(heat.mw,heat.md,by="name")
heat.m$name <- sub(".*s__", "", heat.m$name)
heat.m$name <- stringr::str_trunc(heat.m$name, 31) # truncate sp_name
# Arrange the figure
p <- ggplot(heat.m, aes(x = key.x, y = name, fill = value.x))
p <- p + geom_tile()
p <- p + scale_fill_gradientn("value.x", name ="Test statistics",
breaks = seq(from = -2, to = 2, by = 0.5),
colours = c("darkblue", "blue", "white", "red", "darkred"),
limits = c(-2,2))
# Polish texts
p <- p + theme(axis.text.x=element_text(angle = 90, hjust=1, face = "italic"),
axis.text.y=element_text(size = 8))
p <- p + xlab("") + ylab("Species name")
# Mark the most significant cells with stars
if (length(unique(heat.m$name))>100){
p <- p + geom_text(data = subset(heat.m, value.y == "TRUE"),
aes(x = key.x, y = name, label = "+"), col = "white", size = 2.5)
} else {
p <- p + geom_text(data = subset(heat.m, value.y == "TRUE"),
aes(x = key.x, y = name, label = "+"), col = "white", size = 3)
}
if (length(unique(heat.m$name))>100){
ggsave("heatmap_ANCOMBC.pdf",plot=p,
limitsize = F,
height = 0.1*length(unique(heat.m$name)),
width = 2.5+0.8*length(unique(heat.m$key.x)))
} else {
ggsave("heatmap_ANCOMBC.pdf",plot=p,height = 0.2*length(unique(heat.m$name)))
}
}
setwd("with_species_names")
ANCOMBC_plot_sp(res1)
setwd("../..")
# continue for beta diversity
data1<-data
vsd1 <- varianceStabilizingTransformation(dds1)
# normalized reads count with host transcriptome size and with avoiding removing variation associated with the other conditions
if (length(args) == 5){
mm <- model.matrix(funNew(names(coldata)[2:(ncol(coldata)-1)]), colData(vsd))
} else {
mm <- model.matrix(funNew(names(coldata)[2]), colData(vsd))
}
vsd1.df <- limma::removeBatchEffect(assay(vsd1), vsd$transcriptome_size, design=mm)
vsd1.df[vsd1.df < 0.0] <- 0.0 #adjust negative values after vst
otu_table(data1) <- otu_table(vsd1.df, taxa_are_rows = TRUE)
braycurtis <- phyloseq::distance(data1, method = "bray")
BCmat <- as.matrix(braycurtis)
write.csv(BCmat, file = "braycurtis.csv")
#Created and exported PCoA values
braycurtis.pcoa <- ordinate(data1, method = "PCoA", distance = "bray")
braycurtis.pcoa.export <- as.data.frame(braycurtis.pcoa$vectors, row.names = NULL, optional = FALSE, cut.names = FALSE, col.names = names(braycurtis.pcoa$vectors), fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors())
write.csv(braycurtis.pcoa.export, file="braycurtis-pcoa.csv")
# add the group information in coldata
coldata_order<-coldata # to keep the original order as the samplesheet
coldata_order$order<-1:nrow(coldata_order)
braycurtis.pcoa.export<-merge(braycurtis.pcoa.export, coldata_order,by.x="row.names",by.y = "sample_name")
row.names(braycurtis.pcoa.export)<-braycurtis.pcoa.export[,1]
braycurtis.pcoa.export<-braycurtis.pcoa.export[,-1]
## plot PCoA
b<-braycurtis.pcoa[["values"]][["Relative_eig"]]
#barplot(b[b>0],names.arg =colnames(braycurtis.pcoa.export))
library(forcats)
if (names(braycurtis.pcoa.export)[2]=="Axis.2"){
# reorder the group column following the value of order column
braycurtis.pcoa.export %>%
mutate(group = fct_reorder(group, order)) %>%
ggplot(aes(Axis.1, Axis.2,color=group)) +
geom_point(size=3) +
xlab(paste0("PCoA1: ",round(100*b[1]),"% variance")) +
ylab(paste0("PCoA2: ",round(100*b[2]),"% variance")) +
geom_text_repel(aes(label=row.names(braycurtis.pcoa.export)),size=3) +
coord_fixed() +
theme_bw() +
ggtitle("Bray-Curtis Distances PCoA")
ggsave("PCoA_label_color.pdf")
braycurtis.pcoa.export %>%
mutate(group = fct_reorder(group, order)) %>%
ggplot(aes(Axis.1, Axis.2,color=group)) +
geom_point(size=3) +
xlab(paste0("PCoA1: ",round(100*b[1]),"% variance")) +
ylab(paste0("PCoA2: ",round(100*b[2]),"% variance")) +
coord_fixed() +
theme_bw() +
ggtitle("Bray-Curtis Distances PCoA")
ggsave("PCoA_color.pdf")
} else {
write("No Axis.2 was found on PCoA","No_Axis2_on_PCoA.txt")
}
# anosim test
data2<-t(as.matrix(data1@otu_table))
pathotype.anosim <- anosim(data2, braycurtis.pcoa.export$group)
# plot results
pdf("ANOSIM.pdf")
plot(pathotype.anosim,
main="Analysis of Diversity in Groups",
xlab="",
ylab="")
dev.off()
# Start writing to an output file
sink('ANOSIM-analysis-output.txt')
summary(pathotype.anosim)
# Stop writing to the file
sink()
# plot heatmap (Phyloseq style)
sampledata<-as.data.frame(coldata[,2])
row.names(sampledata)<-coldata[,1]
colnames(sampledata)<-"Groups"
sam = sample_data(sampledata)
sp = otu_table(norm, taxa_are_rows = TRUE)
physeq<-phyloseq(sp, sam)
if (nrow(norm)>100){
ph<-plot_heatmap(physeq,max.label = nrow(norm)) +
theme (axis.text.y = element_text(size=(2.2*225/nrow(norm))))
} else {
ph<-plot_heatmap(physeq)
}
ph$labels$y<-"Species"
print(ph)
ggsave("Heatmap_all.png")
ggsave("Heatmap_all.pdf")
# Add sample data
tax = tax_table(data.adj)
otu = otu_table(data.adj)
data_sam <- phyloseq(otu,tax, sam)
# change color scale for plot_bar
HowManyPhyla <- length(levels(as.factor(data_sam@tax_table[,2])))
getPalette = colorRampPalette(brewer.pal(9, "Set2"))
PhylaPalette = getPalette(HowManyPhyla)
# phyloseq bar plots
try(plot_bar(data, fill="Phylum") +
scale_fill_manual(values =PhylaPalette))
try(ggsave("Bar_phy.pdf"))
try(plot_bar(data_sam, fill="Phylum",facet_grid = ~Groups) +
scale_fill_manual(values =PhylaPalette))
try(ggsave("Bar_group_phy.pdf"))
# relative abundance bar plot
data_sam_relabund<-transform_sample_counts(data_sam, function(x) x / sum(x))
theme_set(theme_grey())
plot_bar(data_sam_relabund, fill="Phylum") +
geom_bar(stat="identity", position="stack") +
labs(x = "", y = "Relative Abundance\n") +
theme(panel.background = element_blank()) +
scale_fill_manual(values =PhylaPalette)
ggsave("Bar_relative_phy.pdf")
# alpha diversity box plot
theme_set(theme_bw())
plot_richness(data_sam,"Groups",measures = c("Shannon","Simpson")) +
geom_boxplot()
ggsave("Alpha_diversity.pdf")
theme_set(theme_grey())
# alpha diversity comparisons
data.alpha_g<-merge(data.alpha,coldata,by.x="row.names",by.y=1)
library(ggpubr)
my_comparisons<-list()
for (i in 1:nrow(coldata_vs)){
my_comparisons[[i]] <- c(coldata_vs$group1[i],coldata_vs$group2[i])
}
ggboxplot(data.alpha_g, x = "group", y = "Shannon",
color = "group", palette = "jco",
add = "jitter") +
stat_compare_means(comparisons = my_comparisons,
method = "t.test") # Add pairwise comparisons p-value
ggsave("Alpha_diversity_Shannon.pdf")
ggboxplot(data.alpha_g, x = "group", y = "Simpson",
color = "group", palette = "jco",
add = "jitter") +
stat_compare_means(comparisons = my_comparisons,
method = "t.test")
ggsave("Alpha_diversity_Simpson.pdf")
}
## draw volcano and bar plot
for (i in 1:nrow(coldata_vs)){
group1<-coldata_vs$group1[i]
group2<-coldata_vs$group2[i]
pvalue_name<-paste0("padj.",group1,"_vs_",group2) # get the adjusted p-value
log2FoldChange_name<-paste0("log2FoldChange.",group1,"_vs_",group2)
# nt4v<-cbind(DEG[,"gene_name"],DEG[,pvalue_name],
# DEG[,log2FoldChange_name])
if (filename == "host_counts.txt"){
nt4v<-cbind(DEG["gene_name"],DEG[pvalue_name],
DEG[log2FoldChange_name])
colnames(nt4v)<-c("gene_name","pvalue","log2FoldChange")
# nt4v<-as.data.frame(nt4v) # volcano plot require data.frame
# nt4v$pvalue<-as.numeric(nt4v$pvalue) #character to numeric
# nt4v$log2FoldChange<-as.numeric(nt4v$log2FoldChange)
# pass undefined gene name
while ("-" %in% nt4v$gene_name){
nt4v$gene_name[match("-",nt4v$gene_name)] <-
DEG$gene_id[match("-",DEG$gene_name)]
}
# drop duplicated row
nt4v<-nt4v[!duplicated(nt4v$gene_name),]
# The significantly differentially expressed genes are the ones found in the upper-left and upper-right corners.
# Add a column to the data frame to specify if they are UP- or DOWN- regulated (log2FoldChange respectively positive or negative)
# add a column of NAs
nt4v$diffexpressed <- "NO"
# if log2Foldchange > 0.5 and pvalue < 0.05, set as "UP"
nt4v$diffexpressed[nt4v$log2FoldChange > 0.5 & nt4v$pvalue < 0.05] <- "UP"
# if log2Foldchange < -0.5 and pvalue < 0.05, set as "DOWN"
nt4v$diffexpressed[nt4v$log2FoldChange< -0.5 & nt4v$pvalue < 0.05] <- "DOWN"
## Volcano plotting
# v<-ggplot(data=nt4v,
# aes(x=log2FoldChange,y=-log10(pvalue)),
# col=diffexpressed) +
# geom_point() +
# theme_minimal()
# # Add vertical lines for log2FoldChange thresholds, and one horizontal line for the p-value threshold
# v2<-v+geom_vline(xintercept = c(-0.5,0.5), col="red") +
# geom_hline(yintercept=-log10(0.05), col="red")
# # Change point color
# mycolors <- c("blue", "red", "black")
# names(mycolors) <- c("DOWN", "UP", "NO")
# v3 <- v2 + scale_colour_manual(values = mycolors)
diffexpressed<-nt4v[nt4v$diffexpressed != "NO",]
# select top 20 DEG of FC
topFC<-rbind(top_n(diffexpressed,20,log2FoldChange),top_n(diffexpressed,-20,log2FoldChange))
topFC$label<-topFC$gene_name
topFC<-topFC[!duplicated(topFC$gene_name),]
# Create a new column "label" to nt4v, that will contain the name of genes differentially expressed (NA in case they are not)
nt4v_label<-merge(nt4v,topFC, all=T)
} else {
nt4v<-cbind(DEG["Name"],DEG[pvalue_name],
DEG[log2FoldChange_name])
colnames(nt4v)<-c("Name","pvalue","log2FoldChange")
# pass undefined gene name
while ("-" %in% nt4v$Name){
nt4v$Name[match("-",nt4v$Name)] <-
DEG$gene_id[match("-",DEG$Name)]
}
# drop duplicated row
nt4v<-nt4v[!duplicated(nt4v$Name),]
# The significantly differentially expressed genes are the ones found in the upper-left and upper-right corners.
# Add a column to the data frame to specify if they are UP- or DOWN- regulated (log2FoldChange respectively positive or negative)
# add a column of NAs
nt4v$diffexpressed <- "NO"
# if log2Foldchange > 0.5 and pvalue < 0.05, set as "UP"
nt4v$diffexpressed[nt4v$log2FoldChange > 0.5 & nt4v$pvalue < 0.05] <- "UP"
# if log2Foldchange < -0.5 and pvalue < 0.05, set as "DOWN"
nt4v$diffexpressed[nt4v$log2FoldChange< -0.5 & nt4v$pvalue < 0.05] <- "DOWN"
diffexpressed<-nt4v[nt4v$diffexpressed != "NO",]
# select top 20 DEG of FC
topFC<-rbind(top_n(diffexpressed,20,log2FoldChange),top_n(diffexpressed,-20,log2FoldChange))
topFC$label<-topFC$Name
topFC<-topFC[!duplicated(topFC$Name),]
# Create a new column "label" to nt4v, that will contain the name of genes differentially expressed (NA in case they are not)
nt4v_label<-merge(nt4v,topFC, all=T)
}
## Volcano plotting
# plot adding up all layers we have seen so far
ggplot(data=nt4v_label, aes(x=log2FoldChange, y=-log10(pvalue),
col=diffexpressed, label=label)) +
geom_point(size = 1.5) +
theme_minimal() +
geom_text_repel(size=3) +
scale_color_manual(values=c("blue", "black", "red")) +
geom_vline(xintercept=c(-0.5, 0.5), col="red",linetype="dashed") +
geom_hline(yintercept=-log10(0.05), col="red",linetype="dashed") +
ggtitle(paste0(group1,"_vs_",group2)) +
theme(plot.title = element_text(hjust = 0.5))
ggsave(paste0(group1,"_vs_",group2,"/Volcano_",group1,"_vs_",group2,".pdf"))
## bar plotting
bar <- diffexpressed
if (nrow(bar)!=0){
# sort log2FoldChange top to down
if (filename == "host_counts.txt"){
bar$c <- with(bar,reorder(gene_name,log2FoldChange))
} else {
bar$c <- with(bar,reorder(Name,log2FoldChange))
}
bar_plot<-ggplot(bar,aes(x=log2FoldChange,y=c,fill=pvalue))+
geom_bar(stat="identity",aes(fill=diffexpressed), width=0.5) +
#scale_fill_brewer(palette="Blues")+
scale_fill_manual(name="Expression",
labels = c("Down", "Up"),
values = c("DOWN"="#00ba38", "UP"="#f8766d")) +
#scale_fill_gradient2(low=rgb(14,37,56,max=255),high=rgb(54,169,243,max=255), mid=rgb(52,109,157,max=255), midpoint=0.01)+
#scale_fill_gradient(low=rgb(54,169,243,max=255), high=rgb(14,37,56,max=255))+
#scale_fill_continuous_sequential(palette = "Blues3", begin=0.4)+
labs(x="log2FoldChange",y=" ") + #could add ,fill="n=", title = "BMC_HvsBMC_L_DOWN")
ggtitle(paste0(group1,"_vs_",group2)) +
#geom_text(aes(x=log2FoldChange+0.6),label="*") +
theme_bw() +
scale_y_discrete(breaks=bar[,1],labels=str_trunc(bar[,1],40)) +
guides(fill = guide_legend(reverse = TRUE)) #reverse the legend to put "Up" in an upper position
ggsave(paste0(group1,"_vs_",group2,"/Barplot_",group1,"_vs_",group2,".pdf"),height=0.2*length(bar_plot[["data"]][[1]]),limitsize = FALSE)
}
}
## Venn Diagram
if (length(unique(coldata$group))>=2){
dv.list<-list()
for (i in unique(coldata$group)){
vd<-coldata[coldata$group==i,]
sample_name<-as.character(vd[,1])
sample_name.norm<-paste0(sample_name,".norm")
if (filename == "host_counts.txt"){
vd.df<-DEG[c("gene_name",sample_name.norm)]
} else {
vd.df<-DEG[c("Name",sample_name.norm)]
}
#Removing rows having all zeros, ignore 1st column with names
vd.df<-vd.df[rowSums(vd.df[-1])>0,]
#add to list
dv.list[[i]]<-vd.df[,1]
}
#myCol <- brewer.pal(length(unique(coldata$group)), "Pastel2")
library(VennDiagram)
# Don't write log file for VennDiagram
futile.logger::flog.threshold(futile.logger::ERROR, name = "VennDiagramLogger")
#venn.diagram(...)
venn.diagram(
x = dv.list,
category.names = names(dv.list),
filename = 'venn_diagramm.png',
output=TRUE,
# # Output features
imagetype="png" ,
# height = 480 ,
# width = 480 ,
# resolution = 300,
compression = "lzw",
# Circles
lwd = 1,
#lty = 'blank',
col=cols,
#fill = myCol,
# keep same colors with heatmap and PCA
fill = alpha(cols,0.6),
# Numbers
#cex = 1,
fontface = "bold",
fontfamily = "sans",
# Set names
#cat.cex = 1,
cat.fontface = "bold",
# cat.default.pos = "outer",
# cat.pos = c(-27, 27, 135),
# cat.dist = c(0.055, 0.055, 0.085),
cat.fontfamily = "sans",
#rotation = 1
)
}
# non-host/host reads ratio comparison
if (filename %in% c("bracken_species_all","bracken_phylum_all","bracken_genus_all")){
files_h <- list.files(paste0(dirname(args[1]),"/temp"), pattern="^Report_host_.*\\.txt$", full.names=TRUE, recursive=FALSE)
lh<-c() #generate a empty list
la<-c()
for (i in files_h){
t<-read.table(i,sep="\t", quote= "")
a<-t[1,1] #get unclassified reads ratio: non-host part
r<-a/(100-a) #non-host/host reads ratio in kraken2 host report
fn<-gsub("^Report_host_|\\.txt$","",basename(i)) #grep the sample name
lh<-c(lh,setNames(r,fn)) #add sample name with value to the list lh; non-host/host reads ratio
la<-c(la,setNames(a,fn)) #unclassified reads ratio: non-host part
}
lah<-cbind(la,lh)
lah<-merge(lah,coldata,by.x="row.names",by.y="sample_name")
colnames(lah)<-c("Sample","unclassified reads ratio %","non-host/host reads ratio","Groups")
# to draw box plot with comparison of unclassified ratio
my_comparisons<-list()
for (i in 1:nrow(coldata_vs)){
my_comparisons[[i]] <- c(coldata_vs$group1[i],coldata_vs$group2[i])
}
ggboxplot(lah, x = "Groups", y = "non-host/host reads ratio",
color = "Groups", palette = "jco",
add = "jitter") +
stat_compare_means(comparisons = my_comparisons,
method = "t.test") # Add pairwise comparisons p-value
ggsave("non-host_vs_host_reads_ratio.pdf")
ggboxplot(lah, x = "Groups", y = "unclassified reads ratio %",
color = "Groups", palette = "jco",
add = "jitter") +
stat_compare_means(comparisons = my_comparisons,
method = "t.test") # Add pairwise comparisons p-value
ggsave("unclassified_reads_ratio.pdf")
}