Compare commits

..

2 Commits

Author SHA1 Message Date
coolneng e5a7b726a9
Add v_segments and j_segments objects 2021-04-07 18:32:58 +02:00
coolneng 38b35f7d12
Align full sequences efficiently 2021-04-07 18:31:39 +02:00
4 changed files with 45 additions and 14 deletions

BIN
data/j_segments_phe.rds Normal file

Binary file not shown.

BIN
data/v_segments.rds Normal file

Binary file not shown.

View File

@ -1,14 +1,14 @@
library(Biostrings) library(Biostrings)
library(parallel) library(parallel)
construct_dataframe <- function(data) { construct_hvr_sequence <- function(data) {
vdj_string_set <- lapply(data, FUN = Biostrings::DNAStringSet) vdj_dataframe <- as.data.frame(data)
vdj_dataframe <- as.data.frame(vdj_string_set) hvr_sequence <- paste(vdj_dataframe$v_sequence,
vdj_dataframe$hvr_region <- paste(vdj_dataframe$v_sequence,
vdj_dataframe$d_sequence, vdj_dataframe$d_sequence,
vdj_dataframe$j_sequence, vdj_dataframe$j_sequence,
sep = "" sep = ""
) )
vdj_dataframe$hvr <- hvr_sequence
return(vdj_dataframe) return(vdj_dataframe)
} }
@ -16,8 +16,33 @@ parse_data <- function(files) {
reversed_sequences <- Biostrings::readQualityScaledDNAStringSet(files[1]) reversed_sequences <- Biostrings::readQualityScaledDNAStringSet(files[1])
sequences <- Biostrings::reverseComplement(reversed_sequences) sequences <- Biostrings::reverseComplement(reversed_sequences)
vdj_alignment <- read.csv(files[2]) vdj_alignment <- read.csv(files[2])
vdj_dataframe <- construct_dataframe(vdj_alignment) vdj_dataframe <- construct_hvr_sequence(vdj_alignment)
return(list(sequences, vdj_dataframe)) vj_segments <- union(
readRDS("data/v_segments.rds"),
readRDS("data/j_segments_phe.rds")
)
return(list(sequences, vj_segments, vdj_dataframe))
}
# TODO Test if grep can return more than one match
get_vj_sequence <- function(identifier, names, sequences) {
row <- grep(names, pattern = identifier)
return(as.character(sequences[row]))
}
construct_full_sequences <- function(vdj_segments, metadata) {
v_sequences <- lapply(metadata$v_call,
names(vdj_segments),
vdj_segments,
FUN = get_vj_sequence
)
j_sequences <- lapply(metadata$j_call,
names(vdj_segments),
vdj_segments,
FUN = get_vj_sequence
)
full_sequence <- paste(v_sequences, metadata$hvr, j_sequences, sep = "")
return(Biostrings::DNAStringSet(full_sequence))
} }
align_sequence <- function(sequence, vdj_segment) { align_sequence <- function(sequence, vdj_segment) {
@ -25,20 +50,25 @@ align_sequence <- function(sequence, vdj_segment) {
pattern = sequence, pattern = sequence,
subject = vdj_segment, subject = vdj_segment,
type = "global-local", type = "global-local",
gapOpening = 1 gapOpening = 1,
)) ))
} }
perform_alignment <- function(sequences, vdj_segments) { perform_alignment <- function(sequences, vdj_segments, metadata) {
sequence_alignment <- mcmapply(sequences, vj_sequences <- construct_full_sequences(vdj_segments, metadata)
vdj_segments$hvr_region, sequence_alignment <- mcmapply(vj_sequences,
vdj_segments,
FUN = align_sequence, FUN = align_sequence,
mc.cores = 4 mc.cores = detectCores()
) )
return(sequence_alignment) return(sequence_alignment)
} }
input_files <- c("data/curesim_sequence.fastq", "data/vdj_alignment.csv") input_files <- c("data/curesim_sequence.fastq", "data/vdj_alignment.csv")
data <- parse_data(input_files) data <- parse_data(input_files)
alignment <- perform_alignment(sequences = data[[1]], vdj_segments = data[[2]]) alignment <- perform_alignment(
sequences = data[[1]],
vdj_segments = data[[2]],
metadata = data[[3]]
)
print(alignment) print(alignment)

View File

@ -20,7 +20,8 @@ save_data <- function(data, reads) {
process_data <- function(repertoire, reads) { process_data <- function(repertoire, reads) {
columns <- c( columns <- c(
"sequence", "v_sequence_alignment", "sequence", "v_sequence_alignment",
"d_sequence_alignment", "j_sequence_alignment" "d_sequence_alignment", "j_sequence_alignment",
"v_call", "j_call"
) )
data <- repertoire[, columns] data <- repertoire[, columns]
dna_sequence <- Biostrings::DNAStringSet(data$sequence) dna_sequence <- Biostrings::DNAStringSet(data$sequence)