R语言动态创建压缩文件

动态创建压缩文件,可以简单地理解为将结果写出到压缩文件,而不是先写出到文件然后压缩。

R语言中,R包vroom就可以实现这一过程。

逐行写出函数vroom_write_lines与数据表写出函数vroom_write,可以通过识别文件后缀名的方式实现动态创建压缩文件。

生物信息中常见的压缩格式为.gz 压缩,这里以拟南芥的基因组序列进行测试。

下载

我的windows上也安装了axel,因此这里直接使用axel下载基因组。下载后的压缩文件为35M左右

library(vroom)

command <- "axel -n 20 https://ftp.ensemblgenomes.ebi.ac.uk/pub/plants/release-57/fasta/arabidopsis_thaliana/dna/Arabidopsis_thaliana.TAIR10.dna.toplevel.fa.gz"
system(command = command)

ath_file <- "Arabidopsis_thaliana.TAIR10.dna.toplevel.fa.gz"

file.size(ath_file)
# [1] 36462703 # 35M左右

按行读取

vroom_lines 按行读入数据,读入结果为字符串向量。

vroom 读入数据表数据,读入结果为数据框(“spec_tbl_df”, “tbl_df” , “tbl” , “data.frame”)。

这里按行读入fasta数据。

ath <- vroom_lines(file = ath_file)

class(ath)
# [1] "character"

ath[1:2]
# [1] ">1 dna:chromosome chromosome:TAIR10:1:1:30427671:1 REF"      
# [2] "CCCTAAACCCTAAACCCTAAACCCTAAACCTCTGAATCCTTAATCCCTAAATCCCTAAAT"

写出未压缩文件

结果文件在116M左右。

vroom_write_lines(ath, file = "ath.fa", eol = "\n")
file.size("ath.fa")
# [1] 121662600  # 116M左右

写出压缩文件

通过.gz后缀名识别并动态创建压缩文件。结果文件在35M左右

vroom_write_lines(ath, file = "ath.fa.gz", eol = "\n")
file.size("ath.fa.gz")

# [1] 36461631 # 35M左右

可以看到,加了.gz的结果文件"ath.fa.gz"与原压缩文件大小相近,且远小于未压缩的"ath.fa" 文件。说明动态常见压缩文件成功了。

全部代码

library(vroom)

command <- "axel -n 20 https://ftp.ensemblgenomes.ebi.ac.uk/pub/plants/release-57/fasta/arabidopsis_thaliana/dna/Arabidopsis_thaliana.TAIR10.dna.toplevel.fa.gz"
system(command = command)

ath_file <- "Arabidopsis_thaliana.TAIR10.dna.toplevel.fa.gz"

file.size(ath_file)
# [1] 36462703 # 35M左右

ath <- vroom_lines(file = ath_file)
class(ath)
# 未压缩
vroom_write_lines(ath, file = "ath.fa", eol = "\n")
file.size("ath.fa")
# [1] 121662600  # 116M左右

# 压缩
vroom_write_lines(ath, file = "ath.fa.gz", eol = "\n")
file.size("ath.fa.gz")

# [1] 36461631 # 35M左右
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值