Applies given function to each sequence. Sequences are passed to function as character vectors
(or numeric, if type of sq
is enc) or single character strings, depending on
parameter.
sqapply(x, fun, ...)
# S3 method for sq
sqapply(
x,
fun,
...,
single_string = FALSE,
NA_letter = getOption("tidysq_NA_letter")
)
[sq
]
An object this function is applied to.
[function(1)
]
A function to apply to each sequence in sq
object; it should
take a character vector, numeric vector or single character string as an input.
further arguments to be passed from or to other methods.
[logical(1)
]
A value indicating in which form sequences should be
passed to the function fun
; if FALSE
(default), they will be treated as character
vectors, if TRUE
, they will be pasted into a single string.
[character(1)
]
A string that is used to interpret and display NA
value in the
context of sq class
. Default value equals to
"!
".
A list of values returned by function for each sequence in corresponding order.
# Creating objects to work on:
sq_dna <- sq(c("ATGCAGGA", "GACCGNBAACGAN", "TGACGAGCTTA"),
alphabet = "dna_bsc")
sq_ami <- sq(c("MIAANYTWIL","TIAALGNIIYRAIE", "NYERTGHLI", "MAYXXXIALN"),
alphabet = "ami_ext")
sq_unt <- sq(c("ATGCAGGA?", "TGACGAGCTTA", "", "TIAALGNIIYRAIE"))
# Counting how may "A" elements are present in sequences:
sqapply(sq_dna, function(sequence) sum(sequence == "A"))
#> [[1]]
#> [1] 3
#>
#> [[2]]
#> [1] 4
#>
#> [[3]]
#> [1] 3
#>
sqapply(sq_ami, function(sequence) sum(sequence == "A"))
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> [1] 0
#>
#> [[4]]
#> [1] 2
#>
sqapply(sq_unt, function(sequence) sum(sequence == "A"))
#> [[1]]
#> [1] 3
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> [1] 0
#>
#> [[4]]
#> [1] 3
#>