Move DNAString to the top of the DNAStringSet
1
0
Entering edit mode
kalamari • 0
@kalamari-22562
Last seen 3.3 years ago

Hello, my question is fairly simple but I still cannot get the correct synthax. I would like to move one member of the DNA String Set to the top of the Set, such as:

Original:

 A DNAStringSet instance of length 10
     width seq                                                                                             names               
 [1] 100 ----------------------------------------------...---------------------------------------------- gene_5
 [2] 100 ----------------------------------------------...---------------------------------------------- gene_3
 [3] 100 ----------------------------------------------...---------------------------------------------- gene_1
 [4] 100 ----------------------------------------------...---------------------------------------------- gene_9

Target:

A DNAStringSet instance of length 10
     width seq                                                                                             names               
 [1] 100 ----------------------------------------------...---------------------------------------------- gene_1
 [2] 100 ----------------------------------------------...---------------------------------------------- gene_5
 [3] 100 ----------------------------------------------...---------------------------------------------- gene_3
 [4] 100 ----------------------------------------------...---------------------------------------------- gene_9
Biostrings Bioconductor • 689 views
ADD COMMENT
2
Entering edit mode
@herve-pages-1542
Last seen 1 day ago
Seattle, WA, United States

Do it the same way you would do it with a character vector or any vector-like object:

library(Biostrings)
x <- DNAStringSet(c(a="TATCA", b="GGGGGCCC", c="AA", d="CAGAGCA"))
x
#   A DNAStringSet instance of length 4
#     width seq                                               names               
# [1]     5 TATCA                                             a
# [2]     8 GGGGGCCC                                          b
# [3]     2 AA                                                c
# [4]     7 CAGAGCA                                           d

Move 3rd element to the top:

c(x[3], x[-3])
#   A DNAStringSet instance of length 4
#     width seq                                               names               
# [1]     2 AA                                                c
# [2]     5 TATCA                                             a
# [3]     8 GGGGGCCC                                          b
# [4]     7 CAGAGCA                                           d

More generally:

moveToTop <- function(x, n) c(x[n], x[-n])

moveToTop(x, 4)
#   A DNAStringSet instance of length 4
#     width seq                                               names               
# [1]     7 CAGAGCA                                           d
# [2]     5 TATCA                                             a
# [3]     8 GGGGGCCC                                          b
# [4]     2 AA                                                c

moveToTop(x, 1)  # no-op
#   A DNAStringSet instance of length 4
#     width seq                                               names               
# [1]     5 TATCA                                             a
# [2]     8 GGGGGCCC                                          b
# [3]     2 AA                                                c
# [4]     7 CAGAGCA                                           d

Also works for moving more than one element to the top:

moveToTop(x, c(4, 2))
#   A DNAStringSet instance of length 4
#     width seq                                               names               
# [1]     7 CAGAGCA                                           d
# [2]     8 GGGGGCCC                                          b
# [3]     5 TATCA                                             a
# [4]     2 AA                                                c

Works on any vector-like object:

moveToTop(101:110, c(5, 2, 4))
#  [1] 105 102 104 101 103 106 107 108 109 110

Hope this helps,

H.

ADD COMMENT

Login before adding your answer.

Traffic: 832 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