Registering a function in Django

81 Views Asked by At

I have a very simple python script that runs Tanimoto similarity index between a molecule sample and a molecule database:

from openbabel import pybel

def get_similar(targetmol):
    results = []
    # calculate fingerprints of the sample
    targetfp = targetmol.calcfp()
    for mol in pybel.readfile("smi", "/path/to/db.smi"):
        # calculate fingerprints of the db
        fp = mol.calcfp()
        # calculate the Tanimoto index via the "|" operator
        tan = fp | targetfp
        if tan[0] >= 0.8:
            results.append(tan)
    return results

targetmol = next(pybel.readfile("smi", "/path/to/sample.smi"))
print(get_similar(targetmol))

My goal here is to add this function to a website that I am developing with Django.

Ideally, users will submit their own molecule and receive a list of the most similar ones present in my database on a new page, as an on-click function.

What I need to do here is to let the script use the users's molecule as a sample but, most importantly, I do not know how to deploy this function in the Django framework.

Since I know that this is not a code shop, I simply would love to receive a clear explanation on how to register my function, how to call it as an on-click function and what I have to change in order to let the script use the users's molecule instead of sample.smi.

For reference, I took inspiration from this repo https://github.com/michal-stuglik/django-blastplus but due to my scarce-to-zero knowledge of python and Django, I get lost.

Thank you for the time spent in reading and eventually answering me.

System

NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
Django==3.1.13
django-admin==2.0.1
openbabel-wheel==3.1.1.7
1

There are 1 best solutions below

0
On

I am not sure about what you want to achieve. But, if it is to develop a simple website on which users can upload a file describing a molecule sample to run it through your script and retrieve similar ones, then I would propose the following answer.

You should start to create a little view using Django that would display a form with a file input enabling users to upload the molecule's file. Then in your view, you should read the file uploaded by the user, pass it through your function and then return the matches to the user.

You can read more about how to create a view & a form on this page: https://docs.djangoproject.com/en/4.0/topics/forms/

My explanation is only a little overview of what you should do, do not hesitate to ask me more specific questions!