21 Commits

Author SHA1 Message Date
ad8abcc4fc Add usage instructions to the README 2021-05-04 01:28:49 +02:00
6440816a87 Remove imperative installation instructions 2021-05-04 00:59:05 +02:00
0e005735bc Create a Nix derivation for CuReSim 2021-05-04 00:57:35 +02:00
4f0936718b Add installation instruction to README 2021-05-03 23:27:19 +02:00
1b6e2d13ea Remove development dependencies 2021-05-03 23:22:16 +02:00
36eb73b458 Add alignment to generation script 2021-05-03 21:51:48 +02:00
81a57657fe Fix HVR end position computation 2021-05-03 21:51:32 +02:00
5afe040592 Isolate HVR sequence and save it to a file 2021-05-03 21:15:40 +02:00
c250c139dd Implement cysteine location in v_alignment 2021-04-27 19:34:01 +02:00
4dec2061fc Generate FASTQ files from the simulated repertoire 2021-04-22 13:59:45 +02:00
4adb92e901 Export original CDR3 to a file 2021-04-22 11:54:58 +02:00
83819b296b Save vj_sequences in a dataframe 2021-04-22 01:18:25 +02:00
a7c1df5ce2 Refactor get_vj_sequence function 2021-04-22 01:17:35 +02:00
81ebd4fbbe Rename function arguments to improve readability 2021-04-21 22:12:29 +02:00
659f0097d8 Get V and J sequences from sequence ID 2021-04-21 21:29:03 +02:00
fb5d781c66 Add space to sequence ID for easier parsing 2021-04-21 21:02:56 +02:00
35406497a3 Format generation script 2021-04-21 20:11:56 +02:00
b771071974 Remove csv from gitignore 2021-04-21 20:11:32 +02:00
2a997a3e5c Rename sequencing_runs to number_of_reads 2021-04-21 20:09:02 +02:00
1020d610d3 Run CuReSim n times for each sequence 2021-04-21 20:00:13 +02:00
5154a35fca Remove sequencing runs argument from repertoire 2021-04-21 19:59:38 +02:00
6 changed files with 166 additions and 53 deletions

2
.gitignore vendored
View File

@@ -1,3 +1 @@
*.csv
*.fasta
*.fastq

View File

