Пример #1
0
def add_gaussv_noise(im: IntensityMatrix, scale: float, cutoff: int,
                     prop: float):
    """
    adds noise to an IntensityMatrix object

    :param im: the intensity matrix object
    :type im: pyms.IntensityMatrix.IntensityMatrix
    :param scale: the scale of the normal distribution from which the noise is drawn
    :type scale: float
    :param cutoff: The level below which the intensity of the ic at that point
        has no effect on the scale of the noise distribution
    :type cutoff: int
    :param scale: The scale of the normal distribution for ic values
    :type scale: int
    :param prop: For intensity values above the cutoff, the scale is
        multiplied by the ic value multiplied by prop
    :type prop: float

    :author: Sean O'Callaghan
    """

    n_scan, n_mz = im.size

    for i in range(n_mz):
        ic = im.get_ic_at_index(i)
        add_gaussv_noise_ic(ic, scale, cutoff, prop)
        im.set_ic_at_index(i, ic)
Пример #2
0
def peak_top_ion_areas(
    im: IntensityMatrix,
    peak: Peak,
    n_top_ions: int = 5,
    max_bound: int = 0,
) -> Dict[float, float]:
    """
	Calculate and return the ion areas of the five most abundant ions in the peak.

	:param im: The originating IntensityMatrix object.
	:param peak:
	:param n_top_ions: Number of top ions to return areas for.
	:param max_bound: Optional value to limit size of detected bound.

	:return: Dictionary of ``ion : ion_area pairs``.

	:authors: Sean O'Callaghan,  Dominic Davis-Foster (type assertions)
	"""

    if not isinstance(im, IntensityMatrix):
        raise TypeError("'im' must be an IntensityMatrix object")

    if not isinstance(peak, Peak):
        raise TypeError("'peak' must be a Peak object")

    if not isinstance(n_top_ions, int):
        raise TypeError("'n_top_ions' must be an integer")

    if not isinstance(max_bound, int):
        raise TypeError("'max_bound' must be an integer")

    # ms = peak.mass_spectrum
    rt = peak.rt
    apex = im.get_index_at_time(rt)

    ion_areas = {}  # Dictionary to store ion:ion_area pairs

    top_ions = peak.top_ions(n_top_ions)
    # print(top_ions)

    for ion in top_ions:
        ion_chrom = im.get_ic_at_mass(ion)
        # need ia as a list not numpy array so use .tolist()
        ia = ion_chrom.intensity_array.tolist()
        area, left, right, l_share, r_share = ion_area(ia, apex, max_bound)
        # need actual mass for single ion areas
        ion_areas[ion] = area

    return ion_areas
Пример #3
0
 def test_equality(self, im):
     assert im == IntensityMatrix(im.time_list, im.mass_list,
                                  im.intensity_array)
     assert im != test_string
     assert im != test_int
     assert im != test_float
     assert im != test_tuple
     assert im != test_list_ints
     assert im != test_list_strs
Пример #4
0
def add_gaussc_noise(im: IntensityMatrix, scale: float):
    """
    Adds noise to an IntensityMatrix object

    :param im: the intensity matrix object
    :type im: pyms.IntensityMatrix.IntensityMatrix

    :param scale: the scale of the normal distribution from which the noise is drawn
    :type scale: float

    :author: Sean O'Callaghan
    """

    n_scan, n_mz = im.size

    for i in range(n_mz):
        ic = im.get_ic_at_index(i)
        add_gaussc_noise_ic(ic, scale)
        im.set_ic_at_index(i, ic)
Пример #5
0
def gcms_sim(
    time_list: List[float],
    mass_list: List[float],
    peak_list: List[Peak.Peak],
) -> IntensityMatrix:
    """
	Simulator of GCMS data.

	:param time_list: the list of scan times
	:param mass_list: the list of m/z channels
	:param peak_list: A list of peaks

	:return: A simulated Intensity Matrix object

	:author: Sean O'Callaghan
	"""

    n_mz = len(mass_list)
    n_scan = len(time_list)

    t1 = time_list[0]
    period = time_list[1] - t1

    # initialise a 2D numpy array for intensity matrix
    i_array = numpy.zeros((n_scan, n_mz), 'd')

    for peak in peak_list:
        print('-', end='')
        index = int((peak.rt - t1) / period)

        height = sum(peak.mass_spectrum.mass_spec)
        # standard deviation = area/(height * sqrt(2/pi))
        sigma: float = peak.area / (height *
                                    (math.sqrt(2 * math.pi)))  # type: ignore
        print("width", sigma)

        for i in range(len(peak.mass_spectrum.mass_list)):
            ion_height = peak.mass_spectrum.mass_spec[i]
            ic = chromatogram(n_scan, index, sigma, ion_height)
            i_array[:, i] += ic

    im = IntensityMatrix(time_list, mass_list, i_array)

    return im
Пример #6
0
def gcms_sim(time_list: List, mass_list: List,
             peak_list: Peak) -> IntensityMatrix:
    """
    Simulator of GCMS data

    :param time_list: the list of scan times
    :type time_list: list
    :param mass_list: the list of m/z channels
    :type mass_list: list
    :param peak_list: A list of peaks
    :type peak_list: :class:`list` of :class:`pyms.Peak.Class.Peak` objects

    :return: A simulated Intensity Matrix object
    :rtype: pyms.IntensityMatrix.IntensityMatrix

    :author: Sean O'Callaghan
    """

    n_mz = len(mass_list)
    n_scan = len(time_list)

    t1 = time_list[0]
    period = time_list[1] - t1

    # initialise a 2D numpy array for intensity matrix
    i_array = numpy.zeros((n_scan, n_mz), 'd')

    for peak in peak_list:
        print("-", end='')
        index = int((peak.rt - t1) / period)
        height = sum(peak.get_mass_spectrum().mass_spec)
        # standard deviation = area/(height * sqrt(2/pi))
        sigma = peak.area / (height * (math.sqrt(2 * math.pi)))
        print("width", sigma)
        for i in range(len(peak.get_mass_spectrum().mass_list)):
            ion_height = peak.get_mass_spectrum().mass_spec[i]
            ic = chromatogram(n_scan, index, sigma, ion_height)
            i_array[:, i] += ic

    im = IntensityMatrix(time_list, mass_list, i_array)

    return im
Пример #7
0
 def test_intensity_array_errors(self, obj, im, expects):
     with pytest.raises(expects):
         IntensityMatrix(im.time_list, im.mass_list, obj)
Пример #8
0
 def test_mass_list_errors(self, obj, im, expects):
     with pytest.raises(TypeError):
         IntensityMatrix(im.time_list, obj, im.intensity_array)
Пример #9
0
    def test_creation(self, im):
        assert isinstance(im, IntensityMatrix)

        IntensityMatrix(im.time_list, im.mass_list, im.intensity_array)