
Creating an Evolutionary Tree Webapp
This is the journey of how I created my first webapp. It displays an interactive evolutionary tree and runs the nearest neighbor algorithm in the background.
COMMENTARYPHYLOGENY
Sohum Bhardwaj
7/26/20264 min read
Intro
Walks into a middle school classroom
Hello kids! Today we are learning about Computational Phylogeny
Crickets
If you understood that example, you understood the predicament I was in just a couple months ago. Computational phylogeny is super interesting, but it is also 10 syllables. Yet, as a part of my project, Future Computational Biologists (futurecompbio.org), I wanted to introduce middle schoolers to evolutionary trees and computational algorithms. While anyone with common sense would be able to see that this was a frivolous endeavor, I believed otherwise.
The plan was simple:
Create an interactive web app
Admire the faces of awe and wonder as middle schoolers tested the limits of my profanity filter.
In this blog post, I delve into a technical discussion of how my webapp works and explain how I found a happy balance of learning and efficiency when working with AI. (https://evolutionaryexplorer.streamlit.app/, 99% chance it will be offline since Supabase pauses automatically with inactivity)
This blog post
Architecture of the App
Before discussing the web technologies I employed to construct my app, let's talk about what it does in the first place. I know, I gave you an entire intro and table of contents, yet you still don't even know the first thing about what I created.
The users, being middle schoolers, are greeted with a basic home page where they are invited to create a new animal by filling out a form. Now, in hindsight, I could've made this form more descriptive. Putting hydrostatic skeleton as an option was only going to lead to confusion. Aside from the skeleton of their organisms, users could choose their symmetry (bilateral, radial, etc.), their covering (fur, skin, mucus...?), if they had a segmented body, and much much more.
Once they hit enter, their creature popped into existence, found its way onto an evolutionary tree that encompassed every animal submitted by every student. One of my biggest selling points was that anyone could see the animal you created. I mean, it seems obvious now, but when I conceived of the idea, it appeared as a lot more daunting.
In other words, I had a serious paucity for web development experience. A dearth, you might say. Sorry, the SAT vocabulary is still in my system.
Thanks to the presence of so many libraries and technologies that simplify web development, I believe that I was able to skip many stages of typical progression, for better or worse. I used a library called Streamlit to build the web page. It manages creating a python server, establishing a web socket connection, and auto-generating react to render the frontend GUI.
Next, I had to make those animals persistent. If someone left the website, I couldn't have their animals disappear when they reopened it, and I needed their animals to show up on other people's sessions. Supabase made it really simple to set up a centralized database that handled storing user animals. I would write code in Python that interacted with Supabase's API and Supabase would handle hosting the database on the cloud. However, if you are trying to follow my footsteps, be warned that you will have to pick up a bit of SQL as well.
Since the evolutionary tree was the centerpiece of the app, I wanted it to be richly animated. I found a library called Apache-echarts that allowed me to do just that. However, it wasn't as simple as reading the documentation and customizing my graph how I wanted it. Displaying the graph with Streamlit was a challenge that I eventually resolved with a little bit of JavaScript (one line).
If you want to create your own web app, as a biologist or researcher, I'd highly suggest a similar architecture to the one I used. You probably won't need Supabase, but Streamlit and Apache-Echarts make displaying data or graphs really easy. Streamlit allows you to deploy your app on their servers free of charge! Although, I'm pretty sure that this option is only available to apps that don't make revenue. It is pretty astonishing how much abstraction exists in modern day tools, I never had to interact with a postgres database, look act JavaScript code, or interact with HTML/CSS. If you are looking for them, there are plenty of modern tools that make deploying web apps simple as well!


Neighbor-Joining Algorithm
I used Neighbor-Joining algorithm to create my tree. It is fast and simple to implement which was ideal for a simple demonstration for children. It constructs a tree based off of a distance matrix which is its input, and crucially, it does not require the molecular clock hypothesis to be true.
There are many theories on how to judge evolutionary trees like maximum parsimony and maximum likelihood. NJ approximates Balanced Minimum Evolution which looks for the tree with the shortest total branch length. The branch length is determined by a weighted sum of evolutionary distances. While BME is an NP-hard problem, neighbor-joining has a time complexity of O(n^3).
NJ looks for a pair of nodes (animals) to connect together with a common ancestor. In every iteration, it finds the pair that will add the least total length to the tree. This approach, taking the most optimal step in the moment with a disregard for the future, is called greedy, and NJ is a greedy algorithm.
Programming w/ AI
You aren't learning anything if a friend gives you answers. A tutor, however, can greatly accelerate your learning by giving you just enough help at the right times. So the challenge with using AI to help me learn how to make a web app was balancing these two competing interests.
At the start, I barely knew what a server was, and AI was key to helping me understand important terminology. I still like reading documentation from the source, so I ended up getting pretty familiar with the streamlit docs. A more efficient person might ask AI to pull only the most relevant aspects of the documentation. Reading the docs exposed me to other utilities and functions of the library that I would have never found out alone, so I'd say it was worth it.
I also didn't ask AI to generate any code for me. I'm not against AI generated code, but I felt that the value of seeing my decisions designing the app evolve as more code was added was useful.
AI use is a slippery slope, but I think I nailed the balance pretty well for this project.