@@ -1,3 +1,49 @@
* locigenesis
locigenesis is a tool that generates an immune repertoire and runs it through a sequence reader simulation tool, to generate sequencing errors.
** Installation
This project uses [[https://nixos.org/][Nix]] to ensure reproducible builds.
1. Install Nix (compatible with MacOS, Linux and [[https://docs.microsoft.com/en-us/windows/wsl/about][WSL]]):
#+begin_src shell
curl -L https://nixos.org/nix/install | sh
#+end_src
1. Clone the repository:
#+begin_src shell
git clone https://git.coolneng.duckdns.org/coolneng/locigenesis
#+end_src
3. Change the working directory to the project:
#+begin_src shell
cd locigenesis
#+end_src
4. Enter the nix-shell:
#+begin_src shell
nix-shell
#+end_src
After running these commands, you will find yourself in a shell that contains all the needed dependencies.
** Usage
An execution script that accepts 2 parameters is provided, the following command invokes it:
#+begin_src shell
./generation.sh <number of sequences> <number of reads>
#+end_src
- <number of sequences>: an integer that specifies the number of different sequences to generate
- <number of reads>: an integer that specifies the number of reads to perform on each sequence
The script will generate 2 files under the data directory:
| HVR.fastq | Contains the original CDR3 sequence |
| CuReSim-HVR.fastq | Contains CDR3 after the read simulation, with sequencing errors |

View File

@@ -1,7 +1,7 @@
#!/bin/sh
usage() {
echo "usage: generation.sh <number of sequences> <sequencing runs>"
echo "usage: generation.sh <number of sequences> <number of reads>"
exit 1
}
@@ -10,14 +10,13 @@ if [ $# != 2 ]; then
fi
sequences=$1
sequencing_runs=$2
read_mean_size=350
read_variance_size=0.0
number_of_reads=$2
data_directory="data/"
fasta=".fasta"
fastq=".fastq"
filename="sequence"
prefix="curesim_"
Rscript src/repertoire.r "$sequences" "$sequencing_runs" && java -jar tools/CuReSim.jar -n "$sequencing_runs" -m "$read_mean_size" -sd "$read_variance_size" -f "$data_directory$filename$fasta" -o "$data_directory$prefix$filename$fastq"
Rscript src/repertoire.r "$sequences" "$number_of_reads" &&
CuReSim -f "$data_directory$filename$fastq" -o "$data_directory$prefix$filename$fastq"
Rscript src/alignment.r
rm "$data_directory/log.txt"

View File

@@ -2,14 +2,35 @@
with pkgs;
mkShell {
let
CuReSim = stdenv.mkDerivation rec {
name = "CuReSim";
version = "1.3";
src = fetchzip {
url =
"http://www.pegase-biosciences.com/wp-content/uploads/2015/08/${name}${version}.zip";
sha256 = "1hvlpgy4haqgqq52mkxhcl9i1fx67kgwi6f1mijvqzk0xff77hkp";
stripRoot = true;
extraPostFetch = ''
chmod go-w $out
'';
};
nativeBuildInputs = [ makeWrapper ];
installPhase = ''
mkdir -pv $out/share/java $out/bin
cp -r ${src} $out/share/java/${name}
makeWrapper ${pkgs.jdk}/bin/java $out/bin/CuReSim --add-flags "-jar $out/share/java/${name}/${name}.jar"
'';
};
in mkShell {
buildInputs = [
R
rPackages.immuneSIM
rPackages.Biostrings
rPackages.stringr
jdk
# Development tools
rPackages.languageserver
rPackages.lintr
CuReSim
];
}

View File

@@ -1,36 +1,44 @@
library(Biostrings)
library(parallel)
parse_data <- function(files) {
reversed_sequences <- Biostrings::readQualityScaledDNAStringSet(files[1])
parse_data <- function(file) {
reversed_sequences <- Biostrings::readQualityScaledDNAStringSet(file)
sequences <- Biostrings::reverseComplement(reversed_sequences)
vdj_metadata <- read.csv(files[2])
vj_segments <- union(
readRDS("data/v_segments.rds"),
readRDS("data/j_segments_phe.rds")
)
return(list(sequences, vj_segments, vdj_metadata))
return(list(sequences, vj_segments))
}
get_vj_sequence <- function(identifier, names, sequences) {
matches <- grep(names, pattern = identifier)
parse_metadata <- function(metadata) {
id_elements <- unlist(strsplit(metadata, split = " "))
v_identifier <- id_elements[2]
j_identifier <- id_elements[3]
return(list(v_id = v_identifier, j_id = j_identifier))
}
match_id_sequence <- function(names, vdj_segments, id) {
matches <- grep(names, pattern = id)
row <- matches[1]
return(as.character(sequences[row]))
return(as.character(vdj_segments[row]))
}
construct_full_sequences <- function(vdj_segments, metadata) {
v_sequences <- lapply(metadata$v_call,
get_vj_sequence <- function(metadata, names, vdj_segments) {
identifiers <- parse_metadata(metadata)
v_sequence <- match_id_sequence(names, vdj_segments, id = identifiers["v_id"])
j_sequence <- match_id_sequence(names, vdj_segments, id = identifiers["j_id"])
return(list(v_seq = v_sequence, j_seq = j_sequence))
}
fetch_vj_sequences <- function(sequences, vdj_segments) {
vj_sequences <- sapply(names(sequences),
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$junction, j_sequences, sep = "")
return(Biostrings::DNAStringSet(full_sequence))
results <- data.frame(t(vj_sequences))
return(results)
}
align_sequence <- function(sequence, vdj_segment) {
@@ -38,25 +46,56 @@ align_sequence <- function(sequence, vdj_segment) {
subject = sequence,
pattern = vdj_segment,
type = "global-local",
gapOpening = 1,
gapOpening = 1
))
}
perform_alignment <- function(sequences, vdj_segments, metadata) {
vj_sequences <- construct_full_sequences(vdj_segments, metadata)
sequence_alignment <- mcmapply(vj_sequences,
vdj_segments,
FUN = align_sequence,
mc.cores = detectCores()
)
return(sequence_alignment)
handle_indels <- function(insertion, deletion, cys, alignment) {
ins_start <- sum(Biostrings::width(deletion[start(deletion) <= cys$start]))
ins_end <- sum(Biostrings::width(deletion[end(deletion) <= cys$end]))
shift_num <- c(0, cumsum(Biostrings::width(insertion))[-length(ins_start)])
shifted_ins <- IRanges::shift(insertion, shift_num)
gaps <- sum(width(shifted_ins[end(shifted_ins) < cys$start + ins_start])) +
nchar(stringr::str_extract(alignedSubject(alignment), "^-*"))
return(list("start" = ins_start - gaps, "end" = ins_end - gaps))
}
input_files <- c("data/curesim_sequence.fastq", "data/vdj_metadata.csv")
data <- parse_data(files = input_files)
alignment <- perform_alignment(
sequences = data[[1]],
vdj_segments = data[[2]],
metadata = data[[3]]
)
print(alignment)
get_cys_coordinates <- function(alignment) {
cys <- list("start" = 310, "end" = 312)
insertion <- unlist(Biostrings::insertion(alignment))
deletion <- unlist(Biostrings::deletion(alignment))
delta_coordinates <- handle_indels(insertion, deletion, cys, alignment)
cys_start <- cys$start + delta_coordinates$start
cys_end <- cys$end + delta_coordinates$end
return(list("start" = cys_start, "end" = cys_end))
}
get_hvr_sequences <- function(sequences, vdj_segments, cores = detectCores()) {
df <- fetch_vj_sequences(sequences, vdj_segments)
v_alignment <- parallel::mcmapply(sequences,
df$v_seq,
FUN = align_sequence,
mc.cores = cores
)
cys_coordinates <- parallel::mclapply(v_alignment, FUN = get_cys_coordinates)
cys_df <- as.data.frame(do.call(rbind, cys_coordinates))
remaining <- Biostrings::subseq(sequences, start = unlist(cys_df$end))
j_alignment <- parallel::mcmapply(remaining,
df$j_seq,
FUN = align_sequence,
mc.cores = cores
)
j_start <- parallel::mclapply(
j_alignment,
function(x) start(Biostrings::Views(x)),
mc.cores = cores
)
hvr_start <- unlist(cys_df$start)
hvr_end <- unlist(cys_df$start) + unlist(j_start) + 2
hvr <- Biostrings::subseq(sequences, start = hvr_start, end = hvr_end)
return(hvr)
}
data <- parse_data(file = "data/curesim_sequence.fastq")
hvr <- get_hvr_sequences(sequences = data[[1]], vdj_segments = data[[2]])
Biostrings::writeXStringSet(hvr, "data/CuReSim-HVR.fastq", format = "fastq")

View File

@@ -10,23 +10,33 @@ generate_repertoire <- function(number_of_sequences) {
))
}
process_data <- function(repertoire, reads) {
columns <- c("sequence", "v_call", "j_call")
data <- repertoire[, columns]
save_data <- function(data) {
Biostrings::writeXStringSet(data$sequence,
"data/sequence.fastq",
format = "fastq"
)
Biostrings::writeXStringSet(data$junction, "data/HVR.fastq", format = "fastq")
}
process_data <- function(data, reads) {
dna_sequence <- Biostrings::DNAStringSet(data$sequence)
data$sequence <- Biostrings::reverseComplement(dna_sequence)
names(data$sequence) <- paste(rownames(data), data$v_call, data$j_call)
Biostrings::writeXStringSet(data$sequence, "data/sequence.fasta")
names(data$sequence) <- paste(rownames(data), data$v_call, data$j_call, " ")
data$junction <- Biostrings::DNAStringSet(data$junction)
names(data$junction) <- rownames(data)
amplified_data <- data[rep(seq_len(nrow(data)), reads), ]
return(amplified_data)
}
parse_cli_arguments <- function() {
args <- commandArgs(trailingOnly = TRUE)
if (length(args) != 2) {
stop("usage: repertoire.r <number of sequences> <sequencing runs>")
stop("usage: repertoire.r <number of sequences> <number of reads>")
}
return(c(args[1], args[2]))
return(args)
}
args <- parse_cli_arguments()
repertoire <- generate_repertoire(number_of_sequences = as.integer(args[1]))
process_data(repertoire = repertoire, reads = as.integer(args[2]))
data <- process_data(data = repertoire, reads = args[2])
save_data(data)