java将一个函数拆分成几个_R:将数据表拆分为多个块并在每个块上应用函数

我正在尝试将大型csv文件作为数据表读取,根据字段“sample_name”将其拆分为64个块,并以并行方式在每个块上应用函数“myfunction” .

library(data.table)

library(plyr)

library(doMC)

registerDoMC(5) #assign 5 cores

#read large csv file with 6485845 rows, 13 columns

dt = fread('~/data/samples.csv')

#example subset of dt (I am showing only 3 columns)

#sample_name snpprobeset_id snp_strand

#C00060 exm1002141 +

#C00060 exm1002260 -

#C00060 exm1002276 +

#C00075 exm1002434 -

#C00075 exm1002585 -

#C00150 exm1002721 -

#C00150 exm1004566 -

#C00154 exm100481 +

#C00154 exm1004821 -

#split into 64 chunks based on column 'sample_name'.

#each chunk is passed as an argument to a function 'myfunction'

ddply(dt,.(sample_name),myfunction,.parallel=TRUE)

#function definition

myfunction

{

#arg1

#write columns 9,11,12 to a tab-limited bed file named 'sample_name.bed' for e.g. C00060.bed, C00075.bed and so on. 64 bed files for 64 chunks would be written out.

write.table(arg1[,c(9,11,12)],paste("~/Desktop/",paste(unique(arg1$sample_name),".bed",sep=""),sep=""),row.names=F,quote=F,sep="\t",col.names=F)

#execute a system command for bam-readcount (bioinformatics program)

#build command

p1

p2

p3

p4

p5

p6 ")

system(p6) #execute system command

#executes something like this, for sample_name=C00060

#bam-readcount -b 20 -f hg19.fa -l C00060.bed C00060.bam > C00060_output.txt

#read back in C00060_output.txt file

#manipulate the file..multiple steps

#write output to another file

}

在这里,当我使用ddply()基于'sample_name'分割我的数据表'dt'时,它被分成数据帧而不是数据表 . 所以我想在将数据帧传递到函数(函数定义的第一行)之后将数据帧转换为数据表,然后使用数据表执行其余的处理 . 有没有更好,更有效的替代方案?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值