# 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:** 1\
**Showing post:** 2

<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.

---

_[View the full topic](https://community.opentargets.org/t/an-r-script-to-use-api-for-fetching-associated-studies-locus-to-gene-pipeline-section-for-a-gene/1135)._
