Skip to content

Commit

Permalink
Merge pull request #134 from carlosp420/vouchers_per_gene
Browse files Browse the repository at this point in the history
Vouchers per gene
  • Loading branch information
carlosp420 committed Apr 28, 2015
2 parents 3bb62d3 + 1226ac5 commit e5947e0
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 27 deletions.
5 changes: 4 additions & 1 deletion voseq/blast_ncbi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def do_blast(self):
"""
Does a blast against NCBI and saves returned XML file to local disk.
"""
fasta_string = open(self.query_file).read()
with open(self.query_file) as handle:
fasta_string = handle.read()

result_handle = NCBIWWW.qblast('blastn', 'nt', fasta_string)

with open(self.output_file, 'w') as writer:
writer.write(result_handle.read())
result_handle.close()
12 changes: 6 additions & 6 deletions voseq/create_dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ def get_number_of_genes_for_taxa(self, partitions):
gene_code = ''
for item in partitions[0]:
if item.startswith('\n'):
if self.file_format == 'NEXUS' or self.file_format == 'PHY':
gene_code = item.strip().replace('[', '').replace(']', '')
continue
if self.file_format == 'TNT':
gene_code = 'dummy'
continue
if self.file_format == 'NEXUS' or self.file_format == 'PHY':
gene_code = item.strip().replace('[', '').replace(']', '')
continue
if self.file_format == 'TNT':
gene_code = 'dummy'
continue
if gene_code != '':
entry = re.sub('\s+', ' ', item)
voucher, sequence = entry.split(' ')
Expand Down
21 changes: 20 additions & 1 deletion voseq/stats/management/commands/create_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from public_interface.models import Vouchers
from public_interface.models import Sequences
from stats.models import Stats
from stats.models import VouchersPerGene


class Command(BaseCommand):
help = 'Extracts total number of orders, families, genera, etc. from ' \
'our database.'
'our database. Also counts the number of vouchers for each of ' \
'our genes.'

def handle(self, *args, **options):
self.count_vouchers_per_gene()

queryset = Vouchers.objects.all()

orders = set()
Expand Down Expand Up @@ -52,3 +56,18 @@ def handle(self, *args, **options):
'sequences': num_sequences,
}
)

def count_vouchers_per_gene(self):
genes = Sequences.objects.all().values('gene_code').distinct()

model_objects = []

pk_index = 1
for gene in genes:
voucher_count = Sequences.objects.filter(gene_code=gene['gene_code']).count()
gene['voucher_count'] = voucher_count
model_objects.append(VouchersPerGene(id=pk_index, **gene))
pk_index += 1

VouchersPerGene.objects.all().delete()
VouchersPerGene.objects.bulk_create(model_objects)
10 changes: 8 additions & 2 deletions voseq/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@


class Stats(models.Model):
"""
Statistics about number of vouchers, photos, etc to show in page footer.
"""Statistics about number of vouchers, photos, etc to show in page footer.
"""
vouchers = models.IntegerField(
help_text='Number of records, or vouchers.'
Expand All @@ -15,3 +14,10 @@ class Stats(models.Model):
genera = models.IntegerField()
species = models.IntegerField()
sequences = models.IntegerField()


class VouchersPerGene(models.Model):
"""Number of vouchers that have sequences for each of our genes.
"""
gene_code = models.CharField(max_length=100)
voucher_count = models.IntegerField()
11 changes: 11 additions & 0 deletions voseq/stats/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.core.management import call_command

from stats.models import Stats
from stats.models import VouchersPerGene


class TestCustomCommand(TestCase):
Expand All @@ -16,3 +17,13 @@ def test_create_stats(self):
res = Stats.objects.get(id=1)
expected = 10
self.assertEqual(expected, res.vouchers)

def test_count_vouchers_per_gene(self):
call_command('create_stats')
res = VouchersPerGene.objects.all().values('gene_code', 'voucher_count')
for i in res:
if i['gene_code'] == 'COI':
self.assertTrue(i['voucher_count'] == 2)

if i['gene_code'] == '16S':
self.assertTrue(i['voucher_count'] == 1)
9 changes: 9 additions & 0 deletions voseq/view_genes/templates/view_genes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ <h3>Existing genes:</h3>
<b><a href="/genes/{{ i.gene_code }}/">{{ i.gene_code }}</a></b>
<i>{{ i.description }}</i>
&mdash; {{ i.length|intcomma }} bp

<span class="badge">
{% if i.voucher_count == 1 %}
<b>{{ i.voucher_count }} voucher</b>
{% else %}
<b>{{ i.voucher_count }} vouchers</b>
{% endif %}
</span>

</li>
{% endfor %}

Expand Down
8 changes: 8 additions & 0 deletions voseq/view_genes/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ def setUp(self):
cmd = 'migrate_db'
call_command(cmd, *args, **opts)

call_command('create_stats')

self.client = Client()

def test_genes(self):
response = self.client.get('/genes/')
self.assertEqual(200, response.status_code)

def test_genes_with_number_of_vouchers(self):
response = self.client.get('/genes/')
expected = '2 vouchers'
result = str(response.content)
self.assertTrue(expected in result)

def test_view_gene(self):
response = self.client.get('/genes/wingless/')
self.assertEqual(200, response.status_code)
Expand Down
22 changes: 20 additions & 2 deletions voseq/view_genes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@

from core.utils import get_version_stats
from public_interface.models import Genes
from stats.models import VouchersPerGene


def index(request):
version, stats = get_version_stats()

queryset = Genes.objects.all()
voucher_count = get_voucher_count()

queryset = Genes.objects.all().values()
result = []
for i in queryset:
gene_code = i['gene_code']
i['voucher_count'] = voucher_count[gene_code]
result.append(i)

return render(request, 'view_genes/index.html',
{
'result': queryset,
'result': result,
'version': version,
'stats': stats,
},
)


def get_voucher_count():
voucher_count = {}
queryset = VouchersPerGene.objects.all().values('gene_code', 'voucher_count')
for i in queryset:
gene_code = i['gene_code']
voucher_count[gene_code] = i['voucher_count']
return voucher_count


def gene(request, gene_code):
version, stats = get_version_stats()

Expand Down
3 changes: 0 additions & 3 deletions voseq/voseq/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'hola'


ALLOWED_HOSTS = []

Expand Down
3 changes: 3 additions & 0 deletions voseq/voseq/settings/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@


print('Testing')

SECRET_KEY = 'hola'

TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'

DATABASES = {
Expand Down
14 changes: 2 additions & 12 deletions voseq/voseq/settings/travis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,8 @@
fails most of the time due to network problems.
"""

import sys
from .testing import *

from .base import *


print('Testing in Travis')
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
print('in Travis')
TRAVIS = True

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test.db',
}
}

0 comments on commit e5947e0

Please sign in to comment.