-
Notifications
You must be signed in to change notification settings - Fork 0
/
NGS-Analysis-in-R.Rmd
495 lines (418 loc) · 12.6 KB
/
NGS-Analysis-in-R.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
---
title: "NGS-Analysis-in-R"
author: "Mehadi Hasan"
date: "04/05/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
**Package Requirements**
```{r}
source("https://bioconductor.org/biocLite.R")
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install(c("Biostrings", "GenomicRanges", "rtracklayer", "systemPipeR", "seqLogo", "ShortRead"))
```
```{r}
library(Biostrings)
library(GenomicRanges)
library(rtracklayer)
library(systemPipeR)
library(seqLogo)
library(ShortRead)
```
**String matching**
```{r}
#Generate sample sequence data set
myseq <- c("ATGCAGACATAGTG", "ATGAACATAGATCC", "GTACAGATCAC")
```
```{r}
#String searching with regular expression support
myseq[grep("ATG", myseq)]
```
```{r}
#Searches myseq for first match of pattern “AT”
pos1 <- regexpr("AT", myseq)
as.numeric(pos1)
attributes(pos1)$match.length # Returns position information of matches
```
```{r}
#Searches myseq for all matches of pattern “AT”
pos2 <- gregexpr("AT", myseq)
as.numeric(pos2[[1]])
attributes(pos2[[1]])$match.length # Returns positions of matches in first sequence
```
```{r}
#String substitution with regular expression support
gsub("^ATG", "atg", myseq)
```
```{r}
#Positional parsing
myseq
nchar(myseq) # Computes length of strings
```
```{r}
# Positional parsing of several fragments from one string
myseq
substring(myseq[1], c(1,3), c(2,5))
# Positional parsing of many strings
substring(myseq, c(1,4,7), c(2,6,10))
```
**Random Sequence Generation**
```{r}
# Random DNA sequences of any length
rand <- sapply(1:100, function(x) paste(sample(c("A","T","G","C"), sample(10:20), replace=TRUE), collapse=""))
rand[1:4]
```
```{r}
#Count identical sequences
table(c(rand[1:4], rand[1]))
```
**Extract reads from reference**
```{r}
library(Biostrings) #this requires Biostrings package.
ref <- DNAString(paste(sample(c("A","T","G","C"), 100000, replace=T), collapse=""))
randstart <- sample(1:(length(ref)-15), 1000)
randreads <- Views(ref, randstart, width=15)
rand_set <- DNAStringSet(randreads)
unlist(rand_set)
```
**Sequence Import and Export**
```{r}
# Download sequences to current working directory and then import them into R
dir.create("data", showWarnings = FALSE) # create a data directory
download.file("https://ftp.ncbi.nlm.nih.gov/genomes/archive/old_genbank/Bacteria/Halobacterium_sp_uid217/AE004437.ffn", "data/AE004437.ffn")
```
```{r}
# Import FASTA file with readDNAStringSet
myseq <- readDNAStringSet("data/AE004437.ffn")
myseq[1:3]
```
```{r}
#Subset sequences with regular expression on sequence name field
sub <- myseq[grep("99.*", names(myseq))]
length(sub)
```
```{r}
#Export subsetted sequences to FASTA file
writeXStringSet(sub, file="./data/AE004437sub.ffn", width=80)
```
```{r}
#Working with XString Containers
library(Biostrings)
# DNA sequences
dna <- DNAString("GCATAT-TAC")
dna
dna[1:4]
```
```{r}
# RNA Sequences
rna <- RNAString("GCAUAU-UAC")
rna <- RNAString(dna) # Converts dna to RNAString object
rna
```
```{r}
#Protein sequences
protein <- AAString("HCWYHH")
protein
```
```{r}
#Any type of character strings
any <- BString("NGS strings is fun. Other XString objects store only the IUPAC characters.")
any
```
**Working with XStringSet Containers**
```{r}
#XStringSet containers allow to store many biosequences in one object
dset <- DNAStringSet(c("GCATATTAC", "AATCGATCC", "GCATATTAC"))
dset
names(dset) <- c("seq1", "seq2", "seq3") # Assigns names
dset[1:2]
width(dset) # Returns the length of each sequences
```
```{r}
d <- dset[[1]] # The [[ subsetting operator returns a single entry as XString object
d
dset2 <- c(dset, dset) # Appends/concatenates two XStringSet objects
dset2
dsetchar <- as.character(dset) # Converts XStringSet to named vector
dsetone <- unlist(dset) # Collapses many sequences to a single one stored in a DNAString container
dsetone
```
```{r}
#Sequence subsetting by positions:
dset
DNAStringSet(dset, start=c(1,2,3), end=c(4,8,5))
```
**Multiple Alignment Class**
```{r}
#The XMultipleAlignment class stores the different types of multiple sequence alignments:
origMAlign <- readDNAMultipleAlignment(filepath = system.file("extdata",
"msx2_mRNA.aln", package = "Biostrings"), format = "clustal")
origMAlign
```
**Basic Sequence Manipulations**
```{r}
## Reverse and Complement
randset <- DNAStringSet(rand)
randset[1:2]
complement(randset[1:2])
reverse(randset[1:2])
reverseComplement(randset[1:2])
```
**Translate DNA into Protein**
```{r}
translate(randset[1:2])
```
**Pattern Matching**
```{r}
# Find pattern matches in reference
myseq1 <- readDNAStringSet("./data/AE004437.ffn")
mypos <- matchPattern("ATGGTG", myseq1[[1]], max.mismatch=1)
#Count only the corresponding matches
countPattern("ATGGCT", myseq1[[1]], max.mismatch=1)
# Count matches in many sequences
vcountPattern("ATGGCT", myseq1, max.mismatch=1)[1:20]
```
```{r}
# Results shown in DNAStringSet object
tmp <- c(DNAStringSet("ATGGTG"), DNAStringSet(mypos))
tmp
```
```{r}
#Return a consensus matrix for query and hits
consensusMatrix(tmp)[1:4,]
```
**PWM Viewing and Searching**
```{r}
library(seqLogo)
pwm <- PWM(DNAStringSet(c("GCT", "GGT", "GCA")))
pwm
seqLogo(t(t(pwm) * 1/colSums(pwm)))
```
**NGS Sequences**
**Sequence and Quality Data: FASTQ Format**
```{r}
#Phred score interconversion
phred <- 1:9
phreda <- paste(sapply(as.raw((phred)+33), rawToChar), collapse="")
phreda
as.integer(charToRaw(phreda))-33
```
**Construct QualityScaledDNAStringSet from scratch**
```{r}
# Creates random sample sequence.
dset <- DNAStringSet(sapply(1:100, function(x) paste(sample(c("A","T","G","C"), 20, replace=T), collapse="")))
#Creates random Phred score list.
myqlist <- lapply(1:100, function(x) sample(1:40, 20, replace=T))
# Converts integer scores into ASCII characters.
myqual <- sapply(myqlist, function(x) toString(PhredQuality(x)))
# Converts to a PhredQuality object.
myqual <- PhredQuality(myqual)
# Combines DNAStringSet and quality data in QualityScaledDNAStringSet object.
dsetq1 <- QualityScaledDNAStringSet(dset, myqual)
dsetq1[1:2]
```
**Processing FASTQ Files with ShortRead**
```{r}
library(ShortRead)
#download and unzip
download.file("http://cluster.hpcc.ucr.edu/~tgirke/HTML_Presentations/Manuals/testdata/samplefastq/data.zip", "data.zip")
unzip("data.zip")
```
```{r}
#Important utilities for accessing FASTQ files
fastq <- list.files("data", "*.fastq$")
fastq <- paste("data/", fastq, sep="")
fastq
names(fastq) <- paste("flowcell6_lane", 1:length(fastq), sep="_")
# Imports first FASTQ file
(fq <- readFastq(fastq[1]))
fq
# Counts numbers of reads in FASTQ files
countLines(dirPath="./data", pattern=".fastq$")/4
```
**FASTQ Quality Reports**
```{r}
library(systemPipeR)
# seeFastq and seeFastqPlot functions generate and plot a series of useful quality statistics for a set of FASTQ files.
# For real data set batchsize to at least 10^5
fqlist <- seeFastq(fastq=fastq, batchsize=800, klength=8)
seeFastqPlot(fqlist)
```
```{r}
# The ShortRead package contains several FASTQ quality reporting functions.
sp <- SolexaPath(system.file('extdata', package='ShortRead'))
fl <- file.path(analysisPath(sp), "s_1_sequence.txt")
fls <- c(fl, fl)
coll <- QACollate(QAFastqSource(fls), QAReadQuality(), QAAdapterContamination(),
QANucleotideUse(), QAQualityUse(), QASequenceUse(), QAFrequentSequence(n=10),
QANucleotideByCycle(), QAQualityByCycle())
x <- qa2(coll, verbose=TRUE)
res <- report(x)
if(interactive())
browseURL(res)
```
**Filtering and Trimming FASTQ Files with ShortRead**
**Adaptor trimming**
```{r}
fqtrim <- trimLRPatterns(Rpattern="GCCCGGGTAA", subject=fq)
sread(fq)[1:2] # Before trimming
sread(fqtrim)[1:2] # After trimming
```
**Read counting and duplicate removal**
```{r}
# Counts read occurences
tables(fq)$distribution
```
```{r}
# Identifies duplicated reads
sum(srduplicated(fq))
```
```{r}
fq[!srduplicated(fq)]
```
**Trimming low quality tails**
```{r}
cutoff <- 30
cutoff <- rawToChar(as.raw(cutoff+33))
sread(trimTails(fq, k=2, a=cutoff, successive=FALSE))[1:2]
```
**Removal of reads with Phred scores below a threshold value **
```{r}
cutoff <- 30
qcount <- rowSums(as(quality(fq), "matrix") <= 20)
fq[qcount == 0] # Number of reads where all Phred scores >= 20
```
**Removal of reads with x Ns and/or low complexity segments**
```{r}
filter1 <- nFilter(threshold=1) # Keeps only reads without Ns
filter2 <- polynFilter(threshold=20, nuc=c("A","T","G","C")) # Removes reads with nucleotide bias, >=20 of any base
filter <- compose(filter1, filter2)
fq[filter(fq)]
```
**Range Operations**
```{r}
#Construct GRanges Object
library(GenomicRanges)
library(rtracklayer)
# Example of creating a GRanges object with its constructor function.
gr <- GRanges(seqnames = Rle(c("chr1", "chr2", "chr1", "chr3"), c(1, 3, 2, 4)), ranges = IRanges(1:10, end = 7:16, names = head(letters, 10)), strand = Rle(strand(c("-", "+", "*", "+", "-")), c(1, 2, 2, 3, 2)), score = 1:10, GC = seq(1, 0, length = 10))
gr
```
**Import GFF into GRanges Object**
```{r}
# Imports a simplified GFF3 genome annotation file.
gff <- import.gff("http://cluster.hpcc.ucr.edu/~tgirke/Documents/R_BioCond/Samples/gff3.gff")
seqlengths(gff) <- end(ranges(gff[which(values(gff)[,"type"]=="chromosome"),]))
names(gff) <- 1:length(gff) # Assigns names to corresponding slot
gff[1:4,]
```
**Coerce GRanges object to data.frame**
```{r}
as.data.frame(gff)[1:4, 1:7]
```
**Accessor and subsetting methods for GRanges objects**
```{r}
gff[1:4]
```
```{r}
gff[1:4, c("type", "ID")]
```
```{r}
gff[1:4, c("type", "ID")]
```
**GRanges objects can be concatenated with the c function**
```{r}
c(gff[1:2], gff[401:402])
```
```{r}
#Acessor functions
seqnames(gff)
ranges(gff)
strand(gff)
seqlengths(gff)
start(gff[1:4])
end(gff[1:4])
width(gff[1:4])
```
**Accessing metadata component**
```{r}
values(gff) # or
elementMetadata(gff)
values(gff)[, "type"][1:20]
gff[values(gff)[ ,"type"] == "gene"]
```
**Remove chromosome ranges**
```{r}
gff <- gff[values(gff)$type != "chromosome"]
```
```{r}
#Erase the strand information
strand(gff) <- "*"
```
```{r}
#Collapses overlapping ranges to continuous ranges.
reduce(gff)
#Return uncovered regions
gaps(gff)
#Returns coverage of ranges
coverage(gff)
```
```{r}
# overlapping ranges
findOverlaps(gff, gff[1:4])
# Counts overlapping ranges
countOverlaps(gff, gff[1:4])[1:40]
# Return only overlapping ranges
subsetByOverlaps(gff, gff[1:4])
```
**GRangesList Objects**
```{r}
sp <- split(gff, seq(along=gff)) # Stores every range in separate component of a GRangesList object
split(gff, seqnames(gff)) # Stores ranges of each chromosome in separate component.
unlist(sp) # Returns data as GRanges object
sp[1:4, "type"] # Subsetting of GRangesList objects is similar to GRanges objects.
lapply(sp[1:4], length) # Looping over GRangesList objects similar to lists
```
**Transcript Ranges **
#Storing annotation ranges in TranscriptDb databases makes many operations more robust and convenient.
```{r}
library(GenomicFeatures)
download.file("http://cluster.hpcc.ucr.edu/~tgirke/Documents/R_BioCond/Samples/gff3.gff", "data/gff3.gff")
txdb <- makeTxDbFromGFF(file="data/gff3.gff", format="gff", dataSource="TAIR", organism="Arabidopsis thaliana")
```
```{r}
# save in local file
saveDb(txdb, file="./data/TAIR10.sqlite")
```
```{r}
transcriptsBy(txdb, by = "gene")
exonsBy(txdb, by = "gene")
```
**txdb from BioMart**
#Alternative we can create txdb databases are BioMart, Bioc annotation packages, UCSC
```{r}
library(GenomicFeatures)
library("biomaRt")
txdb <- makeTxDbFromBiomart(biomart = "plants_mart", dataset = "athaliana_eg_gene", host="plants.ensembl.org")
txdb
```
**Efficient Sequence Parsing**
```{r}
# get GRange object stored in local file
gff <- gff[values(gff)$type != "chromosome"] # Remove chromosome ranges
rand <- DNAStringSet(sapply(unique(as.character(seqnames(gff))), function(x) paste(sample(c("A","T","G","C"), 200000, replace=T), collapse="")))
writeXStringSet(DNAStringSet(rand), "./data/test")
getSeq(FaFile("./data/test"), gff)
```
**extractTranscriptSeqs **
```{r}
library(GenomicFeatures); library(Biostrings); library(Rsamtools)
txdb <- loadDb("./data/TAIR10.sqlite")
indexFa("data/test") # Creates index for genome fasta
```
```{r}
sessionInfo()
```