forked from YuLab-SMU/ggmsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustal.R
41 lines (40 loc) · 1.54 KB
/
clustal.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
##' A color scheme of Culstal. The algorithm to assign colors
##' for Multiple Sequence.
##'
##' @param y sequence alignment with data frame, generated by tidy_msa().
##' @keywords clustal
##' @noRd
color_Clustal <- function(y) {
char_freq <- lapply(split(y, y$position), function(x) table(x$character))
col_convert <- lapply(char_freq, function(seq_column) {
##The white as the background
clustal <- rep("#ffffff", length(seq_column))
names(clustal) <- names(seq_column)
r <- seq_column/sum(seq_column)
for (pos in seq_along(seq_column)) {
char <- names(seq_column)[pos]
i <- grep(char, scheme_clustal$re_position)
for (j in i) {
if (scheme_clustal$type[j] == "combined"){
rr <- sum(r[strsplit(scheme_clustal$re_gp[j], '')[[1]]],
na.rm = TRUE)
if (rr > scheme_clustal$thred[j]) {
clustal[pos] <- scheme_clustal$colour[j]}
} else{
rr1<-r[strsplit(scheme_clustal$re_gp[j], ',')[[1]]]
if (any(rr1> scheme_clustal$thred[j],na.rm = TRUE) ) {
clustal[pos] <- scheme_clustal$colour[j]}
}
break
}
}
return(clustal)
})
yy <- split(y, y$position)
lapply(names(yy), function(n) {
d <- yy[[n]]
col <- col_convert[[n]]
d$color <- col[d$character]
return(d)
}) %>% do.call('rbind', .)
}