-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrBLAST.R
More file actions
164 lines (134 loc) · 4.52 KB
/
Copy pathrBLAST.R
File metadata and controls
164 lines (134 loc) · 4.52 KB
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
library(Biostrings)
library(taxonomizr)
library(ggplot2)
library(ShortRead)
library(dplyr)
library(stringi)
library(forcats)
library(rBLAST)
library(multidplyr)
library(cowplot)
#devtools::install_github("tidyverse/multidplyr"){force:true}
#devtools::install_github("mhahsler/rBLAST")
#find a SRR file.
#use this link then search this sequence
#https://www.ncbi.nlm.nih.gov/Traces/study/?acc=PRJNA605442&ff=on
#PRJNA605442
# Download SRA File:
srr=c('SRR11043497')
system(paste('fastq-dump', srr, sep=' '))
# Read taxonomy database
taxaNodes<-read.nodes.sql("/projectnb/ct-shbioinf/taxonomy/data/nodes.dmp", overwrite=TRUE)
taxaNames<-read.names.sql("/projectnb/ct-shbioinf/taxonomy/data/names.dmp", overwrite=TRUE)
# read fastq
dna = readFastq(paste(srr, '.fastq',sep=''))
reads = sread(dna)
qscores = quality(dna)
# plot readlength
widths = as.data.frame(reads@ranges@width)
(widthplot <- ggplot(widths) +
geom_histogram(aes(x=reads@ranges@width), binwidth = 10) +
theme_linedraw() +
xlab('Read Length (bp)') +
xlim(0,2000) +
ggtitle('Read length distribution for 550bp amplicon'))
ggsave(widthplot, file='readlengths.png')
# plot qscores
numqscores = as(qscores, "matrix") # converts to numeric scores automatically
avgscores = apply(numqscores, 1, mean, na.rm=TRUE) #apply the function mean() across
avgscores = as.data.frame(avgscores)
(qscores = ggplot(avgscores) +
geom_histogram(aes(x=avgscores), binwidth=0.2) +
theme_linedraw() +
xlab('Quality Score') +
ggtitle('Per Read Average Quality'))
ggsave(qscores, file='quality.png')
## blast
bl <- blast(db="/projectnb/ct-shbioinf/blast/nt.fa")
cl <- predict(bl, reads, BLAST_args = '-num_threads 8 -evalue 1e-50')
accid = as.character(cl$SubjectID) # accession IDs of BLAST hits
# Plot results
#takes accession number and gets the taxonomic ID
ids<-accessionToTaxa(accid, '/projectnb/ct-shbioinf/taxonomy/data/accessionTaxa.sql')
#taxlist displays the taxonomic names from each ID #
taxlist=getTaxonomy(ids, taxaNodes, taxaNames)
cltax=cbind(cl,taxlist)
lca2 = function(x) {
require(dplyr)
taxnames = c('superkingdom', 'phylum', 'order', 'family', 'genus', 'species')
x = x %>% filter(!is.na(superkingdom)) %>% filter(superkingdom != 'Bacteria') # need to deal with this more generally
shortnames = apply(x[,taxnames], 2, unique)
countshnames = sapply(shortnames, length)
numcount = countshnames==1
lastuni = tail(names(shortnames[numcount==T]), n=1)
nombre = as.data.frame(x[1,which(colnames(x) == lastuni)])
newtax <- as.list(ifelse(countshnames==1,shortnames,NA))
ret = x %>%
mutate(last_common = as.character(nombre[[1]])) %>%
mutate(level_lca = lastuni) %>%
mutate(superkingdom = newtax$superkingdom) %>%
mutate(phylum = newtax$phylum) %>%
mutate(class = newtax$class) %>%
mutate(order = newtax$order) %>%
mutate(family = newtax$family) %>%
mutate(genus = newtax$genus) %>%
mutate(species = newtax$species)
return(ret)
}
blasthits = cltax
cluster <- new_cluster(8)
cluster_library(cluster, 'dplyr')
cluster_copy(cluster, 'lca2')
#split groups across multiple CPU cores
prepare = blasthits %>%
group_by(QueryID) %>%
partition(cluster) %>% #split groups into cluster units
do({lca2(.)}) %>%
collect()
#count reads matching species:
spcount = prepare %>%
group_by(QueryID) %>%
slice(1) %>%
group_by(species) %>%
summarize(count=n()) %>%
arrange(desc(count)) %>%
filter(!is.na(species))
#count reads matching to genera:
gencount = prepare %>%
group_by(QueryID) %>%
slice(1) %>%
group_by(genus) %>%
summarize(count=n()) %>%
arrange(desc(count)) %>%
filter(!is.na(genus))
#count reads matching to families
famcount = prepare %>%
group_by(QueryID) %>%
slice(1) %>%
group_by(family) %>%
summarize(count=n()) %>%
arrange(desc(count)) %>%
filter(!is.na(family))
#(p1 = ggplot(spcount) +
# geom_col(aes(x=fct_reorder(species, count, .desc=T), y=count)) +
# scale_y_log10() +
#theme_minimal() +
#theme(axis.text.x = element_text(angle=90)) +
#xlab('Species')
#)
(p2 = ggplot(gencount) +
geom_col(aes(x=fct_reorder(genus, count, .desc=T), y=count)) +
scale_y_log10() +
theme_minimal() +
theme(axis.text.x = element_text(angle=90)) +
xlab('Genus')
)
(p3 = ggplot(famcount) +
geom_col(aes(x=fct_reorder(family, count, .desc=T), y=count)) +
scale_y_log10() +
theme_minimal() +
theme(axis.text.x = element_text(angle=90)) +
xlab('Family')
)
gr = plot_grid(p2, p3, ncol=1, nrow=3)
ggsave(gr, file='Blast_analysis.png', height = 9, width = 5, dpi=500)