s2n: simple numerical encoding of a DNA sequence.

Description Usage Arguments Value Note Author(s) References See Also Examples

View source: R/s2n.R

Description

By default, if no levels arguments is provided, this function will just code your DNA sequence in integer values following the lexical order (a > c > g > t), that is 0 for "a", 1 for "c", 2 for "g", 3 for "t" and NA for ambiguous bases.

Usage

1
s2n(seq, levels = s2c("acgt"), base4 = TRUE, forceToLower = TRUE)

Arguments

seq

the sequence as a vector of single chars

levels

allowed char values, by default a, c, g and t

base4

if TRUE the numerical encoding will start at O, if FALSE at 1

forceToLower

if TRUE the sequence is forced to lower case caracters

Value

a vector of integers

Note

The idea of starting numbering at 0 by default is that it enforces a kind of isomorphism between the paste operator on DNA chars and the + operator on integer coding for DNA chars. By this way, you can work either in the char set, either in the integer set, depending on what is more convenient for your purpose, and then switch from one set to the other one as you like.

Author(s)

J.R. Lobry

References

citation("seqinr")

See Also

n2s, factor, unclass

Examples

 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
##
## Example of default behaviour:
##
urndna <- s2c("acgt")
seq <- sample( urndna, 100, replace = TRUE ) ; seq
s2n(seq)
##
## How to deal with RNA:
##
urnrna <- s2c("acgt")
seq <- sample( urnrna, 100, replace = TRUE ) ; seq
s2n(seq)
##
## what happens with unknown characters:
##
urnmess <- c(urndna,"n")
seq <- sample( urnmess, 100, replace = TRUE ) ; seq
s2n(seq)
##
## How to change the encoding for unknown characters:
##
tmp <- s2n(seq) ; tmp[is.na(tmp)] <- -1; tmp
##
## Simple sanity check:
##
stopifnot(all(s2n(s2c("acgt")) == 0:3))

Example output

  [1] "c" "a" "c" "t" "g" "c" "t" "t" "a" "t" "c" "g" "a" "c" "a" "c" "g" "c"
 [19] "c" "t" "g" "c" "t" "a" "t" "c" "a" "g" "g" "t" "t" "t" "g" "a" "c" "g"
 [37] "a" "a" "t" "a" "t" "g" "g" "c" "g" "c" "t" "a" "c" "c" "a" "a" "a" "a"
 [55] "g" "c" "t" "t" "t" "a" "t" "t" "g" "t" "a" "c" "a" "t" "a" "g" "a" "a"
 [73] "c" "g" "c" "t" "a" "t" "a" "c" "c" "g" "t" "t" "t" "a" "t" "t" "c" "a"
 [91] "g" "c" "c" "g" "c" "g" "t" "c" "c" "g"
  [1] 1 0 1 3 2 1 3 3 0 3 1 2 0 1 0 1 2 1 1 3 2 1 3 0 3 1 0 2 2 3 3 3 2 0 1 2 0
 [38] 0 3 0 3 2 2 1 2 1 3 0 1 1 0 0 0 0 2 1 3 3 3 0 3 3 2 3 0 1 0 3 0 2 0 0 1 2
 [75] 1 3 0 3 0 1 1 2 3 3 3 0 3 3 1 0 2 1 1 2 1 2 3 1 1 2
attr(,"levels")
[1] "a" "c" "g" "t"
  [1] "c" "a" "t" "g" "t" "t" "c" "g" "g" "c" "c" "g" "c" "t" "g" "t" "g" "g"
 [19] "a" "t" "a" "a" "a" "a" "c" "c" "g" "a" "g" "c" "a" "t" "c" "g" "g" "g"
 [37] "g" "c" "c" "t" "g" "t" "t" "c" "c" "g" "c" "c" "g" "g" "a" "c" "a" "a"
 [55] "c" "g" "t" "c" "c" "a" "c" "t" "c" "t" "g" "c" "t" "t" "g" "t" "t" "a"
 [73] "t" "t" "t" "t" "g" "t" "g" "g" "a" "g" "a" "a" "c" "c" "a" "a" "t" "g"
 [91] "c" "a" "t" "a" "c" "g" "c" "a" "c" "t"
  [1] 1 0 3 2 3 3 1 2 2 1 1 2 1 3 2 3 2 2 0 3 0 0 0 0 1 1 2 0 2 1 0 3 1 2 2 2 2
 [38] 1 1 3 2 3 3 1 1 2 1 1 2 2 0 1 0 0 1 2 3 1 1 0 1 3 1 3 2 1 3 3 2 3 3 0 3 3
 [75] 3 3 2 3 2 2 0 2 0 0 1 1 0 0 3 2 1 0 3 0 1 2 1 0 1 3
attr(,"levels")
[1] "a" "c" "g" "t"
  [1] "c" "a" "t" "n" "c" "c" "g" "a" "a" "n" "g" "a" "t" "c" "g" "c" "t" "g"
 [19] "g" "n" "n" "t" "c" "t" "c" "a" "t" "n" "g" "c" "t" "g" "a" "c" "n" "a"
 [37] "c" "t" "t" "g" "a" "n" "t" "g" "t" "g" "t" "n" "t" "c" "c" "g" "c" "c"
 [55] "g" "g" "g" "c" "a" "c" "t" "t" "a" "a" "g" "a" "n" "n" "c" "a" "t" "a"
 [73] "a" "c" "t" "c" "t" "c" "c" "t" "a" "a" "n" "g" "g" "a" "g" "n" "c" "c"
 [91] "c" "t" "n" "t" "a" "n" "n" "n" "g" "c"
  [1]  1  0  3 NA  1  1  2  0  0 NA  2  0  3  1  2  1  3  2  2 NA NA  3  1  3  1
 [26]  0  3 NA  2  1  3  2  0  1 NA  0  1  3  3  2  0 NA  3  2  3  2  3 NA  3  1
 [51]  1  2  1  1  2  2  2  1  0  1  3  3  0  0  2  0 NA NA  1  0  3  0  0  1  3
 [76]  1  3  1  1  3  0  0 NA  2  2  0  2 NA  1  1  1  3 NA  3  0 NA NA NA  2  1
attr(,"levels")
[1] "a" "c" "g" "t"
  [1]  1  0  3 -1  1  1  2  0  0 -1  2  0  3  1  2  1  3  2  2 -1 -1  3  1  3  1
 [26]  0  3 -1  2  1  3  2  0  1 -1  0  1  3  3  2  0 -1  3  2  3  2  3 -1  3  1
 [51]  1  2  1  1  2  2  2  1  0  1  3  3  0  0  2  0 -1 -1  1  0  3  0  0  1  3
 [76]  1  3  1  1  3  0  0 -1  2  2  0  2 -1  1  1  1  3 -1  3  0 -1 -1 -1  2  1
attr(,"levels")
[1] "a" "c" "g" "t"

seqinr documentation built on June 9, 2021, 9:06 a.m.