lib/App/Sandy/DB/Handle/Expression.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« lib/App/Sandy/Command/Version.pm ↑ Summary lib/App/Sandy/DB/Handle/Quality.pm »
1 package App::Sandy::DB::Handle::Expression;
2 # ABSTRACT: Class to handle expression-matrix database schemas.
3  
4 1 ▶use App::Sandy::Base 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use App::Sandy::DB;
6 use IO::Compress::Gzip 'gzip';
7 use IO::Uncompress::Gunzip 'gunzip';
8 use Storable qw/nfreeze thaw/;
9 use Scalar::Util 'looks_like_number';
10  
11 with 'App::Sandy::Role::IO';
12  
13 our $VERSION = '0.25'; # VERSION
14  
15 sub insertdb {
16 my ($self, $file, $name, $source, $is_user_provided) = @_;
17 my $schema = App::Sandy::DB->schema;
18  
19 log_msg ":: Checking if there is already an expression-matrix '$name' ...";
20 my $rs = $schema->resultset('ExpressionMatrix')->find({ name => $name });
21 if ($rs) {
22 die "There is already an expression-matrix '$name'\n";
23 } else {
24 log_msg ":: expression-matrix '$name' not found";
25 }
26  
27 log_msg ":: Indexing '$file' ...";
28 my $indexed_file = $self->_index_expression_matrix($file);
29  
30 log_msg ":: Converting data to bytes ...";
31 my $bytes = nfreeze $indexed_file;
32 log_msg ":: Compressing bytes ...";
33 gzip \$bytes => \my $compressed;
34  
35 # Begin transation
36 my $guard = $schema->txn_scope_guard;
37  
38 log_msg ":: Storing expression-matrix '$name'...";
39 $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 $guard->commit;
48 }
49  
50 sub _index_expression_matrix {
51 my ($self, $file) = @_;
52  
53 my $fh = $self->with_open_r($file);
54 my %indexed_file;
55  
56 my $line = 0;
57 while (<$fh>) {
58 $line++;
59 chomp;
60 next if /^\s*$/;
61  
62 my @fields = split;
63  
64 die "Error parsing expression-matrix '$file': Seqid (first column) not found at line $line\n"
65 unless defined $fields[0];
66 die "Error parsing expression-matrix '$file': Count (second column) not found at line $line\n"
67 unless defined $fields[1];
68 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 if ($fields[1] <= 0) {
73 log_msg ":: Parsing expression-matrix '$file': Ignoring seqid '$fields[0]': Count (second column) lesser or equal to zero at line $line\n";
74 next;
75 }
76  
77 $indexed_file{$fields[0]} = $fields[1];
78 }
79  
80 unless (%indexed_file) {
81 die "Error parsing expression-matrix '$file': Maybe the file is empty\n"
82 }
83  
84 $fh->close
85 or die "Cannot close expression-matrix $file: $!\n";
86  
87 return \%indexed_file;
88 }
89  
90 sub retrievedb {
91 my ($self, $expression_matrix) = @_;
92 my $schema = App::Sandy::DB->schema;
93  
94 my $rs = $schema->resultset('ExpressionMatrix')->find({ name => $expression_matrix });
95 die "'$expression_matrix' not found into database\n" unless defined $rs;
96  
97 my $compressed = $rs->matrix;
98 die "expression-matrix entry '$expression_matrix' exists, but the related data is missing\n"
99 unless defined $compressed;
100  
101 gunzip \$compressed => \my $bytes;
102 my $matrix = thaw $bytes;
103 return $matrix;
104 }
105  
106 sub deletedb {
107 my ($self, $expression_matrix) = @_;
108 my $schema = App::Sandy::DB->schema;
109  
110 log_msg ":: Checking if there is already an expression-matrix '$expression_matrix' ...";
111 my $rs = $schema->resultset('ExpressionMatrix')->find({ name => $expression_matrix });
112 die "'$expression_matrix' not found into database\n" unless defined $rs;
113  
114 log_msg ":: Found '$expression_matrix'";
115 die "'$expression_matrix' is not a user provided entry. Cannot be deleted\n"
116 unless $rs->is_user_provided;
117  
118 log_msg ":: Removing '$expression_matrix' entry ...";
119  
120 # Begin transation
121 my $guard = $schema->txn_scope_guard;
122  
123 $rs->delete;
124  
125 # End transation
126 $guard->commit;
127 }
128  
129 sub restoredb {
130 my $self = shift;
131 my $schema = App::Sandy::DB->schema;
132  
133 log_msg ":: Searching for user-provided entries ...";
134 my $user_provided = $schema->resultset('ExpressionMatrix')->search(
135 { is_user_provided => 1 }
136 );
137  
138 my $entry = $user_provided->next;
139 if ($entry) {
140 log_msg ":: Found:";
141 do {
142 log_msg ' ==> ' . $entry->name;
143 } while ($entry = $user_provided->next);
144 } else {
145 die "Not found user-provided entries. There is no need to restoring\n";
146 }
147  
148 log_msg ":: Removing all user-provided entries ...";
149  
150 # Begin transation
151 my $guard = $schema->txn_scope_guard;
152  
153 $user_provided->delete;
154  
155 # End transation
156 $guard->commit;
157 }
158  
159 sub make_report {
160 my $self = shift;
161 my $schema = App::Sandy::DB->schema;
162 my %report;
163  
164 my $rs = $schema->resultset('ExpressionMatrix')->search(undef);
165  
166 while (my $expression_matrix = $rs->next) {
167 my %hash = (
168 source => $expression_matrix->source,
169 provider => $expression_matrix->is_user_provided ? "user" : "vendor",
170 date => $expression_matrix->date
171 );
172 $report{$expression_matrix->name} = \%hash;
173 }
174  
175 return \%report;
176 }
177  
178 __END__
« lib/App/Sandy/Command/Version.pm ↑ Summary lib/App/Sandy/DB/Handle/Quality.pm »