Using getFrag to extract sequence fragments
1
0
Entering edit mode
2.3 years ago
margo ▴ 40

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"
getFrag seqnir sequencing R • 1.1k views
ADD COMMENT
1
Entering edit mode
2.3 years ago
Basti ★ 2.0k

You can unlist it to remove attributes : unlist(getFrag(f,1,5))

ADD COMMENT
0
Entering edit mode

This won't remove attributes. You can use the as.character generic or remove them directly attributes(frag) <- NULL.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
1
Entering edit mode

Indeed, thank you for the clarifications.

ADD REPLY
0
Entering edit mode

The unlist option gave me the desired output. Thank you!

ADD REPLY

Login before adding your answer.

Traffic: 2162 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6