convert to unicode_literals
[htsworkflow.git] / eland_config / views.py
1 from __future__ import absolute_import, print_function, unicode_literals
2
3 from django.conf import settings
4 from django.http import HttpResponse
5 from django.core.exceptions import ObjectDoesNotExist
6
7 from experiments import models
8
9 import os
10
11
12 def _validate_input(data):
13     return data.replace('..', '').replace('/', '_').replace('\\', '_')
14
15
16 def getElandConfig(flowcell, regenerate=False):
17
18     if hasattr(settings, 'UPLOADTO_CONFIG_FILE'):
19         dest = settings.UPLOADTO_CONFIG_FILE
20     else:
21         dest = '/tmp'
22     file_path = os.path.join(dest, flowcell)
23
24     #If we are regenerating the config file, skip
25     # reading of existing file. If the file doesn't
26     # exist, try to generate it form the DB.
27     if not regenerate and os.path.isfile(file_path):
28         f = open(file_path, 'r')
29         data = f.read()
30         f.close()
31         return data
32
33     try:
34         fcObj = models.FlowCell.objects.get(flowcell_id__iexact=flowcell)
35     except ObjectDoesNotExist:
36         return None
37
38     data = []
39
40     #form = form.cleaned_data
41
42     data.append("# FLOWCELL: %s" % (fcObj.flowcell_id))
43     data.append("#")
44
45     notes = fcObj.notes.replace('\r\n', '\n').replace('\r', '\n')
46     notes = notes.replace('\n', '\n#  ')
47     data.append("# NOTES:")
48     data.append("#  %s\n#" % (notes))
49
50     #Convert all newline conventions to unix style
51     for lane in fcObj.lane_set.all():
52         data.append("# Lane%d: %s | %s" %
53                     (lane.lane_number, unicode(lane.library.id),
54                      lane.library.library_name.replace('%', '%%')))
55
56     read_length = fcObj.read_length
57     #data.append("ELAND_REPEAT")
58     data.append("ELAND_MULTIPLE_INSTANCES 8")
59
60     #Construct genome dictionary to figure out what lanes to put
61     # in the config file.
62     genome_dict = {}
63
64     #l1s = form['lane1_species']
65     for lane in fcObj.lane_set.all():
66         species = lane.library.library_species.scientific_name
67         genome_dict.setdefault(species, []).append(unicode(lane.lane_number))
68
69     genome_list = genome_dict.keys()
70     genome_list.sort()
71
72     #Loop through and create entries for each species.
73     for genome in genome_list:
74         lanes = ''.join(genome_dict[genome])
75         if fcObj.paired_end:
76             data.append('%s:ANALYSIS eland_pair' % (lanes))
77         else:
78             data.append('%s:ANALYSIS eland_extended' % (lanes))
79         data.append('%s:READ_LENGTH %s' % (lanes, read_length))
80         data.append('%s:ELAND_GENOME %s' % (lanes, '%%(%s)s' % (genome)))
81         data.append('%s:USE_BASES %s' % (lanes, 'Y'*int(read_length)))
82
83     data.append('SEQUENCE_FORMAT --fastq')
84     data.append('')  # want a trailing newline
85
86     data = '\n'.join(data)
87
88     f = open(file_path, 'w')
89     f.write(data)
90     f.close()
91
92     return data
93
94
95 def config(request, flowcell=None):
96     """
97     Returns eland config file for a given flowcell number,
98     or returns a list of available flowcell numbers.
99     """
100
101     # Provide INDEX of available Flowcell config files.
102     if flowcell is None:
103         fc_list = [fc.flowcell_id for fc in models.FlowCell.objects.all()]
104
105         #Convert FC* list to html links
106         fc_html = ['<a href="/eland_config/%s/">%s</a>' % (fc_name, fc_name)
107                    for fc_name in fc_list]
108
109         return HttpResponse('<br />'.join(fc_html))
110
111     #FIXME: Should validate flowcell input before using.
112     flowcell = _validate_input(flowcell)
113     cfg = getElandConfig(flowcell, regenerate=True)
114
115     if not cfg:
116         return HttpResponse(
117             "Hmm, config file for %s does not seem to exist." % (flowcell))
118
119     return HttpResponse(cfg, mimetype="text/plain")