@@ -183,26 +183,29 @@ def __next__(self):
183183
184184 def __getitem__ (self , identifier ):
185185 """
186- Access spectrum with native id 'identifier'.
186+ Access spectrum or chromatogram with native id 'identifier'.
187187
188188 Arguments:
189189 identifier (str or int): last number in the id tag of the spectrum
190- element
190+ element or a chromatogram identifier like 'TIC'
191191
192192 Returns:
193193 spectrum (Spectrum or Chromatogram): spectrum/chromatogram object
194194 with native id 'identifier'
195195 """
196196 try :
197- if int (identifier ) > self .get_spectrum_count ():
197+ if isinstance (identifier , int ) and identifier > self .get_spectrum_count ():
198198 raise Exception ("Requested identifier is out of range" )
199199 except :
200200 pass
201- spectrum = self .info ["file_object" ][identifier ]
202- spectrum .obo_translator = self .OT
203- if isinstance (spectrum , spec .Spectrum ):
204- spectrum .measured_precision = self .ms_precisions [spectrum .ms_level ]
205- return spectrum
201+
202+ element = self .info ["file_object" ][identifier ]
203+ element .obo_translator = self .OT
204+
205+ if isinstance (element , spec .Spectrum ):
206+ element .measured_precision = self .ms_precisions [element .ms_level ]
207+
208+ return element
206209
207210 def __enter__ (self ):
208211 return self
@@ -454,6 +457,74 @@ def get_chromatogram_count(self):
454457 chromatogram count (int): Number of chromatograms in file.
455458 """
456459 return self .info ["chromatogram_count" ]
460+
461+ def get_spectrum (self , identifier ):
462+ """
463+ Access spectrum with the given identifier.
464+
465+ Arguments:
466+ identifier (str or int): Either a string identifier or an index (0-based)
467+ to access spectra in order.
468+
469+ Returns:
470+ spectrum (Spectrum): spectrum object with the given identifier
471+
472+ Note:
473+ This method provides the same functionality as using the indexing syntax
474+ (e.g., run[0]), but with a more explicit method name.
475+ """
476+ return self [identifier ]
477+
478+ def get_chromatogram (self , identifier ):
479+ """
480+ Access chromatogram with the given identifier.
481+
482+ Arguments:
483+ identifier (str or int): Either a string identifier like 'TIC' or
484+ an index (0-based) to access chromatograms in order.
485+
486+ Returns:
487+ chromatogram (Chromatogram): chromatogram object with the given identifier
488+
489+ Note:
490+ This method is only useful when skip_chromatogram is set to False
491+ if you want to access chromatograms by index. If skip_chromatogram is True,
492+ you can still access chromatograms by string identifiers (e.g., 'TIC').
493+ """
494+ if isinstance (identifier , str ):
495+ return self [identifier ]
496+
497+ if isinstance (identifier , int ):
498+ if self .get_chromatogram_count () is None :
499+ raise Exception ("No chromatograms found in the file" )
500+
501+ if identifier >= self .get_chromatogram_count ():
502+ raise Exception (f"Chromatogram index { identifier } is out of range (0-{ self .get_chromatogram_count ()- 1 } )" )
503+
504+ # Reset the file pointer and iterate to find the chromatogram
505+ temp_skip_chromatogram = self .skip_chromatogram
506+ self .skip_chromatogram = False
507+
508+ self .info ["file_object" ].close ()
509+ self .info ["file_object" ] = self ._open_file (
510+ self .path_or_file , build_index_from_scratch = False
511+ )
512+ self .iter = self ._init_iter ()
513+
514+ chrom_count = 0
515+ try :
516+ for element in self :
517+ if isinstance (element , spec .Chromatogram ):
518+ if chrom_count == identifier :
519+ return element
520+ chrom_count += 1
521+ finally :
522+ # Restore original skip_chromatogram setting
523+ self .skip_chromatogram = temp_skip_chromatogram
524+
525+ raise Exception (f"Chromatogram with index { identifier } not found" )
526+
527+ raise ValueError ("Identifier must be a string or an integer" )
457528
458529 def close (self ):
459530 self .info ["file_object" ].close ()
0 commit comments