# Variant to Gene Query for multiple variants in OT Genetics

**URL:** <https://community.opentargets.org/t/variant-to-gene-query-for-multiple-variants-in-ot-genetics/456>\
**Category:** GraphQL API\
**Tags:** genetics-portal\
**Created:** [15 February 2022 14:52 UTC](https://community.opentargets.org/t/variant-to-gene-query-for-multiple-variants-in-ot-genetics/456 "2022-02-15T14:52:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjordab](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/pjordab/32/160_2.png) [@pjordab](https://community.opentargets.org/u/pjordab)\
**Post date:** [15 February 2022 14:52 UTC](https://community.opentargets.org/t/variant-to-gene-query-for-multiple-variants-in-ot-genetics/456/1 "2022-02-15T14:52:53Z")

</div>

Dear Open Target team,

First of all, thank you very much for your valuable work.

I need to perform a Variant to Gene search for multiple variants (\>200).

I am trying to find a way to do it through a script that generates a table with the results containing the same parameters as when I search the rsid individually through the browser but I’ve been not able to find what I am looking for in the documentation.

Your guidance would be very appreciated.

Thank you very much!

Paloma

---

<div class="post-metadata">

**Author:** ![JeremyS](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/jeremys/32/76_2.png) [@JeremyS](https://community.opentargets.org/u/JeremyS)\
**Post date:** [1 March 2022 10:39 UTC](https://community.opentargets.org/t/variant-to-gene-query-for-multiple-variants-in-ot-genetics/456/2 "2022-03-01T10:39:18Z")

</div>

Thanks for your question Paloma. For a moderate number of variants, you could use the GraphQL API, which can be access via a browser, or programmatically.

For example, a query in the browser might look something like this:  
[GraphQL variant to gene query](https://api.genetics.opentargets.org/graphql/browser?operationName=v2g&query=query%20v2g(%24variantId%3A%20String!)%20%7B%0A%20%20genesForVariant(variantId%3A%20%24variantId)%20%7B%0A%20%20%20%20gene%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%7D%0A%20%20%20%20variant%0A%20%20%20%20overallScore%0A%20%20%20%20distances%20%7B%0A%20%20%20%20%20%20sourceId%0A%20%20%20%20%20%20aggregatedScore%0A%20%20%20%20%20%20tissues%20%7B%0A%20%20%20%20%20%20%20%20tissue%20%7B%0A%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%09distance%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20qtls%20%7B%0A%20%20%20%20%20%20typeId%0A%20%20%20%20%20%20aggregatedScore%0A%20%20%20%20%20%20tissues%20%7B%0A%20%20%20%20%20%20%20%20tissue%20%7B%0A%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20quantile%0A%20%20%20%20%20%20%20%20beta%0A%20%20%20%20%20%20%20%20pval%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20functionalPredictions%20%7B%0A%20%20%20%20%20%20typeId%0A%20%20%20%20%20%20aggregatedScore%0A%20%20%20%20%20%20tissues%20%7B%0A%20%20%20%20%20%20%20%20tissue%20%7B%0A%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20maxEffectLabel%0A%20%20%20%20%20%20%20%20maxEffectScore%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D&variables=%7B%0A%20%20%22variantId%22%3A%20%2217_44352876_C_T%22%0A%7D).

You can use a language of your choice to do the query programmatically, e.g. python or R. Here is an example in R:

```auto
# Install relevant library for HTTP requests
library(httr)

# Set gene_id variable
variantId <- "17_44352876_C_T"

# Build query string
query_string = "
query v2g($variantId: String!) {
  genesForVariant(variantId: $variantId) {
    gene {
      id
    }
    variant
    overallScore
    distances {
      sourceId
      aggregatedScore
      tissues {
      	distance
      }
    }
  }
}"

# Set base URL of GraphQL API endpoint
base_url <- "https://api.genetics.opentargets.org/graphql"

# Set variables object of arguments to be passed to endpoint
variables <- list("variantId" = variantId)

# Construct POST request body object with query string and variables
post_body <- list(query = query_string, variables = variables)

# Perform POST request
r <- POST(url=base_url, body=post_body, encode='json')

df = content(r)
# Print first entry of V2G data console
head(content(r)$data$genesForVariant, 1)

# Flatten the nested result fields into a dataframe
library(rlist)
list_result = content(r)$data$genesForVariant
x = lapply(list_result, list.flatten)

library(dplyr)
df = bind_rows(x)

```

Note that if you want to get the information for individual QTL associations, you would need to do a bit more to flatten the resulting nested lists.

---

<div class="post-metadata">

**Author:** ![pjordab](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/pjordab/32/160_2.png) [@pjordab](https://community.opentargets.org/u/pjordab)\
**Post date:** [10 March 2022 22:24 UTC](https://community.opentargets.org/t/variant-to-gene-query-for-multiple-variants-in-ot-genetics/456/3 "2022-03-10T22:24:14Z")

</div>

Thank you very much for your detailed and very helpful response Jeremy! It worked!

---

<div class="post-metadata">

**Author:** ![llg](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@llg](https://community.opentargets.org/u/llg)\
**Post date:** [19 August 2024 14:35 UTC](https://community.opentargets.org/t/variant-to-gene-query-for-multiple-variants-in-ot-genetics/456/4 "2024-08-19T14:35:27Z")

</div>

Hi JeremyS, thank you for your valuable information!

How should I change “query\_string” to get the data shown as the following?

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

I tried with the full text shown in your example, however, I got a lot of qtls, I did not see anything such as PCHi-C, DHS-promoter corr, VEP… Maybe I missed it.

I tried to find the information on the data structure and failed. Maybe I missed it too.

---

<div class="post-metadata">

**Author:** ![Xiangyu](https://avatars.discourse-cdn.com/v4/letter/x/90db22/32.png) [@Xiangyu](https://community.opentargets.org/u/Xiangyu)\
**Post date:** [20 August 2024 09:09 UTC](https://community.opentargets.org/t/variant-to-gene-query-for-multiple-variants-in-ot-genetics/456/5 "2024-08-20T09:09:32Z")

</div>

Hi llg, in the v2g\_scored file, you will find this information, each row represents an evidence source for a given variant to gene pair (as indicated by the type\_id and source\_id columns), so if you were particularly interested in pchic evidence sources, you can filter the v2g\_scored file based on the type\_id column.

Hope this helps,  
Xiangyu

```auto
root
 |-- chr_id: string (nullable = true)
 |-- position: long (nullable = true)
 |-- ref_allele: string (nullable = true)
 |-- alt_allele: string (nullable = true)
 |-- gene_id: string (nullable = true)
 |-- feature: string (nullable = true)
 |-- type_id: string (nullable = true)
 |-- source_id: string (nullable = true)
 |-- fpred_labels: array (nullable = true)
 | |-- element: string (containsNull = true)
 |-- fpred_scores: array (nullable = true)
 | |-- element: double (containsNull = true)
 |-- fpred_max_label: string (nullable = true)
 |-- fpred_max_score: double (nullable = true)
 |-- qtl_beta: double (nullable = true)
 |-- qtl_se: double (nullable = true)
 |-- qtl_pval: double (nullable = true)
 |-- qtl_score: double (nullable = true)
 |-- interval_score: double (nullable = true)
 |-- qtl_score_q: double (nullable = true)
 |-- interval_score_q: double (nullable = true)
 |-- d: long (nullable = true)
 |-- distance_score: double (nullable = true)
 |-- distance_score_q: double (nullable = true)
 |-- overall_score: double (nullable = true)
 |-- source_list: array (nullable = true)
 | |-- element: string (containsNull = true)
 |-- source_score_list: array (nullable = true)
 | |-- element: double (containsNull = true)

```
