簡體   English   中英

使用 R 從 NCBI 基因數據庫中獲取數據

[英]Obtaining data from NCBI gene database with R

倫特雷茲 package

我在 Linux 的實驗室計算機上的 RStudio(版本 1.1.442)中發現rentrez package(根據本手冊20)。 但是,后來當我想在 Windows 8 Pro (RStudio 2021.09.0) 的筆記本電腦上運行相同的代碼時

library (rentrez)
entrez_dbs()
entrez_db_searchable("gene")
#res <- entrez_search (db = "gene", term = "(Vibrio[Organism] OR vibrio[All Fields]) AND (16s[All Fields]) AND (rna[All Fields]) AND (owensii[All Fields] OR navarrensis[All Fields])", retmax = 500, use_history = TRUE)

即使關閉 session 或重新安裝rentrez package,我也無法擺脫此錯誤

curl::curl_fetch_memory(url, handle = handle): schannel: next InitializeSecurityContext failed: SEC_E_ILLEGAL_MESSAGE (0x80090326) - 此錯誤通常發生在收到致命 SSL/TLS 警報時(例如握手失敗)。

這是我面臨的主要問題。

RS硒package

后來我決定以FASTA 格式修改包含有關基因及其序列的詳細信息的頁面,修改我以前使用的代碼。 它使用rvestrselenium包,結果非常完美。

# Specifying a webpage

url <- "https://www.ncbi.nlm.nih.gov/gene/66940694"    # the last 9 numbers is gene id

library(rvest)
library(RSelenium)

# Opening a browser

driver <- rsDriver(browser = c("firefox"))
remDr <- driver[["client"]]
remDr$errorDetails
remDr$navigate(url)

# Clicked outside in an empty space next to the FASTA button and copied a full xPath (redirecting to a FASTA data containing webpage)

remDr$findElement(using = "xpath", value = '/html/body/div[1]/div[1]/form/div[1]/div[5]/div/div[6]/div[2]/div[3]/div/div/div[3]/div/p/a[2]')$clickElement()

webElem <- remDr$findElement("css", "body")

#scrolling to the end of a webpage: left it from the old code for the case of a long gene

