【问题标题】:How to download .gz files from FTP server in R?如何从 R 中的 FTP 服务器下载 .gz 文件?
【发布时间】:2019-01-04 23:17:40
【问题描述】:

我正在尝试从此链接下载所有 .gz 文件: ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606_b151_GRCh38p7/BED/

到目前为止,我尝试了这个,但没有得到任何结果:

require(RCurl)

url= "ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606_b151_GRCh38p7/BED/"
filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE)
filenames <- strsplit(filenames, "\r\n")
filenames = unlist(filenames)

我收到此错误:

Error in function (type, msg, asError = TRUE)  : 
  Operation timed out after 300552 milliseconds with 0 out of 0 bytes  received

有人可以帮忙吗?

谢谢

编辑: 我尝试使用下面提供给我的文件名运行,所以在我的 r 脚本中我有:

require(RCurl)
my_url <-"ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606_b151_GRCh38p7/BED/"

my_filenames= c("bed_chr_11.bed.gz", ..."bed_chr_9.bed.gz.md5")

my_filenames <- strsplit(my_filenames, "\r\n")
my_filenames = unlist(my_filenames)

for(my_file in my_filenames){
download.file(paste0(my_url, my_file), destfile =  file.path('/mydir', my_file))
}

当我运行脚本时,我会收到以下警告:

trying URL 'ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606_b151_GRCh38p7/BED/bed_chr_11.bed.gz'

download.file(paste0(my_url, my_file), destfile = file.path("/mydir", ) 中的错误: 无法打开 URL 'ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606_b151_GRCh38p7/BED/bed_chr_11.bed.gz' 另外:警告信息: 在 download.file(paste0(my_url, my_file), destfile = file.path("/mydir", : URL 'ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606_b151_GRCh38p7/BED/bed_chr_11.bed.gz': 状态为'已超时' 执行停止

【问题讨论】:

  • 抱歉,只是为了澄清一下,您发布的代码对您有用吗?我问是因为它对我有用,所以我不确定你在哪里得到错误。
  • 不,它没有。在此行之后失败:filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE)

标签: r curl


【解决方案1】:

您尝试访问的文件名是

filenames <- c("bed_chr_11.bed.gz", "bed_chr_11.bed.gz.md5", "bed_chr_12.bed.gz", 
"bed_chr_12.bed.gz.md5", "bed_chr_13.bed.gz", "bed_chr_13.bed.gz.md5", 
"bed_chr_14.bed.gz", "bed_chr_14.bed.gz.md5", "bed_chr_15.bed.gz", 
"bed_chr_15.bed.gz.md5", "bed_chr_16.bed.gz", "bed_chr_16.bed.gz.md5", 
"bed_chr_17.bed.gz", "bed_chr_17.bed.gz.md5", "bed_chr_18.bed.gz", 
"bed_chr_18.bed.gz.md5", "bed_chr_19.bed.gz", "bed_chr_19.bed.gz.md5", 
"bed_chr_20.bed.gz", "bed_chr_20.bed.gz.md5", "bed_chr_21.bed.gz", 
"bed_chr_21.bed.gz.md5", "bed_chr_22.bed.gz", "bed_chr_22.bed.gz.md5", 
"bed_chr_AltOnly.bed.gz", "bed_chr_AltOnly.bed.gz.md5", "bed_chr_MT.bed.gz", 
"bed_chr_MT.bed.gz.md5", "bed_chr_Multi.bed.gz", "bed_chr_Multi.bed.gz.md5", 
"bed_chr_NotOn.bed.gz", "bed_chr_NotOn.bed.gz.md5", "bed_chr_PAR.bed.gz", 
"bed_chr_PAR.bed.gz.md5", "bed_chr_Un.bed.gz", "bed_chr_Un.bed.gz.md5", 
"bed_chr_X.bed.gz", "bed_chr_X.bed.gz.md5", "bed_chr_Y.bed.gz", 
"bed_chr_Y.bed.gz.md5", "bed_chr_1.bed.gz", "bed_chr_1.bed.gz.md5", 
"bed_chr_10.bed.gz", "bed_chr_10.bed.gz.md5", "bed_chr_2.bed.gz", 
"bed_chr_2.bed.gz.md5", "bed_chr_3.bed.gz", "bed_chr_3.bed.gz.md5", 
"bed_chr_4.bed.gz", "bed_chr_4.bed.gz.md5", "bed_chr_5.bed.gz", 
"bed_chr_5.bed.gz.md5", "bed_chr_6.bed.gz", "bed_chr_6.bed.gz.md5", 
"bed_chr_7.bed.gz", "bed_chr_7.bed.gz.md5", "bed_chr_8.bed.gz", 
"bed_chr_8.bed.gz.md5", "bed_chr_9.bed.gz", "bed_chr_9.bed.gz.md5"
)

文件很大,所以我没有检查整个循环,但这至少对第一个文件有效。将此添加到代码的末尾。

my_url <- 'ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606_b151_GRCh38p7/BED/'
for(my_file in filenames){ # loop over the files
  # download each file, saving in a directory that you need to create on your own computer
  download.file(paste0(my_url, my_file), destfile = file.path('c:/users/josep/Documents/', my_file))
}

【讨论】:

  • 嗨,我担心这一行:filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE) 从未被执行,所以我不能循环通过文件名
  • 我无法复制该错误,因为该步骤对我来说效果很好。如果对您有帮助,我很乐意粘贴文件名列表。
  • 请粘贴文件名。非常感谢!
  • 评论太长了,所以我在回复的开头嵌入了它。也就是说,如果 getURL 函数对您不起作用,那么 download.file 也可能不起作用。
  • 我在服务器上做这个,所以只是为了确认这应该是我的 file.path='t.cri.xxx@something.edu:/sam/mydir'
猜你喜欢
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多