as from the title, I am trying to implement an external python code to a Django web server.
I am quite new to programming so any hints will be surely helpful.
Long story short: I am trying to set up a form where the user have to insert an aminoacidic sequence. This sequence should pass to my python script, that is able to compare it with all the sequences already present in the database and gives as a result the most similar ones. My problem is that I am not able to let my form and my script talk each others.
I have followed the Django documentation here https://docs.djangoproject.com/en/3.2/topics/forms/ but this did't helped too much.
Also roaming online and browse already asked questions here was unfruitful.
Please find here below the files:
BLAST_page.html (tried both, commented and uncommented)
{% extends "base_generic.html" %}
{% block content %}
<div class="container-fluid" style="text-align: center;" ></div>
<form method="post" action= {% url 'BLAST-process' %}>
{% csrf_token %}
{{ blast }}
<label for="sequence">Type or paste your sequence in the box below</label><br><br>
<input type="text" id="sequence" class="input_text" name="sequence" value="{{ sequence }}" style="width:600px; height:200px;"><br><br>
<input type="submit" value="Submit">
</form>
</div>
{% endblock %}
<!--
<div class="container-fluid" style="text-align: center;" >
<form method="POST" action= {% url 'BLAST-process' %}>
{% csrf_token %}
<label for="sequence">Type or paste your sequence in the box below</label><br><br>
<input type="text" id="sequence" class="input_text" name="sequence" value="{{ sequence }}" style="width:600px; height:200px;"><br><br>
<input type="submit" value="Submit">
</form>
</div>
-->
In order to check if this form works, I have used this simple .php script. The reasoning behind is that if the form works correctly, the inserted data should be echoed. But this doesn't happen.
<html>
<body>
Sequence: <?php echo $_POST["sequence"]; ?><br>
<?php
echo "<h2>Your Input:</h2>";
echo $sequence;
?>
</body>
</html>
forms.py
from django import forms
class blast(forms.Form):
sequence = forms.CharField(help_text="Enter a sequence", label='sequence')
blast.py the script that should receive data from the form
from Bio.Blast.Applications import NcbiblastpCommandline
from io import StringIO
from Bio.Blast import NCBIXML
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio import SeqIO
import numpy as np
# Create two sequence files
#taken from the textArea.
sequence = sequence
seq1 = SeqRecord(Seq(sequence), id="x")
SeqIO.write(seq1, "seq1.fasta", "fasta")
#SeqIO.write(seq2, "seq2.fasta", "fasta")
# Run BLAST and parse the output as XML
output = NcbiblastpCommandline(query="seq1.fasta",
subject="/Users/santarpia/Documents/tutorial/codorenv/RepOdor/FASTA/db.fasta",
outfmt=5)()[0]
blast_result_record = NCBIXML.read(StringIO(output))
# Print some information on the result
for alignment in blast_result_record.alignments:
for hsp in alignment.hsps:
print('***Alignment****\n')
print('Alignment title', alignment.title)
print('Alignment Length:', alignment.length)
print('E-value:', hsp.expect)
print('Gaps:', hsp.gaps)
print('Identities:', hsp.identities)
print('Positives:', hsp.positives)
print('Score:', hsp.score)
print('Bits:', hsp.bits)
print('Coverage:', hsp.align_length)
print('% Identity:', np.round((hsp.identities / hsp.align_length) * 100, 2))
print("\n")
print (hsp.query[0:])
print("\n")
print (hsp.match[0:])
print("\n")
print (hsp.sbjct[0:])
print('****************************\n\n\n')
As said, any comment on how to set up this would be highly appreciated. If you need more files or information to answer to my question, feel free to ask for them.
So if you followed the documentation correctly after submitting the sequence to the form the code should "enter" theif request.method == 'POST'
portion of the view. You can verify this by putting aprint("hello world")
statement under the if (or animport pdb; pdb.set_trace()
). Once there, you can get the sequence from the form withsequence = form.cleaned_data['sequence']
. Now to pass it to your script you need your script to be a method that can take in an input (the sequence) so wrap you script in something likedef findMostSimilarSequence(sequence):
and remove that first linesequence = sequence
then in your view you can import the method and call it with the sequence varible from your form.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.