1 from django.conf import settings
2 from django.http import HttpResponse
3 from django.shortcuts import render_to_response
4 from django.core.exceptions import ObjectDoesNotExist
6 from htsworkflow.frontend.eland_config import forms
7 from htsworkflow.frontend.experiments import models
13 def _validate_input(data):
14 return data.replace('..', '').replace('/', '_').replace('\\', '_')
17 def getElandConfig(flowcell, regenerate=False):
19 if hasattr(settings, 'UPLOADTO_CONFIG_FILE'):
20 dest = settings.UPLOADTO_CONFIG_FILE
23 file_path = os.path.join(dest, flowcell)
25 #If we are regenerating the config file, skip
26 # reading of existing file. If the file doesn't
27 # exist, try to generate it form the DB.
28 if not regenerate and os.path.isfile(file_path):
29 f = open(file_path, 'r')
35 fcObj = models.FlowCell.objects.get(flowcell_id__iexact=flowcell)
36 except ObjectDoesNotExist:
41 #form = form.cleaned_data
43 BASE_DIR = '/data-store01/compbio/genomes'
45 data.append("# FLOWCELL: %s" % (fcObj.flowcell_id))
48 notes = fcObj.notes.replace('\r\n', '\n').replace('\r', '\n')
49 notes = notes.replace('\n', '\n# ')
50 data.append("# NOTES:")
51 data.append("# %s\n#" % (notes))
53 #Convert all newline conventions to unix style
54 for lane in fcObj.lane_set.all():
55 data.append("# Lane%d: %s | %s" % \
56 (lane.lane_number, unicode(lane.library.id), lane.library.library_name.replace('%', '%%')))
58 #data.append("GENOME_DIR %s" % (BASE_DIR))
59 #data.append("CONTAM_DIR %s" % (BASE_DIR))
60 read_length = fcObj.read_length
61 #data.append("ELAND_REPEAT")
62 data.append("ELAND_MULTIPLE_INSTANCES 8")
64 #Construct genome dictionary to figure out what lanes to put
68 #l1s = form['lane1_species']
69 for lane in fcObj.lane_set.all():
70 species = lane.library.library_species.scientific_name
71 genome_dict.setdefault(species, []).append(unicode(lane.lane_number))
73 genome_list = genome_dict.keys()
76 #Loop through and create entries for each species.
77 for genome in genome_list:
78 lanes = ''.join(genome_dict[genome])
80 data.append('%s:ANALYSIS eland_pair' % (lanes))
82 data.append('%s:ANALYSIS eland_extended' % (lanes))
83 data.append('%s:READ_LENGTH %s' % (lanes, read_length))
84 data.append('%s:ELAND_GENOME %s' % (lanes, '%%(%s)s' % (genome)))
85 data.append('%s:USE_BASES %s' % (lanes, 'Y'*int(read_length)))
87 data.append('SEQUENCE_FORMAT --fastq')
88 data.append('') # want a trailing newline
90 data = '\n'.join(data)
92 f = open(file_path, 'w')
99 def config(request, flowcell=None):
101 Returns eland config file for a given flowcell number,
102 or returns a list of available flowcell numbers.
105 # Provide INDEX of available Flowcell config files.
107 #Find all FC* config files and report an index html file
108 #fc_list = [ os.path.split(file_path)[1] for file_path in glob.glob(os.path.join(settings.UPLOADTO_CONFIG_FILE, 'FC*')) ]
109 fc_list = [ fc.flowcell_id for fc in models.FlowCell.objects.all() ]
111 #Convert FC* list to html links
112 fc_html = [ '<a href="/eland_config/%s/">%s</a>' % (fc_name, fc_name) for fc_name in fc_list ]
114 return HttpResponse('<br />'.join(fc_html))
116 #FIXME: Should validate flowcell input before using.
117 flowcell = _validate_input(flowcell)
118 cfg = getElandConfig(flowcell, regenerate=True)
121 return HttpResponse("Hmm, config file for %s does not seem to exist." % (flowcell))
123 return HttpResponse(cfg, mimetype="text/plain")