/
PublicReports.pm
793 lines (631 loc) · 25.6 KB
/
PublicReports.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
package LIMS2::WebApp::Controller::PublicReports;
use Moose;
use LIMS2::Report;
use Try::Tiny;
use Data::Printer;
use LIMS2::Model::Util::EngSeqParams qw( generate_well_eng_seq_params );
use LIMS2::Model::Util::CrisprESQCView qw(crispr_damage_type_for_ep_pick);
use LIMS2::Model::Util::Crisprs qw( crisprs_for_design );
use List::MoreUtils qw( uniq );
use namespace::autoclean;
use feature 'switch';
BEGIN { extends 'Catalyst::Controller'; }
=head1 NAME
LIMS2::WebApp::Controller::PublicReports - Catalyst Controller
=head1 DESCRIPTION
Catalyst Controller for reports that a un-authenticated user can access.
=cut
=head2 index
List of public reports
=cut
sub index : Path( '/public_reports' ) : Args(0) {
my ( $self, $c ) = @_;
return;
}
=head2 cre_knockin_project_status
Report listing the status of cre knockin projects.
=cut
sub cre_knockin_project_status : Path( '/public_reports/cre_knockin_project_status' ) : Args(0) {
my ( $self, $c ) = @_;
my $report_id = LIMS2::Report::cached_report(
model => $c->model( 'Golgi' ),
report => 'LegacyCreKnockInProjects',
params => {},
);
$c->stash(
template => 'publicreports/await_report.tt',
report_name => 'Cre_KnockIn_Project_Status',
report_id => $report_id,
);
return;
}
=head2 download_report
Downloads a csv report of a given report_id
=cut
sub download_report :Path( '/public_reports/download' ) :Args(1) {
my ( $self, $c, $report_id ) = @_;
my ( $report_name, $report_fh ) = LIMS2::Report::read_report_from_disk( $report_id );
$c->response->status( 200 );
$c->response->content_type( 'text/csv' );
$c->response->header( 'Content-Disposition' => "attachment; filename=$report_name.csv" );
$c->response->body( $report_fh );
return;
}
=head2 download_compressed
Generates a gzipped file for download, and downloads it
=cut
sub download_compressed :Path( '/public_reports/download_compressed' ) :Args(1) {
my ( $self, $c, $report_id ) = @_;
my ( $report_name, $compressed_fh ) = LIMS2::Report::compress_report_on_disk( $report_id );
$c->response->status( 200 );
$c->response->content_type( 'text/gzip' );
# $c->response->content_encoding( 'gzip' );
$c->response->header( 'Content-Disposition' => "attachment; filename=$report_name.csv.gz" );
$c->response->body( $compressed_fh );
return;
}
=head2 cre_knockin_project_status
Report listing the status of cre knockin projects.
=cut
sub allele_dump : Path( '/public_reports/allele_dump' ) : Args(0) {
my ( $self, $c ) = @_;
my $report_id = LIMS2::Report::cached_report(
model => $c->model( 'Golgi' ),
report => 'AlleleDump',
params => {},
);
$c->stash(
template => 'publicreports/await_report.tt',
report_name => 'Allele_Dump',
report_id => $report_id
);
return;
}
=head2 index
=cut
sub sponsor_report :Path( '/public_reports/sponsor_report' ) {
my ( $self, $c, $targeting_type ) = @_;
my $species;
my $cache_param;
my $sub_cache_param;
my $top_cache_param;
# If logged in always use live top level report and cached sub_reports
# The cache_param refers to the sub_reports
if ( $c->request->params->{'generate_cache'} ){
$sub_cache_param = 'without_cache';
$top_cache_param = 'without_cache';
}
elsif ($c->user_exists) {
$c->request->params->{'species'} = $c->session->{'selected_species'};
if ( !$c->request->params->{'cache_param'} ) {
$sub_cache_param = 'with_cache';
$top_cache_param = 'without_cache';
}
else {
$sub_cache_param = $c->request->params->{'cache_param'};
$top_cache_param = 'without_cache';
}
}
else {
# not logged in - always use cached reports for top level and sub-reports
$sub_cache_param = 'with_cache';
$top_cache_param = 'with_cache';
}
if (!$c->request->params->{'species'}) {
$c->request->params->{'species'} = 'Human';
}
$species = $c->request->params->{'species'};
$c->session->{'selected_species'} = $species;
if ( defined $targeting_type ) {
# show report for the requested targeting type
$self->_generate_front_page_report ( $c, $targeting_type, $species, $sub_cache_param );
}
else {
# by default show the single_targeted report
if ( $top_cache_param eq 'with_cache' ) {
$self->_view_cached_lines( $c, lc( $species ) );
}
else {
$self->_generate_front_page_report ( $c, 'single_targeted', $species, $sub_cache_param );
}
}
if ( $top_cache_param eq 'without_cache' ) {
$c->stash(
template => 'publicreports/sponsor_report.tt',
);
}
return;
}
sub _generate_front_page_report {
my ( $self, $c, $targeting_type, $species, $cache_param ) = @_;
# Call ReportForSponsors plugin to generate report
my $sponsor_report = LIMS2::Model::Util::ReportForSponsors->new({
'species' => $species,
'model' => $c->model( 'Golgi' ),
'targeting_type' => $targeting_type,
});
my $report_params = $sponsor_report->generate_top_level_report_for_sponsors( );
# Fetch details from returned report parameters
my $report_id = $report_params->{ report_id };
my $title = $report_params->{ title };
my $columns = $report_params->{ columns };
my $rows = $report_params->{ rows };
my $data = $report_params->{ data };
# Store report values in stash for display onscreen
$c->stash(
'report_id' => $report_id,
'title' => $title,
'species' => $species,
'targeting_type' => $targeting_type,
'cache_param' => $cache_param,
'columns' => $columns,
'rows' => $rows,
'data' => $data,
);
return;
}
sub view_cached_csv : Path( '/public_reports/cached_sponsor_csv' ) : Args(1) {
my ( $self, $c, $sponsor_id ) = @_;
$sponsor_id =~ s/\ /_/g;
return $self->_view_cached_csv($c, $sponsor_id);
}
sub _view_cached_csv {
my $self = shift;
my $c = shift;
my $csv_name = shift;
my $server_path = $c->uri_for('/');
my $cache_server;
for ($server_path) {
if (/^http:\/\/www.sanger.ac.uk\/htgt\/lims2\/$/) { $cache_server = 'production/'; }
elsif (/http:\/\/www.sanger.ac.uk\/htgt\/lims2\/+staging\//) { $cache_server = 'staging/'; }
elsif (/http:\/\/t87-dev.internal.sanger.ac.uk:(\d+)\//) { $cache_server = "$1/"; }
else { die 'Error finding path for cached sponsor report'; }
}
my $cached_file_name = '/opt/t87/local/report_cache/lims2_cache_fp_report/' . $cache_server . $csv_name . '.csv';
$c->response->status( 200 );
$c->response->content_type( 'text/csv' );
$c->response->header( 'Content-Disposition' => "attachment; filename=$csv_name.csv" );
my @lines_out;
open( my $csv_handle, "<:encoding(UTF-8)", $cached_file_name )
or die "unable to open cached file ($cached_file_name): $!";
while (<$csv_handle>) {
if ( ! $c->user_exists ) {
$_ = _filter_public_attributes( $_ );
}
push @lines_out, $_;
}
close $csv_handle
or die "unable to close cached file: $!";
return $c->response->body( join( '', @lines_out ));
}
# some fields should not be present in the publicly available CSV file
sub _filter_public_attributes {
my $line = shift;
my $mod_line;
# match anything except the last comma separated field (the info field).
if ( $line =~ /^(.*),.*$/xgms ) {
$mod_line = $1;
}
# /^(.*),.*$/xgms
# ^ assert position at start of a line
# 1st Capturing group (.*)
# .* matches any character
# Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
# , matches the character , literally
# .* matches any character
# Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
# $ assert position at end of a line
# x modifier: extended. Spaces and text after a # in the pattern are ignored
# g modifier: global. All matches (don't return on first match)
# m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
# s modifier: single line. Dot matches newline characters
return $mod_line . "\n";
}
sub view : Path( '/public_reports/sponsor_report' ) : Args(3) {
my ( $self, $c, $targeting_type, $sponsor_id, $stage ) = @_;
# expecting :
# targeting type i.e. 'st' or 'dt' for single- or double-targeted
# sponsor id is the project sponsor e.g. Syboss, Pathogens
# stage is the level e.g. genes, DNA
# depending on combination of targeting type and stage fetch details
my $species = $c->session->{selected_species};
my $cache_param = $c->request->params->{'cache_param'};
if ($c->request->params->{generate_cache}) {
$c->session->{display_type} = 'wide';
} else {
$c->session->{display_type} = 'default';
}
# Call ReportForSponsors plugin to generate report
my $sponsor_report = LIMS2::Model::Util::ReportForSponsors->new( {
'species' => $species,
'model' => $c->model( 'Golgi' ),
'targeting_type' => $targeting_type,
});
my $report_params = $sponsor_report->generate_sub_report($sponsor_id, $stage);
# Fetch details from returned report parameters
my $report_id = $report_params->{ 'report_id' };
my $disp_target_type = $report_params->{ 'disp_target_type' };
my $disp_stage = $report_params->{ 'disp_stage' };
my $columns = $report_params->{ 'columns' };
my $display_columns = $report_params->{ 'display_columns' };
my $data = $report_params->{ 'data' };
my $link = "/public_reports/sponsor_report/$targeting_type/$sponsor_id/$stage";
my $type;
# csv download
if ($c->request->params->{csv}) {
$c->response->status( 200 );
$c->response->content_type( 'text/csv' );
$c->response->header( 'Content-Disposition' => 'attachment; filename=report.csv');
my $body = join(',', map { $_ } @{$display_columns}) . "\n";
my @csv_colums;
if (@{$columns}[-1] eq 'ep_data') {
@csv_colums = splice (@{$columns}, 0, -1);
} else {
@csv_colums = @{$columns};
}
foreach my $column ( @{$data} ) {
$body .= join(',', map { $column->{$_} } @csv_colums ) . "\n";
}
$c->response->body( $body );
}
else {
if ($disp_stage eq 'Genes') {
if (! $c->request->params->{type}) {
$c->request->params->{type} = 'simple';
}
$type = $c->request->params->{type};
if ($type eq 'simple') {
$data = $self->_simple_transform( $data );
}
}
my $template = 'publicreports/sponsor_sub_report.tt';
if ($sponsor_id eq 'Cre Knockin' || $sponsor_id eq 'EUCOMMTools Recovery' || $sponsor_id eq 'MGP Recovery' || $sponsor_id eq 'Pathogens' || $sponsor_id eq 'Syboss' || $sponsor_id eq 'Core' ) {
$template = 'publicreports/sponsor_sub_report_old.tt';
}
# Store report values in stash for display onscreen
$c->stash(
'template' => $template,
'report_id' => $report_id,
'disp_target_type' => $disp_target_type,
'disp_stage' => $disp_stage,
'sponsor_id' => $sponsor_id,
'columns' => $columns,
'display_columns' => $display_columns,
'data' => $data,
'link' => $link,
'type' => $type,
'species' => $species,
'cache_param' => $cache_param,
);
}
return;
}
sub _simple_transform {
my $self = shift;
my $data = shift;
foreach my $column ( @{$data} ) {
while ( my ($key, $value) = each %{$column} ) {
if (! ${$column}{$key}) {
${$column}{$key} = '';
}
else {
${$column}{$key} = '✔'
unless ($key eq 'gene_id'
|| $key eq 'gene_symbol'
|| $key eq 'sponsors'
|| $key eq 'ep_data'
|| $key eq 'recovery_class'
|| $key eq 'effort_concluded'
|| $key eq 'chromosome' );
}
}
}
return $data;
}
sub view_cached : Path( '/public_reports/cached_sponsor_report' ) : Args(1) {
my ( $self, $c, $report_name ) = @_;
$c->log->info( "Generate public detail report for : $report_name" );
return $self->_view_cached_lines($c, $report_name );
}
sub view_cached_simple : Path( '/public_reports/cached_sponsor_report_simple' ) : Args(1) {
my ( $self, $c, $report_name ) = @_;
$c->log->info( "Generate public detail report for : $report_name" );
return $self->_view_cached_lines($c, $report_name, 1 );
}
sub _view_cached_lines {
my $self = shift;
my $c = shift;
my $report_name = shift;
my $simple = shift;
my $server_path = $c->uri_for('/');
my $cache_server;
for ($server_path) {
if (/^http:\/\/www.sanger.ac.uk\/htgt\/lims2\/$/) { $cache_server = 'production/'; }
elsif (/http:\/\/www.sanger.ac.uk\/htgt\/lims2\/+staging\//) { $cache_server = 'staging/'; }
elsif (/http:\/\/t87-dev.internal.sanger.ac.uk:(\d+)\//) { $cache_server = "$1/"; }
else { die 'Error finding path for cached sponsor report'; }
}
my $suffix = '.html';
if ($simple) {$suffix = '_simple.html'}
$report_name =~ s/\ /_/g; # convert spaces to underscores in report name
my $cached_file_name = '/opt/t87/local/report_cache/lims2_cache_fp_report/' . $cache_server . $report_name . $suffix;
my @lines_out;
open( my $html_handle, "<:encoding(UTF-8)", $cached_file_name )
or die "unable to open cached file ($cached_file_name): $!";
while (<$html_handle>) {
push @lines_out, $_;
}
close $html_handle
or die "unable to close cached file: $!";
return $c->response->body( join( '', @lines_out ));
}
=head2 well_genotyping_info_search
Page to choose the desired well, no arguments
=cut
sub well_genotyping_info_search :Path( '/public_reports/well_genotyping_info_search' ) :Args(0) {
my ( $self, $c ) = @_;
return;
}
=head2 well_genotyping_info
Page to display chosen well, takes a well id (later a barcode) or a plate/well combo
=cut
sub well_genotyping_info :Path( '/public_reports/well_genotyping_info' ) :Args() {
my ( $self, $c, @args ) = @_;
if ( @args == 1 ) {
my $barcode = shift @args;
try {
$self->_stash_well_genotyping_info( $c, { barcode => $barcode } );
} catch {
$c->stash( error_msg => "$_" );
$c->go( 'well_genotyping_info_search' );
return;
};
}
elsif ( @args == 2 ) {
my ( $plate_name, $well_name ) = @args;
try {
$self->_stash_well_genotyping_info(
$c, { plate_name => $plate_name, well_name => $well_name }
);
} catch {
$c->stash( error_msg => "$_" );
$c->go( 'well_genotyping_info_search' );
return;
};
}
else {
$c->stash( error_msg => "Invalid number of arguments" );
}
return;
}
sub _stash_well_genotyping_info {
my ( $self, $c, $search ) = @_;
#well_id will become barcode
my $well;
try { $well = $c->model('Golgi')->retrieve_well( $search ) };
unless($well){
try{ $well = $c->model('Golgi')->retrieve_well_from_old_plate_version( $search ) };
if($well){
$c->stash->{info_msg} = ("Well ".$well->name." was not found on the current version of plate ".
$well->plate->name.". Reporting info for this well on version ".$well->plate->version
." of the plate.");
}
}
unless ( $well ) {
$c->stash( error_msg => "Well doesn't exist" );
return;
}
try {
#needs to be given a method for finding genes
my $gene_finder = sub { $c->model('Golgi')->find_genes( @_ ); };
my $data = $well->genotyping_info( $gene_finder );
if(my $ms_qc_data = $well->ms_qc_data($gene_finder) ){
$data->{ms_qc_data} = $ms_qc_data;
}
$data->{child_barcodes} = $well->distributable_child_barcodes;
my @crispr_data;
my @crisprs = $well->parent_crispr_wells;
foreach my $crispr_well ( @crisprs ) {
my $process_crispr = $crispr_well->process_output_wells->first->process->process_crispr;
if ( $process_crispr ) {
my $crispr_data_hash = $process_crispr->crispr->as_hash;
$crispr_data_hash->{crispr_well} = $crispr_well->as_string;
push @crispr_data, $crispr_data_hash;
}
}
my @crispr_pair_schema = $c->model('Golgi')->schema->resultset('CrisprPair')->search({ left_crispr_id => $crispr_data[0]->{id}})->all;
my $crispr_pair = $c->model('Golgi')->retrieve_crispr_pair( { id => $crispr_pair_schema[0]->{_column_data}->{id} } );
$crispr_pair = $crispr_pair->as_hash;
my $crispr_primers = $data->{primers}->{crispr_primers};
my $match;
foreach my $set (values %$crispr_primers)
{
foreach my $primer(@$set){
$match = search_primers($primer->{seq}, $crispr_pair->{crispr_primers});
$primer->{is_validated} = $match->{is_validated};
$primer->{is_rejected} = $match->{is_rejected};
}
}
$c->stash( data => $data, crispr_data => \@crispr_data);
}
catch {
#get string representation if its a lims2::exception
$c->stash( error_msg => ref $_ && $_->can('as_string') ? $_->as_string : $_ );
};
return;
}
sub search_primers {
my ($seq, $validation_primers) = @_;
foreach my $valid_primer(@$validation_primers)
{
if ($seq eq $valid_primer->{primer_seq}){
return $valid_primer;
}
}
return;
}
=head2 public_gene_report
Public gene report, only show targeted clone details:
- Summary of targeted clones:
- number of visible clones (accepted)
- number of each clone with type of crispr damage
- List of targeted clones
- Crispr qc alignment view
- First allele genbank file
- Crispr damage type
=cut
## no critic (Subroutines::ProhibitExcessComplexity)
sub public_gene_report :Path( '/public_reports/gene_report' ) :Args(1) {
my ( $self, $c, $gene_id ) = @_;
# by default type is Targeted, Distributable as an option
my $type = 'Targeted';
if ($c->request->param('type') eq 'distributable') {
$type = 'Distributable';
}
$c->log->info( "Generate public $type clone report page for gene: $gene_id" );
my $model = $c->model('Golgi');
my $species = $c->session->{selected_species} || 'Human';
# get the gene name/symbol
my $gene = $model->find_gene({
species => $species,
search_term => $gene_id
});
my $gene_symbol = $gene->{'gene_symbol'};
$gene_id = $gene->{'gene_id'};
# get the summary rows
my $design_summaries_rs = $model->schema->resultset('Summary')->search(
{
design_gene_id => $gene_id,
ep_pick_plate_name => { '!=', undef },
to_report => 't',
},
{ distinct => 1 }
);
# start colecting the clones data, starting by the targeted clones
my %clones;
while ( my $sr = $design_summaries_rs->next ) {
next if (!defined $sr->ep_pick_well_id || exists $clones{ $sr->ep_pick_well_id } );
my %ep_pick_data;
$ep_pick_data{id} = $sr->ep_pick_well_id;
$ep_pick_data{name} = $sr->ep_pick_plate_name . '_' . $sr->ep_pick_well_name;
$ep_pick_data{plate_name} = $sr->ep_pick_plate_name;
$ep_pick_data{well_name} = $sr->ep_pick_well_name;
$ep_pick_data{accepted} = $sr->ep_pick_well_accepted ? 'yes' : 'no';
$ep_pick_data{created_at} = $sr->ep_pick_well_created_ts->ymd;
# get crispr es qc data if available
my $well = $model->schema->resultset('Well')->find( { id => $sr->ep_pick_well_id } );
my $gene_finder = sub { $model->find_genes( @_ ) };
# grab data for alignment popup
try { $ep_pick_data{crispr_qc_data} = $well->genotyping_info( $gene_finder, 1 ) };
my $damage_type = crispr_damage_type_for_ep_pick($model,$sr->ep_pick_well_id);
$ep_pick_data{crispr_damage} = ( $damage_type ? $damage_type : '-' );
# save the clone if targeted clones, keep qc data for distributable clones
$clones{ $sr->ep_pick_well_id } = \%ep_pick_data unless ( $type eq 'Distributable' );
# no need to the distributable clones unless we want them
next unless ( $type eq 'Distributable' );
# get the PIQ clones
if ( defined $sr->piq_well_id && !exists $clones{ $sr->piq_well_id } ) {
my %data;
$data{id} = $sr->piq_well_id;
$data{name} = $sr->piq_plate_name . '_' . $sr->piq_well_name;
$data{plate_name} = $sr->piq_plate_name;
$data{well_name} = $sr->piq_well_name;
$data{accepted} = $sr->piq_well_accepted ? 'yes' : 'no';
$data{created_at} = $sr->piq_well_created_ts->ymd;
$data{crispr_damage} = $ep_pick_data{crispr_damage};
$data{crispr_qc_data} = $ep_pick_data{crispr_qc_data};
$clones{ $sr->piq_well_id } = \%data;
}
# get the ancestor PIQ clones
if ( defined $sr->ancestor_piq_well_id && !exists $clones{ $sr->ancestor_piq_well_id } ) {
my %data;
$data{id} = $sr->ancestor_piq_well_id;
$data{name} = $sr->ancestor_piq_plate_name . '_' . $sr->ancestor_piq_well_name;
$data{plate_name} = $sr->ancestor_piq_plate_name;
$data{well_name} = $sr->ancestor_piq_well_name;
$data{accepted} = $sr->ancestor_piq_well_accepted ? 'yes' : 'no';
$data{created_at} = $sr->ancestor_piq_well_created_ts->ymd;
$data{crispr_damage} = $ep_pick_data{crispr_damage};
$data{crispr_qc_data} = $ep_pick_data{crispr_qc_data};
$clones{ $sr->ancestor_piq_well_id } = \%data;
}
}
my @clones = sort _sort_by_damage_type values %clones;
my %summaries;
for my $tc ( @clones ) {
$summaries{genotyped}++ if ($tc->{crispr_damage} && ($tc->{crispr_damage} eq 'frameshift' ||
$tc->{crispr_damage} eq 'in-frame' || $tc->{crispr_damage} eq 'wild_type' || $tc->{crispr_damage} eq 'mosaic' ||
$tc->{crispr_damage} eq 'splice_acceptor' || $tc->{crispr_damage} eq 'splice_donor' || $tc->{crispr_damage} eq 'intron') );
$summaries{ $tc->{crispr_damage} }++ if ($tc->{crispr_damage} && $tc->{crispr_damage} ne 'unclassified');
}
$c->stash(
'gene_id' => $gene_id,
'gene_symbol' => $gene_symbol,
'summary' => \%summaries,
'clones' => \@clones,
'type' => $type,
);
return;
}
## use critic
sub _sort_by_damage_type{
my %crispr_damage_order = (
'frameshift' => 1,
'in-frame' => 2,
'wild_type' => 3,
'mosaic' => 4,
'no-call' => 5,
'-' => 6,
);
if ( !$a->{crispr_damage} ) {
return 1;
}
elsif ( !$b->{crispr_damage} ) {
return -1;
}
return $crispr_damage_order{$a->{crispr_damage}} <=> $crispr_damage_order{$b->{crispr_damage}};
}
=head2 well_eng_seq
Generate Genbank file for a well
=cut
sub well_eng_seq :Path( '/public_reports/well_eng_seq' ) :Args(1) {
my ( $self, $c, $well_id ) = @_;
my $model = $c->model('Golgi');
my $well = $model->retrieve_well( { id => $well_id } );
my $params = $c->request->params;
my ( $method, undef , $eng_seq_params ) = generate_well_eng_seq_params( $model, $params, $well );
my $eng_seq = $model->eng_seq_builder->$method( %{ $eng_seq_params } );
my $gene;
if ( my $design = $well->design ) {
my $gene_id = $design->genes->first->gene_id;
my $gene_data = try { $model->retrieve_gene( { species => $design->species_id, search_term => $gene_id } ) };
$gene = $gene_data ? $gene_data->{gene_symbol} : $gene_id;
}
my $stage = $method =~ /vector/ ? 'vector' : 'allele';
my $file_name = $well->as_string . "_$stage";
$file_name .= "_$gene" if $gene;
my $file_format = exists $params->{file_format} ? $params->{file_format} : 'Genbank';
$self->download_genbank_file( $c, $eng_seq, $file_name, $file_format );
return;
}
sub download_genbank_file {
my ( $self, $c, $eng_seq, $file_name, $file_format ) = @_;
$file_format ||= 'Genbank';
my $suffix = $file_format eq 'Genbank' ? 'gbk' : 'fa';
my $formatted_seq;
Bio::SeqIO->new(
-fh => IO::String->new($formatted_seq),
-format => $file_format,
)->write_seq($eng_seq);
$file_name = $file_name . ".$suffix";
$c->response->content_type( 'chemical/seq-na-genbank' );
$c->response->header( 'Content-Disposition' => "attachment; filename=$file_name" );
$c->response->body( $formatted_seq );
return;
}
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
__PACKAGE__->meta->make_immutable;
1;