| 1 | package MS::Reader::MzML::Record; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use parent qw/MS::Reader::XML::Record::CV/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | use Compress::Zlib; | ||
| 9 | use MIME::Base64; | ||
| 10 | use List::Util qw/first/; | ||
| 11 | use MS::CV qw/:MS :UO/; | ||
| 12 | |||
| 13 | # Abbreviate some constants | ||
| 14 | use constant NUMPRESS_LIN => MS_MS_NUMPRESS_LINEAR_PREDICTION_COMPRESSION; | ||
| 15 | use constant NUMPRESS_PIC => MS_MS_NUMPRESS_POSITIVE_INTEGER_COMPRESSION; | ||
| 16 | use constant NUMPRESS_SLOF => MS_MS_NUMPRESS_SHORT_LOGGED_FLOAT_COMPRESSION; | ||
| 17 | |||
| 18 | sub _pre_load { | ||
| 19 | |||
| 20 | my ($self) = @_; | ||
| 21 | |||
| 22 | # Lookup tables to quickly check elements | ||
| 23 | $self->{_make_named_array} = { | ||
| 24 | cvParam => 'accession', | ||
| 25 | userParam => 'name', | ||
| 26 | }; | ||
| 27 | $self->{_make_anon_array} = { map {$_ => 1} qw/ | ||
| 28 | referenceableParamGroupRef | ||
| 29 | product | ||
| 30 | binaryDataArray | ||
| 31 | precursor | ||
| 32 | selectedIon | ||
| 33 | scanWindow | ||
| 34 | scan | ||
| 35 | / }; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | sub _get_raw { | ||
| 40 | |||
| 41 | my ($self, $array) = @_; | ||
| 42 | return decode_base64( $array->{binary}->{pcdata} ); | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | sub _get_code { | ||
| 47 | |||
| 48 | my ($self, $array) = @_; | ||
| 49 | |||
| 50 | return defined $self->param(MS_32_BIT_FLOAT, ref => $array) ? 'f<' | ||
| 51 | : defined $self->param(MS_64_BIT_FLOAT, ref => $array) ? 'd<' | ||
| 52 | : defined $self->param(MS_32_BIT_INTEGER, ref => $array) ? 'l<' | ||
| 53 | : defined $self->param(MS_64_BIT_INTEGER, ref => $array) ? 'q<' | ||
| 54 | : undef; | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | # binary arrays are only decoded upon request, to increase parse speed | ||
| 59 | sub get_array { | ||
| 60 | |||
| 61 | my ($self, $acc) = @_; | ||
| 62 | |||
| 63 | |||
| 64 | # fetch from cache if exists | ||
| 65 | if ($self->{__use_cache} && exists $self->{__memoized}->{arrays}->{$acc}) { | ||
| 66 | my $ret = $self->{__memoized}->{arrays}->{$acc}; | ||
| 67 | # return hash in array context, or first data array else | ||
| 68 | return wantarray ? @$ret : $ret->[1]; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Find data array reference by CV accession | ||
| 72 | my @arrays = grep {defined $self->param($acc, ref => $_)} | ||
| 73 | @{ $self->{binaryDataArrayList}->{binaryDataArray} }; | ||
| 74 | |||
| 75 | my @ret; | ||
| 76 | |||
| 77 | for my $array (@arrays) { | ||
| 78 | |||
| 79 | # Extract metadata necessary to unpack array | ||
| 80 | my $raw = $self->_get_raw($array); | ||
| 81 | my $is_zlib = 0; | ||
| 82 | my $numpress = 'none'; | ||
| 83 | if (! defined $self->param(MS_NO_COMPRESSION, ref => $array) ) { | ||
| 84 | $is_zlib = defined $self->param(MS_ZLIB_COMPRESSION, ref => $array); | ||
| 85 | $numpress | ||
| 86 | = defined $self->param(NUMPRESS_LIN, ref => $array) ? 'np-lin' | ||
| 87 | : defined $self->param(NUMPRESS_PIC, ref => $array) ? 'np-pic' | ||
| 88 | : defined $self->param(NUMPRESS_SLOF, ref => $array) ? 'np-slof' | ||
| 89 | : 'none'; | ||
| 90 | # Compression type (or lack thereof) MUST be specified! | ||
| 91 | die "Uknown compression scheme (no known schemes specified) ??" | ||
| 92 | if (! $is_zlib && $numpress eq 'none'); | ||
| 93 | } | ||
| 94 | my $code = $self->_get_code($array); | ||
| 95 | |||
| 96 | die "floating point precision required if numpress not used" | ||
| 97 | if (! defined $code && $numpress eq 'none'); | ||
| 98 | |||
| 99 | my $data = _decode_raw( | ||
| 100 | $raw, | ||
| 101 | $code, | ||
| 102 | $is_zlib, | ||
| 103 | $numpress, | ||
| 104 | ); | ||
| 105 | # Convert minutes to seconds | ||
| 106 | if ($acc eq MS_TIME_ARRAY) { | ||
| 107 | |||
| 108 | my ($t, $units) = $self->param(MS_TIME_ARRAY, ref => $array); | ||
| 109 | if (defined $units && $units eq UO_MINUTE) { | ||
| 110 | $data = [ map {$_*60} @{$data} ]; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | # Sanity checks (no noticeable effect on speed during benchmarking) | ||
| 115 | #die "ERROR: array data compressed length mismatch" | ||
| 116 | #if ($is_compressed && $len != $array->{encodedLength}); | ||
| 117 | #my $c = scalar @{$data}; | ||
| 118 | #my $e = $self->{defaultArrayLength}; | ||
| 119 | #die "ERROR: array list count mismatch ($e v $c) for record" | ||
| 120 | #if (scalar(@{$data}) != $self->{defaultArrayLength}); | ||
| 121 | |||
| 122 | |||
| 123 | my $name = $self->param($acc, ref => $array); | ||
| 124 | push @ret, $name, $data; | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | $self->{__memoized}->{arrays}->{$acc} = [@ret] if ($self->{__use_cache}); | ||
| 129 | |||
| 130 | # return hash in array context, or first data array else | ||
| 131 | return wantarray ? @ret : $ret[1]; | ||
| 132 | |||
| 133 | } | ||
| 134 | |||
| 135 | sub _decode_raw { | ||
| 136 | |||
| 137 | my ($data, $code, $is_zlib, $numpress) = @_; | ||
| 138 | |||
| 139 | return [] if (length($data) < 1); | ||
| 140 | |||
| 141 | $data = uncompress($data) if ($is_zlib); | ||
| 142 | my $array; | ||
| 143 | if ($numpress eq 'none') { | ||
| 144 | $array = [ unpack "$code*", $data ]; | ||
| 145 | } | ||
| 146 | elsif ($numpress eq 'np-pic') { | ||
| 147 | $array = _decode_trunc_ints( $data ); | ||
| 148 | } | ||
| 149 | elsif ($numpress eq 'np-slof') { | ||
| 150 | $array = _decode_np_slof( $data ); | ||
| 151 | } | ||
| 152 | elsif ($numpress eq 'np-lin') { | ||
| 153 | $array = _decode_np_linear( $data ); | ||
| 154 | } | ||
| 155 | |||
| 156 | return $array; | ||
| 157 | |||
| 158 | } | ||
| 159 | |||
| 160 | sub _decode_np_linear { | ||
| 161 | |||
| 162 | my ($data) = @_; | ||
| 163 | |||
| 164 | my $fp = unpack 'd>', substr($data,0,8,''); | ||
| 165 | my @v = unpack 'VV', substr($data,0,8,''); | ||
| 166 | if (length $data) { | ||
| 167 | push @v, 2*$v[-1] - $v[-2] + $_ | ||
| 168 | for ( @{ _decode_trunc_ints($data) } ); | ||
| 169 | } | ||
| 170 | @v = map {$_/$fp} @v; | ||
| 171 | |||
| 172 | return \@v; | ||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | sub _decode_np_slof { | ||
| 177 | |||
| 178 | my ($data) = @_; | ||
| 179 | |||
| 180 | my $fp = unpack 'd>', substr($data,0,8,''); | ||
| 181 | my @v = map {exp($_/$fp)-1} unpack 'v*', $data; | ||
| 182 | |||
| 183 | return \@v; | ||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | sub _decode_trunc_ints { | ||
| 188 | |||
| 189 | # Unpack string of truncated integer nybbles into longs | ||
| 190 | |||
| 191 | my ($data) = (@_); | ||
| 192 | |||
| 193 | my @nybbles; | ||
| 194 | for (unpack 'C*', $data) { | ||
| 195 | # push most-significant first! | ||
| 196 | push @nybbles, ($_ >> 4); | ||
| 197 | push @nybbles, ($_ & 0xf); | ||
| 198 | } | ||
| 199 | my $array; | ||
| 200 | while (scalar(@nybbles)) { | ||
| 201 | |||
| 202 | my $long = 0; | ||
| 203 | my $head = shift @nybbles; | ||
| 204 | # ignore trailing non-zero nybble | ||
| 205 | last if (!scalar(@nybbles) && $head != 0x8); | ||
| 206 | my $n = 0; | ||
| 207 | if ($head <= 8) { | ||
| 208 | $n = $head; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $n = $head - 8; | ||
| 212 | my $shift = (8-$n)*4; | ||
| 213 | $long = $long | ((0xffffffff >> $shift) << $shift); | ||
| 214 | } | ||
| 215 | |||
| 216 | my $i = $n; | ||
| 217 | while ($i < 8) { | ||
| 218 | my $nyb = shift @nybbles; | ||
| 219 | $long = $long | ($nyb << (($i-$n)*4)); | ||
| 220 | ++$i; | ||
| 221 | } | ||
| 222 | $long = unpack 'l<', pack 'l<',$long; # cast to signed long - slow? | ||
| 223 | push @{$array}, $long; | ||
| 224 | } | ||
| 225 | |||
| 226 | return $array; | ||
| 227 | |||
| 228 | } | ||
| 229 | |||
| 230 | #sub param { | ||
| 231 | # | ||
| 232 | #my ($self, $cv, $idx) = @_; | ||
| 233 | #$idx //= 0; | ||
| 234 | #my $val = $self->{cvParam}->{$cv}->[$idx]->{value}; | ||
| 235 | #my $units = $self->{cvParam}->{$cv}->[$idx]->{unitAccession}; | ||
| 236 | #return wantarray ? ($val, $units) : $val; | ||
| 237 | # | ||
| 238 | #} | ||
| 239 | |||
| 240 | 1; | ||
| 241 | |||
| 242 | __END__ | ||