line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Sandy::DB::Handle::Expression; |
2
|
|
|
|
|
|
|
# ABSTRACT: Class to handle expression-matrix database schemas. |
3
|
|
|
|
|
|
|
|
4
|
6
|
|
|
6
|
|
58
|
use App::Sandy::Base 'class'; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
77
|
|
5
|
6
|
|
|
6
|
|
2718
|
use App::Sandy::DB; |
|
6
|
|
|
|
|
1298
|
|
|
6
|
|
|
|
|
324
|
|
6
|
6
|
|
|
6
|
|
3387
|
use IO::Compress::Gzip 'gzip'; |
|
6
|
|
|
|
|
174070
|
|
|
6
|
|
|
|
|
459
|
|
7
|
6
|
|
|
6
|
|
3078
|
use IO::Uncompress::Gunzip 'gunzip'; |
|
6
|
|
|
|
|
78687
|
|
|
6
|
|
|
|
|
394
|
|
8
|
6
|
|
|
6
|
|
48
|
use Storable qw/nfreeze thaw/; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
303
|
|
9
|
6
|
|
|
6
|
|
57
|
use Scalar::Util 'looks_like_number'; |
|
6
|
|
|
|
|
70
|
|
|
6
|
|
|
|
|
8721
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'App::Sandy::Role::IO'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.24'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub insertdb { |
16
|
0
|
|
|
0
|
0
|
|
my ($self, $file, $name, $source, $is_user_provided) = @_; |
17
|
0
|
|
|
|
|
|
my $schema = App::Sandy::DB->schema; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
log_msg ":: Checking if there is already an expression-matrix '$name' ..."; |
20
|
0
|
|
|
|
|
|
my $rs = $schema->resultset('ExpressionMatrix')->find({ name => $name }); |
21
|
0
|
0
|
|
|
|
|
if ($rs) { |
22
|
0
|
|
|
|
|
|
die "There is already an expression-matrix '$name'\n"; |
23
|
|
|
|
|
|
|
} else { |
24
|
0
|
|
|
|
|
|
log_msg ":: expression-matrix '$name' not found"; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
log_msg ":: Indexing '$file' ..."; |
28
|
0
|
|
|
|
|
|
my $indexed_file = $self->_index_expression_matrix($file); |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
log_msg ":: Converting data to bytes ..."; |
31
|
0
|
|
|
|
|
|
my $bytes = nfreeze $indexed_file; |
32
|
0
|
|
|
|
|
|
log_msg ":: Compressing bytes ..."; |
33
|
0
|
|
|
|
|
|
gzip \$bytes => \my $compressed; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Begin transation |
36
|
0
|
|
|
|
|
|
my $guard = $schema->txn_scope_guard; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
log_msg ":: Storing expression-matrix '$name'..."; |
39
|
0
|
|
|
|
|
|
$rs = $schema->resultset('ExpressionMatrix')->create({ |
40
|
|
|
|
|
|
|
name => $name, |
41
|
|
|
|
|
|
|
source => $source, |
42
|
|
|
|
|
|
|
is_user_provided => $is_user_provided, |
43
|
|
|
|
|
|
|
matrix => $compressed |
44
|
|
|
|
|
|
|
}); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# End transation |
47
|
0
|
|
|
|
|
|
$guard->commit; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _index_expression_matrix { |
51
|
0
|
|
|
0
|
|
|
my ($self, $file) = @_; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my $fh = $self->with_open_r($file); |
54
|
0
|
|
|
|
|
|
my %indexed_file; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $line = 0; |
57
|
0
|
|
|
|
|
|
while (<$fh>) { |
58
|
0
|
|
|
|
|
|
$line++; |
59
|
0
|
|
|
|
|
|
chomp; |
60
|
0
|
0
|
|
|
|
|
next if /^\s*$/; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my @fields = split; |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
die "Error parsing expression-matrix '$file': Seqid (first column) not found at line $line\n" |
65
|
|
|
|
|
|
|
unless defined $fields[0]; |
66
|
0
|
0
|
|
|
|
|
die "Error parsing expression-matrix '$file': Count (second column) not found at line $line\n" |
67
|
|
|
|
|
|
|
unless defined $fields[1]; |
68
|
0
|
0
|
|
|
|
|
die "Error parsing expression-matrix '$file': Count (second column) does not look like a number at line $line\n" |
69
|
|
|
|
|
|
|
if not looks_like_number($fields[1]); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Only throws a warning, because it is common zero values in expression matrix |
72
|
0
|
0
|
|
|
|
|
if ($fields[1] <= 0) { |
73
|
0
|
|
|
|
|
|
log_msg ":: Parsing expression-matrix '$file': Ignoring seqid '$fields[0]': Count (second column) lesser or equal to zero at line $line\n"; |
74
|
0
|
|
|
|
|
|
next; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$indexed_file{$fields[0]} = $fields[1]; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
unless (%indexed_file) { |
81
|
0
|
|
|
|
|
|
die "Error parsing expression-matrix '$file': Maybe the file is empty\n" |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$fh->close |
85
|
0
|
0
|
|
|
|
|
or die "Cannot close expression-matrix $file: $!\n"; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return \%indexed_file; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub retrievedb { |
91
|
0
|
|
|
0
|
0
|
|
my ($self, $expression_matrix) = @_; |
92
|
0
|
|
|
|
|
|
my $schema = App::Sandy::DB->schema; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
my $rs = $schema->resultset('ExpressionMatrix')->find({ name => $expression_matrix }); |
95
|
0
|
0
|
|
|
|
|
die "'$expression_matrix' not found into database\n" unless defined $rs; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my $compressed = $rs->matrix; |
98
|
0
|
0
|
|
|
|
|
die "expression-matrix entry '$expression_matrix' exists, but the related data is missing\n" |
99
|
|
|
|
|
|
|
unless defined $compressed; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
gunzip \$compressed => \my $bytes; |
102
|
0
|
|
|
|
|
|
my $matrix = thaw $bytes; |
103
|
0
|
|
|
|
|
|
return $matrix; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub deletedb { |
107
|
0
|
|
|
0
|
0
|
|
my ($self, $expression_matrix) = @_; |
108
|
0
|
|
|
|
|
|
my $schema = App::Sandy::DB->schema; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
log_msg ":: Checking if there is already an expression-matrix '$expression_matrix' ..."; |
111
|
0
|
|
|
|
|
|
my $rs = $schema->resultset('ExpressionMatrix')->find({ name => $expression_matrix }); |
112
|
0
|
0
|
|
|
|
|
die "'$expression_matrix' not found into database\n" unless defined $rs; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
log_msg ":: Found '$expression_matrix'"; |
115
|
0
|
0
|
|
|
|
|
die "'$expression_matrix' is not a user provided entry. Cannot be deleted\n" |
116
|
|
|
|
|
|
|
unless $rs->is_user_provided; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
log_msg ":: Removing '$expression_matrix' entry ..."; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Begin transation |
121
|
0
|
|
|
|
|
|
my $guard = $schema->txn_scope_guard; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
$rs->delete; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# End transation |
126
|
0
|
|
|
|
|
|
$guard->commit; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub restoredb { |
130
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
131
|
0
|
|
|
|
|
|
my $schema = App::Sandy::DB->schema; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
log_msg ":: Searching for user-provided entries ..."; |
134
|
0
|
|
|
|
|
|
my $user_provided = $schema->resultset('ExpressionMatrix')->search( |
135
|
|
|
|
|
|
|
{ is_user_provided => 1 } |
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my $entry = $user_provided->next; |
139
|
0
|
0
|
|
|
|
|
if ($entry) { |
140
|
0
|
|
|
|
|
|
log_msg ":: Found:"; |
141
|
0
|
|
|
|
|
|
do { |
142
|
0
|
|
|
|
|
|
log_msg ' ==> ' . $entry->name; |
143
|
|
|
|
|
|
|
} while ($entry = $user_provided->next); |
144
|
|
|
|
|
|
|
} else { |
145
|
0
|
|
|
|
|
|
die "Not found user-provided entries. There is no need to restoring\n"; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
log_msg ":: Removing all user-provided entries ..."; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Begin transation |
151
|
0
|
|
|
|
|
|
my $guard = $schema->txn_scope_guard; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
$user_provided->delete; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# End transation |
156
|
0
|
|
|
|
|
|
$guard->commit; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub make_report { |
160
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
161
|
0
|
|
|
|
|
|
my $schema = App::Sandy::DB->schema; |
162
|
0
|
|
|
|
|
|
my %report; |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
my $rs = $schema->resultset('ExpressionMatrix')->search(undef); |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
while (my $expression_matrix = $rs->next) { |
167
|
0
|
0
|
|
|
|
|
my %hash = ( |
168
|
|
|
|
|
|
|
source => $expression_matrix->source, |
169
|
|
|
|
|
|
|
provider => $expression_matrix->is_user_provided ? "user" : "vendor", |
170
|
|
|
|
|
|
|
date => $expression_matrix->date |
171
|
|
|
|
|
|
|
); |
172
|
0
|
|
|
|
|
|
$report{$expression_matrix->name} = \%hash; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
return \%report; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
__END__ |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=pod |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=encoding UTF-8 |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 NAME |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
App::Sandy::DB::Handle::Expression - Class to handle expression-matrix database schemas. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 VERSION |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
version 0.24 |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 AUTHORS |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=over 4 |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item * |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Thiago L. A. Miller <tmiller@mochsl.org.br> |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item * |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
J. Leonel Buzzo <lbuzzo@mochsl.org.br> |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=item * |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Felipe R. C. dos Santos <fsantos@mochsl.org.br> |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item * |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Helena B. Conceição <hconceicao@mochsl.org.br> |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item * |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Rodrigo Barreiro <rbarreiro@mochsl.org.br> |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item * |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Gabriela Guardia <gguardia@mochsl.org.br> |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item * |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Fernanda Orpinelli <forpinelli@mochsl.org.br> |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item * |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Rafael Mercuri <rmercuri@mochsl.org.br> |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item * |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Rodrigo Barreiro <rbarreiro@mochsl.org.br> |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item * |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Pedro A. F. Galante <pgalante@mochsl.org.br> |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=back |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
This software is Copyright (c) 2023 by Teaching and Research Institute from Sírio-Libanês Hospital. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
This is free software, licensed under: |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
The GNU General Public License, Version 3, June 2007 |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=cut |