This won't remove attributes. You can use the as.character generic or remove them directly attributes(frag) <- NULL.
I have a DNA sequence in SeqFastadna format as a result of seqnir package in R. I am using the getFrag() function to extract sequence fragments. For example my SeqFastadna object (f) looks like this:
$genome
[1] "t" "t" "a" "a" "a" "a" "a" "a" "g" "a" "g" "a" "t" "c"
[1] "genome"
attr(,"Annot")
[1] ">genome, complete genome"
attr(,"class")
[1] "SeqFastadna"
When using the getFrag function getFrag(f, 1, 5), it prints out unnecessary information. I am only wanting the 5 bases, however this function is printing out other attributes too:
[1] "t" "t" "a" "a" "a"
attr(,"seqMother")
[1] "genome"
attr(,"begin")
[1] 1
attr(,"end")
[1] 5
attr(,"class")
[1] "SeqFrag"
Is there any way where I can just return the bases and have the following output?:
[1] "t" "t" "a" "a" "a"
1 answer
You can unlist it to remove attributes : unlist(getFrag(f,1,5))
My code produces the desired output, which is of class character and without attributes. as.character will not produce the desired output and attributes(getFrag(f,1,5)) is already NULL.
From what information they provided the output is a vector, which would be equivalent to the below code that uses a vector of characters as input.
library("seqinr")
s <- read.fasta(file = system.file("sequences/malM.fasta", package = "seqinr"))
> getFrag(s[[1]], 1, 10)
[1] "a" "t" "g" "a" "a" "a" "a" "t" "g" "a"
attr(,"seqMother")
[1] "XYLEECOM.MALM"
attr(,"begin")
[1] 1
attr(,"end")
[1] 10
attr(,"class")
[1] "SeqFrag"
In this case unlist won't work since it's not a list.
> getFrag(s[[1]], 1, 10) |> unlist()
[1] "a" "t" "g" "a" "a" "a" "a" "t" "g" "a"
attr(,"seqMother")
[1] "XYLEECOM.MALM"
attr(,"begin")
[1] 1
attr(,"end")
[1] 10
attr(,"class")
[1] "SeqFrag"
But as.character would work (same with setting attributes to NULL).
> getFrag(s[[1]], 1, 10) |> as.character()
[1] "a" "t" "g" "a" "a" "a" "a" "t" "g" "a"
Your code would work if the input was an unaltered SeqFastadna object, but that wouldn't match with the OP's output.
> getFrag(s, 1, 10)
[[1]]
[1] "a" "t" "g" "a" "a" "a" "a" "t" "g" "a"
attr(,"seqMother")
[1] "XYLEECOM.MALM"
attr(,"begin")
[1] 1
attr(,"end")
[1] 10
attr(,"class")
[1] "SeqFrag"
> getFrag(s, 1, 10) |> unlist()
[1] "a" "t" "g" "a" "a" "a" "a" "t" "g" "a"
It is possible that OP didn't provide the full output though, in which case your code would work incidentally.
Log in to answer this question.