#seqlogo
# install.packages('ggseqlogo')
library(ggseqlogo)

data(ggseqlogo_sample)
ggseqlogo( seqs_dna$MA0001.1 )
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

#多位点seqlogo
# install.packages('seqinr')
library(seqinr)
getwd()
## [1] "/Volumes/岳_T7/2022卓越计划/0-卓越计划-笔记/第2次考核_SCI绘图之道-笔记"
seqs <- read.fasta('Epitope.fas')
seqs_string <- vector(mode = 'character')
for(i in 1:length(seqs)){
  seqs_string[i] <- toupper(c2s(seqs[[i]])) 
}

ggseqlogo(seqs_string)

#单位点seqlogo
set.seed(2019)
library(dplyr)
## 
## 载入程辑包:'dplyr'
## The following object is masked from 'package:seqinr':
## 
##     count
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(cowplot)
seqs <- tibble(bases = sample(c('A','G','T','C'),1200,rep = T),
               genotype = rep(c('GT1','GT2','GT3',
                                'GT4','GT5','GT6'),
                              each = 200))

seq_mat <- table(seqs$bases,seqs$genotype)
p1 <- ggseqlogo(seq_mat) + 
  scale_x_continuous(breaks = 1:6,
                     labels = unique(seqs$genotype))
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
p2 <- ggseqlogo(seq_mat,method = 'probability') + 
  scale_x_continuous(breaks = 1:6,
                     labels = unique(seqs$genotype))
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
plot_grid(p1,p2,ncol = 2,labels = c('A','B'),align = 'h')

#seqlogo的注释
ggplot()+
  annotate("rect", xmin = 0.5, xmax = 3.5, ymin = -0.05, 
           ymax = 1.9, 
           alpha=0.1, col="black", fill="yellow")+
  geom_logo(seqs_dna$MA0001.1, stack_width = 0.9,
            col_scheme = 'nucleotide2')+
  annotate("segment", x=4, xend = 8, y=1.2, yend = 1.2, size=2)+
  annotate("text", x=6, y=1.3, label="Text annotation")+
  theme_logo()
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

#进化树
# BiocManager::install('ggtree')
library(ggtree)
## ggtree v3.4.0 For help: https://yulab-smu.top/treedata-book/
## 
## If you use the ggtree package suite in published research, please cite
## the appropriate paper(s):
## 
## Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam.
## ggtree: an R package for visualization and annotation of phylogenetic
## trees with their covariates and other associated data. Methods in
## Ecology and Evolution. 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
## 
## S Xu, Z Dai, P Guo, X Fu, S Liu, L Zhou, W Tang, T Feng, M Chen, L
## Zhan, T Wu, E Hu, Y Jiang, X Bo, G Yu. ggtreeExtra: Compact
## visualization of richly annotated phylogenetic data. Molecular Biology
## and Evolution. 2021, 38(9):4039-4042. doi: 10.1093/molbev/msab166
## 
## Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods
## for mapping and visualizing associated data on phylogeny using ggtree.
## Molecular Biology and Evolution. 2018, 35(12):3041-3043.
## doi:10.1093/molbev/msy194
library(tidytree)
## If you use the ggtree package suite in published research, please cite
## the appropriate paper(s):
## 
## LG Wang, TTY Lam, S Xu, Z Dai, L Zhou, T Feng, P Guo, CW Dunn, BR
## Jones, T Bradley, H Zhu, Y Guan, Y Jiang, G Yu. treeio: an R package
## for phylogenetic tree input and output with richly annotated and
## associated data. Molecular Biology and Evolution. 2020, 37(2):599-603.
## doi: 10.1093/molbev/msz240
## 
## S Xu, Z Dai, P Guo, X Fu, S Liu, L Zhou, W Tang, T Feng, M Chen, L
## Zhan, T Wu, E Hu, Y Jiang, X Bo, G Yu. ggtreeExtra: Compact
## visualization of richly annotated phylogenetic data. Molecular Biology
## and Evolution. 2021, 38(9):4039-4042. doi: 10.1093/molbev/msab166
## 
## 载入程辑包:'tidytree'
## The following object is masked from 'package:stats':
## 
##     filter
library(treeio)
## treeio v1.20.0 For help: https://yulab-smu.top/treedata-book/
## 
## If you use the ggtree package suite in published research, please cite
## the appropriate paper(s):
## 
## LG Wang, TTY Lam, S Xu, Z Dai, L Zhou, T Feng, P Guo, CW Dunn, BR
## Jones, T Bradley, H Zhu, Y Guan, Y Jiang, G Yu. treeio: an R package
## for phylogenetic tree input and output with richly annotated and
## associated data. Molecular Biology and Evolution. 2020, 37(2):599-603.
## doi: 10.1093/molbev/msz240
## 
## Guangchuang Yu. Using ggtree to visualize data on tree-like structures.
## Current Protocols in Bioinformatics. 2020, 69:e96. doi:10.1002/cpbi.96
## 
## S Xu, Z Dai, P Guo, X Fu, S Liu, L Zhou, W Tang, T Feng, M Chen, L
## Zhan, T Wu, E Hu, Y Jiang, X Bo, G Yu. ggtreeExtra: Compact
## visualization of richly annotated phylogenetic data. Molecular Biology
## and Evolution. 2021, 38(9):4039-4042. doi: 10.1093/molbev/msab166
## 
## 载入程辑包:'treeio'
## The following object is masked from 'package:seqinr':
## 
##     read.fasta
beast_file <- system.file("examples/MCC_FluA_H3.tree", 
                          package="ggtree")
