-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElectroporationProductionSummary.pm
180 lines (151 loc) · 5.92 KB
/
ElectroporationProductionSummary.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
package LIMS2::Report::ElectroporationProductionSummary;
use Moose;
use DateTime;
use DateTime::Format::ISO8601;
use DateTime::Format::Strptime;
use LIMS2::Report::FirstElectroporationProductionDetail;
use LIMS2::Report::SecondElectroporationProductionDetail;
use Iterator::Simple qw( iter );
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 'Electroporation Production Summary ' . $append;
};
override _build_columns => sub {
return [
"Month",
"First Allele Electroporated",
"First Allele Picked",
"First Allele Accepted",
"Cumulative First Allele Electroporated",
"Cumulative First Allele Picked",
"Cumulative First Allele Accepted",
"Second Allele (Promoter) Electroporated",
"Second Allele (Promoter) Picked",
"Second Allele (Promoter) Accepted",
"Cumulative Second Allele (Promoter) Electroporated",
"Cumulative Second Allele (Promoter) Picked",
"Cumulative Second Allele (Promoter) Accepted",
"Second Allele (Promoterless) Electroporated",
"Second Allele (Promoterless) Picked",
"Second Allele (Promoterless) Accepted",
"Cumulative Second Allele (Promoterless) Electroporated",
"Cumulative Second Allele (Promoterless) Picked",
"Cumulative Second Allele (Promoterless) Accepted",
];
};
override iterator => sub {
my ( $self ) = @_;
my $date_formatter = DateTime::Format::Strptime->new( pattern => '%b %Y' );
my %by_month;
$self->summarize_first_ep( \%by_month );
$self->summarize_second_ep( \%by_month );
my @months = sort { $by_month{$a}{dt} <=> $by_month{$b}{dt} } keys %by_month;
my %cumulative;
return iter sub {
my $month = shift @months
or return;
for my $k ( qw( first_ep_created first_ep_picked first_ep_accepted
second_ep_promoter_created second_ep_promoter_picked second_ep_promoter_accepted
second_ep_promoterless_created second_ep_promoterless_picked second_ep_promoterless_accepted
) ) {
for my $gene ( keys %{ $by_month{$month}{$k} } ) {
$cumulative{$k}{$gene}++;
}
}
return [
$date_formatter->format_datetime( $by_month{$month}{dt} ),
map { scalar keys %{$_} } (
@{$by_month{$month}}{ qw( first_ep_created first_ep_picked first_ep_accepted ) },
@cumulative{ qw( first_ep_created first_ep_picked first_ep_accepted ) },
@{$by_month{$month}}{ qw( second_ep_promoter_created second_ep_promoter_picked second_ep_promoter_accepted ) },
@cumulative{ qw( second_ep_promoter_created second_ep_promoter_picked second_ep_promoter_accepted ) },
@{$by_month{$month}}{ qw( second_ep_promoterless_created second_ep_promoterless_picked second_ep_promoterless_accepted ) },
@cumulative{ qw( second_ep_promoterless_created second_ep_promoterless_picked second_ep_promoterless_accepted ) }
)
]
};
};
sub summarize_first_ep {
my ( $self, $by_month ) = @_;
my $detail;
if ( $self->has_sponsor ){
$detail = LIMS2::Report::FirstElectroporationProductionDetail->new(
model => $self->model, species => $self->species, sponsor => $self->sponsor
);
}
else{
$detail = LIMS2::Report::FirstElectroporationProductionDetail->new(
model => $self->model, species => $self->species
);
}
my @detail_cols = @{ $detail->columns };
my $it = $detail->iterator;
while ( my $row = $it->next ) {
my %h;
@h{@detail_cols} = @{$row};
my $created_month = DateTime::Format::ISO8601->parse_datetime( $h{'Created At'} )->truncate( to => 'month' );
my $gene_id = $h{'Gene Id'};
$by_month->{$created_month}{dt} ||= $created_month;
$by_month->{$created_month}{first_ep_created}{$gene_id}++;
if ( $h{'Number Picked'} ) {
$by_month->{$created_month}{first_ep_picked}{$gene_id}++;
}
if ( $h{'Number Accepted'} ) {
$by_month->{$created_month}{first_ep_accepted}{$gene_id}++;
}
}
return;
}
sub summarize_second_ep {
my ( $self, $by_month ) = @_;
my $detail;
if ( $self->has_sponsor ) {
$detail = LIMS2::Report::SecondElectroporationProductionDetail->new(
model => $self->model, species => $self->species, sponsor => $self->sponsor
);
}
else {
$detail = LIMS2::Report::SecondElectroporationProductionDetail->new(
model => $self->model, species => $self->species
);
}
my @detail_cols = @{ $detail->columns };
my $it = $detail->iterator;
while ( my $row = $it->next ) {
my %h;
@h{@detail_cols} = @{$row};
my $created_month = DateTime::Format::ISO8601->parse_datetime( $h{'Created At'} )->truncate( to => 'month' );
my $gene_id = $h{'Second Allele Gene Id'};
$by_month->{$created_month}{dt} ||= $created_month;
my $type = 'second_ep_' . $h{'Second Allele Cassette Type'};
$by_month->{$created_month}{ $type . '_created' }{$gene_id}++;
if ( $h{'Number Picked'} ) {
$by_month->{$created_month}{ $type . '_picked' }{$gene_id}++;
}
if ( $h{'Number Accepted'} ) {
$by_month->{$created_month}{ $type . '_accepted' }{$gene_id}++;
}
}
return;
}
__PACKAGE__->meta->make_immutable;
1;
__END__