循环遍历文件并使用函数,然后使用该结果在 r 中形成数据帧
我有一个已排序的 bam 文件目录,我想对其使用 pileup
函数。 pileup
函数的输出是一个数据帧。然后我想使用每个文件的结果并形成一个数据框。
对于每个文件,我使用以下代码:
r16<-pileup(filename, index=filename, scanBamParam = ScanBamParam(), pileupParam = PileupParam())
r16$sample_id <- "sample id"
对于 sample_id
列,我希望它是文件的名称,例如:
- 文件名是
file1.sorted.bam,我希望
sample_id
为file1
处理完所有文件后,我将使用 rbind
获取一个大数据帧并保存它到 RData 文件。
到目前为止,我已经尝试在它们上使用循环,但它没有给我任何输出。
library(pasillaBamSubset)
library(Rsamtools)
filenames<-Sys.glob("*.sorted.bam")
for (file in filenames) {
output <- pileup(pileup(filenames, index=filenames, scanBamParam = ScanBamParam(), pileupParam = PileupParam()))
save(output, file = "res.RData")
}
I have a directory of sorted bam files that I want to use pileup
function to. The output of pileup
function is a dataframe. Then I would like to use the result of each file and form a dataframe.
For each file, I use the follow codes:
r16<-pileup(filename, index=filename, scanBamParam = ScanBamParam(), pileupParam = PileupParam())
r16$sample_id <- "sample id"
For sample_id
column, I would like it to be the name of the file, for example:
- the file name is
file1.sorted.bam
, I would likesample_id
to befile1
And after all files are processed, I would use rbind
to get a big dataframe and save it to a RData file.
So far, I have tried to use the loops on them, but it is not giving me any outputs.
library(pasillaBamSubset)
library(Rsamtools)
filenames<-Sys.glob("*.sorted.bam")
for (file in filenames) {
output <- pileup(pileup(filenames, index=filenames, scanBamParam = ScanBamParam(), pileupParam = PileupParam()))
save(output, file = "res.RData")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您想要将所有 data.frames 堆叠在一起(行绑定)。
map
(来自 purrr)或lapply
可以将函数应用于中的每个项目给定的列表/向量(本例中的每个文件名)。
map_dfr
执行相同的操作,行绑定所有输出。I am assuming that you want to stack all the data.frames on top of each other (row bind).
map
(from purrr) orlapply
can apply a function to each item ina given list/vector (each filename in this case).
map_dfr
does the same and row binds all the outputs.