| 1 | package App::Sandy::DB::Handle::Quality; | ||
| 2 | # ABSTRACT: Class to handle quality 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 | |||
| 10 | use constant { | ||
| 11 | SEED => 1717, | ||
| 12 | PARTIL => 10, | ||
| 13 | DEFAULT_PROFILE => { | ||
| 14 | poisson => { | ||
| 15 | 'mean' => '-m', | ||
| 16 | 'stdd' => '-d', | ||
| 17 | 'error' => '-e', | ||
| 18 | 'type' => 'both', | ||
| 19 | 'source' => 'default', | ||
| 20 | 'provider' => 'vendor', | ||
| 21 | 'date' => '2018-08-08' | ||
| 22 | } | ||
| 23 | } | ||
| 24 | }; | ||
| 25 | |||
| 26 | with qw{ | ||
| 27 | App::Sandy::Role::IO | ||
| 28 | App::Sandy::Role::Statistic | ||
| 29 | App::Sandy::Role::Counter | ||
| 30 | }; | ||
| 31 | |||
| 32 | our $VERSION = '0.25'; # VERSION | ||
| 33 | |||
| 34 | sub insertdb { | ||
| 35 | my ($self, $file, $name, $source, $is_user_provided, $error, $single_molecule, $type) = @_; | ||
| 36 | my $schema = App::Sandy::DB->schema; | ||
| 37 | my %default_profile = %{ &DEFAULT_PROFILE }; | ||
| 38 | |||
| 39 | log_msg ":: Checking if there is already a quality-profile '$name' ..."; | ||
| 40 | my $rs = $schema->resultset('QualityProfile')->find({ name => $name }); | ||
| 41 | if ($rs || $default_profile{$name}) { | ||
| 42 | die "There is already a quality-profile '$name'\n"; | ||
| 43 | } else { | ||
| 44 | log_msg ":: quality-profile '$name' not found"; | ||
| 45 | } | ||
| 46 | |||
| 47 | if ($type !~ /^(fastq|raw)$/) { | ||
| 48 | die "Unknown indexing type '$type': Valids are 'raw' and 'fastq'\n"; | ||
| 49 | } | ||
| 50 | |||
| 51 | log_msg ":: Indexing '$file'. It may take several minutes ..."; | ||
| 52 | my ($arr, $mean, $stdd, $deepth) = $self->_index_quality_type($file, $type); | ||
| 53 | |||
| 54 | log_msg ":: Converting array to bytes ..."; | ||
| 55 | my $bytes = nfreeze $arr; | ||
| 56 | log_msg ":: Compressing bytes ..."; | ||
| 57 | gzip \$bytes => \my $compressed; | ||
| 58 | |||
| 59 | # Begin transation | ||
| 60 | my $guard = $schema->txn_scope_guard; | ||
| 61 | |||
| 62 | log_msg ":: Storing quality matrix entry at '$name' ..."; | ||
| 63 | $rs = $schema->resultset('QualityProfile')->create({ | ||
| 64 | 'name' => $name, | ||
| 65 | 'source' => $source, | ||
| 66 | 'is_user_provided' => $is_user_provided, | ||
| 67 | 'is_single_molecule' => $single_molecule, | ||
| 68 | 'mean' => $mean, | ||
| 69 | 'stdd' => $stdd, | ||
| 70 | 'error' => $error, | ||
| 71 | 'deepth' => $deepth, | ||
| 72 | 'partil' => PARTIL, | ||
| 73 | 'matrix' => $compressed | ||
| 74 | }); | ||
| 75 | |||
| 76 | # End transation | ||
| 77 | $guard->commit; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _index_quality { | ||
| 81 | my ($self, $quality_ref, $rng) = @_; | ||
| 82 | my (@partil, @sizes); | ||
| 83 | |||
| 84 | for my $entry (@$quality_ref) { | ||
| 85 | my @q = split // => $entry; | ||
| 86 | |||
| 87 | my $size = scalar @q; | ||
| 88 | push @sizes => $size; | ||
| 89 | |||
| 90 | my $bin = int($size / PARTIL); | ||
| 91 | my $left = $size % PARTIL; | ||
| 92 | |||
| 93 | my $pos = 0; | ||
| 94 | my $skip = $self->with_make_counter($size - $left, $left, $rng); | ||
| 95 | |||
| 96 | for (my $i = 0; $i < PARTIL; $i++) { | ||
| 97 | for (my $j = 0; $j < $bin; $j++) { | ||
| 98 | $pos++ if $skip->(); | ||
| 99 | push @{ $partil[$i] } => $q[$pos++]; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | my $deepth = scalar @{ $partil[0] }; | ||
| 105 | for (my $i = 1; $i < PARTIL; $i++) { | ||
| 106 | if ($deepth != scalar(@{ $partil[$i] })) { | ||
| 107 | croak "deepth differs at partil $i"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | # Basic stats | ||
| 112 | my $mean = int($self->with_mean(\@sizes) + 0.5); | ||
| 113 | my $stdd = int($self->with_stdd(\@sizes) + 0.5); | ||
| 114 | |||
| 115 | return (\@partil, $mean, $stdd, $deepth); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _index_quality_type { | ||
| 119 | my ($self, $file, $type) = @_; | ||
| 120 | my $fh = $self->with_open_r($file); | ||
| 121 | |||
| 122 | log_msg ":: Counting number of lines in '$file' ..."; | ||
| 123 | my $num_lines = $self->_wcl($file); | ||
| 124 | |||
| 125 | if ($num_lines == 0) { | ||
| 126 | die "The file '$file' is empty\n"; | ||
| 127 | } | ||
| 128 | |||
| 129 | log_msg ":: Number of lines: $num_lines"; | ||
| 130 | |||
| 131 | my $num_left = 0; | ||
| 132 | my $line = 0; | ||
| 133 | my $acm = 0; | ||
| 134 | |||
| 135 | my $getter; | ||
| 136 | |||
| 137 | if ($type eq 'fastq') { | ||
| 138 | log_msg ":: Setting fastq validation and getter"; | ||
| 139 | $num_left = int($num_lines / 4); | ||
| 140 | |||
| 141 | $getter = sub { | ||
| 142 | return if eof($fh); | ||
| 143 | |||
| 144 | my @stack; | ||
| 145 | |||
| 146 | for (1..4) { | ||
| 147 | $line++; | ||
| 148 | defined(my $entry = <$fh>) | ||
| 149 | or die "Truncated fastq entry in '$file' at line $line\n"; | ||
| 150 | push @stack => $entry; | ||
| 151 | } | ||
| 152 | |||
| 153 | chomp @stack; | ||
| 154 | |||
| 155 | if ($stack[0] !~ /^\@/ || $stack[2] !~ /^\+/) { | ||
| 156 | die "Fastq entry at '$file' line '", $line - 3, "' not seems to be a valid read\n"; | ||
| 157 | } | ||
| 158 | |||
| 159 | return $stack[3]; | ||
| 160 | }; | ||
| 161 | } else { | ||
| 162 | log_msg ":: Setting raw validation and getter"; | ||
| 163 | $num_left = $num_lines; | ||
| 164 | |||
| 165 | $getter = sub { | ||
| 166 | defined(my $entry = <$fh>) | ||
| 167 | or return; | ||
| 168 | |||
| 169 | $line++; | ||
| 170 | chomp $entry; | ||
| 171 | |||
| 172 | return $entry; | ||
| 173 | }; | ||
| 174 | } | ||
| 175 | |||
| 176 | log_msg ":: Calculating the number of entries to pick ..."; | ||
| 177 | my $picks = $num_left < 1000 ? $num_left : 1000; | ||
| 178 | my $picks_left = $picks; | ||
| 179 | |||
| 180 | my $rng = App::Sandy::RNG->new(SEED); | ||
| 181 | my $do_pick = $self->with_make_counter($num_left, $picks, $rng); | ||
| 182 | my @quality; | ||
| 183 | |||
| 184 | log_msg ":: Picking $picks entries in '$file' ..."; | ||
| 185 | while (my $entry = $getter->()) { | ||
| 186 | if ($do_pick->()) { | ||
| 187 | push @quality => $entry if length($entry) >= PARTIL; | ||
| 188 | if ($picks >= 10 && (++$acm % int($picks/10) == 0)) { | ||
| 189 | log_msg sprintf " ==> %d%% processed", ($acm / $picks) * 100; | ||
| 190 | } | ||
| 191 | last if --$picks_left <= 0; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | if (scalar(@quality) == 0) { | ||
| 196 | die "All quality entries have length lesser then 10 in file '$file'.\n" . | ||
| 197 | "quality-profile constraints the quality length to values greater or equal to 10.\n" . | ||
| 198 | "If you need somehow profiles with length lesser than 10. Please use --quality-profile=poisson\n"; | ||
| 199 | } | ||
| 200 | |||
| 201 | $fh->close | ||
| 202 | or die "Cannot close file '$file'\n"; | ||
| 203 | |||
| 204 | return $self->_index_quality(\@quality, $rng); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub _wcl { | ||
| 208 | my ($self, $file) = @_; | ||
| 209 | my $fh = $self->with_open_r($file); | ||
| 210 | my $num_lines = 0; | ||
| 211 | $num_lines++ while <$fh>; | ||
| 212 | $fh->close; | ||
| 213 | return $num_lines; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub retrievedb { | ||
| 217 | my ($self, $quality_profile) = @_; | ||
| 218 | my $schema = App::Sandy::DB->schema; | ||
| 219 | my %default_profile = %{ &DEFAULT_PROFILE }; | ||
| 220 | |||
| 221 | if ($default_profile{$quality_profile}) { | ||
| 222 | die "Cannot retrieve '$quality_profile' because it is based on a theoretical distribution\n"; | ||
| 223 | } | ||
| 224 | |||
| 225 | my $rs = $schema->resultset('QualityProfile')->find({ name => $quality_profile }); | ||
| 226 | die "'$quality_profile' not found into database\n" unless defined $rs; | ||
| 227 | |||
| 228 | my $compressed = $rs->matrix; | ||
| 229 | die "quality-profile entry '$quality_profile' exists, but the related data is missing\n" | ||
| 230 | unless defined $compressed; | ||
| 231 | |||
| 232 | my $deepth = $rs->deepth; | ||
| 233 | my $partil = $rs->partil; | ||
| 234 | |||
| 235 | gunzip \$compressed => \my $bytes; | ||
| 236 | my $matrix = thaw $bytes; | ||
| 237 | return ($matrix, $deepth, $partil); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub deletedb { | ||
| 241 | my ($self, $quality_profile) = @_; | ||
| 242 | my $schema = App::Sandy::DB->schema; | ||
| 243 | my %default_profile = %{ &DEFAULT_PROFILE }; | ||
| 244 | |||
| 245 | log_msg ":: Checking if there is a quality-profile '$quality_profile' ..."; | ||
| 246 | my $rs = $schema->resultset('QualityProfile')->find({ name => $quality_profile }); | ||
| 247 | |||
| 248 | unless ($rs || $default_profile{$quality_profile}) { | ||
| 249 | die "'$quality_profile' not found into database\n"; | ||
| 250 | } | ||
| 251 | |||
| 252 | log_msg ":: Found '$quality_profile'"; | ||
| 253 | |||
| 254 | if (($rs && !$rs->is_user_provided) || $default_profile{$quality_profile}) { | ||
| 255 | die "'$quality_profile' is not a user provided entry. Cannot be deleted\n"; | ||
| 256 | } | ||
| 257 | |||
| 258 | log_msg ":: Removing '$quality_profile' entry ..."; | ||
| 259 | |||
| 260 | # Begin transation | ||
| 261 | my $guard = $schema->txn_scope_guard; | ||
| 262 | |||
| 263 | $rs->delete; | ||
| 264 | |||
| 265 | # End transation | ||
| 266 | $guard->commit; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub restoredb { | ||
| 270 | my $self = shift; | ||
| 271 | my $schema = App::Sandy::DB->schema; | ||
| 272 | |||
| 273 | log_msg ":: Searching for user-provided entries ..."; | ||
| 274 | my $user_provided = $schema->resultset('QualityProfile')->search( | ||
| 275 | { is_user_provided => 1 }, | ||
| 276 | ); | ||
| 277 | |||
| 278 | my $entry = $user_provided->next; | ||
| 279 | if ($entry) { | ||
| 280 | log_msg ":: Found:"; | ||
| 281 | do { | ||
| 282 | log_msg ' ==> ' . $entry->name; | ||
| 283 | } while ($entry = $user_provided->next); | ||
| 284 | } else { | ||
| 285 | die "Not found user-provided entries. There is no need to restoring\n"; | ||
| 286 | } | ||
| 287 | |||
| 288 | log_msg ":: Removing all user-provided entries ..."; | ||
| 289 | |||
| 290 | # Begin transation | ||
| 291 | my $guard = $schema->txn_scope_guard; | ||
| 292 | |||
| 293 | $user_provided->delete; | ||
| 294 | |||
| 295 | # End transation | ||
| 296 | $guard->commit; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub make_report { | ||
| 300 | my $self = shift; | ||
| 301 | my $schema = App::Sandy::DB->schema; | ||
| 302 | my %report = %{ &DEFAULT_PROFILE }; | ||
| 303 | |||
| 304 | my $rs = $schema->resultset('QualityProfile')->search(undef); | ||
| 305 | |||
| 306 | while (my $quality = $rs->next) { | ||
| 307 | my %hash = ( | ||
| 308 | 'mean' => $quality->mean, | ||
| 309 | 'stdd' => $quality->stdd, | ||
| 310 | 'error' => $quality->error, | ||
| 311 | 'type' => $quality->is_single_molecule ? 'single-molecule' : 'fragment', | ||
| 312 | 'source' => $quality->source, | ||
| 313 | 'provider' => $quality->is_user_provided ? "user" : "vendor", | ||
| 314 | 'date' => $quality->date | ||
| 315 | ); | ||
| 316 | $report{$quality->name} = \%hash; | ||
| 317 | } | ||
| 318 | |||
| 319 | return \%report; | ||
| 320 | } | ||
| 321 | |||
| 322 | __END__ | ||