Err, missed a spot.
[htsworkflow.git] / gaworkflow / frontend / fctracker / views.py
1 # Create your views here.
2 from gaworkflow.frontend.fctracker.models import Library
3 from gaworkflow.frontend.fctracker.results import get_flowcell_result_dict, flowcellIdStrip
4 from gaworkflow.pipeline.runfolder import ElementTree
5 from gaworkflow.pipeline import runfolder
6 from gaworkflow.frontend import settings
7 from gaworkflow.util import makebed
8 from gaworkflow.util import opener
9
10 from django.http import HttpResponse
11 from django.template.loader import get_template
12 from django.template import Context
13
14 import StringIO
15
16 #from django.db.models import base 
17
18 def library(request):
19     library_list = Library.objects.all() #.order_by('-pub_date')
20     rep_string = '<a href="/library/%s/">%s - %s (%s)</a>'
21     output = '<br />\n'.join([rep_string \
22       % (l.library_id,
23          l.library_id,
24          l.library_name,
25          l.library_species.scientific_name) for l in library_list])
26     return HttpResponse(output)
27
28 def library_to_flowcells(request, lib_id):
29     """
30     Display information about all the flowcells a library has been run on.
31     """
32     t = get_template("summary_stats.html")
33     
34     try:
35       lib = Library.objects.get(library_id=lib_id)
36     except:
37       return HttpResponse("Library %s does not exist" % (lib_id))
38     
39     output = []
40     
41     output.append('<b>Library ID:</b> %s' % (lib.library_id))
42     output.append('<b>Name:</b> %s' % (lib.library_name))
43     output.append('<b>Species:</b> %s' % (lib.library_species.scientific_name))
44     output.append('')
45     
46     output.append('<b>FLOWCELL - LANE:</b>')
47     
48     output.extend([ '%s - Lane 1 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 1)) for fc in lib.lane_1_library.all() ])
49     output.extend([ '%s - Lane 2 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 2)) for fc in lib.lane_2_library.all() ])
50     output.extend([ '%s - Lane 3 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 3)) for fc in lib.lane_3_library.all() ])
51     output.extend([ '%s - Lane 4 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 4)) for fc in lib.lane_4_library.all() ])
52     output.extend([ '%s - Lane 5 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 5)) for fc in lib.lane_5_library.all() ])
53     output.extend([ '%s - Lane 6 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 6)) for fc in lib.lane_6_library.all() ])
54     output.extend([ '%s - Lane 7 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 7)) for fc in lib.lane_7_library.all() ])
55     output.extend([ '%s - Lane 8 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 8)) for fc in lib.lane_8_library.all() ])
56     
57     record_count = lib.lane_1_library.count() + \
58                     lib.lane_2_library.count() + \
59                     lib.lane_3_library.count() + \
60                     lib.lane_4_library.count() + \
61                     lib.lane_5_library.count() + \
62                     lib.lane_6_library.count() + \
63                     lib.lane_7_library.count() + \
64                     lib.lane_8_library.count()
65     
66     flowcell_list = []
67     flowcell_list.extend([ (fc.flowcell_id, 1) for fc in lib.lane_1_library.all() ])
68     flowcell_list.extend([ (fc.flowcell_id, 2) for fc in lib.lane_2_library.all() ])
69     flowcell_list.extend([ (fc.flowcell_id, 3) for fc in lib.lane_3_library.all() ])
70     flowcell_list.extend([ (fc.flowcell_id, 4) for fc in lib.lane_4_library.all() ])
71     flowcell_list.extend([ (fc.flowcell_id, 5) for fc in lib.lane_5_library.all() ])
72     flowcell_list.extend([ (fc.flowcell_id, 6) for fc in lib.lane_6_library.all() ])
73     flowcell_list.extend([ (fc.flowcell_id, 7) for fc in lib.lane_7_library.all() ])
74     flowcell_list.extend([ (fc.flowcell_id, 8) for fc in lib.lane_8_library.all() ])
75     flowcell_list.sort()
76     
77     output.append('<br />')
78     
79     data_dict_list = []
80     for fc, lane in flowcell_list:
81         dicts, err_list = _summary_stats(fc, lane)
82         
83         data_dict_list.extend(dicts)
84     
85         for err in err_list:    
86             output.append(err)
87     
88     html = t.render(Context({'data_dict_list': data_dict_list}))
89     output.append('<br />')
90     output.append('<br />')
91     output.append(html)
92     output.append('<br />')
93     output.append('<br />')
94     
95     if record_count == 0:
96         output.append("None Found")
97     
98     return HttpResponse('<br />\n'.join(output))
99
100
101 def summaryhtm_fc_cnm(request, fc_id, cnm):
102     """
103     returns a Summary.htm file if it exists.
104     """
105     fc_id = flowcellIdStrip(fc_id)
106     d = get_flowcell_result_dict(fc_id)
107     
108     if d is None:
109         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
110     
111     if cnm not in d:
112         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
113     
114     summary_filepath = d[cnm]['summary']
115     
116     if summary_filepath is None:
117         return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
118     
119     f = open(summary_filepath, 'r')
120     
121     return HttpResponse(f)
122
123
124 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
125     """
126     returns an eland_file upon calling.
127     """
128     fc_id = flowcellIdStrip(fc_id)
129     d = get_flowcell_result_dict(fc_id)
130     
131     if d is None:
132         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
133     
134     if cnm not in d:
135         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
136     
137     erd = d[cnm]['eland_results']
138     lane = int(lane)
139     
140     if lane not in erd:
141         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
142     
143     filepath = erd[lane]
144     
145     f = opener.autoopen(filepath, 'r')
146     
147     return HttpResponse(f, mimetype="application/x-elandresult")
148
149
150 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
151     """
152     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
153     """
154     return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
155
156
157 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
158     """
159     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
160     """
161     fc_id = flowcellIdStrip(fc_id)
162     d = get_flowcell_result_dict(fc_id)
163     
164     if d is None:
165         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
166     
167     if cnm not in d:
168         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
169     
170     erd = d[cnm]['eland_results']
171     lane = int(lane)
172     
173     if lane not in erd:
174         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
175     
176     filepath = erd[lane]
177     
178     # Eland result file
179     fi = opener.autoopen(filepath, 'r')
180     # output memory file
181     
182     
183     name, description = makebed.make_description(settings.DATABASE_NAME,
184                                                  fc_id,
185                                                  lane)
186     
187     bedgen = makebed.make_bed_from_eland_stream_generator(fi, name, description)
188     
189     if ucsc_compatible:
190         return HttpResponse(bedgen)
191     else:
192         return HttpResponse(bedgen, mimetype="application/x-bedfile")
193
194
195 def _summary_stats(flowcell_id, lane):
196     """
197     return a dictionary of summary stats for a given flowcell_id & lane.
198     """
199     fc_id = flowcellIdStrip(flowcell_id)
200     fc_result_dict = get_flowcell_result_dict(fc_id)
201     
202     dict_list = []
203     err_list = []
204     
205     if fc_result_dict is None:
206         err_list.append('Results for Flowcell %s not found.' % (fc_id))
207         return (dict_list, err_list)
208     
209     for cnm in fc_result_dict:
210     
211         xmlpath = fc_result_dict[cnm]['runx_xml']
212         
213         if xmlpath is None:
214             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
215             continue
216         
217         tree = ElementTree.parse(xmlpath).getroot()
218         results = runfolder.PipelineRun(pathname='', xml=tree)
219         
220         lane_results = results.gerald.summary[str(lane)]
221         lrs = lane_results
222         
223         d = {}
224         
225         d['average_alignment_score'] = lrs.average_alignment_score
226         d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
227         d['cluster'] = lrs.cluster
228         d['lane'] = lrs.lane
229         d['flowcell'] = flowcell_id
230         d['percent_error_rate'] = lrs.percent_error_rate
231         d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
232         d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
233         d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
234         
235         #FIXME: function finished, but need to take advantage of
236         #   may need to take in a list of lanes so we only have to
237         #   load the xml file once per flowcell rather than once
238         #   per lane.
239         dict_list.append(d)
240     
241     return (dict_list, err_list)
242     
243     
244
245     
246 def _files(flowcell_id, lane):
247     """
248     Sets up available files for download
249     """
250     flowcell_id = flowcellIdStrip(flowcell_id)
251     d = get_flowcell_result_dict(flowcell_id)
252     
253     if d is None:
254         return ''
255     
256     output = []
257     
258     # c_name == 'CN-M' (i.e. C1-33)
259     for c_name in d:
260         
261         if d[c_name]['summary'] is not None:
262             output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
263                           % (flowcell_id, c_name, c_name))
264         
265         erd = d[c_name]['eland_results']
266         
267         if int(lane) in erd:
268             output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
269             output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
270     
271     if len(output) == 0:
272         return ''
273     
274     return '(' + '|'.join(output) + ')'
275