Using 'BSgenome::export' to write each chromosome of an organism into separate files
1
0
Entering edit mode
• 0
@-10147
Last seen 6.3 years ago

PROBLEM

I have the following script where I am using the export function from BSgenome package to write only the first seven chromosomes of an object (of class BSgenome) into separate files.

source("https://bioconductor.org/biocLite.R")
biocLite("BSgenome.Scerevisiae.UCSC.sacCer3")
library(BSgenome.Scerevisiae.UCSC.sacCer3)
library(BSgenome)
    
export(BSgenome.Scerevisiae.UCSC.sacCer3::Scerevisiae$chrI, "Scerevisiae_chr1.fasta", compress=FALSE)
export(BSgenome.Scerevisiae.UCSC.sacCer3::Scerevisiae$chrII, "Scerevisiae_chr2.fasta", compress=FALSE)
export(BSgenome.Scerevisiae.UCSC.sacCer3::Scerevisiae$chrIII, "Scerevisiae_chr3.fasta", compress=FALSE)
export(BSgenome.Scerevisiae.UCSC.sacCer3::Scerevisiae$chrIV, "Scerevisiae_chr4.fasta", compress=FALSE)
export(BSgenome.Scerevisiae.UCSC.sacCer3::Scerevisiae$chrV, "Scerevisiae_chr5.fasta", compress=FALSE)
export(BSgenome.Scerevisiae.UCSC.sacCer3::Scerevisiae$chrVI, "Scerevisiae_chr6.fasta", compress=FALSE)
export(BSgenome.Scerevisiae.UCSC.sacCer3::Scerevisiae$chrVII, "Scerevisiae_chr7.fasta", compress=FALSE)

The problem is that I have many other organisms for which I must do the same.

OBJECTIVE

How can I automate the export process in a way that the filename of the output matches the object and element used?

So as an example, when Scerevisiae$chrVII is exported, I want to see Scerevisiae_chrVII.fasta for the name of the FASTA file.

WHAT I HAVE TRIED AND HAS NOT WORKED

BiocGenerics::sapply(X = Scerevisiae, MARGIN = c(1:7), 
    function(x) BSgenome::export(con = paste0(substitute(x), ".fasta")))

BiocGenerics::sapply(X = Scerevisiae, FUN = export)

bsapply(new("BSParams", X = Scerevisiae, FUN = function(x) BSgenome::export(con = paste0(substitute(x), ".fasta"))))
bsgenome biocgenerics bsapply export • 1.2k views
ADD COMMENT
2
Entering edit mode
@james-w-macdonald-5106
Last seen 10 hours ago
United States
> dontprint <- lapply(grep("chr", names(Scerevisiae), value = TRUE), function(x) BSgenome::export(Scerevisiae[[x]], paste0("Scerevisiae_", x, ".fasta")))

> dir(".", ".fasta$")
 [1] "Scerevisiae_chrI.fasta"    "Scerevisiae_chrII.fasta"  
 [3] "Scerevisiae_chrIII.fasta"  "Scerevisiae_chrIV.fasta"  
 [5] "Scerevisiae_chrIX.fasta"   "Scerevisiae_chrM.fasta"   
 [7] "Scerevisiae_chrV.fasta"    "Scerevisiae_chrVI.fasta"  
 [9] "Scerevisiae_chrVII.fasta"  "Scerevisiae_chrVIII.fasta"
[11] "Scerevisiae_chrX.fasta"    "Scerevisiae_chrXI.fasta"  
[13] "Scerevisiae_chrXII.fasta"  "Scerevisiae_chrXIII.fasta"
[15] "Scerevisiae_chrXIV.fasta"  "Scerevisiae_chrXV.fasta"  
[17] "Scerevisiae_chrXVI.fasta"
>
ADD COMMENT

Login before adding your answer.

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