Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
complete ENA Exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
anatskiy committed Jun 9, 2018
1 parent ab9b5a3 commit eae4e06
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
52 changes: 39 additions & 13 deletions ena_exporter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,20 @@ def get_galaxy_status(self, request):
authentication_classes=[CsrfExemptSessionAuthentication])
def download(self, request, pk=None):
samples = json.loads(request.data.get('samples', '[]'))
study_abstract = request.data.get('study_abstract', '')
study_type = request.data.get('study_type', '')
study_title = request.data.get('study_title', '')
study_abstract = request.data.get('study_abstract', '')

response = HttpResponse(content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=ENA.zip'

experiments_file, samples_file, studies_file, runs_file = \
self._generate_files(samples, study_abstract, study_type)
self._generate_files(samples, study_data={
'title': study_title,
'study_type': study_type,
'study_abstract': study_abstract,
'alias': f'study_{study_title.replace(" ", "_")}',
})

# Archive the files
in_memory = io.BytesIO()
Expand All @@ -133,8 +139,9 @@ def upload(self, request, pk=None):
url = request.data.get('galaxy_url', '')
api_key = request.data.get('galaxy_api_key', '')
samples = json.loads(request.data.get('samples', '[]'))
study_abstract = request.data.get('study_abstract', '')
study_type = request.data.get('study_type', '')
study_title = request.data.get('study_title', '')
study_abstract = request.data.get('study_abstract', '')

experiments_file_path = 'experiments.tsv'
samples_file_path = 'samples.tsv'
Expand All @@ -152,7 +159,12 @@ def upload(self, request, pk=None):
})

experiments_file, samples_file, studies_file, runs_file = \
self._generate_files(samples, study_abstract, study_type)
self._generate_files(samples, study_data={
'title': study_title,
'study_type': study_type,
'study_abstract': study_abstract,
'alias': f'study_{study_title.replace(" ", "_")}',
})

files = {
experiments_file_path: experiments_file,
Expand Down Expand Up @@ -180,26 +192,40 @@ def upload(self, request, pk=None):
return Response({'success': True})

@staticmethod
def _generate_files(data, study_abstract, study_type):
def _generate_files(data, **kwargs):
def getrow(header, item, type, index):
row = []
name = item.get('library_name')
for h in header:
value = item.get(h, '')
row.append(value if value else 'None')
if h == 'alias' and type != 'study':
value = f'{type}_{name}'
elif h == 'experiment_alias':
value = f'experiment_{name}'
elif h == 'sample_alias':
value = f'sample_{name}'
elif h == 'run_alias':
value = f'run_{name}'
else:
value = item.get(h, 'None')
row.append(value)
return row

def getfile(type, header, data, **kwargs):
study_data = kwargs.get('study_data', {})
file = io.StringIO()
writer = csv.writer(file, dialect='excel-tab')
writer.writerow(header)

if type == 'study':
study_data = kwargs.get('study_data', {})
item = {**dict(data[0]), **study_data}
writer.writerow(getrow(header, item, type, 1))
else:
for i, item in enumerate(data):
writer.writerow(getrow(header, item, type, i))
item_ = {
**dict(item),
**{'study_alias': study_data.get('alias', '')},
}
writer.writerow(getrow(header, item_, type, i))

return file

Expand All @@ -223,7 +249,8 @@ def getfile(type, header, data, **kwargs):
'instrument_model',
'submission_date',
]
experiments_file = getfile('experiment', header, data)
experiments_file = getfile('experiment', header, data,
study_data=kwargs.get('study_data'))

# Create samples.tsv
header = [
Expand All @@ -249,9 +276,8 @@ def getfile(type, header, data, **kwargs):
'pubmed_id',
'submission_date',
]
studies_file = getfile('study', header, data, study_data={
'study_type': study_type, 'study_abstract': study_abstract
})
studies_file = getfile('study', header, data,
study_data=kwargs.get('study_data'))

# Create runs.tsv
header = [
Expand Down
18 changes: 11 additions & 7 deletions static/main-hub/app/view/enaexporter/ENAExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Ext.define('MainHub.view.enaexporter.ENAExporter', {

title: 'ENA Exporter',

height: 480,
height: 525,
width: 700,

modal: true,
Expand Down Expand Up @@ -44,8 +44,9 @@ Ext.define('MainHub.view.enaexporter.ENAExporter', {
border: 0,
defaultType: 'textfield',
defaults: {
anchor: '100%',
labelWidth: 100,
anchor: '100%'
allowBlank: false
},
items: [
{
Expand All @@ -54,17 +55,21 @@ Ext.define('MainHub.view.enaexporter.ENAExporter', {
emptyText: 'Request',
readOnly: true
},
{
name: 'study_title',
fieldLabel: 'Title',
emptyText: 'Title of the study as would be used in a publication'
},
{
xtype: 'combobox',
name: 'study_type',
fieldLabel: 'Study Type',
fieldLabel: 'Type',
emptyText: 'Select Study Type',
queryMode: 'local',
displayField: 'name',
valueField: 'name',
store: 'ENAStudyTypes',
forceSelection: true,
allowBlank: false,
listConfig: {
getInnerTpl: function () {
return '<span data-qtip="{description}">{name}</span>';
Expand All @@ -74,9 +79,8 @@ Ext.define('MainHub.view.enaexporter.ENAExporter', {
{
name: 'study_abstract',
xtype: 'textarea',
fieldLabel: 'Study Abstract',
emptyText: 'Briefly describe the goals, purpose, and scope of the Study',
allowBlank: false,
fieldLabel: 'Abstract',
emptyText: 'Briefly describe the goals, purpose, and scope of the study',
height: 75
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Ext.define('MainHub.view.enaexporter.ENAExporterController', {
var params = {
samples: Ext.JSON.encode(Ext.Array.pluck(store.data.items, 'data')),
study_abstract: data.study_abstract,
study_title: data.study_title,
study_type: data.study_type
};

Expand Down
4 changes: 3 additions & 1 deletion static/main-hub/app/view/enaexporter/Samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Ext.define('MainHub.view.enaexporter.Samples', {
// Experiments
{
text: 'Library Name',
dataIndex: 'library_name'
dataIndex: 'library_name',
regex: new RegExp(/^[A-Za-z0-9_-]+$/),
regexText: 'Only A-Za-z0-9 as well as _ and - are allowed'
},
{
text: 'Library Strategy',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ Ext.define('MainHub.view.libraries.BatchAddWindowController', {
flex: 1,
editor: {
xtype: 'textfield',
regex: new RegExp('^[A-Za-z0-9_\-]+$'),
regex: new RegExp(/^[A-Za-z0-9_-]+$/),
regexText: 'Only A-Za-z0-9 as well as _ and - are allowed'
},
renderer: this.errorRenderer
Expand Down

0 comments on commit eae4e06

Please sign in to comment.