使用package httr从Web API

时间:2017-01-14 02:07:54

标签: r bioinformatics httr

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

$ 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 output

我尝试使用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 个答案:

答案 0 :(得分:6)

我使用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"))
## <environment: 0x7fb1ba2b2d40>

可以直接使用:

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
##          <chr>   <int> <int> <int>  <int>     <chr>     <dbl>      <dbl>
## 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 <chr>