当前位置:  开发笔记 > 编程语言 > 正文

使用package httr从Web API获取数据

如何解决《使用packagehttr从WebAPI获取数据》经验,为你挑选了1个好方法。

我一直在尝试访问用于生物信息目的的在线API.API在他们的网站上列出了使用curl的示例.他们使用的例子是:

$ curl --data "method=smm&sequence_text=SLYNTVATLYCVHQRIDV&allele=HLA-A*01:01&length=9" http://tools-cluster-interface.iedb.org/tools_api/mhci/

使用在线unix终端,我可以得到正确的输出: Unix curl输出

我尝试使用httr设置R脚本:

library(httr)
url="http://tools-cluster-interface.iedb.org/tools_api/mhci/"
results=POST(url,body="method=smm&sequence_text=SLYNTVATLYCVHQRIDV&allele=HLA-A*01:01&length=9")
content(results,"text")

但是,我得到的结果不包含有用的信息

[1] "Available methods:\nann\ncomblib_sidney2008\nconsensus\nnetmhccons\nnetmhcpan\nnetmhcstabpan\npickpocket\nrecommended\nsmm\nsmmpmbec\n\n* Please go to the link below for usage info:\nhttp://tools.iedb.org/main/html/tools_api.html\n"

我只想知道1)如果我的脚本是用curl反映他们的API方法的正确方法吗?2)如何在R中使用他们的api?



1> hrbrmstr..:

curlconverter以前处理curl命令行:

library(curlconverter)
library(httr)

curl_line <- 'curl --data "method=smm&sequence_text=SLYNTVATLYCVHQRIDV&allele=HLA-A*01:01&length=9" http://tools-cluster-interface.iedb.org/tools_api/mhci/'

straighten(curl_line) %>%
  make_req() -> req

req[[1]]
## function () 
## httr::VERB(verb = "POST", url = "http://tools-cluster-interface.iedb.org/tools_api/mhci/", 
##     body = list(method = "smm", sequence_text = "SLYNTVATLYCVHQRIDV", 
##         allele = "HLA-A*01:01", length = "9"))
## 

哪个可以直接使用:

content(req[[1]]())
## [1] "allele\tseq_num\tstart\tend\tlength\tpeptide\tic50\tpercentile\trank\nHLA-A*01:01\t1\t2\t10\t9\tLYNTVATLY\t1286.92\t1.3\nHLA-A*01:01\t1\t4\t12\t9\tNTVATLYCV\t6061.08\t5.5\nHLA-A*01:01\t1\t3\t11\t9\tYNTVATLYC\t27896.52\t21\nHLA-A*01:01\t1\t8\t16\t9\tTLYCVHQRI\t36774.77\t24\nHLA-A*01:01\t1\t5\t13\t9\tTVATLYCVH\t49607.80\t30\nHLA-A*01:01\t1\t10\t18\t9\tYCVHQRIDV\t56565.29\t32\nHLA-A*01:01\t1\t7\t15\t9\tATLYCVHQR\t83090.24\t39\nHLA-A*01:01\t1\t1\t9\t9\tSLYNTVATL\t85418.12\t40\nHLA-A*01:01\t1\t6\t14\t9\tVATLYCVHQ\t282195.45\t68\nHLA-A*01:01\t1\t9\t17\t9\tLYCVHQRID\t491529.97\t79\n"

但可以转换为:

POST(url = "http://tools-cluster-interface.iedb.org/tools_api/mhci/",
     body = list(method = "smm",
                 sequence_text = "SLYNTVATLYCVHQRIDV",
                 allele = "HLA-A*01:01",
                 length = "9")) %>%
  content()
## [1] "allele\tseq_num\tstart\tend\tlength\tpeptide\tic50\tpercentile\trank\nHLA-A*01:01\t1\t2\t10\t9\tLYNTVATLY\t1286.92\t1.3\nHLA-A*01:01\t1\t4\t12\t9\tNTVATLYCV\t6061.08\t5.5\nHLA-A*01:01\t1\t3\t11\t9\tYNTVATLYC\t27896.52\t21\nHLA-A*01:01\t1\t8\t16\t9\tTLYCVHQRI\t36774.77\t24\nHLA-A*01:01\t1\t5\t13\t9\tTVATLYCVH\t49607.80\t30\nHLA-A*01:01\t1\t10\t18\t9\tYCVHQRIDV\t56565.29\t32\nHLA-A*01:01\t1\t7\t15\t9\tATLYCVHQR\t83090.24\t39\nHLA-A*01:01\t1\t1\t9\t9\tSLYNTVATL\t85418.12\t40\nHLA-A*01:01\t1\t6\t14\t9\tVATLYCVHQ\t282195.45\t68\nHLA-A*01:01\t1\t9\t17\t9\tLYCVHQRID\t491529.97\t79\n"

而且,您可以通过以下方式获取数据框:

library(readr)
library(httr)
library(magrittr)

POST(url = "http://tools-cluster-interface.iedb.org/tools_api/mhci/",
     body = list(method = "smm",
                 sequence_text = "SLYNTVATLYCVHQRIDV",
                 allele = "HLA-A*01:01",
                 length = "9")) -> res

content(res, as="text") %>%
  read_delim(delim="\t")
## # A tibble: 10 × 9
##         allele seq_num start   end length   peptide      ic50 percentile
##                                 
## 1  HLA-A*01:01       1     2    10      9 LYNTVATLY   1286.92        1.3
## 2  HLA-A*01:01       1     4    12      9 NTVATLYCV   6061.08        5.5
## 3  HLA-A*01:01       1     3    11      9 YNTVATLYC  27896.52       21.0
## 4  HLA-A*01:01       1     8    16      9 TLYCVHQRI  36774.77       24.0
## 5  HLA-A*01:01       1     5    13      9 TVATLYCVH  49607.80       30.0
## 6  HLA-A*01:01       1    10    18      9 YCVHQRIDV  56565.29       32.0
## 7  HLA-A*01:01       1     7    15      9 ATLYCVHQR  83090.24       39.0
## 8  HLA-A*01:01       1     1     9      9 SLYNTVATL  85418.12       40.0
## 9  HLA-A*01:01       1     6    14      9 VATLYCVHQ 282195.45       68.0
## 10 HLA-A*01:01       1     9    17      9 LYCVHQRID 491529.97       79.0
## # ... with 1 more variables: rank 

推荐阅读
手机用户2502852037
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有