-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortReads.R
194 lines (127 loc) · 4.97 KB
/
shortReads.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
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
###########################################################
# #
# Sequence reads assessment
# #
# #
# #
###########################################################
# Analisis de genomas y transcriptomas con plataforma NGS
# Master Biotecnologia
# APR 2023
# Jose V. Die, Dep Genetics, UCO
# Install R packages (from CRAN):
install.packages("BiocManager") # <- Bioconductor manager
#install.packages("R.utils") # <- uncompress *.gz files
# Install R packages (from Bioconductor):
BiocManager::install("ShortRead") # <- manipulate *.fastq files
# Dependencies
library(ShortRead)
# Organize the Project
dir.create("data")
dir.create("results")
# Download files
#https://www.ncbi.nlm.nih.gov/data-hub/genome/GCF_000146045.2/
download.file("https://www.dropbox.com/s/4ft480eky7kghzw/Saccharomyces_cerevisiae_genome.fa.gz?dl=1",
destfile = "data/Saccharomyces_cerevisiae_genome.fa.gz")
#https://www.ncbi.nlm.nih.gov/sra/?term=SRR11397715
download.file(url = "https://www.dropbox.com/scl/fi/uf3c6zbj0mndzvtznqkme/SRR11397715.fastq.gz?rlkey=8bfvh2o3i51lp7vjt9mvckgov&dl=1",
destfile = "data/SRR11397715.fastq.gz")
# Uncompress fasta.gz & fastq.gz files
#R.utils::gunzip("SRR11397715.fastq.gz", remove = FALSE)
system("time gunzip -k data/SRR11397715.fastq.gz")
system("time gunzip -k data/Saccharomyces_cerevisiae_genome.fa.gz")
# ## ---fasta------------------------------------------------------------------
# Read fasta : Saccharomyces genome
fa_sample <- readFasta(dirPath = "data/Saccharomyces_cerevisiae_genome.fa")
# Print sample
fa_sample
# Accesors ShortRead
methods(class = "ShortRead")
id(fa_sample)
length(fa_sample)
width(fa_sample)
sread(fa_sample)
# Extract sequence from DNAStringSet object
toString(sread(fa_sample)[1])
as.character(sread(fa_sample)[1])
# Write a ShortRead object
writeFasta(object = fa_sample, file = "results/Saccchromosomes.fasta", compress = TRUE)
## ---fastq---------------------------------------------------------------------
# Read fastq
fqsample <- readFastq(dirPath = "data", pattern = "fastq")
fqsample <- readFastq(dirPath = "data/SRR11397715.fastq")
# Print fqsample
fqsample
# Accesors : fastq
methods(class = "ShortReadQ")
length(fqsample)
sread(fqsample)[1:20]
width(fqsample)[1:20]
sum(width(fqsample)) / 1e6 # comparar con slide "entrez" : 96.3M
# Plotting
hist(width(fqsample))
abline(v = 150, col = "red", lwd = 2)
fqsample150 <- fqsample[width(fqsample) >= 150]
hist(width(fqsample150))
length(fqsample) / 1e3
length(fqsample150) / 1e3
# Write a ShortRead object
writeFasta(object = fqsample150, file = "results/reads150.fastq")
## subset from a fastq file -----------------------------------------------------
set.seed(2023)
fqsampler <- FastqSampler("data/SRR11397715.fastq", 300000)
# extract the sample from the stored file
fqsubset <- yield(fqsampler)
#Warning message:
# cerrando la conexion (reads/SRR11397715.fastq) que no esta siendo utilizada
close(fqsampler)
length(fqsample)
length(fqsubset)
width(fqsubset)[1:20]
sread(fqsubset)[1:20]
sum(width(fqsubset))/1e6 # N Mb representados en ese subset
# alternative: filter out 500 random sequences fron the 150bp dataset.
length(fqsample150)
sample(1:10, 3, replace = FALSE)
set.seed(2023)
idx = sample(1:length(fqsample150), 500, replace = FALSE)
fq_sample150_500 <- fqsample150[idx]
length(fq_sample150_500)
sread(fq_sample150_500)
## convertQuality---------------------------------------------------------------
qreads <- quality(fqsubset) # quality encoded into ASCII characters
qreads
encoding(qreads)
dim(as(qreads , "matrix"))
as(qreads , "matrix")[1:6, 1:15]
sort(encoding(qreads)[c("A", "E", "/", "<")])
# PhredQuality instance
pq <- PhredQuality(qreads)
# transform the encoding into scores
qs <- as(pq, "IntegerList")
qs[[1]]
qs[1]
# Remember : a score of 30 is considered a good quality as it means
# the accuracy of base call is 99.9%
table(qs[[1]] >= 30)
# Quaility assesment & summary
qaSummary <- qa(fqsubset, lane = 1)
class(qaSummary)
methods(class = "ShortReadQQA")
names(qaSummary)
# qa elements are accessed with qa[["name"]]
qaSummary[["readCounts"]]
qaSummary[["baseCalls"]]
qaSummary[["adapterContamination"]]
# get html report
browseURL(report(qaSummary))
## Duplicates ---------------------------------------------------------------
table(srduplicated(fqsample)) # counts of the duplicated reads
# Cleaning reads from duplicates
uniqueReads <- fqsample[srduplicated(fqsample) == FALSE]
table(srduplicated(uniqueReads))
## Write a ShortRead object ----------------------------------------------------
# system("mkdir results")
# writeFasta(object = fq_sample150_500, file = "results/my_fasta.fasta")
# writeFastq(object = fq_sample150_500, file = "results/fqsample.fastq.gz",compress=TRUE)