# An R script to use API for fetching "Associated studies: locus-to-gene pipeline" section for a gene

**URL:** <https://community.opentargets.org/t/an-r-script-to-use-api-for-fetching-associated-studies-locus-to-gene-pipeline-section-for-a-gene/1135>\
**Category:** GraphQL API\
**Created:** [5 July 2023 09:51 UTC](https://community.opentargets.org/t/an-r-script-to-use-api-for-fetching-associated-studies-locus-to-gene-pipeline-section-for-a-gene/1135 "2023-07-05T09:51:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yazdan59](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/yazdan59/32/442_2.png) [@yazdan59](https://community.opentargets.org/u/yazdan59)\
**Post date:** [5 July 2023 09:51 UTC](https://community.opentargets.org/t/an-r-script-to-use-api-for-fetching-associated-studies-locus-to-gene-pipeline-section-for-a-gene/1135/1 "2023-07-05T09:51:54Z")

</div>

Hello, I want to write a query in R that fetch all information regarding the “ **Associated studies: locus-to-gene pipeline** ” section (especially **L2G pipeline** scores) based on an “ **Ensemble ID** ” of a gene as input. Could you please help me to know how should I have such a query in the R script?

---

<div class="post-metadata">

**Author:** ![dsuveges](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/dsuveges/32/30_2.png) [@dsuveges](https://community.opentargets.org/u/dsuveges)\
**Post date:** [12 July 2023 10:59 UTC](https://community.opentargets.org/t/an-r-script-to-use-api-for-fetching-associated-studies-locus-to-gene-pipeline-section-for-a-gene/1135/2 "2023-07-12T10:59:54Z")

</div>

Hi Yazdan, Welcome to the OpenTargets community! The table on the gene [page](https://genetics.opentargets.org/gene/ENSG00000197959) with the l2g scores are provided by the GraphQL API, via a single request. This request is easy to script in R to automate data retrieval (for examples, please see [this](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510) or other threads on the community).

The API request to get L2G predictions for genes looks like this:

```graphql
query GenePageL2GPipelineQuery($geneId: String!) {
  studiesAndLeadVariantsForGeneByL2G(geneId: $geneId) {
    pval
    yProbaModel
    study {
      studyId
      traitReported
      pubAuthor
      pubDate
      pmid
      nInitial
      nReplication
      hasSumstats
      nCases
    }
    variant {
      rsId
      id
    }
    odds {
      oddsCI
      oddsCILower
      oddsCIUpper
    }
    beta {
      betaCI
      betaCILower
      betaCIUpper
      direction
    }
  }
}

```

You can play around and optimize the query [here](https://api.genetics.opentargets.org/graphql/browser?query=query%20GenePageL2GPipelineQuery(%24geneId%3A%20String!)%20%7B%0A%20%20studiesAndLeadVariantsForGeneByL2G(geneId%3A%20%24geneId)%20%7B%0A%20%20%20%20pval%0A%20%20%20%20yProbaModel%0A%20%20%20%20study%20%7B%0A%20%20%20%20%20%20studyId%0A%20%20%20%20%20%20traitReported%0A%20%20%20%20%20%20pubAuthor%0A%20%20%20%20%20%20pubDate%0A%20%20%20%20%20%20pmid%0A%20%20%20%20%20%20nInitial%0A%20%20%20%20%20%20nReplication%0A%20%20%20%20%20%20hasSumstats%0A%20%20%20%20%20%20nCases%0A%20%20%20%20%7D%0A%20%20%20%20variant%20%7B%0A%20%20%20%20%20%20rsId%0A%20%20%20%20%20%20id%0A%20%20%20%20%7D%0A%20%20%20%20odds%20%7B%0A%20%20%20%20%20%20oddsCI%0A%20%20%20%20%20%20oddsCILower%0A%20%20%20%20%20%20oddsCIUpper%0A%20%20%20%20%7D%0A%20%20%20%20beta%20%7B%0A%20%20%20%20%20%20betaCI%0A%20%20%20%20%20%20betaCILower%0A%20%20%20%20%20%20betaCIUpper%0A%20%20%20%20%20%20direction%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=GenePageL2GPipelineQuery&variables=%7B%0A%20%20%22geneId%22%3A%20%22ENSG00000197959%22%0A%7D). Please let us know if you have further questions.

---

<div class="post-metadata">

**Author:** ![Shicheng\_Guo](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/shicheng_guo/32/217_2.png) [@Shicheng\_Guo](https://community.opentargets.org/u/Shicheng_Guo)\
**Post date:** [11 August 2023 06:14 UTC](https://community.opentargets.org/t/an-r-script-to-use-api-for-fetching-associated-studies-locus-to-gene-pipeline-section-for-a-gene/1135/3 "2023-08-11T06:14:17Z")

</div>

Dear Team,

I am trying to repeat the R graphql-api code, however, I received the following error:

Response [[https://api.platform.opentargets.org/api/v4/graphql](https://api.platform.opentargets.org/api/v4/graphql)]  
Date: 2023-08-11 06:05  
Status: 400  
Content-Type: application/json  
Size: 167 B

Could you please give some comments? Thanks.

> **[GraphQL API](https://platform-docs.opentargets.org/data-access/graphql-api#sample-scripts)**

 ![1691734444476](https://europe1.discourse-cdn.com/flex017/uploads/opentargets/original/1X/8decec6d07b8eeee45e82e97911ab65862cf205c.png)

---

<div class="post-metadata">

**Author:** ![irene](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/irene/32/50_2.png) [@irene](https://community.opentargets.org/u/irene)\
**Post date:** [11 August 2023 08:46 UTC](https://community.opentargets.org/t/an-r-script-to-use-api-for-fetching-associated-studies-locus-to-gene-pipeline-section-for-a-gene/1135/4 "2023-08-11T08:46:00Z")

</div>

This has been solved here [Sample R script to query GraphQL API - #5 by irene](https://community.opentargets.org/t/sample-r-script-to-query-graphql-api/510/5)
