当我们想获得一个gse的matrix文件和补充文件,
一般情况下可以直接用网页下载,
用 R
的话也可以使用 getGEO(gse)
和 getGEOSuppFiles(gse)函数
,
但是如果在服务器或者网络非常不好的情况下,
就必须依赖可以断点续传而又网速稳定的ftp链接,
那么如何方便的获得这些链接呢?
当然是上代码和做网页啦~
网页地址:
getgeofilelinks.yeyeziblog.eu.org
当输入GSE号, 可以有两种选择, 第一是获得matrix文件地址, 第二是补充文件地址, 而如果使用GPL文件, 可以获取注释文件地址.
首先, 使用过 GEOquery
包的话, 大家一定都看见过在下载之前有一个一闪而过的链接, 这个链接就是之前说的"可以断点续传而又网速稳定的ftp链接"了, 但是我们当然不能每一次需要这个链接就使用一次 getGEO(gse)
, 那样反而是本末倒置了.
r$> getGEO("gse17536")
Found 1 file(s)
GSE17536_series_matrix.txt.gz
trying URL 'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE17nnn/GSE17536/matrix/GSE17536_series_matrix.txt.gz'
Content type 'application/x-gzip' length 52242943 bytes (49.8 MB)
事实上这个链接可以在浏览器打开, 会直接下载一个压缩文件, 如果在链接中去掉文件名, 可以看到这个储存点的庐山真面目:
基于此, 有大佬设计了基于文本替换和网页元素爬取的ftp链接获取代码, 我又稍加修改, 加入了GPL的注释信息链接获取. 在此再次特别感谢🙏Horan Chen, 在生信的学习路上教了我许多许多东西, 无疑是一位技术高超又乐于助人的Linus Torvalds式人物, 这里是他的公众号, 生命数据科学
, 希望大家多多关注, 一起学知识.
# functiion for getting filelinks
getFileList <- function(gseAcc, typeDown = "matrix") { # gseAcc <- "GSE166424"
# get file directional content
getDirListing <- function(url) {
# Takes a URL and returns a character vector of filenames
a <- xml2::read_html(url)
fnames <- grep("^G", xml_text(xml_find_all(a, "//a/@href")), value = TRUE)
return(fnames)
}
# get a type of file
if ("matrix" %in% typeDown) {
gdsurl <- "https://ftp.ncbi.nlm.nih.gov/geo/series/%s/%s/matrix/"
} else if ("suppl" %in% typeDown) {
gdsurl <- "https://ftp.ncbi.nlm.nih.gov/geo/series/%s/%s/suppl/"
} else if ("annot" %in% typeDown) {
gdsurl <- "https://ftp.ncbi.nlm.nih.gov/geo/platforms/%s/%s/annot/"
}
# merge links in list
stub <- gsub("\\d{1,3}$", "nnn", gseAcc, perl = TRUE)
b <- getDirListing(sprintf(gdsurl, stub, gseAcc))
ret <- list()
for (x in 1:length(b)) {
ret[[x]] <- sprintf(
paste0(gdsurl, "%s"),
stub, gseAcc, b[x]
)
}
merge <- c(unlist(ret))
print(merge)
return(merge)
}
# 获取GSE166424的补充文件和matrix文件下载链接
getFileList("GSE166424", typeDown = "suppl")
getFileList("GSE166424", typeDown = "matrix")
getFileList("GPL570", typeDown = "annot")
然后, ChatGPT的帮助下我们获得了一个shinyAPP代码:
library(shiny)
library(xml2)
library(DT)
library(shinyjs)
# Define UI
ui <- fluidPage(
useShinyjs(),
titlePanel("GEO File Downloader"),
sidebarLayout(
sidebarPanel(
textInput("geoID", "Enter a GEO accession number:"),
uiOutput("fileType"),
actionButton("submitBtn", "Submit")
),
mainPanel(
DTOutput("fileList")
)
)
)
# Define server
server <- function(input, output) {
# Define function to get directory listing
getDirListing <- function(url) {
a <- read_html(url)
fnames <- grep("^G", xml_text(xml_find_all(a, "//a/@href")), value = TRUE)
return(fnames)
}
# Determine file type options based on input type
output$fileType <- renderUI({
if (grepl("^GSE", input$geoID)) {
selectInput("fileOption", "Choose file type:", c("matrix", "suppl"))
} else if (grepl("^GPL", input$geoID)) {
selectInput("fileOption", "Choose file type:", "annot")
}
})
# Get list of files and display in table when submit button is clicked
output$fileList <- renderDT(
{
req(input$submitBtn)
if (input$fileOption == "matrix") {
gdsurl <- "https://ftp.ncbi.nlm.nih.gov/geo/series/%s/%s/matrix/"
} else if (input$fileOption == "suppl") {
gdsurl <- "https://ftp.ncbi.nlm.nih.gov/geo/series/%s/%s/suppl/"
} else if (input$fileOption == "annot") {
gdsurl <- "https://ftp.ncbi.nlm.nih.gov/geo/platforms/%s/%s/annot/"
}
stub <- gsub("\\d{1,3}$", "nnn", input$geoID, perl = TRUE)
b <- getDirListing(sprintf(gdsurl, stub, input$geoID))
ret <- list()
for (x in 1:length(b)) {
ret[[x]] <- sprintf(
paste0(gdsurl, "%s"),
stub, input$geoID, b[x]
)
}
merge <- c(unlist(ret))
output <- data.frame("File Name" = merge)
output$file_link <- sprintf("<button class='btn btn-primary' onclick='copyToClipboard(\"%s\")'>Copy Link</button>", output$File.Name)
output <- cbind(output$file_link, output)
output <- output[, c(2, 1)]
output
},
escape = FALSE,
rownames = FALSE,
options = list(
dom = "Bfrtip",
buttons = list("copy")
)
)
}
# Run the application
shinyApp(ui = ui, server = server)
library(GEOquery)
getGEO("gse17536")
首先, 创建一个空仓库, 之后到本地文件夹内:
git init
git remote add origin git@github.com:username/reponame
git branch -M main
然后就可以用脚本自动化
# #!/bin/zsh
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
echo $SHELL_FOLDER
cd $SHELL_FOLDER
git add .
commitTime=`date +"%Y-%m-%d %H-%M-%S"`
git commit -a -m "${commitTime}"
git push origin main
位置于 https://github.com/sandy9707/getGEOFileLinks.git
一开始选择使用Heroku进行部署, 但失败了, 所以使用服务器部署.
使用 git clone https://github.com/sandy9707/getGEOFileLinks.git
下载文件, 在服务器上使用 Rscript shinyAPP.R
就可以了, 之后是利用宝塔面板做反向代理, 不属于本篇重点所以不再赘述.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。