corems.mass_spectra.input.brukerSolarix_utils

Utility functions for Bruker data processing.

 1"""Utility functions for Bruker data processing."""
 2
 3
 4
 5def get_scan_attributes(scan_attr, imaging_info_attr) -> dict:
 6    """
 7    Get the scan attributes from the scan.xml or ImagingInfo.xml file.
 8
 9    Parameters
10    ----------
11    d_directory_location : str, Path, or S3Path
12        Directory containing the XML files
13
14    Returns
15    -------
16    dict
17        Dictionary containing the scan number as key and a tuple of retention time, TIC,
18        and optionally maxpeak and spotname as values.
19
20
21    TODO: We need to reformat the dictionary to actually include keys and values so it is self-descriptive.
22    TODO: This will break the code, so a new version is needed.
23    TODO: Will need to make sure theres tests which capture this change.
24
25    """
26    from bs4 import BeautifulSoup
27
28    scan_xml_exists = scan_attr.exists()
29    imaging_info_exists = imaging_info_attr.exists()
30
31    if scan_xml_exists:
32        try:
33            soup = BeautifulSoup(scan_attr.open(), "xml")
34            list_rt = [float(rt.text) for rt in soup.find_all("minutes")]
35            list_tic = [float(tic.text) for tic in soup.find_all("tic")]
36            list_scan = [int(scan.text) for scan in soup.find_all("count")]
37
38            # Check if maxpeak exists (more comprehensive version)
39            # TODO: Enable this, but it could break code so a new version is needed
40            enable_maxpeak = False
41            if enable_maxpeak:
42                maxpeak_elements = soup.find_all("maxpeak")
43                if maxpeak_elements:
44                    list_maxpeak = [float(maxpeak.text) for maxpeak in maxpeak_elements]
45                    dict_scan_rt_tic = dict(
46                        zip(list_scan, zip(list_rt, list_tic, list_maxpeak))
47                    )
48                else:
49                    dict_scan_rt_tic = dict(zip(list_scan, zip(list_rt, list_tic)))
50
51            dict_scan_rt_tic = dict(zip(list_scan, zip(list_rt, list_tic)))
52
53            return dict_scan_rt_tic
54        except Exception as e:
55            raise FileNotFoundError(f"Error reading scan.xml: {e}")
56    elif imaging_info_exists:
57        try:
58            soup = BeautifulSoup(imaging_info_attr.open(), "xml")
59            list_rt = [float(rt.text) for rt in soup.find_all("minutes")]
60            list_tic = [float(tic.text) for tic in soup.find_all("tic")]
61            list_maxpeak = [float(maxpeak.text) for maxpeak in soup.find_all("maxpeak")]
62            list_scan = [int(scan.find("count").text) for scan in soup.find_all("scan")]
63            list_spotname = [
64                scan.find("spotName").text for scan in soup.find_all("scan")
65            ]
66            dict_scan_rt_tic = dict(
67                zip(list_scan, zip(list_rt, list_tic, list_maxpeak, list_spotname))
68            )
69            return dict_scan_rt_tic
70        except Exception as e:
71            raise FileNotFoundError(f"Error reading ImagingInfo.xml: {e}")
72    else:
73        raise FileNotFoundError(
74            "Dataset does not contain a 'scan.xml' or 'ImagingInfo.xml' file."
75        )
def get_scan_attributes(scan_attr, imaging_info_attr) -> dict:
 6def get_scan_attributes(scan_attr, imaging_info_attr) -> dict:
 7    """
 8    Get the scan attributes from the scan.xml or ImagingInfo.xml file.
 9
10    Parameters
11    ----------
12    d_directory_location : str, Path, or S3Path
13        Directory containing the XML files
14
15    Returns
16    -------
17    dict
18        Dictionary containing the scan number as key and a tuple of retention time, TIC,
19        and optionally maxpeak and spotname as values.
20
21
22    TODO: We need to reformat the dictionary to actually include keys and values so it is self-descriptive.
23    TODO: This will break the code, so a new version is needed.
24    TODO: Will need to make sure theres tests which capture this change.
25
26    """
27    from bs4 import BeautifulSoup
28
29    scan_xml_exists = scan_attr.exists()
30    imaging_info_exists = imaging_info_attr.exists()
31
32    if scan_xml_exists:
33        try:
34            soup = BeautifulSoup(scan_attr.open(), "xml")
35            list_rt = [float(rt.text) for rt in soup.find_all("minutes")]
36            list_tic = [float(tic.text) for tic in soup.find_all("tic")]
37            list_scan = [int(scan.text) for scan in soup.find_all("count")]
38
39            # Check if maxpeak exists (more comprehensive version)
40            # TODO: Enable this, but it could break code so a new version is needed
41            enable_maxpeak = False
42            if enable_maxpeak:
43                maxpeak_elements = soup.find_all("maxpeak")
44                if maxpeak_elements:
45                    list_maxpeak = [float(maxpeak.text) for maxpeak in maxpeak_elements]
46                    dict_scan_rt_tic = dict(
47                        zip(list_scan, zip(list_rt, list_tic, list_maxpeak))
48                    )
49                else:
50                    dict_scan_rt_tic = dict(zip(list_scan, zip(list_rt, list_tic)))
51
52            dict_scan_rt_tic = dict(zip(list_scan, zip(list_rt, list_tic)))
53
54            return dict_scan_rt_tic
55        except Exception as e:
56            raise FileNotFoundError(f"Error reading scan.xml: {e}")
57    elif imaging_info_exists:
58        try:
59            soup = BeautifulSoup(imaging_info_attr.open(), "xml")
60            list_rt = [float(rt.text) for rt in soup.find_all("minutes")]
61            list_tic = [float(tic.text) for tic in soup.find_all("tic")]
62            list_maxpeak = [float(maxpeak.text) for maxpeak in soup.find_all("maxpeak")]
63            list_scan = [int(scan.find("count").text) for scan in soup.find_all("scan")]
64            list_spotname = [
65                scan.find("spotName").text for scan in soup.find_all("scan")
66            ]
67            dict_scan_rt_tic = dict(
68                zip(list_scan, zip(list_rt, list_tic, list_maxpeak, list_spotname))
69            )
70            return dict_scan_rt_tic
71        except Exception as e:
72            raise FileNotFoundError(f"Error reading ImagingInfo.xml: {e}")
73    else:
74        raise FileNotFoundError(
75            "Dataset does not contain a 'scan.xml' or 'ImagingInfo.xml' file."
76        )

Get the scan attributes from the scan.xml or ImagingInfo.xml file.

Parameters
  • d_directory_location (str, Path, or S3Path): Directory containing the XML files
Returns
  • dict: Dictionary containing the scan number as key and a tuple of retention time, TIC, and optionally maxpeak and spotname as values.
  • TODO (We need to reformat the dictionary to actually include keys and values so it is self-descriptive.):

  • TODO (This will break the code, so a new version is needed.):

  • TODO (Will need to make sure theres tests which capture this change.):