de7c6c85e7bc73e53167f956d6b5e7cfb85996eb
[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.frontend import settings
5 from gaworkflow.util import makebed
6 from gaworkflow.util import opener
7 from django.http import HttpResponse
8
9 import StringIO
10
11 #from django.db.models import base 
12
13 def library(request):
14     library_list = Library.objects.all() #.order_by('-pub_date')
15     rep_string = '<a href="/library/%s/">%s - %s (%s)</a>'
16     output = '<br />\n'.join([rep_string \
17       % (l.library_id,
18          l.library_id,
19          l.library_name,
20          l.library_species.scientific_name) for l in library_list])
21     return HttpResponse(output)
22
23 def library_to_flowcells(request, lib_id):
24     
25     try:
26       lib = Library.objects.get(library_id=lib_id)
27     except:
28       return HttpResponse("Library %s does not exist" % (lib_id))
29     
30     output = []
31     
32     output.append('<b>Library ID:</b> %s' % (lib.library_id))
33     output.append('<b>Name:</b> %s' % (lib.library_name))
34     output.append('<b>Species:</b> %s' % (lib.library_species.scientific_name))
35     output.append('')
36     
37     output.append('<b>FLOWCELL - LANE:</b>')
38     
39     output.extend([ '%s - Lane 1 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 1)) for fc in lib.lane_1_library.all() ])
40     output.extend([ '%s - Lane 2 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 2)) for fc in lib.lane_2_library.all() ])
41     output.extend([ '%s - Lane 3 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 3)) for fc in lib.lane_3_library.all() ])
42     output.extend([ '%s - Lane 4 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 4)) for fc in lib.lane_4_library.all() ])
43     output.extend([ '%s - Lane 5 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 5)) for fc in lib.lane_5_library.all() ])
44     output.extend([ '%s - Lane 6 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 6)) for fc in lib.lane_6_library.all() ])
45     output.extend([ '%s - Lane 7 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 7)) for fc in lib.lane_7_library.all() ])
46     output.extend([ '%s - Lane 8 %s' % (fc.flowcell_id, _files(fc.flowcell_id, 8)) for fc in lib.lane_8_library.all() ])
47     
48     record_count = lib.lane_1_library.count() + \
49                     lib.lane_2_library.count() + \
50                     lib.lane_3_library.count() + \
51                     lib.lane_4_library.count() + \
52                     lib.lane_5_library.count() + \
53                     lib.lane_6_library.count() + \
54                     lib.lane_7_library.count() + \
55                     lib.lane_8_library.count()
56     
57     if record_count == 0:
58         output.append("None Found")
59     
60     return HttpResponse('<br />\n'.join(output))
61
62
63 def summaryhtm_fc_cnm(request, fc_id, cnm):
64     """
65     returns a Summary.htm file if it exists.
66     """
67     fc_id = flowcellIdStrip(fc_id)
68     d = get_flowcell_result_dict(fc_id)
69     
70     if d is None:
71         return HttpResponse('<b>Results for Flowcell %s not found.' % (fc_id))
72     
73     if cnm not in d:
74         return HttpResponse('<b>Results for Flowcell %s; %s not found.' % (fc_id, cnm))
75     
76     summary_filepath = d[cnm]['summary']
77     
78     if summary_filepath is None:
79         return HttpResponse('<b>Summary.htm for Flowcell %s; %s not found.' % (fc_id, cnm))
80     
81     f = open(summary_filepath, 'r')
82     
83     return HttpResponse(f)
84
85
86 def result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
87     """
88     returns an eland_file upon calling.
89     """
90     fc_id = flowcellIdStrip(fc_id)
91     d = get_flowcell_result_dict(fc_id)
92     
93     if d is None:
94         return HttpResponse('<b>Results for Flowcell %s not found.' % (fc_id))
95     
96     if cnm not in d:
97         return HttpResponse('<b>Results for Flowcell %s; %s not found.' % (fc_id, cnm))
98     
99     erd = d[cnm]['eland_results']
100     lane = int(lane)
101     
102     if lane not in erd:
103         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.' % (fc_id, cnm, lane))
104     
105     filepath = erd[lane]
106     
107     f = opener.autoopen(filepath, 'r')
108     
109     return HttpResponse(f, mimetype="application/x-elandresult")
110
111
112 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
113     """
114     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
115     """
116     return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
117
118
119 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
120     """
121     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
122     """
123     fc_id = flowcellIdStrip(fc_id)
124     d = get_flowcell_result_dict(fc_id)
125     
126     if d is None:
127         return HttpResponse('<b>Results for Flowcell %s not found.' % (fc_id))
128     
129     if cnm not in d:
130         return HttpResponse('<b>Results for Flowcell %s; %s not found.' % (fc_id, cnm))
131     
132     erd = d[cnm]['eland_results']
133     lane = int(lane)
134     
135     if lane not in erd:
136         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.' % (fc_id, cnm, lane))
137     
138     filepath = erd[lane]
139     
140     # Eland result file
141     fi = opener.autoopen(filepath, 'r')
142     # output memory file
143     
144     
145     name, description = makebed.make_description(settings.DATABASE_NAME,
146                                                  fc_id,
147                                                  lane)
148     
149     bedgen = makebed.make_bed_from_eland_stream_generator(fi, name, description)
150     
151     if ucsc_compatible:
152         return HttpResponse(bedgen)
153     else:
154         return HttpResponse(bedgen, mimetype="application/x-bedfile")
155
156     
157 def _files(flowcell_id, lane):
158     """
159     Sets up available files for download
160     """
161     flowcell_id = flowcellIdStrip(flowcell_id)
162     d = get_flowcell_result_dict(flowcell_id)
163     
164     if d is None:
165         return ''
166     
167     output = []
168     
169     # c_name == 'CN-M' (i.e. C1-33)
170     for c_name in d:
171         
172         if d[c_name]['summary'] is not None:
173             output.append('<a href="/results/%s/%s/summary/">summary(%s)</a>' \
174                           % (flowcell_id, c_name, c_name))
175         
176         erd = d[c_name]['eland_results']
177         
178         if int(lane) in erd:
179             output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
180             output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
181     
182     if len(output) == 0:
183         return ''
184     
185     return '(' + '|'.join(output) + ')'
186