Tom*_*ers 1 ruby regex r web-scraping pubchem
许多化学家面临的一个令人讨厌的问题是将化学化合物的CAS登记号(存储在一些不易获取的商业数据库中)转换为Pubchem标识符(公开可用).Pubchem类支持两者之间的转换,但只能通过他们的手动Web界面,而不是他们的官方PUG REST编程接口.
这里给出了Ruby的解决方案,基于电子实用程序界面:http://depth-first.com/articles/2007/09/13/hacking-pubchem-convert-cas-numbers-into-pubchem-cids-与-红宝石/
有谁知道这将如何转化为R?
编辑:根据答案,最优雅的解决方案是:
library(XML)
library(RCurl)
CAStocids=function(query) {
xmlresponse = xmlParse( getURL(paste("http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&retmax=100&term=",query,sep="") ) )
cids = sapply(xpathSApply(xmlresponse, "//Id"), function(n){xmlValue(n)})
return(cids)
}
> CAStocids("64318-79-2")
[1] "6434870" "5282237"
Run Code Online (Sandbox Code Playgroud)
汤姆,欢呼声
这个Ruby代码如何实现它,转换为R,使用RCurl和XML:
> xmlresponse = xmlParse( getURL("http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&retmax=100&term=64318-79-2") )
Run Code Online (Sandbox Code Playgroud)
以下是如何提取Id节点:
> sapply(xpathSApply(xmlresponse, "//Id"), function(n){xmlValue(n)})
[1] "6434870" "5282237"
Run Code Online (Sandbox Code Playgroud)
将所有内容包装在函数中....
convertU = function(query){
xmlresponse = xmlParse(getURL(
paste0("http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&retmax=100&term=",query)))
sapply(xpathSApply(xmlresponse, "//Id"), function(n){xmlValue(n)})
}
> convertU("64318-79-2")
[1] "6434870" "5282237"
> convertU("64318-79-1")
list()
> convertU("64318-78-2")
list()
> convertU("64313-78-2")
[1] "313"
Run Code Online (Sandbox Code Playgroud)
如果找不到可能需要测试.
| 归档时间: |
|
| 查看次数: |
1394 次 |
| 最近记录: |