* Added option to use default return type by adding /ucsc/ at the end
[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 result_fc_cnm_eland_lane(request, fc_id, cnm, lane):
64     """
65     returns an eland_file upon calling.
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     erd = d[cnm]['eland_results']
77     lane = int(lane)
78     
79     if lane not in erd:
80         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.' % (fc_id, cnm, lane))
81     
82     filepath = erd[lane]
83     
84     f = opener.autoopen(filepath, 'r')
85     
86     return HttpResponse(f, mimetype="application/x-elandresult")
87
88
89 def bedfile_fc_cnm_eland_lane_ucsc(request, fc_id, cnm, lane):
90     """
91     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane (ucsc compatible)
92     """
93     return bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=True)
94
95
96 def bedfile_fc_cnm_eland_lane(request, fc_id, cnm, lane, ucsc_compatible=False):
97     """
98     returns a bed file for a given flowcell, CN-M (i.e. C1-33), and lane
99     """
100     fc_id = flowcellIdStrip(fc_id)
101     d = get_flowcell_result_dict(fc_id)
102     
103     if d is None:
104         return HttpResponse('<b>Results for Flowcell %s not found.' % (fc_id))
105     
106     if cnm not in d:
107         return HttpResponse('<b>Results for Flowcell %s; %s not found.' % (fc_id, cnm))
108     
109     erd = d[cnm]['eland_results']
110     lane = int(lane)
111     
112     if lane not in erd:
113         return HttpResponse('<b>Results for Flowcell %s; %s; lane %s not found.' % (fc_id, cnm, lane))
114     
115     filepath = erd[lane]
116     
117     # Eland result file
118     fi = opener.autoopen(filepath, 'r')
119     # output memory file
120     
121     
122     name, description = makebed.make_description(settings.DATABASE_NAME,
123                                                  fc_id,
124                                                  lane)
125     
126     bedgen = makebed.make_bed_from_eland_stream_generator(fi, name, description)
127     
128     if ucsc_compatible:
129         return HttpResponse(bedgen)
130     else:
131         return HttpResponse(bedgen, mimetype="application/x-bedfile")
132
133     
134 def _files(flowcell_id, lane):
135     """
136     Sets up available files for download
137     """
138     flowcell_id = flowcellIdStrip(flowcell_id)
139     d = get_flowcell_result_dict(flowcell_id)
140     
141     if d is None:
142         return ''
143     
144     output = []
145     
146     # c_name == 'CN-M' (i.e. C1-33)
147     for c_name in d:
148         erd = d[c_name]['eland_results']
149         
150         if int(lane) in erd:
151             output.append('<a href="/results/%s/%s/eland_result/%s">eland_result(%s)</a>' % (flowcell_id, c_name, lane, c_name))
152             output.append('<a href="/results/%s/%s/bedfile/%s">bedfile(%s)</a>' % (flowcell_id, c_name, lane, c_name))
153     
154     if len(output) == 0:
155         return ''
156     
157     return '(' + '|'.join(output) + ')'
158