corems.mass_spectra.input.massList
1__author__ = "Yuri E. Corilo" 2__date__ = "Jun 12, 2019" 3 4 5from pathlib import Path 6from threading import Thread 7import warnings 8from corems.mass_spectrum.input.massList import ReadCoremsMasslist 9from corems.mass_spectra.factory.lc_class import LCMSBase 10 11 12class ReadCoremsMassSpectraText(ReadCoremsMasslist, Thread): 13 """ 14 Class for reading CoreMS mass spectra from a text file. 15 16 Parameters 17 ---------- 18 file_location : str, pathlib.Path, or s3path.S3Path 19 Path object from pathlib containing the file location 20 analyzer : str, optional 21 Name of the analyzer, by default 'Unknown' 22 instrument_label : str, optional 23 Label of the instrument, by default 'Unknown' 24 25 Attributes 26 ---------- 27 lcms : LCMSBase 28 LCMSBase object for storing the mass spectra data. 29 30 Methods 31 ------- 32 * get_scans_filename(). Get the filenames of all the scan files associated with the CoreMS file. 33 * set_filepath_datatype_and_delimiter(file_path_obj). Set the file path, data type, and delimiter based on the file path object. 34 * import_mass_spectra(). Import the mass spectra from the scan files and add them to the LCMSBase object. 35 * run(). Run the import_mass_spectra method to create the LCMSBase object. 36 * get_lcms_obj(). Get the LCMSBase object. 37 """ 38 39 def __init__(self, file_location, analyzer="Unknown", instrument_label="Unknown"): 40 if isinstance(file_location, str): 41 # if obj is a string it defaults to create a Path obj, pass the S3Path if needed 42 file_location = Path(file_location) 43 44 if not file_location.exists(): 45 raise FileNotFoundError("%s not found" % file_location) 46 47 if not file_location.suffix == ".corems": 48 raise TypeError("%s is not a valid CoreMS file" % file_location) 49 50 Thread.__init__(self) 51 52 ReadCoremsMasslist.__init__(self, file_location) 53 54 self.lcms = LCMSBase( 55 self.file_location, analyzer=analyzer, instrument_label=instrument_label 56 ) 57 58 def get_scans_filename(self) -> list: 59 all_other = self.file_location.glob("*_scan*[!.json]") 60 61 scans_filepath = [ 62 (file_path_obj.stem.split("scan")[1], file_path_obj) 63 for file_path_obj in all_other 64 ] 65 66 scans_filepath.sort(key=lambda m: int(m[0])) 67 68 return scans_filepath 69 70 def set_filepath_datatype_and_delimiter(self, file_path_obj) -> None: 71 self.file_location = file_path_obj 72 73 if file_path_obj.suffix == ".pkl": 74 self.data_type == "dataframe" 75 76 else: 77 if file_path_obj.suffix == ".csv": 78 self.data_type == "txt" 79 self.delimiter = "," 80 81 elif file_path_obj.suffix == ".xlsx": 82 self.data_type == "excel" 83 self.delimiter = "," 84 85 elif file_path_obj.suffix == ".txt": 86 self.data_type == "txt" 87 self.delimiter = "\t" 88 warnings.warn("using tab as delimiter") 89 else: 90 raise NotImplementedError( 91 "%s data not yet supported " % file_path_obj.suffix 92 ) 93 94 def import_mass_spectra(self) -> None: 95 list_rt, list_tic, list_scan = list(), list(), list() 96 97 for scan_number, file_path_obj in self.get_scans_filename(): 98 self.set_filepath_datatype_and_delimiter(file_path_obj) 99 100 mass_spec = self.get_mass_spectrum(int(scan_number)) 101 102 list_scan.append(int(scan_number)) 103 104 list_rt.append(mass_spec.retention_time) 105 106 list_tic.append(mass_spec.tic) 107 108 self.lcms.add_mass_spectrum(mass_spec) 109 110 self.lcms.retention_time = list_rt 111 self.lcms.tic_list = list_tic # TODO: check if this is correct 112 self.lcms.scans_number = list_scan 113 114 def run(self) -> None: 115 """Creates the LCMS object and imports mass spectra.""" 116 117 self.import_mass_spectra() 118 119 def get_lcms_obj(self) -> LCMSBase: 120 """ 121 Returns the LCMSBase object associated with the massList. 122 123 If the LCMSBase object is already initialized, it is returned. 124 Otherwise, an exception is raised. 125 126 Raises: 127 Exception: If the LCMSBase object is not initialized. 128 """ 129 if self.lcms: 130 return self.lcms 131 else: 132 raise Exception("returning an empty lcms class")
13class ReadCoremsMassSpectraText(ReadCoremsMasslist, Thread): 14 """ 15 Class for reading CoreMS mass spectra from a text file. 16 17 Parameters 18 ---------- 19 file_location : str, pathlib.Path, or s3path.S3Path 20 Path object from pathlib containing the file location 21 analyzer : str, optional 22 Name of the analyzer, by default 'Unknown' 23 instrument_label : str, optional 24 Label of the instrument, by default 'Unknown' 25 26 Attributes 27 ---------- 28 lcms : LCMSBase 29 LCMSBase object for storing the mass spectra data. 30 31 Methods 32 ------- 33 * get_scans_filename(). Get the filenames of all the scan files associated with the CoreMS file. 34 * set_filepath_datatype_and_delimiter(file_path_obj). Set the file path, data type, and delimiter based on the file path object. 35 * import_mass_spectra(). Import the mass spectra from the scan files and add them to the LCMSBase object. 36 * run(). Run the import_mass_spectra method to create the LCMSBase object. 37 * get_lcms_obj(). Get the LCMSBase object. 38 """ 39 40 def __init__(self, file_location, analyzer="Unknown", instrument_label="Unknown"): 41 if isinstance(file_location, str): 42 # if obj is a string it defaults to create a Path obj, pass the S3Path if needed 43 file_location = Path(file_location) 44 45 if not file_location.exists(): 46 raise FileNotFoundError("%s not found" % file_location) 47 48 if not file_location.suffix == ".corems": 49 raise TypeError("%s is not a valid CoreMS file" % file_location) 50 51 Thread.__init__(self) 52 53 ReadCoremsMasslist.__init__(self, file_location) 54 55 self.lcms = LCMSBase( 56 self.file_location, analyzer=analyzer, instrument_label=instrument_label 57 ) 58 59 def get_scans_filename(self) -> list: 60 all_other = self.file_location.glob("*_scan*[!.json]") 61 62 scans_filepath = [ 63 (file_path_obj.stem.split("scan")[1], file_path_obj) 64 for file_path_obj in all_other 65 ] 66 67 scans_filepath.sort(key=lambda m: int(m[0])) 68 69 return scans_filepath 70 71 def set_filepath_datatype_and_delimiter(self, file_path_obj) -> None: 72 self.file_location = file_path_obj 73 74 if file_path_obj.suffix == ".pkl": 75 self.data_type == "dataframe" 76 77 else: 78 if file_path_obj.suffix == ".csv": 79 self.data_type == "txt" 80 self.delimiter = "," 81 82 elif file_path_obj.suffix == ".xlsx": 83 self.data_type == "excel" 84 self.delimiter = "," 85 86 elif file_path_obj.suffix == ".txt": 87 self.data_type == "txt" 88 self.delimiter = "\t" 89 warnings.warn("using tab as delimiter") 90 else: 91 raise NotImplementedError( 92 "%s data not yet supported " % file_path_obj.suffix 93 ) 94 95 def import_mass_spectra(self) -> None: 96 list_rt, list_tic, list_scan = list(), list(), list() 97 98 for scan_number, file_path_obj in self.get_scans_filename(): 99 self.set_filepath_datatype_and_delimiter(file_path_obj) 100 101 mass_spec = self.get_mass_spectrum(int(scan_number)) 102 103 list_scan.append(int(scan_number)) 104 105 list_rt.append(mass_spec.retention_time) 106 107 list_tic.append(mass_spec.tic) 108 109 self.lcms.add_mass_spectrum(mass_spec) 110 111 self.lcms.retention_time = list_rt 112 self.lcms.tic_list = list_tic # TODO: check if this is correct 113 self.lcms.scans_number = list_scan 114 115 def run(self) -> None: 116 """Creates the LCMS object and imports mass spectra.""" 117 118 self.import_mass_spectra() 119 120 def get_lcms_obj(self) -> LCMSBase: 121 """ 122 Returns the LCMSBase object associated with the massList. 123 124 If the LCMSBase object is already initialized, it is returned. 125 Otherwise, an exception is raised. 126 127 Raises: 128 Exception: If the LCMSBase object is not initialized. 129 """ 130 if self.lcms: 131 return self.lcms 132 else: 133 raise Exception("returning an empty lcms class")
Class for reading CoreMS mass spectra from a text file.
Parameters
- file_location (str, pathlib.Path, or s3path.S3Path): Path object from pathlib containing the file location
- analyzer (str, optional): Name of the analyzer, by default 'Unknown'
- instrument_label (str, optional): Label of the instrument, by default 'Unknown'
Attributes
- lcms (LCMSBase): LCMSBase object for storing the mass spectra data.
Methods
- get_scans_filename(). Get the filenames of all the scan files associated with the CoreMS file.
- set_filepath_datatype_and_delimiter(file_path_obj). Set the file path, data type, and delimiter based on the file path object.
- import_mass_spectra(). Import the mass spectra from the scan files and add them to the LCMSBase object.
- run(). Run the import_mass_spectra method to create the LCMSBase object.
- get_lcms_obj(). Get the LCMSBase object.
40 def __init__(self, file_location, analyzer="Unknown", instrument_label="Unknown"): 41 if isinstance(file_location, str): 42 # if obj is a string it defaults to create a Path obj, pass the S3Path if needed 43 file_location = Path(file_location) 44 45 if not file_location.exists(): 46 raise FileNotFoundError("%s not found" % file_location) 47 48 if not file_location.suffix == ".corems": 49 raise TypeError("%s is not a valid CoreMS file" % file_location) 50 51 Thread.__init__(self) 52 53 ReadCoremsMasslist.__init__(self, file_location) 54 55 self.lcms = LCMSBase( 56 self.file_location, analyzer=analyzer, instrument_label=instrument_label 57 )
This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.
args is a list or tuple of arguments for the target invocation. Defaults to ().
kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.
71 def set_filepath_datatype_and_delimiter(self, file_path_obj) -> None: 72 self.file_location = file_path_obj 73 74 if file_path_obj.suffix == ".pkl": 75 self.data_type == "dataframe" 76 77 else: 78 if file_path_obj.suffix == ".csv": 79 self.data_type == "txt" 80 self.delimiter = "," 81 82 elif file_path_obj.suffix == ".xlsx": 83 self.data_type == "excel" 84 self.delimiter = "," 85 86 elif file_path_obj.suffix == ".txt": 87 self.data_type == "txt" 88 self.delimiter = "\t" 89 warnings.warn("using tab as delimiter") 90 else: 91 raise NotImplementedError( 92 "%s data not yet supported " % file_path_obj.suffix 93 )
95 def import_mass_spectra(self) -> None: 96 list_rt, list_tic, list_scan = list(), list(), list() 97 98 for scan_number, file_path_obj in self.get_scans_filename(): 99 self.set_filepath_datatype_and_delimiter(file_path_obj) 100 101 mass_spec = self.get_mass_spectrum(int(scan_number)) 102 103 list_scan.append(int(scan_number)) 104 105 list_rt.append(mass_spec.retention_time) 106 107 list_tic.append(mass_spec.tic) 108 109 self.lcms.add_mass_spectrum(mass_spec) 110 111 self.lcms.retention_time = list_rt 112 self.lcms.tic_list = list_tic # TODO: check if this is correct 113 self.lcms.scans_number = list_scan
115 def run(self) -> None: 116 """Creates the LCMS object and imports mass spectra.""" 117 118 self.import_mass_spectra()
Creates the LCMS object and imports mass spectra.
120 def get_lcms_obj(self) -> LCMSBase: 121 """ 122 Returns the LCMSBase object associated with the massList. 123 124 If the LCMSBase object is already initialized, it is returned. 125 Otherwise, an exception is raised. 126 127 Raises: 128 Exception: If the LCMSBase object is not initialized. 129 """ 130 if self.lcms: 131 return self.lcms 132 else: 133 raise Exception("returning an empty lcms class")
Returns the LCMSBase object associated with the massList.
If the LCMSBase object is already initialized, it is returned. Otherwise, an exception is raised.
Raises: Exception: If the LCMSBase object is not initialized.
Inherited Members
- corems.mass_spectrum.input.massList.ReadCoremsMasslist
- get_mass_spectrum
- restore_peak_calibration
- add_molecular_formula
- corems.mass_spectrum.input.baseClass.MassListBaseClass
- file_location
- header_lines
- isCentroid
- isThermoProfile
- headerless
- analyzer
- instrument_label
- sample_name
- parameters
- set_parameter_from_toml
- set_parameter_from_json
- data_type
- delimiter
- encoding_detector
- set_data_type
- get_dataframe
- load_settings
- get_output_parameters
- clean_data_frame
- check_columns
- read_xml_peaks
- get_xml_polarity