« blib/lib/MS/Reader/MzIdentML/SpectrumIdentificationResult.pm
↑ Summary
blib/lib/MS/Reader/MzML/Chromatogram.pm »
| 1 | package MS::Reader::MzML; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 3 | ▶ | use parent qw/MS::Reader::XML::CV/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Digest::SHA; | ||
| 11 | use List::Util qw/first/; | ||
| 12 | |||
| 13 | use MS::Reader::MzML::Spectrum; | ||
| 14 | use MS::Reader::MzML::Chromatogram; | ||
| 15 | use MS::CV qw/:MS/; | ||
| 16 | |||
| 17 | use constant FLOAT_TOL => 0.000001; | ||
| 18 | |||
| 19 | sub _pre_load { | ||
| 20 | |||
| 21 | my ($self) = @_; | ||
| 22 | |||
| 23 | # ---------------------------------------------------------------------------# | ||
| 24 | # These tables are the main configuration point between the parser and the | ||
| 25 | # specific document schema. For more information, see the documentation | ||
| 26 | # for the parent class MS::Reader::XML | ||
| 27 | # ---------------------------------------------------------------------------# | ||
| 28 | |||
| 29 | $self->{_toplevel} = 'mzML'; | ||
| 30 | $self->{__rt_index} = undef; | ||
| 31 | |||
| 32 | $self->{__record_classes} = { | ||
| 33 | spectrum => 'MS::Reader::MzML::Spectrum', | ||
| 34 | chromatogram => 'MS::Reader::MzML::Chromatogram', | ||
| 35 | }; | ||
| 36 | |||
| 37 | $self->{_skip_inside} = { map {$_ => 1} qw/ | ||
| 38 | spectrum | ||
| 39 | chromatogram | ||
| 40 | indexList | ||
| 41 | / }; | ||
| 42 | |||
| 43 | $self->{_make_index} = { map {$_ => 'id'} qw/ | ||
| 44 | spectrum | ||
| 45 | chromatogram | ||
| 46 | / }; | ||
| 47 | |||
| 48 | $self->{_make_named_array} = { | ||
| 49 | cvParam => 'accession', | ||
| 50 | userParam => 'name', | ||
| 51 | }; | ||
| 52 | |||
| 53 | $self->{_make_named_hash} = { map {$_ => 'id'} qw/ | ||
| 54 | cv | ||
| 55 | dataProcessing | ||
| 56 | instrumentConfiguration | ||
| 57 | referenceableParamGroup | ||
| 58 | sample | ||
| 59 | scanSettings | ||
| 60 | software | ||
| 61 | sourceFile | ||
| 62 | / }; | ||
| 63 | |||
| 64 | $self->{_make_anon_array} = { map {$_ => 1} qw/ | ||
| 65 | analyzer | ||
| 66 | contact | ||
| 67 | detector | ||
| 68 | processingMethod | ||
| 69 | referenceableParamGroupRef | ||
| 70 | source | ||
| 71 | sourceFileRef | ||
| 72 | target | ||
| 73 | / }; | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub _load_new { | ||
| 79 | |||
| 80 | my ($self) = @_; | ||
| 81 | |||
| 82 | $self->SUPER::_load_new(); | ||
| 83 | |||
| 84 | if ( | ||
| 85 | defined $self->{indexedmzML}->{fileChecksum} | ||
| 86 | && defined $self->{indexedmzML}->{fileChecksum}->{pcdata} | ||
| 87 | && length $self->{indexedmzML}->{fileChecksum}->{pcdata} | ||
| 88 | && $self->{indexedmzML}->{fileChecksum}->{pcdata} ne '0' | ||
| 89 | ) { | ||
| 90 | |||
| 91 | # compare supplied and calculated SHA1 sums to validate | ||
| 92 | my $sha1_given = $self->{indexedmzML}->{fileChecksum}->{pcdata}; | ||
| 93 | croak "ERROR: SHA1 digest mismatch\n" | ||
| 94 | if ($sha1_given ne $self->_calc_sha1); | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | # Outer <mzML> may optionally be wrapped in <indexedmxML> tags. For | ||
| 99 | # consistent downstream handling, everything outside <mzML> should be | ||
| 100 | # discarded before returning. | ||
| 101 | if (defined $self->{indexedmzML}) { | ||
| 102 | $self->{mzML}->{$_} = $self->{indexedmzML}->{mzML}->{$_} | ||
| 103 | for (keys %{ $self->{indexedmzML}->{mzML} }); | ||
| 104 | delete $self->{indexedmzML}; | ||
| 105 | } | ||
| 106 | |||
| 107 | return; | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | sub fetch_spectrum { | ||
| 112 | |||
| 113 | my ($self, $idx, %args) = @_; | ||
| 114 | my $ref = $self->{run}->{spectrumList}; | ||
| 115 | return $self->fetch_record($ref, $idx, %args); | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | sub next_spectrum { | ||
| 120 | |||
| 121 | my ($self, %args) = @_; | ||
| 122 | my $ref = $self->{run}->{spectrumList}; | ||
| 123 | return $self->next_record( $ref, %args ); | ||
| 124 | |||
| 125 | } | ||
| 126 | |||
| 127 | sub find_by_time { | ||
| 128 | |||
| 129 | my ($self, $rt, $ms_level) = @_; | ||
| 130 | |||
| 131 | # lazy load | ||
| 132 | if (! defined $self->{__rt_index}) { | ||
| 133 | $self->_index_rt(); | ||
| 134 | } | ||
| 135 | |||
| 136 | my @sorted = @{ $self->{__rt_index} }; | ||
| 137 | |||
| 138 | croak "Retention time out of bounds" | ||
| 139 | if ($rt < 0 || $rt > $self->{__rt_index}->[-1]->[1] + FLOAT_TOL); | ||
| 140 | |||
| 141 | # binary search | ||
| 142 | my ($lower, $upper) = (0, $#sorted); | ||
| 143 | while ($lower != $upper) { | ||
| 144 | my $mid = int( ($lower+$upper)/2 ); | ||
| 145 | ($lower,$upper) = $rt > $sorted[$mid]->[1] + FLOAT_TOL | ||
| 146 | ? ( $mid+1 , $upper ) | ||
| 147 | : ( $lower , $mid ); | ||
| 148 | } | ||
| 149 | |||
| 150 | my $i = $sorted[$lower]->[0]; #return closest scan index >= $ret | ||
| 151 | my $ref = $self->{run}->{spectrumList}; | ||
| 152 | while (defined $ms_level | ||
| 153 | && $self->fetch_record($ref => $i)->ms_level() != $ms_level) { | ||
| 154 | ++$i; | ||
| 155 | } | ||
| 156 | return $i; | ||
| 157 | |||
| 158 | } | ||
| 159 | |||
| 160 | sub spectrum_index_by_id { | ||
| 161 | |||
| 162 | my ($self, $id) = @_; | ||
| 163 | my $ref = $self->{run}->{spectrumList}; | ||
| 164 | return $self->get_index_by_id( $ref => $id ); | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | sub goto_spectrum { | ||
| 169 | |||
| 170 | my ($self, $idx) = @_; | ||
| 171 | my $ref = $self->{run}->{spectrumList}; | ||
| 172 | $self->goto($ref => $idx); | ||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | sub curr_spectrum_index { | ||
| 177 | |||
| 178 | my ($self) = @_; | ||
| 179 | my $ref = $self->{run}->{spectrumList}; | ||
| 180 | return $self->curr_index($ref); | ||
| 181 | |||
| 182 | } | ||
| 183 | |||
| 184 | sub _index_rt { | ||
| 185 | |||
| 186 | my ($self) = @_; | ||
| 187 | |||
| 188 | my @spectra; | ||
| 189 | my $ref = $self->{run}->{spectrumList}; | ||
| 190 | my $saved_pos = $ref->{__pos}; | ||
| 191 | $self->goto($ref => 0); | ||
| 192 | my $curr_pos = $ref->{__pos}; | ||
| 193 | while (my $spectrum = $self->next_spectrum) { | ||
| 194 | |||
| 195 | my $ret = $spectrum->rt; | ||
| 196 | push @spectra, [$curr_pos, $ret]; | ||
| 197 | $curr_pos = $ref->{__pos}; | ||
| 198 | |||
| 199 | } | ||
| 200 | @spectra = sort {$a->[1] <=> $b->[1]} @spectra; | ||
| 201 | $self->goto($ref => $saved_pos); | ||
| 202 | $self->{__rt_index} = [@spectra]; | ||
| 203 | |||
| 204 | # Since we took the time to index RTs, go ahead and store the updated | ||
| 205 | # structure to file | ||
| 206 | $self->_write_index; | ||
| 207 | |||
| 208 | return; | ||
| 209 | |||
| 210 | } | ||
| 211 | |||
| 212 | sub _write_index { | ||
| 213 | |||
| 214 | my ($self) = @_; | ||
| 215 | $self->{__memoized_refs} = [$self->{run}->{spectrumList}]; | ||
| 216 | return $self->SUPER::_write_index(); | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | sub _calc_sha1 { | ||
| 221 | |||
| 222 | my ($self) = @_; | ||
| 223 | |||
| 224 | my $fh = $self->{__fh}; | ||
| 225 | seek $fh, 0, 0; | ||
| 226 | |||
| 227 | my $sha1 = Digest::SHA->new(1); | ||
| 228 | local $/ = '>'; | ||
| 229 | while (my $chunk = <$fh>) { | ||
| 230 | $sha1->add($chunk); | ||
| 231 | last if (substr($chunk, -14) eq '<fileChecksum>'); | ||
| 232 | } | ||
| 233 | |||
| 234 | return $sha1->hexdigest; | ||
| 235 | |||
| 236 | } | ||
| 237 | |||
| 238 | sub get_tic { | ||
| 239 | |||
| 240 | my ($self, $force) = @_; | ||
| 241 | |||
| 242 | if (! $force) { | ||
| 243 | my $ref = $self->{run}->{chromatogramList}; | ||
| 244 | $self->goto($ref => 0); | ||
| 245 | while (my $c = $self->next_record($ref)) { | ||
| 246 | next if (! exists $c->{cvParam}->{&MS_TOTAL_ION_CURRENT_CHROMATOGRAM}); | ||
| 247 | return $c; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | return MS::Reader::MzML::Chromatogram->new(type => 'tic', raw => $self); | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | sub get_xic { | ||
| 256 | |||
| 257 | my ($self, %args) = @_; | ||
| 258 | return MS::Reader::MzML::Chromatogram->new(type => 'xic',raw => $self, %args); | ||
| 259 | |||
| 260 | } | ||
| 261 | |||
| 262 | sub get_bpc { | ||
| 263 | |||
| 264 | my ($self, $force) = @_; | ||
| 265 | |||
| 266 | if (! $force) { | ||
| 267 | my $ref = $self->{run}->{chromatogramList}; | ||
| 268 | $self->goto($ref => 0); | ||
| 269 | while (my $c = $self->next_record($ref)) { | ||
| 270 | next if (! exists $c->{cvParam}->{&MS_BASEPEAK_CHROMATOGRAM}); | ||
| 271 | return $c; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | return MS::Reader::MzML::Chromatogram->new(type => 'bpc',raw => $self); | ||
| 276 | |||
| 277 | } | ||
| 278 | |||
| 279 | sub n_spectra { | ||
| 280 | |||
| 281 | my ($self) = @_; | ||
| 282 | my $ref = $self->{run}->{spectrumList}; | ||
| 283 | return $self->record_count($ref); | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | sub id { return $_[0]->{run}->{id} } | ||
| 288 | |||
| 289 | 1; | ||
| 290 | |||
| 291 | __END__ | ||