-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb2worms_to_align.R
150 lines (102 loc) · 3.41 KB
/
db2worms_to_align.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
# 1) Generar un formato fasta con la nueva nomenclatura worms,
# 2) entonces hacemos un archivo filtrado de la lista de especies del gom
rm(list = ls())
options(stringsAsFactors = FALSE)
dir <- '/Users/cigom/metagenomics/db/fish_divergence'
setwd(dir)
qr <- c("Phylum", "Class", "Order", "Family", "Genus", "Species")
wr <- paste0(qr, "_wm")
# qr_ncbi <- c("phylum", "class", "order", "family", "genus", "Species")
bold2w <- read.csv('metafishgom_B2W.tsv', sep = '\t')
ncbi2w <- read.csv('metafishgom_subN2W.tsv', sep = '\t')
library(dplyr)
library(stringr)
library(tidyr)
bold2w %>%
as_tibble() %>%
select(processid, wr) %>%
mutate(Species_wm = str_replace(Species_wm, " ", "_")) %>%
unite('Taxonomy', wr, sep = ';') -> b2w
# 2
ncbi2w %>%
as_tibble() %>%
select(accnum, wr) %>%
mutate(Species = str_replace(Species_wm, " ", "_")) %>%
unite('Taxonomy', wr, sep = ';') -> n2w
# Parse sequence to taxonomy ----
ncbiF <- 'ncbi_complete.fasta'
boldF <- 'peces_bold_sorted.tmp'
boldL <- 'metafishgom_bold_processid.list'
ncbiL <- 'metafishgom_ncbi_accnum.list'
nrow(boldL <- read.table(boldL)) # 38, 575
nrow(ncbiL <- read.table(ncbiL)) # 44, 928
filter_fa <- function(fasta, list) {
library('Biostrings')
db <- readDNAStringSet(fasta, format="fasta")
db <- db[names(db) %in% list[,1]]
return(db)
}
ncbi <- filter_fa(ncbiF, ncbiL)
bold <- filter_fa(boldF, boldL)
# sanity check
n2w <- n2w[match(names(ncbi), n2w$accnum),]
b2w <- b2w[match(names(bold), b2w$processid),]
identical(names(ncbi), n2w$accnum)
identical(names(bold), b2w$processid)
# Save files
nn <- unite(n2w, 'names', accnum:Taxonomy, sep = ' ')
bn <- unite(b2w, 'names', processid:Taxonomy, sep = ' ')
names(ncbi) <- nn$names
names(bold) <- bn$names
writeXStringSet(bold, filepath = paste0(dir, '/metafishgom_bold.fa'),
append=FALSE,
compress=FALSE, compression_level=NA, format="fasta")
writeXStringSet(ncbi, filepath = paste0(dir, '/metafishgom_ncbi.fa'),
append=FALSE,
compress=FALSE, compression_level=NA, format="fasta")
# Create tiles by group ----
# if group by Order-groups?
ncbi2w %>%
as_tibble() %>%
select(accnum, wr) %>%
group_by(Order_wm) %>%
select(Order_wm) %>%
table() %>%
data.table() -> ncbi2w_group
ncbi2w_group[order(N)]
bold2w %>%
as_tibble() %>%
select(processid, wr) %>%
group_by(Order_wm) %>%
select(Order_wm) %>%
table() %>%
data.table() -> bold2w_group
bold2w_group[order(N)]
# n_groups(ncbi2w_group) #
library(data.table)
x <- data.table(db = 'GB', table(ncbi2w_group[,1]))
y <- data.table(db = 'BOLD', table(bold2w_group[,1]))
GB <- data.table(db = 'GB',
group_keys(ncbi2w_group),
size = group_size(ncbi2w_group))
BOLD <- data.table(db = 'BOLD',
group_keys(bold2w_group),
size = group_size(bold2w_group))
db <- rbind(GB,BOLD, use.names=FALSE)
db <- db %>% spread(db, size, fill = NA)
#pct <- function(x) {x / sum(x) * 100}
#db[, BOLD := (pct(BOLD))]
#db[, GB := (pct(GB))]
# sanity check
colSums(db[,-1])
library(ggalluvial)
#
# ggplot(data = db,
# aes(axis1 = BOLD, axis2 = order ,
# axis3 = GB)) +
# geom_alluvium() +
# scale_x_discrete(limits = c("BOLD", "Group", "Gene Bank")) +
# geom_stratum() + geom_text(stat = "stratum", label.strata = TRUE) +
# theme_minimal(base_size=16) +
# labs(title = "", x = "" , y = "n_groups")
quit(save = 'no')