沉浸式阅读Nature | 高分文章的引言还差什么?

2022-12-09  本文已影响0人  生命数据科学

前言:在GEO数据库挖掘过程中,常常不止需要下载单个文件,手动下载很费时间,整理起来也麻烦,因此迫切需要批量自动下载脚本。

这两个脚本与其他常规GEO文件下载脚本有什么不同呢,熟悉GEO数据库下载的都比较清楚,在下载多个、大量的GEO文件时,常常会因为网络波动导致下载失败。以下脚本通过识别报错信息,自动重新下载报错文件,省时省力,边喝咖啡边干活~

准备工作

  1. 一列GSE号的文件GSE.txt

  2. 一列GPL号的文件GPL.txt

  3. 里面有些工作目录需要修改,不然下载起来会比较乱

一、GSE批量下载

options('download.file.method.GEOquery'='auto')
options('GEOquery.inmemory.gpl'=FALSE)
options( 'download.file.method.GEOquery' = 'libcurl' )
options(timeout=600)

# if (!requireNamespace("BiocManager", quietly = TRUE))
#   install.packages("BiocManager")
# 
# BiocManager::install("GEOquery")
# install.package("openxlsx")
# library(openxlsx)
library(GEOquery)

#读取数据
setwd("your_working_directory") #需修改
rt=read.table("GSE.txt",header = F) #GSE号汇总的文件,第一列是GSE号就行
#rt=openxlsx::read.xlsx("GSEid.xlsx",1,header=F)# 
rt1=as.character(rt[,1])
rt1<-unique(rt1)
#新建函数withWarnings(识别错误信息)
withWarnings <- function(expr) {
  myWarnings <- NULL
  wHandler <- function(w) {
    myWarnings <<- c(myWarnings, list(w))
    invokeRestart("muffleWarning")
  }
  val <- withCallingHandlers(expr, warning = wHandler)
  list(value = val, warnings = myWarnings)
} 

#批量下载
a<-list()
for (i in 1:length(rt1))
  {
  a[[i]]<-withWarnings(getGEO(rt1[[i]],#系列编号
         destdir = '.', #, #下载目录,“.”代表当前目录
         getGPL = F))

  while (is.list(a[[i]][["warnings"]])) {
    a[[i]]<-withWarnings(getGEO(rt1[[i]],#如果出现warning,则重复下载
                                destdir = '.', AnnotGPL = F,getGPL = F
                                  ))
  }
}

二、GPL批量下载

library(GEOquery)
library(stringr)
# if (!requireNamespace("BiocManager", quietly = TRUE))
#   install.packages("BiocManager")
# 
# BiocManager::install("GEOquery")

#读取数据
setwd("D:\\") #需修改
rt=read.table("GPL.txt",header = F)
#rt=openxlsx::read.xlsx("GSEid.xlsx",1)
rt1=as.character(rt[,1])
rt1<-unique(rt1)

finished<-list.files(pattern = "GPL[0-9]*",full.names = F)
finished_name<-str_extract(finished,"GPL[0-9]*")
last_file<-setdiff(rt1,finished_name)#识别工作目录中是否存在需要下载的文件,去重
for (i in last_file) {
  a<-getGEO(i,#系列编号
            destdir = '.', #, #下载目录
            getGPL = T, AnnotGPL = T)
  output<-a@dataTable@table
  write.table(output,paste("D:\\plat\\",i,".txt",sep=""),sep = "\t",row.names = F,col.names = T)#下载到plat文件夹中
}` 
上一篇 下一篇

猜你喜欢

热点阅读