Skip to content

Commit

Permalink
Merge pull request #109 from carlosp420/dataset_button
Browse files Browse the repository at this point in the history
Dataset button
  • Loading branch information
carlosp420 committed Mar 27, 2015
2 parents a50bfe0 + 0e5c1dc commit cf591e3
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ htmlcov
# old VoSeq files to ignore
conf.php

# avoid including dataset files
voseq/create_dataset/dataset_files/*txt

# avoid including migrations files
voseq/public_interface/migrations/*
voseq/stats/migrations/*
Expand Down
20 changes: 19 additions & 1 deletion voseq/create_dataset/dataset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import collections
import os
import uuid
import re

from core.utils import chain_and_flatten
Expand Down Expand Up @@ -26,6 +28,20 @@ def __init__(self, codon_positions, partition_by_positions, seq_objs, gene_codes
self.warnings = []
self.partition_list = None

self.cwd = os.path.dirname(__file__)
self.guid = self.make_guid()
self.dataset_file = os.path.join(self.cwd,
'dataset_files',
self.file_format + '_' + self.guid + '.txt',
)

def save_dataset_to_file(self, dataset_str):
with open(self.dataset_file, 'w') as handle:
handle.write(dataset_str)

def make_guid(self):
return uuid.uuid4().hex

def get_number_chars_from_partition_list(self, partitions):
chars = 0
gene_codes_and_lengths = collections.OrderedDict()
Expand Down Expand Up @@ -132,7 +148,9 @@ def convert_lists_to_dataset(self, partitions):
for i in partitions:
out += '\n'
out += '\n'.join(i)
return out.strip()
dataset_str = out.strip()
self.save_dataset_to_file(dataset_str)
return dataset_str

def get_codons_in_each_partition(self, codons):
partition_list = ()
Expand Down
Empty file.
32 changes: 23 additions & 9 deletions voseq/create_dataset/templates/create_dataset/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h3>Create dataset:</h3>
<div class="container">
<div class="row">

<div class="col-xs-12 col-sm-12 col-md-11 col-lg-9 col-lg-offset-1">
<div class="col-xs-12 col-sm-12 col-md-11 col-lg-11 ">

<form action="/create_dataset/results/" method="post">
<div class="panel panel-primary" style="min-width: 790px;">
Expand Down Expand Up @@ -159,6 +159,16 @@ <h3 class="panel-title"><b>Enter the required info to make yourself a ready-to-r
{{ form.voucher_codes }}
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
<td>
<button type="submit" class="btn btn-info" id="submit_button">
Create dataset
</button>
</td>
</tr>
</table>
</td>

Expand All @@ -179,13 +189,19 @@ <h3 class="panel-title"><b>Enter the required info to make yourself a ready-to-r
(if geneset chosen, adds extra genes)
</td>
<td>
<table class="table table-condensed table-striped">
<tr>
<td>
<table class="table table-condensed table-striped small_fonts">
{% for i in form.gene_codes %}
{{ i }}&nbsp;&nbsp;&nbsp;&nbsp;
{% if forloop.counter == 1 %}
<tr>
{% endif %}
<td>
{{ i }}
</td>
{% if forloop.counter|divisibleby:"4" %}
</tr>
{% endif %}

{% endfor %}
</td>
</tr>
</table>
</td>
Expand All @@ -198,9 +214,7 @@ <h3 class="panel-title"><b>Enter the required info to make yourself a ready-to-r

</div><!-- panel -->

<button type="submit" class="btn btn-info" id="submit_button">
Create dataset
</button>

</form>


Expand Down
13 changes: 4 additions & 9 deletions voseq/create_dataset/templates/create_dataset/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,24 @@ <h3 class="panel-title"><b>Your dataset file:</b></h3>

</div><!-- panel -->

<a href="/genbank_fasta/results/{{ protein_file }}">
<a href="/create_dataset/results/{{ dataset_file }}">
<button class="btn btn-info">
<i class="fa fa-download"></i>
Download dataset file
</button></a>


<br />
<br />

{% if phylip_partition_file %}
{% if charset_block %}
<table class="table table-bordered">
<tr>
<td>
<textarea readonly style="height: 150px; white-space: nowrap;" class="form-control">{{ charset_block }}</textarea>
</td>
</tr>
</table>

<a href="/genbank_fasta/results/{{ protein_file }}">
<button class="btn btn-info">
<i class="fa fa-download"></i>
Download PHYLIP partitions file
</button>
</a>
{% endif %}


Expand Down
2 changes: 1 addition & 1 deletion voseq/create_dataset/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
'',
url(r'^/$', views.index, name='index'),
url(r'^/results/$', views.results, name='results'),

url(r'^/results/(?P<file_name>.+\.txt)/$', views.serve_file, name='serve_file'),
)
32 changes: 14 additions & 18 deletions voseq/create_dataset/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import os
import uuid

from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord

Expand All @@ -24,17 +21,8 @@ def __init__(self, *args, **kwargs):
self.number_taxa = len(self.voucher_codes)
self.number_chars = None
self.vouchers_to_drop = None
self.cwd = os.path.dirname(__file__)
self.guid = self.make_guid()
self.phylip_partition_file = os.path.join(self.cwd,
'phylip_files',
'phylip_' + self.guid + '_partitions.phy',
)
self.charset_block = None

def make_guid(self):
return uuid.uuid4().hex

def get_charset_block(self):
charset_block = []

Expand Down Expand Up @@ -99,7 +87,9 @@ def convert_lists_to_dataset(self, partitions):
out += [' ' * 55 + line[-1] + '\n']

self.get_charset_block()
return ''.join(out)
dataset_str = ''.join(out)
self.save_dataset_to_file(dataset_str)
return dataset_str


class CreateTNT(Dataset):
Expand Down Expand Up @@ -145,7 +135,9 @@ def convert_lists_to_dataset(self, partitions):
out += '\n' + i

out += '\n;\nproc/;'
return out.strip()
dataset_str = out.strip()
self.save_dataset_to_file(dataset_str)
return dataset_str


class CreateNEXUS(Dataset):
Expand Down Expand Up @@ -218,7 +210,9 @@ def convert_lists_to_dataset(self, partitions):
out += self.get_charset_block()
out += self.get_partitions_block()
out += self.get_final_block()
return '\n'.join(out)
dataset_str = '\n'.join(out)
self.save_dataset_to_file(dataset_str)
return dataset_str


class CreateDataset(object):
Expand Down Expand Up @@ -246,7 +240,7 @@ def __init__(self, cleaned_data):
self.gene_codes_metadata = self.get_gene_codes_metadata()
self.warnings = []
self.outgroup = cleaned_data['outgroup']
self.phylip_partition_file = None
self.dataset_file = None
self.charset_block = None
self.dataset_str = self.create_dataset()

Expand All @@ -261,6 +255,7 @@ def create_dataset(self):
self.file_format)
fasta_dataset = fasta.from_seq_objs_to_dataset()
self.warnings += fasta.warnings
self.dataset_file = fasta.dataset_file
return fasta_dataset

if self.file_format == 'PHY':
Expand All @@ -270,8 +265,7 @@ def create_dataset(self):
self.minimum_number_of_genes)
phylip_dataset = phy.from_seq_objs_to_dataset()
self.warnings += phy.warnings
self.phylip_partition_file = phy.phylip_partition_file
print(">>>>>>>>>>>>> self.charset_block", phy.charset_block)
self.dataset_file = phy.dataset_file
self.charset_block = phy.charset_block
return phylip_dataset

Expand All @@ -282,6 +276,7 @@ def create_dataset(self):
self.minimum_number_of_genes)
tnt_dataset = tnt.from_seq_objs_to_dataset()
self.warnings += tnt.warnings
self.dataset_file = tnt.dataset_file
return tnt_dataset

if self.file_format == 'NEXUS':
Expand All @@ -291,6 +286,7 @@ def create_dataset(self):
self.minimum_number_of_genes)
nexus_dataset = nexus.from_seq_objs_to_dataset()
self.warnings += nexus.warnings
self.dataset_file = nexus.dataset_file
return nexus_dataset

def create_seq_record(self, s):
Expand Down
28 changes: 22 additions & 6 deletions voseq/create_dataset/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import re

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.http import HttpResponse

from core.utils import get_version_stats
from .forms import CreateDatasetForm
Expand All @@ -28,19 +30,19 @@ def results(request):
if form.is_valid():
print(">>>>", form.cleaned_data)
dataset_creator = CreateDataset(form.cleaned_data)
dataset = dataset_creator.dataset_str
dataset = dataset_creator.dataset_str[0:1500] + '\n...\n\n\n' + '#######\nComplete dataset file available for download.\n#######'
errors = dataset_creator.errors
warnings = dataset_creator.warnings

phylip_file = dataset_creator.phylip_partition_file
if phylip_file is not None:
phylip_partition_file = re.search('(phylip_[a-z0-9]+_partitions\.phy)', phylip_file).groups()[0]
dataset_file_abs = dataset_creator.dataset_file
if dataset_file_abs is not None:
dataset_file = re.search('([A-Z]+_[a-z0-9]+\.txt)', dataset_file_abs).groups()[0]
else:
phylip_partition_file = False
dataset_file = False

return render(request, 'create_dataset/results.html',
{
'phylip_partition_file': phylip_partition_file,
'dataset_file': dataset_file,
'charset_block': dataset_creator.charset_block,
'dataset': dataset,
'errors': errors,
Expand All @@ -60,3 +62,17 @@ def results(request):
)
else:
return HttpResponseRedirect('/create_dataset/')


def serve_file(request, file_name):
cwd = os.path.dirname(__file__)
dataset_file = os.path.join(cwd,
'dataset_files',
file_name,
)
response = HttpResponse(open(dataset_file, 'r').read(), content_type='application/text')
response['Content-Disposition'] = 'attachment; filename=dataset_file.txt'

if os.path.isfile(dataset_file):
os.remove(dataset_file)
return response
5 changes: 5 additions & 0 deletions voseq/public_interface/static/css/pi.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ body {
font-family: 'Source Code Pro', Courier, serif;
font-weight: 600;
}

.small_fonts {
font-family: 'Source Code Pro', sans-serif;
font-size: 12px;
}

0 comments on commit cf591e3

Please sign in to comment.