| 1 | package MS::Reader::MzXML::Spectrum; | ||
| 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/; |
|
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 Carp; | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | |||
| 15 | *id = \&scan_number | ||
| 16 | |||
| 17 | } | ||
| 18 | |||
| 19 | sub _pre_load { | ||
| 20 | |||
| 21 | my ($self) = @_; | ||
| 22 | |||
| 23 | $self->{_toplevel} = 'scan'; | ||
| 24 | |||
| 25 | # Lookup tables to quickly check elements | ||
| 26 | $self->{_make_named_hash} = { | ||
| 27 | nameValue => 'name', | ||
| 28 | peaks => 'contentType', | ||
| 29 | }; | ||
| 30 | $self->{_make_anon_array} = { map {$_ => 1} qw/ | ||
| 31 | scanOrigin | ||
| 32 | precursorMz | ||
| 33 | comment | ||
| 34 | / }; | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | sub _post_load { | ||
| 39 | |||
| 40 | my ($self) = @_; | ||
| 41 | |||
| 42 | # Nested scans don't work with the current infrastructure (plus we never | ||
| 43 | # use this format). Could be supported in future if there was demand. | ||
| 44 | croak "Nested scans in mzXML not currently supported!" | ||
| 45 | if (exists $self->{scan}->{scan}); | ||
| 46 | |||
| 47 | } | ||
| 48 | sub ms_level { | ||
| 49 | |||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | croak "Invalid mzXML (missing msLevel property)" | ||
| 53 | if (! defined $self->{msLevel}); | ||
| 54 | |||
| 55 | return $self->{msLevel}; | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub rt { | ||
| 60 | |||
| 61 | my ($self) = @_; | ||
| 62 | |||
| 63 | return if (! defined $self->{retentionTime}); | ||
| 64 | |||
| 65 | if ($self->{retentionTime} =~ /^PT(?:([\d\.]+)M)?(?:([\d\.]+)S)?/) { | ||
| 66 | my $s = 0; | ||
| 67 | $s += $1*60 if (defined $1); | ||
| 68 | $s += $2 if (defined $2); | ||
| 69 | return $s; | ||
| 70 | } | ||
| 71 | |||
| 72 | croak "Unexpected retention time format: $self->{retentionTime}"; | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | sub scan_number { | ||
| 77 | |||
| 78 | my ($self) = @_; | ||
| 79 | |||
| 80 | croak "Invalid mzXML (missing scan number property)" | ||
| 81 | if (! defined $self->{num}); | ||
| 82 | |||
| 83 | return $self->{num}; | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | sub precursor { | ||
| 88 | |||
| 89 | my ($self) = @_; | ||
| 90 | croak "precursor() only valid for MS2 spectra" | ||
| 91 | if ($self->{msLevel} < 2); | ||
| 92 | croak "precursor() only valid for single precursor spectra, use direct access.\n" | ||
| 93 | if (scalar @{$self->{precursorMz}} != 1); | ||
| 94 | |||
| 95 | my $pre = $self->{precursorMz}->[0]; | ||
| 96 | my $id = $pre->{precursorScanNum}; | ||
| 97 | my $mono_mz = $pre->{pcdata}; | ||
| 98 | my $iso_mz = $mono_mz; | ||
| 99 | my $iso_lower = $iso_mz - $pre->{windowWideness}/2; | ||
| 100 | my $iso_upper = $iso_mz + $pre->{windowWideness}/2; | ||
| 101 | my $charge = $pre->{precursorCharge}; | ||
| 102 | my $int = $pre->{precursorIntensity}; | ||
| 103 | |||
| 104 | croak "missing monoisotopic m/z" if (! defined $mono_mz); | ||
| 105 | return { | ||
| 106 | scan_id => $id, | ||
| 107 | iso_mz => $iso_mz, | ||
| 108 | iso_lower => $iso_lower, | ||
| 109 | iso_upper => $iso_upper, | ||
| 110 | mono_mz => $mono_mz, | ||
| 111 | charge => $charge, | ||
| 112 | intensity => $int, | ||
| 113 | }; | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | sub mz { | ||
| 118 | |||
| 119 | my ($self) = @_; | ||
| 120 | |||
| 121 | return $self->{__mz} if (exists $self->{__mz}); | ||
| 122 | |||
| 123 | if (defined $self->{peaks}->{'m/z'}) { | ||
| 124 | $self->{__mz} = $self->get_array('m/z'); | ||
| 125 | } | ||
| 126 | elsif (defined $self->{peaks}->{'m/z-int'}) { | ||
| 127 | my $v = $self->get_array('m/z-int'); | ||
| 128 | my $l = scalar(@$v)/2; | ||
| 129 | croak "Odd-numbered m/z-int array" if ( ($l%2) != 0); | ||
| 130 | $self->{__mz} = [ map {$v->[$_]} map {$_*2} (0..$l-1) ]; | ||
| 131 | $self->{__int} = [ map {$v->[$_]} map {$_*2+1} (0..$l-1) ]; | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | croak "m/z array undefined"; | ||
| 135 | } | ||
| 136 | |||
| 137 | return $self->{__mz}; | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | sub int { | ||
| 142 | |||
| 143 | my ($self) = @_; | ||
| 144 | |||
| 145 | return $self->{__int} if (exists $self->{__int}); | ||
| 146 | |||
| 147 | if (defined $self->{peaks}->{'intensity'}) { | ||
| 148 | $self->{__int} = $self->get_array('intensity'); | ||
| 149 | } | ||
| 150 | elsif (defined $self->{peaks}->{'m/z-int'}) { | ||
| 151 | my $v = $self->get_array('m/z-int'); | ||
| 152 | my $l = scalar(@$v)/2; | ||
| 153 | croak "Odd-numbered m/z-int array" if ( ($l%2) != 0); | ||
| 154 | $self->{__mz} = [ map {$v->[$_]} map {$_*2} (0..$l-1) ]; | ||
| 155 | $self->{__int} = [ map {$v->[$_]} map {$_*2+1} (0..$l-1) ]; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | croak "intensity array undefined"; | ||
| 159 | } | ||
| 160 | |||
| 161 | return $self->{__int}; | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | sub get_array { | ||
| 166 | |||
| 167 | my ($self, $type) = @_; | ||
| 168 | |||
| 169 | my $ref = $self->{peaks}->{$type}; | ||
| 170 | croak "Undefined array type $type" if (! defined $ref); | ||
| 171 | |||
| 172 | my $data = decode_base64( $ref->{pcdata} ); | ||
| 173 | $data = uncompress($data) if ($ref->{compressionType} eq 'zlib'); | ||
| 174 | my $code = $ref->{precision} eq '64' ? 'd>' : 'f>'; | ||
| 175 | return [ unpack "$code*", $data ]; | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | sub scan_window { | ||
| 180 | |||
| 181 | my ($self) = @_; | ||
| 182 | |||
| 183 | my $l = $self->{startMz} // $self->{lowMz}; | ||
| 184 | my $r = $self->{endMZ} // $self->{highMz}; | ||
| 185 | |||
| 186 | return if (! defined $l || ! defined $r); | ||
| 187 | return [$l, $r]; | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | 1; | ||
| 192 | |||
| 193 | __END__ | ||