beast_tree <- read.beast(beast_file)
p1 <- ggtree(beast_tree, mrsd="2013-01-01") + 
  theme_tree2()

p2 <- ggtree(beast_tree, mrsd="2013-01-01") + 
  theme_tree2()+
  geom_nodepoint(aes(size = posterior),color = 'deeppink',
                 alpha=.6)+
  theme(legend.position = c(.15,.8))

beast_tree_data <- as_tibble(beast_tree)
set.seed(2019)
infos <- tibble(locations = sample(c('China','Japan','USA','UK',
                                     'Brazil'),76,rep = T),
                label = na.omit(unique(beast_tree_data$label)))

beast_tree_data2 <- full_join(beast_tree_data,infos,by = 'label')
beast_tree2 <- as.treedata(beast_tree_data2)
library(ggsci)
p3 <- ggtree(beast_tree2, mrsd="2013-01-01") + 
  theme_tree2()+
  geom_tippoint(aes(color = locations),size = 2)+
  scale_color_lancet()+
  theme(legend.position = c(.15,.8))

plot_grid(p1,p2,p3,ncol = 3,labels = c('A','B','C'))

#在进化树旁边叠加其它信息
beast_file <- system.file("examples/MCC_FluA_H3.tree", 
                          package="ggtree")
beast_tree <- read.beast(beast_file)

genotype_file <- system.file("examples/Genotype.txt", 
                             package="ggtree")
genotype <- read.table(genotype_file, sep="\t", 
                       stringsAsFactor=F)
colnames(genotype) <- sub("\\.$", "", colnames(genotype))
p <- ggtree(beast_tree, mrsd="2013-01-01") + 
  geom_treescale(x=2008, y=1, offset=2) + 
  geom_tiplab(size=2)
gheatmap(p, genotype, offset=5, width=0.5, font.size=3, 
         colnames_angle=-45, hjust=0) +
  scale_fill_lancet(breaks=c("HuH3N2", "pdm", "trig"), 
                    name="genotype")
## Scale for 'fill' is already present. Adding another scale for 'fill', which
## will replace the existing scale.

#code 79 - 圆形进化树
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
circ <- ggtree(tree, layout = "circular")
df <- data.frame(first=c("a", "b", "a", "c", "d", "d", 
                         "a", "b", "e", "e", "f", "c", "f"),
                 second= c("z", "z", "z", "z", "y", "y", 
                           "y", "y", "x", "x", "x", "a", "a"))
rownames(df) <- tree$tip.label



gheatmap(circ, df, offset=.8, width=.2,
               colnames_angle=95, colnames_offset_y = .25) +
  scale_fill_viridis_d(option="D", name="discrete\nvalue")
## Scale for 'fill' is already present. Adding another scale for 'fill', which
## will replace the existing scale.