-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine_adapters.R
45 lines (38 loc) · 1.31 KB
/
combine_adapters.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
#
# Author: Stephen Turner
# https://github.com/stephenturner/adapters/blob/master/combine_adapters.R
library(tidyverse)
# Function to take paths to fasta files and read them all into a single character vector
fasta_combine <- function(paths) {
paths %>%
map(read_lines) %>%
unlist()
}
# Function to take a fasta file as a character vector and turn into a data frame
fasta_to_df <- function(fasta) {
tibble(name=fasta[seq(1, length(fasta), 2)],
seq=fasta[seq(2, length(fasta), 2)])
}
# Function to look through the fasta, get unique sequences, and combine the sequence IDs
fasta_unique_sequences <- function(fasta_df) {
fasta_df %>%
group_by(seq) %>%
summarize(name=name %>% unique %>% paste(collapse=" | ") %>% str_replace_all(fixed(" | >"), " | ")) %>%
select(name, seq)
}
# Function to take a fasta df and create a flat character vector
fasta_df_to_flat <- function(fasta_df) {
fasta <- deframe(fasta_df)
out <- NULL
for (i in seq_along(fasta)) {
out <- c(out, names(fasta)[i])
out <- c(out, unname(fasta[i]))
}
return(out)
}
adapters <- list.files(pattern="*.fa", recursive=FALSE) %>%
fasta_combine() %>%
fasta_to_df() %>%
fasta_unique_sequences() %>%
fasta_df_to_flat()
write_lines(adapters, path=paste0("adapters_combined_", length(adapters)/2, "_unique.fasta"))