-
Notifications
You must be signed in to change notification settings - Fork 2
/
CGRphylo-v2.rmd
267 lines (182 loc) · 7.2 KB
/
CGRphylo-v2.rmd
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
---
title: 'CGRPhylo Pipeline: Chaos Game Representation for Phylogeny'
author: Amarinder Singh Thind
output:
html_document: default
pdf_document: default
date: "`r Sys.Date()`"
---
```{r setup, include=FALSE, echo=FALSE}
require("knitr")
opts_knit$set(root.dir = ".")
```
## Setting working directory, loading source, and input files
```{r}
source('cgat_function.r')
source('distances_n_other.r')
source('cgrplot.r')
file <- "Input_recom_SARS_cov2.fasta"
library("seqinr")
fastafile <- seqinr::read.fasta(file = file, seqtype = "DNA", as.string = TRUE, set.attributes = FALSE)
#fastafile <- fastafile[c(1:8,21:28,41:48,53)] ## for example purpose taking subsets
#seqinr::write.fasta(sequences=fastafile,names =names(fastafile),file.out=paste("Sep-2022-manuscript/recombinant_XBB.1/xbb.1.fasta",sep = ''))
```
## Filtering of the sequencing with a specified number of 'N' bases
```{r}
library(stringr)
N_filter <- 50 ## filter sequence with n bases > this value
fasta_filtered <- fastafile_new(fastafile, N_filter) ## create filtered sequence file
#write fasta file from filtered sequences
#seqinr::write.fasta(sequences=fasta_filtered,names =names(fasta_filtered),file.out=paste("Sep-2022-manuscript/recombinant_XBB.1/Filter",N_filter,"bJ.1.v2.fasta",sep = ''))
```
## Create Meta info from the sequence
```{r}
meta <- create_meta(fastafile, N_filter) ## create seq features information
print(paste("std dev for seq length is",sd(meta$length),sep=" "))
print(paste("Median of the seq length is",median(meta$length),sep=" "))
print("Range of the seq length")
range(meta$length)
####### plot sequence length
#boxplot(meta$length, ylab="Sequence length") ## overall
dotchart(meta$length, labels = meta$name, xlab = "Sequence length", pch = 21, bg = "green", pt.cex = 1, cex = 0.7)
dotchart(meta$GC_content, labels = meta$name, xlab = "GC content", pch = 21, bg = "green", pt.cex = 1, cex = 0.7)
###########
## box plot for each strains #(In this example first part of the name is strain name)
meta$strains <- as.character(lapply(meta$name, function(x) strsplit(x, '_')[[1]][1])) ## split strains names
library(ggplot2)
ggplot(meta, aes(x = strains, y =length, color = strains )) + geom_boxplot()+ ylab("Sequence length (group level)")+coord_flip()
library(ggplot2)
ggplot(meta, aes(x = strains, y =GC_content, color = strains )) + geom_boxplot()+ ylab("GC content (group level)")+coord_flip()
len_trim <- min(meta$length)
####### Save metadata into a file
#write.csv(meta,paste("length_and_names",N_filter,".csv"))
```
## CGR plot
```{r}
cgr1 <- cgrplot(1) ## enter the number of sequence from "fasta_filtered"
plot(cgr1[,1],cgr1[,2], main=paste("CGR plot of", names(fasta_filtered)[1],sep=''),
xlab = "", ylab = "", cex=0.2, pch = 4, frame = TRUE)
#compare 2 CGR plots
cgr2 <- cgrplot(4)
library('RColorBrewer')
pal <- brewer.pal(8,"Dark2")
par(mfrow=c(1,2))
plot(cgr1[,1],cgr1[,2], main=paste("CGR plot of ", names(fasta_filtered)[1],sep=''),
xlab = "", ylab = "", cex.main=0.5, cex=0.4, pch = 4, frame = TRUE)
plot(cgr2[,1],cgr2[,2], main=paste("CGR plot of ", names(fasta_filtered)[2],sep=''),
xlab = "", ylab = "",cex.main=0.5,cex=0.4,pch = 4, frame = TRUE)
```
## Create frequency object for sequences
```{r}
k_mer <- 3 ## define the value of K
Freq_mat_obj <- list()
sequence_new <- names(fasta_filtered)
for(n in 1:length(fasta_filtered)) {
##skip passing whole data to the function ##increase speed ## "fasta_filtered" name is fixed
Freq_mat_obj[[n]] <- cgat(k_mer,n, len_trim) # executing one seq at a time #k-mer,seq_length,trimmed_length
print(paste("processing sequence : ",n , sequence_new[n], sep=" "))
}
names(Freq_mat_obj) <- sequence_new
```
## Calculate distances
```{r}
j <- length(sequence_new)
distance <- matrix(0,j,j) ## defining the empty matrix from distance calculations
row.names(distance) <- sequence_new[1:j]
colnames(distance) <- sequence_new[1:j]
for(n in 1:j) {
for(cr in 1:j) {
distance[n,cr] <- matrixDistance(Freq_mat_obj[[n]],Freq_mat_obj[[cr]],distance_type ='Euclidean' ) ##'Euclidean','S_Euclidean' and Manhattan
}}
distance <- round(distance, 5)
distance[1:5,1:3]
#plot(hclust(as.dist(distance)))
```
## Save distance matrix into mega or phylip format
```{r}
Mega_file_name <- paste('Recombi_N_filtering_',N_filter,'_mega_distance_file_for_k_',k_mer,'.meg')
### save
saveMegaDistance(Mega_file_name, distance) ## inputs are file name (with path) and distance
PhylipFile_name <- paste('Phylip_N_filtering_',N_filter,'distances_k_',k_mer,'.txt')
## inputs are file name (with path) and distance
savePhylipDistance(PhylipFile_name, distance, mode= 'original') ## relaxed or original/other
##typical phylip allows 10 charcter for texa name ##new relaxed formart allows 250
## http://www.phylo.org/index.php/help/relaxed_phylip
```
#### Integrating with other visualization tools (e.g ape and treeio )
```{r}
library(ape)
my_nj <- ape::nj(distance)
plot(my_nj)
plot(my_nj,type = "cladogram")
bs<-boot.phylo(my_nj, distance, nj, quiet = TRUE)
plot(bs)
nodelabels(bs)
my_nj$node.label<-bs
write.tree(my_nj,"njwithbs.tree.nwk")
foo <- function() {
col <- "green"
for (i in 1:2)
axis(i, col = col, col.ticks = col, col.axis = col, las = 1)
box(lty = "19")
}
mytr <- my_nj
#layout(matrix(1:4, 2, 2, byrow = TRUE))
plot(mytr); foo()
plot(mytr, "c", FALSE); foo()
plot(mytr, "u"); foo()
par(xpd = TRUE)
plot(mytr, "f"); foo()
box("outer")
#layout(matrix(1:4, 2, 2))
#par(las = 1)
plot(mytr, "u"); foo()
plot(mytr, "u", FALSE); foo()
plot(mytr, "f"); foo()
plot(mytr, "f", FALSE); foo()
```
#### write/read tree (Newick and Nexus format )
```{r}
tree <-write.tree(my_nj, file = "", append = FALSE, digits = 10, tree.names = FALSE)
ape::write.tree(my_nj, file=paste('Newick_NJ_tree_k',k_mer,'.txt', sep='')) #for Newick format #write out trees in a text file
ape::write.nexus(my_nj, file=paste('Nexus_NJ_tree_k',k_mer,'.txt', sep='')) ##for Nexus format
## reading tree from file
#newick.tree <- ape::read.tree("Newick_NJ_tree.txt")
#nexus.tree <- ape::read.nexus("Nexus_NJ_tree.nex")
```
#### integration with treeio
```{r}
# ## BiocManager::install("treeio") #install treeio, if not installed
#
# library('treeio')
#
# newick.tree <- treeio::read.newick("Newick_NJ_tree.txt")
# newick.tree
# nexus.tree <-treeio::read.nexus("Newick_NJ_tree.nex")
# nexus.tree
#
# phylo <- as.phylo( newick.tree)
# phylo
# test <- as.treedata( newick.tree, boot = NULL)
#
#
# file <- system.file(".", "Newick_NJ_tree.nex", package="treeio")
# #beast <- read.nexus(file)
# bea
#
#
# ggtree(test, color="firebrick", size=2, linetype="dotted")
# ggtree(test, branch.length="none")
##########################
# library("ggtree")
# ggplot(my_nj, aes(x, y)) + geom_tree() + theme_tree()
##Rerooting tree
# library(tidyverse)
# #BiocManager::install("ggtree")
# library(ggtree)
# # build a ggplot with a geom_tree
# ggplot(tree) + geom_tree() + theme_tree()
#
# # This is convenient shorthand
# ggtree(as.data.frame(distance))
```