for (i in 1:5){
  Sys.sleep(2)
  webElem$sendKeysToElement(list(key = "end"))

# Let's get gene FASTA, for example

page <- read_html(remDr$getPageSource()[[1]])
fasta <- page %>%
html_nodes('pre') %>%
  html_text()
print(fasta)

Output: ">NZ_QKKR01000022.1:c3037-151 副霍亂弧菌菌株 2016V-1111 2016V-1111_ori_contig_18,全基因組鳥槍法序列\nGGT...

該代碼可以很好地獲取有關該基因的其他詳細信息,例如其登錄號、position、生物體等。

循環處理多個基因 ID

后來我嘗試更改代碼,以按照我在這里為我的另一個項目得到的解釋同時獲取多個基因 ID 的相同信息。

# Specifying a list of gene IDs

res_id <- c('57838769','61919208','66940694')
dt <- res_id    # <lapply> looping function refused to work if an argument had a different name rather than <dt>
  
driver <- rsDriver(browser = c("firefox"))  
remDr <- driver[["client"]]

## Writing a function of GET_FASTA dependent on GENE_ID (x)

get_fasta <- function(x){
  link = paste0('https://www.ncbi.nlm.nih.gov/gene/',x)
  remDr$navigate(link)
  remDr$findElement(using = "xpath", value = '/html/body/div[1]/div[1]/form/div[1]/div[5]/div/div[6]/div[2]/div[3]/div/div/div[3]/div/p/a[2]')$clickElement()

...下面有續集但是這里出現了錯誤,說之前成功使用的xPath找不到了。

錯誤:摘要:NoSuchElement 詳細信息:使用給定的搜索參數無法在頁面上找到元素。 class:org.openqa.selenium.NoSuchElementException 更多細節:運行errorDetails方法

我試圖在 xPath 的末尾刪除/a[2]以獲取/html/.../p ,因為它在初始代碼中工作,但后來又出現了錯誤。

  webElem <- remDr$findElement("css", "body")

  for (i in 1:5){
    Sys.sleep(2)
    webElem$sendKeysToElement(list(key = "end"))
  } 

 # Addressing selectors of FASTA on the website
  
  fasta <- remDr$getPageSource()[[1]] %>% 
    read_html() %>%
    html_nodes('pre') %>%
    html_text()
  fasta
  return(fasta)
}

## Writing a function of GET_ACC_NUM dependent on GENE_ID (x)

get_acc_num <- function(x){
  link = paste0( 'https://www.ncbi.nlm.nih.gov/gene/', x)
  remDr$navigate(link)
  remDr$findElement(using = "xpath", value = '/html/body/div[1]/div[1]/form/div[1]/div[5]/div/div[6]/div[2]/div[3]/div/div/div[3]/div/p')$clickElement()
  webElem <- remDr$findElement("css", "body")

  for (i in 1:5){
    Sys.sleep(2)
    webElem$sendKeysToElement(list(key = "end"))
  } 
  
  # Addressing selectors of ACC_NUM on the website
  
  acc_num <- remDr$getPageSource()[[1]] %>% 
    read_html() %>%
    html_nodes('.itemid') %>%
    html_text() %>%
    str_sub(start= -17)
  acc_num
  return(acc_num)
}

## Collecting all FUNCTION into tibble

get_data_table <- function(x){

      # Extract the Basic information from the HTML
      fasta <- get_fasta(x)
      acc_num <- get_acc_num(x)

      # Combine into a tibble
      combined_data <- tibble( Acc_Number = acc_num,
                               FASTA = fasta)
}

## Running FUNCTION for all x

df <- lapply(dt, get_data_table)

head(df)         

我也試着寫代碼

  • 只有rvest ,
  • for (i in res_id) {}編寫循環,
  • 使用if () {} else {}引入兩個以/html/.../p/a[2].../p結尾的不同 xPath

但結果更加令人困惑。

我在處理此類任務時正在學習 R 編碼,因此歡迎提出任何建議和批評。

首先, pre不是一個有效的節點。 您必須在class中尋找價值。

webElem$sendKeysToElement(list(key = "end")你不需要這個命令,因為沒有必要滾動頁面。

下面是獲取基因序列的代碼。

首先,我們必須通過rvest獲得基因序列的鏈接

library(rvest)
library(dplyr)
res_id <- c('57838769','61919208','66940694')

link = vector()
for(i in res_id){
  url = paste0('https://www.ncbi.nlm.nih.gov/gene/', i)
  df = url %>%
    read_html() %>% 
    html_node('.note-link') 
  
  link1 = xml_attrs(xml_child(df, 3))[["href"]]
  link1 = paste0('https://www.ncbi.nlm.nih.gov', link1)
  link = rbind(link, link1)
}

link1 "https://www.ncbi.nlm.nih.gov/nuccore/NZ_ADAF01000001.1?report=fasta&from=257558&to=260444"       
link1 "https://www.ncbi.nlm.nih.gov/nuccore/NZ_VARQ01000103.1?report=fasta&from=64&to=2616&strand=true" 
link1 "https://www.ncbi.nlm.nih.gov/nuccore/NZ_QKKR01000022.1?report=fasta&from=151&to=3037&strand=true"

獲得鏈接后,我們將獲得我們通過RSelenium完成的基因序列。 我試圖用rvest ,但無法得到序列。

啟動瀏覽器

library(RSelenium)
driver = rsDriver(browser = c("firefox"))
remDr <- driver[["client"]]

Function 得到序列

get_seq = function(link){
  remDr$navigate(link)
  Sys.sleep(5)
  df = remDr$getPageSource()[[1]] %>% 
    read_html() %>% 
    html_nodes(xpath = '//*[@id="viewercontent1"]') %>% 
    html_text()
  return(df)
}

df = lapply(link, get_seq)

現在我們有了包含所有信息的列表df

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM