-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorProductionSummary.pm
156 lines (123 loc) · 4.67 KB
/
VectorProductionSummary.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
package LIMS2::Report::VectorProductionSummary;
use Moose;
use DateTime;
use DateTime::Format::ISO8601;
use DateTime::Format::Strptime;
use LIMS2::Report::VectorProductionDetail;
use List::MoreUtils qw( any );
use namespace::autoclean;
extends qw( LIMS2::ReportGenerator );
has species => (
is => 'ro',
isa => 'Str',
required => 1
);
has sponsor => (
is => 'ro',
isa => 'Str',
predicate => 'has_sponsor'
);
has '+param_names' => (
default => sub { [ 'species', 'sponsor' ] }
);
override _build_name => sub {
my $self = shift;
my $dt = DateTime->now();
my $append = $self->has_sponsor ? ' - Sponsor ' . $self->sponsor . ' ' : '';
$append .= $dt->ymd;
return 'Vector Production Summary ' . $append;
};
override _build_columns => sub {
return [
"Month",
"First Allele Created", "First Allele Accepted", "First Allele Efficiency",
"Cumulative First Allele Created", "Cumulative First Allele Accepted", "Cumulative First Allele Efficiency",
"Second Allele (Promoter) Created", "Second Allele (Promoter) Accepted", "Second Allele (Promoter) Efficiency",
"Cumulative Second Allele (Promoter) Created", "Cumulative Second Allele (Promoter) Accepted", "Cumulative Second Allele (Promoter) Efficiency",
"Second Allele (Promoterless) Created", "Second Allele (Promoterless) Accepted", "Second Allele (Promoterless) Efficiency",
"Cumulative Second Allele (Promoterless) Created", "Cumulative Second Allele (Promoterless) Accepted", "Cumulative Second Allele (Promoterless) Efficiency",
];
};
override iterator => sub {
my ( $self ) = @_;
my $date_formatter = DateTime::Format::Strptime->new( pattern => '%b %Y' );
my $detail;
if ( $self->has_sponsor ) {
$detail = LIMS2::Report::VectorProductionDetail->new( model => $self->model, species => $self->species, sponsor => $self->sponsor );
}
else{
$detail = LIMS2::Report::VectorProductionDetail->new( model => $self->model, species => $self->species );
}
my @detail_cols = @{ $detail->columns };
my @vectors;
my $it = $detail->iterator;
while ( my $row = $it->next ) {
my %h;
@h{@detail_cols} = @{$row};
push @vectors, {
created_month => DateTime::Format::ISO8601->parse_datetime( $h{'Created At'} )->truncate( to => 'month' ),
gene_id => $h{'Gene Id'},
allele_type => $self->allele_type_for( \%h ),
accepted => ( $h{'Accepted?'} eq 'yes' ? 1 : 0 )
};
}
@vectors = sort { $a->{created_month} <=> $b->{created_month} } @vectors;
my %cumulative;
return Iterator::Simple::iter sub {
my $vector = shift @vectors
or return;
my $month = $vector->{created_month};
my %this_month;
while ( $vector and $vector->{created_month} == $month ) {
$this_month{ $vector->{allele_type} }{created}{ $vector->{gene_id} }++;
$cumulative{ $vector->{allele_type} }{created}{ $vector->{gene_id} }++;
if ( $vector->{accepted} ) {
$this_month{ $vector->{allele_type} }{accepted}{ $vector->{gene_id} }++;
$cumulative{ $vector->{allele_type} }{accepted}{ $vector->{gene_id} }++;
}
$vector = shift @vectors;
}
return [
$date_formatter->format_datetime( $month ),
map {
( $self->counts_and_efficiency( \%this_month, $_ ), $self->counts_and_efficiency( \%cumulative, $_ ) )
} qw( first_allele second_allele_promoter second_allele_promoterless )
];
}
};
sub allele_type_for {
my ( $self, $data ) = @_;
if ( $self->is_second_allele( $data ) ) {
if ( $self->is_promoter( $data ) ) {
return 'second_allele_promoter';
}
else {
return 'second_allele_promoterless';
}
}
else {
return 'first_allele';
}
}
sub is_promoter {
my ( $self, $data ) = @_;
return $data->{'Cassette Type'} eq 'promoter';
}
sub is_second_allele {
my ( $self, $data ) = @_;
return $data->{Recombinases} =~ m/\bCre\b/;
}
sub count_for {
my ( $self, $data, $allele_type, $status ) = @_;
return scalar keys %{ $data->{$allele_type}->{$status} || {} };
}
sub counts_and_efficiency {
my ( $self, $data, $allele_type ) = @_;
my $created = $self->count_for( $data, $allele_type, 'created' );
my $accepted = $self->count_for( $data, $allele_type, 'accepted' );
my $efficiency = $created > 0 ? int( $accepted * 100 / $created ) . '%' : '-';
return( $created, $accepted, $efficiency );
}
__PACKAGE__->meta->make_immutable;
1;
__END__