7d577164facc8530759556d5eaba8ce81cd5ffa2
[htsworkflow.git] / htsworkflow / frontend / eland_config / views.py
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
5
6 from htsworkflow.frontend.eland_config import forms
7 from htsworkflow.frontend.experiments import models
8
9 import os
10 import glob
11
12
13 def _validate_input(data):
14   return data.replace('..', '').replace('/', '_').replace('\\', '_')
15
16
17 def getElandConfig(flowcell, regenerate=False):
18
19   if hasattr(settings, 'UPLOADTO_CONFIG_FILE'):
20     dest = settings.UPLOADTO_CONFIG_FILE
21   else:
22     dest = '/tmp'
23   file_path = os.path.join(dest, flowcell)
24
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')
30     data = f.read()
31     f.close()
32     return data
33
34   try:
35     fcObj = models.FlowCell.objects.get(flowcell_id__iexact=flowcell)
36   except ObjectDoesNotExist:
37     return None
38
39   data = []
40
41   #form = form.cleaned_data
42
43   BASE_DIR = '/data-store01/compbio/genomes'
44
45   data.append("# FLOWCELL: %s" % (fcObj.flowcell_id))
46   data.append("#")
47
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))
52
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('%', '%%')))
57
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")
63
64   #Construct genome dictionary to figure out what lanes to put
65   # in the config file.
66   genome_dict = {}
67
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))
72
73   genome_list = genome_dict.keys()
74   genome_list.sort()
75
76   #Loop through and create entries for each species.
77   for genome in genome_list:
78     lanes = ''.join(genome_dict[genome])
79     if fcObj.paired_end:
80         data.append('%s:ANALYSIS eland_pair' % (lanes))
81     else:
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)))
86
87   data.append('SEQUENCE_FORMAT --fastq')
88   data.append('') # want a trailing newline
89
90   data = '\n'.join(data)
91
92   f = open(file_path, 'w')
93   f.write(data)
94   f.close()
95
96   return data
97
98
99 def config(request, flowcell=None):
100   """
101   Returns eland config file for a given flowcell number,
102   or returns a list of available flowcell numbers.
103   """
104
105   # Provide INDEX of available Flowcell config files.
106   if flowcell is None:
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() ]
110
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 ]
113
114     return HttpResponse('<br />'.join(fc_html))
115
116   #FIXME: Should validate flowcell input before using.
117   flowcell = _validate_input(flowcell)
118   cfg = getElandConfig(flowcell, regenerate=True)
119
120   if not cfg:
121     return HttpResponse("Hmm, config file for %s does not seem to exist." % (flowcell))
122
123   return HttpResponse(cfg, mimetype="text/plain")