-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
203 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import glob | ||
import os | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.conf import settings | ||
|
||
from .utils import BLAST | ||
|
||
|
||
class BlastLocalTest(TestCase): | ||
def setUp(self): | ||
args = [] | ||
opts = {'dumpfile': 'test_db_dump.xml'} | ||
cmd = 'migrate_db' | ||
call_command(cmd, *args, **opts) | ||
|
||
blast_type = 'local' | ||
voucher_code = 'CP100-10' | ||
gene_code = 'COI' | ||
self.blast = BLAST(blast_type, voucher_code, gene_code) | ||
self.seq_file = '' | ||
|
||
''' | ||
def test_have_blast_db(self): | ||
result = self.blast.have_blast_db() | ||
self.assertEqual(False, result) | ||
def test_have_blast_db_true(self): | ||
self.blast.create_blast_db() | ||
result = self.blast.have_blast_db() | ||
self.assertEqual(True, result) | ||
''' | ||
|
||
def test_save_seqs_to_file(self): | ||
self.blast.save_seqs_to_file() | ||
self.seq_file = os.path.join(settings.BASE_DIR, | ||
'..', | ||
'blast_local', | ||
'db', | ||
'COI_seqs.fas', | ||
) | ||
result = os.path.isfile(self.seq_file) | ||
self.assertTrue(result) | ||
|
||
def test_create_blast_db(self): | ||
self.blast.save_seqs_to_file() | ||
self.blast.create_blast_db() | ||
files = glob.glob( | ||
os.path.join(settings.BASE_DIR, | ||
'..', | ||
'blast_local', | ||
'db', | ||
'COI_seqs.fas.n*') | ||
) | ||
self.assertTrue(len(files) > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.conf.urls import patterns | ||
from django.conf.urls import url | ||
|
||
from . import views | ||
|
||
|
||
urlpatterns = patterns( | ||
'', | ||
url(r'^/(?P<voucher_code>.+)/(?P<gene_code>.+)/$', views.index, name='index'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import os | ||
import re | ||
import subprocess | ||
|
||
from Bio.Blast.Applications import NcbiblastnCommandline | ||
from Bio.Seq import Seq | ||
from Bio import SeqIO | ||
from Bio.SeqRecord import SeqRecord | ||
|
||
from public_interface.models import Sequences | ||
|
||
|
||
class BLAST(object): | ||
""" | ||
Class to handle duties related to local blast against sequences of one gene, | ||
and full blast against all sequences in our database. | ||
""" | ||
def __init__(self, blast_type, voucher_code, gene_code): | ||
""" | ||
Type of blast to do: local, full, remote | ||
:param blast_type: local, full, remote. | ||
:param voucher_code: | ||
:param gene_code: | ||
""" | ||
self.blast_type = blast_type | ||
self.voucher_code = voucher_code | ||
self.gene_code = gene_code | ||
self.mask = True | ||
self.cwd = os.path.dirname(__file__) | ||
|
||
def have_blast_db(self): | ||
""" | ||
Finds out whether we already have a blast db with our sequences. | ||
:return: True or False | ||
""" | ||
pass | ||
|
||
def is_blast_db_up_to_date(self): | ||
""" | ||
Finds out whether our blast db contains all our sequences. In other | ||
words, it finds out whether there are sequences in our postgres db with | ||
time_created or time_edited more recent than our blast db files. | ||
:return: | ||
""" | ||
pass | ||
|
||
def save_seqs_to_file(self): | ||
""" | ||
Query sequences for each gene from our database and save them to local | ||
disk. | ||
Sets attribute `self.seq_file` containing necessary sequences from our | ||
database. | ||
""" | ||
if self.blast_type == 'local': | ||
self.seq_file = os.path.join(self.cwd, | ||
'db', | ||
self.gene_code + "_seqs.fas", | ||
) | ||
queryset = Sequences.objects.all().filter(gene_code=self.gene_code) | ||
|
||
my_records = [] | ||
for i in queryset: | ||
id = i.code_id + '|' + i.gene_code | ||
seq = self.strip_question_marks(i.sequences) | ||
seq_record = SeqRecord(Seq(seq), | ||
id=id) | ||
my_records.append(seq_record) | ||
SeqIO.write(my_records, self.seq_file, "fasta") | ||
|
||
def strip_question_marks(self, seq): | ||
seq = re.sub('^\?+', '', seq) | ||
seq = re.sub('\?+$', '', seq) | ||
return seq | ||
|
||
def create_blast_db(self): | ||
""" | ||
Creates a BLAST database from our sequences file in FASTA format. | ||
Optionally eliminates low-complexity regions from the sequences. | ||
:return: | ||
""" | ||
if self.mask is True: | ||
command = 'dustmasker -in ' + self.seq_file + ' -infmt fasta ' | ||
command += '-outfmt maskinfo_asn1_bin -out ' + self.seq_file + '_dust.asnb' | ||
subprocess.check_output(command, shell=True) # identifying low-complexity regions. | ||
|
||
command = 'makeblastdb -in ' + self.seq_file + ' -input_type fasta -dbtype nucl ' | ||
command += '-mask_data ' + self.seq_file + '_dust.asnb ' | ||
command += '-out ' + self.seq_file + ' -title "Whole Genome without low-complexity regions"' | ||
print("creating database...") | ||
subprocess.check_output(command, shell=True) # Overwriting the genome file. | ||
else: | ||
command = 'makeblastdb -in ' + self.seq_file + ' -input_type fasta -dbtype nucl ' | ||
command += '-out ' + self.seq_file + ' -title "Whole Genome unmasked"' | ||
print("creating database...") | ||
subprocess.check_output(command, shell=True) | ||
|
||
def do_blast(self): | ||
blastn_cline = NcbiblastnCommandline(query=self.query, db=self.db, | ||
evalue=0.001, outfmt=5, out="opuntia.xml") | ||
blastn_cline() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
|
||
from django.shortcuts import render | ||
|
||
|
||
def index(request, voucher_code, gene_code): | ||
return render(request, 'public_interface/base.html') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters