# Can we get all Bibliography data per gene using the api?

**URL:** <https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137>\
**Category:** GraphQL API\
**Created:** [6 July 2023 07:41 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137 "2023-07-06T07:41:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjeynes](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@cjeynes](https://community.opentargets.org/u/cjeynes)\
**Post date:** [6 July 2023 07:41 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137/1 "2023-07-06T07:41:50Z")

</div>

when a gene is searched most tabs have this option for the api  
 ![image](https://europe1.discourse-cdn.com/flex017/uploads/opentargets/original/1X/8a66b29a8966a5c51a0867a6313b5ea7d8951c3a.png)  
BUt for the bibliography it’s not there …

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/opentargets/original/1X/eedb4f966d2ac9f4982894390915253a346d5852.png)  
it would be really handy to be able to get this data through the api  
PS i do realise you can download all the bibliography data through FTP, but there is quite a lot of it !!  
thanks

Charlie

---

<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:** [14 July 2023 10:08 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137/2 "2023-07-14T10:08:21Z")

</div>

Hi Charlie,

Yes, indeed, the Bibliography widget is one of the exceptions where these functionalities are not available. The above table is generated by the following API query:

```graphql
query SimilarEntitiesQuery(
  $id: String!
  $ids: [String!] = []
  $startYear: Int = null
  $startMonth: Int = null
  $endYear: Int = null
  $endMonth: Int = null
  $threshold: Float = 0.5
  $size: Int! = 15
  $entityNames: [String!] = []
  $cursor: String = null
) {
  target(ensemblId: $id) {
    id
    approvedName
    similarEntities(
      additionalIds: $ids
      threshold: $threshold
      size: $size
      entityNames: $entityNames
    ) {
      id
      score
      object {
        ... on Target {
          id
          approvedSymbol
        }
        ... on Drug {
          id
          name
        }
        ... on Disease {
          id
          name
        }
      }
    }
    literatureOcurrences(
      additionalIds: $ids
      cursor: $cursor
      startYear: $startYear
      startMonth: $startMonth
      endYear: $endYear
      endMonth: $endMonth
    ) {
      count
      earliestPubYear
      cursor
      rows {
        pmid
        pmcid
        publicationDate
      }
    }
  }
}

```

With the following parameters:

```json
{
  "ids": [],
  "startYear": null,
  "startMonth": null,
  "endYear": null,
  "endMonth": null,
  "threshold": 0.5,
  "size": 15,
  "entityNames": [
    "disease",
    "drug",
    "target"
  ],
  "cursor": null,
  "id": "ENSG00000144029"
}

```

You can see from the query and the returned data, that the the bare minimum is returned, we then use [EuroPMC’s API](https://europepmc.org/RestfulWebService#!/Europe32PMC32Articles32RESTful32API/searchPOST) to retrieve title and all publication related metadata. However, if you explore our API further, you can see there are options to extract matches.

In the meantime, I have opened a [ticket](https://github.com/opentargets/issues/issues/3021) to explore the possibilities to get this feature included.

---

<div class="post-metadata">

**Author:** ![cjeynes](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@cjeynes](https://community.opentargets.org/u/cjeynes)\
**Post date:** [19 July 2023 13:52 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137/3 "2023-07-19T13:52:42Z")

</div>

HI thanks very much for this - I’m being this dim but running your code through python like this : but getting the error  
400  
{‘data’: None, ‘errors’: [{‘message’: 'Variable '$startYear' expected value of type 'Int' but got: “null”

```auto
variables = {
  "ids": [],
  "startYear": "null",
  "startMonth": "null",
  "endYear": "null",
  "endMonth": "null",
  "threshold": 0.5,
  "size": 15,
  "entityNames": [
    "disease",
    "drug",
    "target"
  ],
  "cursor": "null",
  "id": "ENSG00000144029"
}

query_string = """
query SimilarEntitiesQuery(
  $id: String!
  $ids: [String!] = []
  $startYear: Int = null
  $startMonth: Int = null
  $endYear: Int = null
  $endMonth: Int = null
  $threshold: Float = 0.5
  $size: Int! = 15
  $entityNames: [String!] = []
  $cursor: String = null
) {
  target(ensemblId: $id) {
    id
    approvedName
    similarEntities(
      additionalIds: $ids
      threshold: $threshold
      size: $size
      entityNames: $entityNames
    ) {
      id
      score
      object {
        ... on Target {
          id
          approvedSymbol
        }
        ... on Drug {
          id
          name
        }
        ... on Disease {
          id
          name
        }
      }
    }
    literatureOcurrences(
      additionalIds: $ids
      cursor: $cursor
      startYear: $startYear
      startMonth: $startMonth
      endYear: $endYear
      endMonth: $endMonth
    ) {
      count
      earliestPubYear
      cursor
      rows {
        pmid
        pmcid
        publicationDate
      }
    }
  }
}
"""

base_url = "https://api.platform.opentargets.org/api/v4/graphql"

# Perform POST request and check status code of response
r = requests.post(base_url, json={"query": query_string, "variables": variables})
print(r.status_code)

# Transform API response from JSON into Python dictionary and print in console
api_response = json.loads(r.text)
print(api_response)

```

---

<div class="post-metadata">

**Author:** ![cjeynes](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@cjeynes](https://community.opentargets.org/u/cjeynes)\
**Post date:** [19 July 2023 14:04 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137/4 "2023-07-19T14:04:12Z")

</div>

OK i’m solving myself but leaving if others were also stumped:

use None in python not null and it works

---

<div class="post-metadata">

**Author:** ![cjeynes](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@cjeynes](https://community.opentargets.org/u/cjeynes)\
**Post date:** [19 July 2023 14:07 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137/5 "2023-07-19T14:07:45Z")

</div>

One thing i’m a bit confused about this coocurrences FTP download

[http://ftp.ebi.ac.uk/pub/databases/opentargets/platform/21.04/output/literature/cooccurrences/](http://ftp.ebi.ac.uk/pub/databases/opentargets/platform/21.04/output/literature/cooccurrences/)

where it seems open targets has done all this work themselves, but then you are talking about accessing data from EuroPMCs api - so how exactly is the FTP site used in open targets?

---

<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:** [19 July 2023 14:22 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137/6 "2023-07-19T14:22:24Z")

</div>

Hi,

I am happy to hear the issue with the query is sorted. Regarding how the literature data ingested to our website and how we are further process that dataset, please take a look at this blog post:

> **[How we overhauled our literature processing pipeline using Named Entity...](https://blog.opentargets.org/developing-the-open-targets-literature-pipeline/)**
>
> The Open Targets Platform uses Natural Language Processing and Named Entity Recognition with a Word2Vec machine learning model to systematically query the knowledge contained in scientific literature. This allows users to find similar entities using...

Best,  
Daniel

---

<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:** [16 July 2024 16:40 UTC](https://community.opentargets.org/t/can-we-get-all-bibliography-data-per-gene-using-the-api/1137/7 "2024-07-16T16:40:03Z")

</div>

Here is the R solution:

```auto
library(httr)
library(jsonlite)

# Define the query string
query_string <- '
query SimilarEntitiesQuery(
  $id: String!
  $ids: [String!] = []
  $startYear: Int = null
  $startMonth: Int = null
  $endYear: Int = null
  $endMonth: Int = null
  $threshold: Float = 0.5
  $size: Int! = 15
  $entityNames: [String!] = []
  $cursor: String = null
) {
  target(ensemblId: $id) {
    id
    approvedName
    similarEntities(
      additionalIds: $ids
      threshold: $threshold
      size: $size
      entityNames: $entityNames
    ) {
      id
      score
      object {
        ... on Target {
          id
          approvedSymbol
        }
        ... on Drug {
          id
          name
        }
        ... on Disease {
          id
          name
        }
      }
    }
    literatureOcurrences(
      additionalIds: $ids
      cursor: $cursor
      startYear: $startYear
      startMonth: $startMonth
      endYear: $endYear
      endMonth: $endMonth
    ) {
      count
      earliestPubYear
      cursor
      rows {
        pmid
        pmcid
        publicationDate
      }
    }
  }
}
'

# Define the variables
variables <- list(
  id = "ENSG00000157764", # Example Ensembl ID
  size = 15,
  threshold = 0.5
)

# Define the URL for the API endpoint
base_url <- "https://api.platform.opentargets.org/api/v4/graphql"

# Perform the POST request
response <- POST(
  url = base_url,
  body = list(query = query_string, variables = variables),
  encode = "json"
)

# Check the status code of the response
if (status_code(response) == 200) {
  # Parse the response from JSON
  api_response <- content(response, as = "parsed", type = "application/json")
  
  # Print the response in a readable format
  print(toJSON(api_response, pretty = TRUE))
} else {
  # Print the error message if the request failed
  print(paste("Error:", status_code(response)))
}

```
