Coerces sequences from a sq object to a matrix, in which rows correspond to sequences and columns to positions.

# S3 method for sq
as.matrix(x, ...)

Arguments

x

[sq]
An object this function is applied to.

...

further arguments to be passed from or to other methods.

Value

A matrix with number of rows the same as number of sequences and number of columns corresponding to the length of the longest sequence in the converted sq object.

Details

This method for class sq allows converting sequences from the sq object into a matrix. Each row corresponds to the separate sequence from the sq object, whereas each column indicates a single position within a sequence. Dimensions of matrix are determined by the number of sequences (rows) and the length of the longest sequence (columns). If length of a sequence is smaller than the length of the longest sequence, the remaining columns are filled with NA. All attributes of the input sq are lost during the conversion to matrix.

See also

Functions from output module: as.character.sq(), as.sq(), export_sq(), write_fasta()

Examples

# Creating objects to work on:
sq_dna <- sq(c("CGATAGACA", "TGACAAAAC", "GTGACCGTA"),
             alphabet = "dna_bsc")
sq_rna <- sq(c("CUGAAUGCAGUACCGUAAU", "AUGCCGUAAAUGCCAU", "CAGACCANNNAUAG"),
             alphabet = "rna_ext")

# Sequences of the same lengths can be converted easily:
as.matrix(sq_dna)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
#> [1,] "C"  "G"  "A"  "T"  "A"  "G"  "A"  "C"  "A" 
#> [2,] "T"  "G"  "A"  "C"  "A"  "A"  "A"  "A"  "C" 
#> [3,] "G"  "T"  "G"  "A"  "C"  "C"  "G"  "T"  "A" 

# Sequences that differ in length are filled with NA to the maximum length:
as.matrix(sq_rna)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,] "C"  "U"  "G"  "A"  "A"  "U"  "G"  "C"  "A"  "G"   "U"   "A"   "C"   "C"  
#> [2,] "A"  "U"  "G"  "C"  "C"  "G"  "U"  "A"  "A"  "A"   "U"   "G"   "C"   "C"  
#> [3,] "C"  "A"  "G"  "A"  "C"  "C"  "A"  "N"  "N"  "N"   "A"   "U"   "A"   "G"  
#>      [,15] [,16] [,17] [,18] [,19]
#> [1,] "G"   "U"   "A"   "A"   "U"  
#> [2,] "A"   "U"   NA    NA    NA   
#> [3,] NA    NA    NA    NA    NA