Using Neo4j to visualize medicines’ class and their ingredients
Tags: neo4jgraphdatabase, pharmaceuticalsindus, python, neo4j
Published: 2022-10-24 11:38:04

---
title: Using Neo4j to visualize medicines’ class and their ingredients
published: true
date: 2022-10-22 07:09:44 UTC
tags: neo4jgraphdatabase,pharmaceuticalsindus,python,neo4j
canonical_url: https://towardsdatascience.com/using-neo4j-to-visualize-medicines-class-and-their-ingredients-12598f79d05b
---

![](https://cdn-images-1.medium.com/max/1024/0*vTuc5JhiS-uBfYuy.png)

In my [first post](http://blog.adnansiddiqi.me/visualizing-python-modules-and-dependencies-with-neo4j/) about [Neo4j](http://blog.adnansiddiqi.me/tag/neo4j/), I explored Python modules and their dependencies. Now in this post, I am discussing how you can find the relationship between medicines, their ingredients, and their classification. But why did I choose this dataset in the first place so here is the background:

**_Background:_** _Currently, there is a shortage of Panadol in Pakistan, a very famous brand to cure headaches and bodily pain because the company has stopped producing it as they_ [_don’t find it viable anymore_](https://www.dawn.com/news/1711680)_. Existing Panadol strips which used to cost Rs. 30 are even available for Rs.80 because of the lack of awareness and amazing marketing by the company. Many local websites are suggesting alternatives to Panadol. I am not a Pharma guy but I knew that in my childhood my parents used to buy Calpol and Paracetamol syrup and a few years back I watched the_ [_famous episode_](https://www.youtube.com/watch?v=kBXwyclpr9A) _of Aamir Khan’s program Satyamev Jyate in which he invited a doctor who introduced the term Generic Name of the medicine. The episode went viral not only in India but Pakistan as well. To be honest it was a shocking revelation. The recent Panadol issue recalled everything and I started exploring websites to find data about medicines and their alternatives. There are many websites available but could not find one as friendly as_ [_Drugs.com_](https://www.drugs.com/)_. I had to equip myself with a few terms before exploring the website and finding the relevant information. Basically, I was looking for a certain brand of medicine name and the ingredients/active agent/generic name/ used in it. Once the data was available, I thought it’s a good excuse for my next post related to Neo4j_.

If you want to see the demo then you can view the video below:

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FiY7Jg7T_gvU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiY7Jg7T_gvU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FiY7Jg7T_gvU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c78c83c708f862800677451510a09d8/href">https://medium.com/media/6c78c83c708f862800677451510a09d8/href</a></iframe>
### Development Setup

Unlike the previous post in which I used a Docker-based community edition of Neo4j, I preferred to use Neo4j for Desktop which you can download from [here](https://neo4j.com/download/).

So, as I mentioned above, the goal is to find the relationship between the main ingredient a.k.a _Active Agent,_ and the medicine itself. Bonus point, I also thought to fetch the drug’s class as well. Drugs.com’s website has all such information available. So I wrote a couple of quick scrapers to pull my required info and store it in an SQLite database. I am not gonna discuss the scraping details. I will upload it on Github and you can read the code here.

Assuming all the data available in the DB. The very first step was to pull all the Ingredient names and create their nodes.

![](https://cdn-images-1.medium.com/max/1024/0*X8g7d9axFXQ-84ap.png)

If all goes well and If I want to visualize all Ingredient nodes then they look like the below:

![](https://cdn-images-1.medium.com/max/1024/0*N4tYahDm3vdLdQ16.png)

These are the initial 300 nodes only. If I want to view a single node then it looks like the below:

![](https://cdn-images-1.medium.com/max/1024/0*sWQy9exejaAnYEhT.png)

You can see both the name and ID of the node in the right pane. Similarly the _Drug_ node and the _DrugClass_ Node

![](https://cdn-images-1.medium.com/max/1024/0*8dGUno3nYpy2ldEg.png)

![](https://cdn-images-1.medium.com/max/1024/0*LfTsN2PTDc2X4SX-.png)

Below is the simple routine of creating a node with a label.

![](https://cdn-images-1.medium.com/max/1024/0*IJUHHdfsCFPVenIh.png)

Now all nodes are available, we can now create a relationship between _Ingredient_ and _Drug_ first and then between _Drug_ and _DrugClass_. The code for creating a relationship is given below:

![](https://cdn-images-1.medium.com/max/1024/0*ln9o3xyxT7g-D6Q-.png)

A relationship between an _Ingredient_ and a _Drug_ node looks like the below:

![](https://cdn-images-1.medium.com/max/1024/0*N3eDixXwJfws438G.png)

What If I want to know which _DrugClass_ this _Drug_ belongs to?

![](https://cdn-images-1.medium.com/max/1024/0*jLxVgbe3vS_dEIHj.png)

In simple words, Panadol consists of an Ingredient called _Acetaminophen_ and it belongs to the drug class that deals with _miscellaneous analgesics_.

What if I want to see what other medicines act as a pain killer and belong to _miscellaneous analgesics?_

![](https://cdn-images-1.medium.com/max/1024/0*8PhLJqmDUvxsgG3J.png)

You may see some famous names like _Tylenol_ and _Paracetamol_ here. Cute, isn’t it? If I want, I can use this graph database to find alternative medicine if my desired medicine is not available.

You can also change the color of a node.

![](https://cdn-images-1.medium.com/max/1024/0*-aZ0njP0AisdoNgX.png)

So the color of relationship text.

![](https://cdn-images-1.medium.com/max/902/0*rk-02eDCWAkTzMx0.png)

You can reset to default by running :style reset common in the Neo4j browser. Also, if you want you can load the GraSS content from a url by running :style <URL>.

It is also possible to change the style programmatically. If you run the :style command in the browser pane, it shows the style that Neo4j terms as _Graph Stylesheet (GraSS)_ in a window.

```
node {
  diameter: 50px;
  color: #A5ABB6;
  border-color: #9AA1AC;
  border-width: 2px;
  text-color-internal: #FFFFFF;
  font-size: 10px;
}
relationship {
  color: #A5ABB6;
  shaft-width: 1px;
  font-size: 8px;
  padding: 3px;
  text-color-external: #000000;
  text-color-internal: #FFFFFF;
  caption: "<type>";
}
```

Pretty straightforward if you already know CSS.

As you notice, the label is not entirely visible. How about just increasing the node diameter? I explored the style.grass file, changed the diameter parameter to 60px and drag and drop to the same import pane. I also changed the arrow width.

![](https://cdn-images-1.medium.com/max/1024/0*aRhfbCg5-29PapnJ.png)

The final changed .grass file looks like the below:

```
node {
  diameter: 60px;
  color: #A5ABB6;
  border-color: #9AA1AC;
  border-width: 2px;
  text-color-internal: #FFFFFF;
  font-size: 8px;
}
relationship {
  color: #A5ABB6;
  shaft-width: 2px;
  font-size: 8px;
  padding: 3px;
  text-color-external: #000000;
  text-color-internal: #FFFFFF;
  caption: "<type>";
}
```

### Conclusion

In this post, we explored how Neo4j helps to identify medicines that use certain active agents or discover further medicines in a certain drug class. Graph databases like Neo4j are not all about pharma or the medical field, you can use them for any kind of data that has certain relationships available. Like always, the code is available on [GitHub](https://github.com/kadnan/Neo4jDrugsVisualization).

_If you like such posts then you may_ [_support my content_](http://blog.adnansiddiqi.me/support-this-content/) _and could help to buy a cup of ☕ 🙂_

_Originally published at_ [_http://blog.adnansiddiqi.me_](http://blog.adnansiddiqi.me/using-neo4j-to-visualize-medicines-class-and-their-ingredients/) _on October 22, 2022._

* * *