0d2eecb4d998ce6944210b651be33ce9a338c9f5
[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_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         for summary in summary_list:
89             output.append(summary.replace('\n', '<br />\n'))
90     html = t.render(Context({'data_dict_list': data_dict_list}))
91     output.append('<br />')
92     output.append('<br />')
93     output.append(html)
94     output.append('<br />')
95     output.append('<br />')
96     
97     if record_count == 0:
98         output.append("None Found")
99     
100     return HttpResponse('<br />\n'.join(output))
101
102
103 def summaryhtm_fc_cnm(request, fc_id, cnm):
104     """
105     returns a Summary.htm file if it exists.
106     """
107     fc_id = flowcellIdStrip(fc_id)
108     d = get_flowcell_result_dict(fc_id)
109     
110     if d is None:
111         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
112     
113     if cnm not in d:
114         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
115     
116     summary_filepath = d[cnm]['summary']
117     
118     if summary_filepath is None:
119         return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
120     
121     f = open(summary_filepath, 'r')
122     
123     return HttpResponse(f)
124
125
126 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
127     """
128     returns an eland_file upon calling.
129     """
130     fc_id = flowcellIdStrip(fc_id)
131     d = get_flowcell_result_dict(fc_id)
132     
133     if d is None:
134         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
135     
136     if cnm not in d:
137         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
138     
139     erd = d[cnm]['eland_results']
140     lane = int(lane)
141     
142     if lane not in erd:
143         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
144     
145     filepath = erd[lane]
146     
147     f = opener.autoopen(filepath, 'r')
148     
149     return HttpResponse(f, mimetype="application/x-elandresult")
150
151
152 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
153     """
154     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
155     """
156     return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
157
158
159 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
160     """
161     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
162     """
163     fc_id = flowcellIdStrip(fc_id)
164     d = get_flowcell_result_dict(fc_id)
165     
166     if d is None:
167         return HttpResponse('<b>Results for Flowcell %s not found.</b>' % (fc_id))
168     
169     if cnm not in d:
170         return HttpResponse('<b>Results for Flowcell %s; %s not found.</b>' % (fc_id, cnm))
171     
172     erd = d[cnm]['eland_results']
173     lane = int(lane)
174     
175     if lane not in erd:
176         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.</b>' % (fc_id, cnm, lane))
177     
178     filepath = erd[lane]
179     
180     # Eland result file
181     fi = opener.autoopen(filepath, 'r')
182     # output memory file
183     
184     
185     name, description = makebed.make_description(settings.DATABASE_NAME,
186                                                  fc_id,
187                                                  lane)
188     
189     bedgen = makebed.make_bed_from_eland_stream_generator(fi, name, description)
190     
191     if ucsc_compatible:
192         return HttpResponse(bedgen)
193     else:
194         return HttpResponse(bedgen, mimetype="application/x-bedfile")
195
196
197 def _summary_stats(flowcell_id, lane):
198     """
199     return a dictionary of summary stats for a given flowcell_id & lane.
200     """
201     fc_id = flowcellIdStrip(flowcell_id)
202     fc_result_dict = get_flowcell_result_dict(fc_id)
203     
204     dict_list = []
205     err_list = []
206     summary_list = []
207     
208     if fc_result_dict is None:
209         err_list.append('Results for Flowcell %s not found.' % (fc_id))
210         return (dict_list, err_list, summary_list)
211     
212     for cnm in fc_result_dict:
213     
214         xmlpath = fc_result_dict[cnm]['run_xml']
215         
216         if xmlpath is None:
217             err_list.append('Run xml for Flowcell %s(%s) not found.' % (fc_id, cnm))
218             continue
219         
220         tree = ElementTree.parse(xmlpath).getroot()
221         results = runfolder.PipelineRun(pathname='', xml=tree)
222         try:
223             summary_list.append(runfolder.summary_report([results]))
224         except:
225             summary_list.append("Summary report needs to be updated.")
226         
227         lane_results = results.gerald.summary[0][lane]
228         lrs = lane_results
229         
230         d = {}
231         
232         d['average_alignment_score'] = lrs.average_alignment_score
233         d['average_first_cycle_intensity'] = lrs.average_first_cycle_intensity
234         d['cluster'] = lrs.cluster
235         d['lane'] = lrs.lane
236         d['flowcell'] = flowcell_id
237         d['cnm'] = cnm
238         d['percent_error_rate'] = lrs.percent_error_rate
239         d['percent_intensity_after_20_cycles'] = lrs.percent_intensity_after_20_cycles
240         d['percent_pass_filter_align'] = lrs.percent_pass_filter_align
241         d['percent_pass_filter_clusters'] = lrs.percent_pass_filter_clusters
242         
243         #FIXME: function finished, but need to take advantage of
244         #   may need to take in a list of lanes so we only have to
245         #   load the xml file once per flowcell rather than once
246         #   per lane.
247         dict_list.append(d)
248     
249     return (dict_list, err_list, summary_list)
250     
251     
252
253     
254 def _files(flowcell_id, lane):
255     """
256     Sets up available files for download
257     """
258     flowcell_id = flowcellIdStrip(flowcell_id)
259     d = get_flowcell_result_dict(flowcell_id)
260     
261     if d is None:
262         return ''
263     
264     output = []
265     
266     # c_name == 'CN-M' (i.e. C1-33)
267     for c_name in d:
268         
269         if d[c_name]['summary'] is not None:
270             output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
271                           % (flowcell_id, c_name, c_name))
272         
273         erd = d[c_name]['eland_results']
274         
275         if int(lane) in erd:
276             output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
277             output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
278     
279     if len(output) == 0:
280         return ''
281     
282     return '(' + '|'.join(output) + ')'
283