1 from __future__ import absolute_import, print_function
3 from django.conf import settings
4 from django.http import HttpResponse
5 from django.core.exceptions import ObjectDoesNotExist
7 from experiments import models
12 def _validate_input(data):
13 return data.replace('..', '').replace('/', '_').replace('\\', '_')
16 def getElandConfig(flowcell, regenerate=False):
18 if hasattr(settings, 'UPLOADTO_CONFIG_FILE'):
19 dest = settings.UPLOADTO_CONFIG_FILE
22 file_path = os.path.join(dest, flowcell)
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')
34 fcObj = models.FlowCell.objects.get(flowcell_id__iexact=flowcell)
35 except ObjectDoesNotExist:
40 #form = form.cleaned_data
42 data.append("# FLOWCELL: %s" % (fcObj.flowcell_id))
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))
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('%', '%%')))
56 read_length = fcObj.read_length
57 #data.append("ELAND_REPEAT")
58 data.append("ELAND_MULTIPLE_INSTANCES 8")
60 #Construct genome dictionary to figure out what lanes to put
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))
69 genome_list = genome_dict.keys()
72 #Loop through and create entries for each species.
73 for genome in genome_list:
74 lanes = ''.join(genome_dict[genome])
76 data.append('%s:ANALYSIS eland_pair' % (lanes))
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)))
83 data.append('SEQUENCE_FORMAT --fastq')
84 data.append('') # want a trailing newline
86 data = '\n'.join(data)
88 f = open(file_path, 'w')
95 def config(request, flowcell=None):
97 Returns eland config file for a given flowcell number,
98 or returns a list of available flowcell numbers.
101 # Provide INDEX of available Flowcell config files.
103 fc_list = [fc.flowcell_id for fc in models.FlowCell.objects.all()]
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]
109 return HttpResponse('<br />'.join(fc_html))
111 #FIXME: Should validate flowcell input before using.
112 flowcell = _validate_input(flowcell)
113 cfg = getElandConfig(flowcell, regenerate=True)
117 "Hmm, config file for %s does not seem to exist." % (flowcell))
119 return HttpResponse(cfg, mimetype="text/plain")