Exemplo n.º 1
0
    def write_intensities_stream(self, file_name):

        """
        @summary: Writes all intensities to a file

        @param file_name: Output file name
        @type file_name: StringType

        This function loop over all scans, and for each scan
        writes intensities to the file, one intenisity per
        line. Intensities from different scans are joined
        without any delimiters.

        @author: Vladimir Likic
        """

        if not is_str(file_name):
            error("'file_name' must be a string")

        N = len(self.__scan_list)

        print" -> Writing scans to a file"

        fp = open_for_writing(file_name)

        for ii in range(len(self.__scan_list)):
            scan = self.__scan_list[ii]
            intensities = scan.get_intensity_list()
            for I in intensities:
                fp.write("%8.4f\n" % ( I ) )

        close_for_writing(fp)
Exemplo n.º 2
0
    def __init__(self, time_list, scan_list):

        """
        @summary: Initialize the GC-MS data

        @param time_list: List of scan retention times
        @type time_list: ListType
        @param scan_list: List of Scan objects
        @type scan_list: ListType

        @author: Qiao Wang
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_list(time_list) or not is_number(time_list[0]):
            error("'time_list' must be a list of numbers")

        if not is_list(scan_list) or not isinstance(scan_list[0], Scan):
            error("'scan_list' must be a list of Scan objects")

        self.__set_time(time_list)
        self.__scan_list = scan_list
        self.__set_min_max_mass()
        self.__calc_tic()
Exemplo n.º 3
0
def rel_threshold(pl, percent=2):

    """
    @summary: Remove ions with relative intensities less than the given
        relative percentage of the maximum intensity.

    @param pl: A list of Peak objects
    @type pl: ListType
    @param percent: Threshold for relative percentage of intensity (Default 2%)
    @type percent: FloatType

    @return: A new list of Peak objects with threshold ions
    @rtype: ListType

    @author: Andrew Isaac
    """

    if not is_number(percent) or percent <= 0:
        error("'percent' must be a number > 0")

    pl_copy = copy.deepcopy(pl)
    new_pl = []
    for p in pl_copy:
        ms = p.get_mass_spectrum()
        ia = ms.mass_spec
        # assume max(ia) big so /100 1st
        cutoff = (max(ia)/100.0)*float(percent)
        for i in range(len(ia)):
            if ia[i] < cutoff:
                ia[i] = 0
        ms.mass_spec = ia
        p.set_mass_spectrum(ms)
        new_pl.append(p)
    return new_pl
Exemplo n.º 4
0
def amin(v):

    """
    @summary: Finds the minimum element in a list or array

    @param v: A list or array
    @type v: ListType, TupleType, or numpy.core.ndarray

    @return: Tuple (maxi, maxv), where maxv is the minimum 
        element in the list and maxi is its index
    @rtype: TupleType

    @author: Vladimir Likic
    """

    if not is_list(v):
        error("argument neither list nor array")

    minv = max(v) # built-in max() function
    mini = None

    for ii in range(len(v)):
        if v[ii] < minv:
            minv = v[ii]
            mini = ii

    if mini == None:
        error("finding maximum failed")

    return mini, minv
Exemplo n.º 5
0
def read_expr_list(file_name):
    """
    @summary: Reads the set of experiment files and returns a list of
    Experiment objects

    @param file_name: The name of the file which lists experiment
        dump file names, one file per line
    @type file_name: StringType

    @return: A list of Experiment instances
    @rtype: ListType

    @author: Vladimir Likic
    """

    if not is_str(file_name):
        error("file_name argument must be a string")
    try:
        fp = open(file_name, 'r')
    except IOError:
        error("error opening file '%s' for reading" % file_name)

    exprfiles = fp.readlines()
    fp.close()

    exprl = []

    for exprfile in exprfiles:

        exprfile = string.strip(exprfile)
        expr = load_expr(exprfile)

        exprl.append(expr)

    return exprl
Exemplo n.º 6
0
def plot_ic(ic, line_label=" ", plot_title=" "):
    """
    @summary: Plots an Ion Chromatogram or List of same
    
    @param ic: The ion chromatogram
    @type ic: pyms.GCMS.Class.IonChromatogram
    
    @param line_label: plot legend
    @type line_label: stringType
    
    @param plot_title: A label for the plot
    @type plot_title: String Type
    
    @author: Sean O'Callaghan
    """

    #Plotting Variables
    fig = plt.figure()
    ax = fig.add_subplot(111)

    if not isinstance(ic, IonChromatogram):
        error("ics argument must be an IonChromatogram\
            or a list of Ion Chromatograms")

    time_list = ic.get_time_list()

    intensity_list = ic.get_intensity_array()

    ic_plot = plt.plot(time_list, intensity_list, label=line_label)

    t = ax.set_title(plot_title)
    l = ax.legend()

    fig.canvas.draw
    plt.show()
Exemplo n.º 7
0
    def __init__(self, expr):

        """
        @param expr: The experiment to be converted into an alignment object
        @type expr: pyms.Experiment.Class.Experiment

        @author: Woon Wai Keen
        @author: Qiao Wang
        @author: Vladimir Likic
        """
        if expr == None:
            self.peakpos = []
            self.peakalgt = []
            self.expr_code =  []
            self.similarity = None
        else:
            if not isinstance(expr, Experiment):
                error("'expr' must be an Experiment object")
            #for peak in expr.get_peak_list():
            #    if peak.get_area() == None or peak.get_area() <= 0:
            #        error("All peaks must have an area for alignment")
            self.peakpos = [ copy.deepcopy(expr.get_peak_list()) ]
            self.peakalgt = numpy.transpose(self.peakpos)
            self.expr_code =  [ expr.get_expr_code() ]
            self.similarity = None
Exemplo n.º 8
0
    def plot_peaks(self, peak_list, label = "Peaks"):
	
        """
        @summary: Plots the locations of peaks as found
		  by PyMS.
		
	@param peak_list: List of peaks
	@type peak_list: list of pyms.Peak.Class.Peak
		
	@param label: label for plot legend
	@type label: StringType
        """
        
        if not isinstance(peak_list, list):
            error("peak_list is not a list")
		
	time_list = []
	height_list=[]
        
        # Copy to self.__peak_list for onclick event handling
        self.__peak_list = peak_list
	
	for peak in peak_list:
	    time_list.append(peak.get_rt())
	    height_list.append(sum(peak.get_mass_spectrum().mass_spec))
		
	self.__tic_ic_plots.append(plt.plot(time_list, height_list, 'o',\
	    label = label))
Exemplo n.º 9
0
def vector_by_step(vstart,vstop,vstep):

    """
    @summary: generates a list by using start, stop, and step values

    @param vstart: Initial value 
    @type vstart: A number
    @param vstop: Max value
    @type vstop: A number
    @param vstep: Step
    @type vstep: A number
   
    @return: A list generated
    @rtype: ListType

    @author: Vladimir Likic
    """

    if not is_number(vstart) or not is_number(vstop) or not is_number(vstep):
        error("parameters start, stop, step must be numbers")

    v = []

    p = vstart 
    while p < vstop:
        v.append(p)
        p = p + vstep

    return v
Exemplo n.º 10
0
def MAD(v):

    """
    @summary: median absolute deviation

    @param v: A list or array
    @type v: ListType, TupleType, or numpy.core.ndarray

    @return: median absolute deviation
    @rtype: FloatType

    @author: Vladimir Likic
    """

    if not is_list(v):
        error("argument neither list nor array")

    m = median(v)
    m_list = []

    for xi in v:
        d = math.fabs(xi - m)
        m_list.append(d)

    mad = median(m_list)/0.6745

    return mad
Exemplo n.º 11
0
def median(v):

    """
    @summary: Returns a median of a list or numpy array

    @param v: Input list or array
    @type v: ListType or numpy.core.ndarray
    @return: The median of the input list
    @rtype: FloatType

    @author: Vladimir Likic
    """

    if not is_list(v):
        error("argument neither list nor array")

    local_data = copy.deepcopy(v)
    local_data.sort()
    N = len(local_data)

    if (N % 2) == 0:
        # even number of points
        K = N/2 - 1 
        median = (local_data[K] + local_data[K+1])/2.0
    else:
	    # odd number of points
        K = (N - 1)/2 - 1
        median = local_data[K+1]

    return median
Exemplo n.º 12
0
def rmsd(list1, list2):

    """
    @summary: Calculates RMSD for the 2 lists

    @param list1: First data set
    @type list1: ListType, TupleType, or numpy.core.ndarray 
    @param list2: Second data set
    @type list2: ListType, TupleType, or numpy.core.ndarray 
    @return: RMSD value
    @rtype: FloatType

    @author: Qiao Wang
    @author: Andrew Isaac
    @author: Vladimir Likic
    """

    if not is_list(list1):
        error("argument neither list nor array")

    if not is_list(list2):
        error("argument neither list nor array")

    sum = 0.0
    for i in range(len(list1)):
        sum = sum + (list1[i] - list2[i]) ** 2
    rmsd = math.sqrt(sum / len(list1))
    return rmsd
Exemplo n.º 13
0
def std(v):

    """
    @summary: Calculates standard deviation

    @param v: A list or array
    @type v: ListType, TupleType, or numpy.core.ndarray

    @return: Mean
    @rtype: FloatType

    @author: Vladimir Likic
    """

    if not is_list(v):
        error("argument neither list nor array")

    v_mean = mean(v)

    s = 0.0 
    for e in v:
        d = e - v_mean
        s = s + d*d
    s_mean = s/float(len(v)-1)
    v_std = math.sqrt(s_mean)

    return v_std
Exemplo n.º 14
0
    def get_ic_at_index(self, ix):

        """
        @summary: Returns the ion chromatogram at the specified index

        @param ix: Index of an ion chromatogram in the intensity data
            matrix
        @type ix: IntType

        @return: Ion chromatogram at given index
        @rtype: IonChromatogram

        @author: Qiao Wang
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_int(ix):
            error("index not an integer")
        try:
            ia = []
            for i in range(len(self.__intensity_matrix)):
                ia.append(self.__intensity_matrix[i][ix])
        except IndexError:
            error("index out of bounds.")

        ic_ia = numpy.array(ia)
        mass = self.get_mass_at_index(ix)
        rt = copy.deepcopy(self.__time_list)

        return IonChromatogram(ic_ia, rt, mass)
Exemplo n.º 15
0
    def plot_peaks(self, peak_list, label="Peaks"):
        """
        @summary: Plots the locations of peaks as found
		  by PyMS.
		
	@param peak_list: List of peaks
	@type peak_list: list of pyms.Peak.Class.Peak
		
	@param label: label for plot legend
	@type label: StringType
        """

        if not isinstance(peak_list, list):
            error("peak_list is not a list")

        time_list = []
        height_list = []

        # Copy to self.__peak_list for onclick event handling
        self.__peak_list = peak_list

        for peak in peak_list:
            time_list.append(peak.get_rt())
            height_list.append(sum(peak.get_mass_spectrum().mass_spec))

        self.__tic_ic_plots.append(plt.plot(time_list, height_list, 'o',\
            label = label))
Exemplo n.º 16
0
    def __set_time(self, time_list):

        """
        @summary: Sets time-related properties of the data

        @param time_list: List of retention times
        @type time_list: ListType

        @author: Vladimir Likic
        """

        # calculate the time step, its spreak, and along the way
        # check that retention times are increasing
        time_diff_list = []

        for ii in range(len(time_list)-1):
            t1 = time_list[ii]
            t2 = time_list[ii+1]
            if not t2 > t1:
                error("problem with retention times detected")
            time_diff = t2 - t1
            time_diff_list.append(time_diff)

        time_step = mean(time_diff_list)
        time_step_std = std(time_diff_list)

        self.__time_list = time_list
        self.__time_step = time_step
        self.__time_step_std = time_step_std
        self.__min_rt = min(time_list)
        self.__max_rt = max(time_list)
Exemplo n.º 17
0
    def __set_time(self, time_list):

        """
        @summary: Sets time-related properties of the data

        @param time_list: List of retention times
        @type time_list: ListType

        @author: Vladimir Likic
        """

        # calculate the time step, its spreak, and along the way
        # check that retention times are increasing
        time_diff_list = []

        for ii in range(len(time_list)-1):
            t1 = time_list[ii]
            t2 = time_list[ii+1]
            if not t2 > t1:
                error("problem with retention times detected")
            time_diff = t2 - t1
            time_diff_list.append(time_diff)

        time_step = mean(time_diff_list)
        time_step_std = std(time_diff_list)

        self.__time_list = time_list
        self.__time_step = time_step
        self.__time_step_std = time_step_std
        self.__min_rt = min(time_list)
        self.__max_rt = max(time_list)
Exemplo n.º 18
0
    def write(self, file_name, minutes=False):

        """
        @summary: Writes the ion chromatogram to the specified file

        @param file_name: Output file name
        @type file_name: StringType
        @param minutes: A boolean value indicating whether to write
            time in minutes
        @type minutes: BooleanType

        @return: none
        @rtype: NoneType

        @author: Lewis Lee
        @author: Vladimir Likic
        """

        if not is_str(file_name):
            error("'file_name' must be a string")

        fp = open_for_writing(file_name)

        time_list = copy.deepcopy(self.__time_list)

        if minutes:
            for ii in range(len(time_list)):
                time_list[ii] = time_list[ii]/60.0

        for ii in range(len(time_list)):
            fp.write("%8.4f %#.6e\n" % (time_list[ii], self.__ia[ii]))

        close_for_writing(fp)
Exemplo n.º 19
0
    def get_ic_at_mass(self, mass = None):

        """
        @summary: Returns the ion chromatogram for the specified mass.
            The nearest binned mass to mass is used.

            If no mass value is given, the function returns the total
            ion chromatogram.

        @param mass: Mass value of an ion chromatogram
        @type mass: IntType

        @return: Ion chromatogram for given mass
        @rtype: IonChromatogram

        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if mass == None:
            return self.get_tic()

        if mass < self.__min_mass or mass > self.__max_mass:
            print "min mass: ", self.__min_mass, "max mass:", self.__max_mass
            error("mass is out of range")

        ix = self.get_index_of_mass(mass)

        return self.get_ic_at_index(ix)
Exemplo n.º 20
0
def window_analyzer(ic, window=_DEFAULT_WINDOW, n_windows=_DEFAULT_N_WINDOWS, rand_seed=None ):

    """
    @summary: A simple estimator of the signal noise based on randomly
        placed windows and median absolute deviation

        The noise value is estimated by repeatedly and picking random
        windows (of a specified width) and calculating median absolute
        deviation (MAD). The noise estimate is given by the minimum MAD.

    @param ic: An IonChromatogram object
    @type ic: pyms.IO.Class.IonCromatogram
    @param window: Window width selection
    @type window: IntType or StringType
    @param n_windows: The number of windows to calculate
    @type n_windows: IntType
    @param rand_seed: Random seed generator
    @type rand_seed: IntType

    @return: The noise estimate
    @rtype: FloatType

    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("argument must be an IonChromatogram object")

    ia = ic.get_intensity_array() # fetch the intensities

    # create an instance of the Random class
    if rand_seed != None:
        generator = random.Random(rand_seed)
    else:
        generator = random.Random()

    window_pts = window_sele_points(ic, window)

    maxi = ia.size - window_pts
    noise_level = math.fabs(ia.max()-ia.min())
    best_window_pos = None
    seen_positions = []

    cntr = 0

    while cntr < n_windows:
        # generator.randrange(): last point not included in range
        try_pos = generator.randrange(0, maxi+1)
        # only process the window if not analyzed previously
        if try_pos not in seen_positions:
            end_slice = try_pos + window_pts
            slice = ia[try_pos:end_slice]
            crnt_mad = MAD(slice)
            if crnt_mad < noise_level:
                noise_level = crnt_mad
                best_window_pos = try_pos
        cntr = cntr + 1
        seen_positions.append(try_pos)

    return noise_level
Exemplo n.º 21
0
def rel_threshold(pl, percent=2):
    """
    @summary: Remove ions with relative intensities less than the given
        relative percentage of the maximum intensity.

    @param pl: A list of Peak objects
    @type pl: ListType
    @param percent: Threshold for relative percentage of intensity (Default 2%)
    @type percent: FloatType

    @return: A new list of Peak objects with threshold ions
    @rtype: ListType

    @author: Andrew Isaac
    """

    if not is_number(percent) or percent <= 0:
        error("'percent' must be a number > 0")

    pl_copy = copy.deepcopy(pl)
    new_pl = []
    for p in pl_copy:
        ms = p.get_mass_spectrum()
        ia = ms.mass_spec
        # assume max(ia) big so /100 1st
        cutoff = (max(ia) / 100.0) * float(percent)
        for i in range(len(ia)):
            if ia[i] < cutoff:
                ia[i] = 0
        ms.mass_spec = ia
        p.set_mass_spectrum(ms)
        new_pl.append(p)
    return new_pl
Exemplo n.º 22
0
    def __init__(self, mass_list, intensity_list):

        """
        @summary: Initialize the Scan data

        @param mass_list: mass values
        @type mass_list: ListType

        @param intensity_list: intensity values
        @type intensity_list: ListType

        @author: Qiao Wang
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_list(mass_list) or not is_number(mass_list[0]):
            error("'mass_list' must be a list of numbers")
        if not is_list(intensity_list) or \
           not is_number(intensity_list[0]):
            error("'intensity_list' must be a list of numbers")

        self.__mass_list = mass_list
        self.__intensity_list = intensity_list
        self.__min_mass = min(mass_list)
        self.__max_mass = max(mass_list)
Exemplo n.º 23
0
    def get_ic_at_index(self, ix):

        """
        @summary: Returns the ion chromatogram at the specified index

        @param ix: Index of an ion chromatogram in the intensity data
            matrix
        @type ix: IntType

        @return: Ion chromatogram at given index
        @rtype: IonChromatogram

        @author: Qiao Wang
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_int(ix):
            error("index not an integer")
        try:
            ia = []
            for i in range(len(self.__intensity_matrix)):
                ia.append(self.__intensity_matrix[i][ix])
        except IndexError:
            error("index out of bounds.")

        ic_ia = numpy.array(ia)
        mass = self.get_mass_at_index(ix)
        rt = copy.copy(self.__time_list) #JT: changed to copy form deep copy

        return IonChromatogram(ic_ia, rt, mass)
Exemplo n.º 24
0
    def __init__(self, time_list, scan_list):

        """
        @summary: Initialize the GC-MS data

        @param time_list: List of scan retention times
        @type time_list: ListType
        @param scan_list: List of Scan objects
        @type scan_list: ListType

        @author: Qiao Wang
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_list(time_list) or not is_number(time_list[0]):
            error("'time_list' must be a list of numbers")

        if not is_list(scan_list) or not isinstance(scan_list[0], Scan):
            error("'scan_list' must be a list of Scan objects")

        self.__set_time(time_list)
        self.__scan_list = scan_list
        self.__set_min_max_mass()
        self.__calc_tic()
Exemplo n.º 25
0
    def get_index_at_time(self, time):
        """
        @summary: Returns the nearest index corresponding to the given time

        @param time: Time in seconds
        @type time: FloatType

        @return: Nearest index corresponding to given time
        @rtype: IntType

        @author: Lewis Lee
        @author: Tim Erwin
        @author: Vladimir Likic
        """

        if not is_number(time):
            error("'time' must be a number")

        if time < min(self.__time_list) or time > max(self.__time_list):
            error("time %.2f is out of bounds (min: %.2f, max: %.2f)" %
                  (time, self.__min_rt, self.__max_rt))

        time_list = self.__time_list
        time_diff_min = max(self.__time_list)
        ix_match = None

        for ix in range(len(time_list)):

            time_diff = math.fabs(time - time_list[ix])

            if time_diff < time_diff_min:
                ix_match = ix
                time_diff_min = time_diff

        return ix_match
Exemplo n.º 26
0
    def write(self, file_name, minutes=False):

        """
        @summary: Writes the ion chromatogram to the specified file

        @param file_name: Output file name
        @type file_name: StringType
        @param minutes: A boolean value indicating whether to write
            time in minutes
        @type minutes: BooleanType

        @return: none
        @rtype: NoneType

        @author: Lewis Lee
        @author: Vladimir Likic
        """

        if not is_str(file_name):
            error("'file_name' must be a string")

        fp = open_for_writing(file_name)

        time_list = copy.deepcopy(self.__time_list)

        if minutes:
            for ii in range(len(time_list)):
                time_list[ii] = time_list[ii]/60.0

        for ii in range(len(time_list)):
            fp.write("%8.4f %#.6e\n" % (time_list[ii], self.__ia[ii]))

        close_for_writing(fp)
Exemplo n.º 27
0
    def __init__(self, expr):

        """
        @param expr: The experiment to be converted into an alignment object
        @type expr: pyms.Experiment.Class.Experiment

        @author: Woon Wai Keen
        @author: Qiao Wang
        @author: Vladimir Likic
        """
        if expr == None:
            self.peakpos = []
            self.peakalgt = []
            self.expr_code =  []
            self.similarity = None
        else:
            if not isinstance(expr, Experiment):
                error("'expr' must be an Experiment object")
            #for peak in expr.get_peak_list():
            #    if peak.get_area() == None or peak.get_area() <= 0:
            #        error("All peaks must have an area for alignment")
            self.peakpos = [ copy.deepcopy(expr.get_peak_list()) ]
            self.peakalgt = numpy.transpose(self.peakpos)
            self.expr_code =  [ expr.get_expr_code() ]
            self.similarity = None
Exemplo n.º 28
0
    def write_intensities_stream(self, file_name):

        """
        @summary: Writes all intensities to a file

        @param file_name: Output file name
        @type file_name: StringType

        This function loop over all scans, and for each scan
        writes intensities to the file, one intenisity per
        line. Intensities from different scans are joined
        without any delimiters.

        @author: Vladimir Likic
        """

        if not is_str(file_name):
            error("'file_name' must be a string")

        N = len(self.__scan_list)

        print" -> Writing scans to a file"

        fp = open_for_writing(file_name)

        for ii in range(len(self.__scan_list)):
            scan = self.__scan_list[ii]
            intensities = scan.get_intensity_list()
            for I in intensities:
                fp.write("%8.4f\n" % ( I ) )

        close_for_writing(fp)
Exemplo n.º 29
0
Arquivo: IO.py Projeto: ma-bio21/pyms
def load_expr(file_name):

    """
    @summary: Loads an experiment saved with 'store_expr'

    @param file_name: Experiment file name
    @type file_name: StringType

    @return: The experiment intensity matrix and peak list
    @rtype: pyms.Experiment.Class.Experiment

    @author: Vladimir Likic
    @author: Andrew Isaac
    """

    if not is_str(file_name):
        error("'file_name' not a string")

    fp = open(file_name,'rb')
    expr = cPickle.load(fp)
    fp.close()

    if not isinstance(expr, Experiment):
        error("'file_name' is not an Experiment object")

    return expr
Exemplo n.º 30
0
    def __init__(self, mass_list, intensity_list):

        """
        @summary: Initialize the Scan data

        @param mass_list: mass values
        @type mass_list: ListType

        @param intensity_list: intensity values
        @type intensity_list: ListType

        @author: Qiao Wang
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_list(mass_list) or not is_number(mass_list[0]):
            error("'mass_list' must be a list of numbers")
        if not is_list(intensity_list) or \
           not is_number(intensity_list[0]):
            error("'intensity_list' must be a list of numbers")

        self.__mass_list = mass_list
        self.__intensity_list = intensity_list
        self.__min_mass = min(mass_list)
        self.__max_mass = max(mass_list)
Exemplo n.º 31
0
Arquivo: IO.py Projeto: ma-bio21/pyms
def store_expr(file_name, expr):

    """
    @summary: stores an expriment to a file

    @param file_name: The name of the file
    @type file_name: StringType
    @param expr: An experiment object
    @type expr: pyms.Experiment.Class.Experiment

    @return: none
    @rtype: NoneType

    @author: Vladimir Likic
    @author: Andrew Isaac
    """

    if not isinstance(expr, Experiment):
        error("argument not an instance of the class 'Experiment'")

    if not is_str(file_name):
        error("'file_name' not a string")

    fp = open(file_name,'wb')
    cPickle.dump(expr, fp, 1)
    fp.close()
Exemplo n.º 32
0
    def get_ic_at_mass(self, mass = None):

        """
        @summary: Returns the ion chromatogram for the specified mass.
            The nearest binned mass to mass is used.

            If no mass value is given, the function returns the total
            ion chromatogram.

        @param mass: Mass value of an ion chromatogram
        @type mass: IntType

        @return: Ion chromatogram for given mass
        @rtype: IonChromatogram

        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if mass == None:
            return self.get_tic()

        if mass < self.__min_mass or mass > self.__max_mass:
            print "min mass: ", self.__min_mass, "max mass:", self.__max_mass
            error("mass is out of range")

        ix = self.get_index_of_mass(mass)

        return self.get_ic_at_index(ix)
Exemplo n.º 33
0
def load_expr(file_name):
    """
    @summary: Loads an experiment saved with 'store_expr'

    @param file_name: Experiment file name
    @type file_name: StringType

    @return: The experiment intensity matrix and peak list
    @rtype: pyms.Experiment.Class.Experiment

    @author: Vladimir Likic
    @author: Andrew Isaac
    """

    if not is_str(file_name):
        error("'file_name' not a string")

    fp = open(file_name, 'r')
    expr = cPickle.load(fp)
    fp.close()

    if not isinstance(expr, Experiment):
        error("'file_name' is not an Experiment object")

    return expr
Exemplo n.º 34
0
def exprl2alignment(exprl):

    """
    @summary: Converts experiments into alignments

    @param exprl: The list of experiments to be converted into an alignment
        objects
    @type exprl: ListType

    @author: Vladimir Likic
    """

    if not is_list(exprl):
        error("the argument is not a list")

    algts = []

    for item in exprl:
        if not isinstance(item, Experiment):
            error("list items must be 'Experiment' instances")
        else:
            algt = Class.Alignment(item)
        algts.append(algt)

    return algts
Exemplo n.º 35
0
def store_expr(file_name, expr):
    """
    @summary: stores an expriment to a file

    @param file_name: The name of the file
    @type file_name: StringType
    @param expr: An experiment object
    @type expr: pyms.Experiment.Class.Experiment

    @return: none
    @rtype: NoneType

    @author: Vladimir Likic
    @author: Andrew Isaac
    """

    if not isinstance(expr, Experiment):
        error("argument not an instance of the class 'Experiment'")

    if not is_str(file_name):
        error("'file_name' not a string")

    fp = open(file_name, 'w')
    cPickle.dump(expr, fp, 1)
    fp.close()
Exemplo n.º 36
0
def exprl2alignment(exprl):

    """
    @summary: Converts experiments into alignments

    @param exprl: The list of experiments to be converted into an alignment
        objects
    @type exprl: ListType

    @author: Vladimir Likic
    """

    if not is_list(exprl):
        error("the argument is not a list")

    algts = []

    for item in exprl:
        if not isinstance(item, Experiment):
            error("list items must be 'Experiment' instances")
        else:
            algt = Class.Alignment(item)
        algts.append(algt)

    return algts
Exemplo n.º 37
0
    def plot_ics(self, ics, labels = None):
		
	"""
        @summary: Adds an Ion Chromatogram or a 
	list of Ion Chromatograms to plot list
	
	@param ics: List of Ion Chromatograms m/z channels
		for plotting
	@type ics: list of pyms.GCMS.Class.IonChromatogram 
	
	@param labels: Labels for plot legend
	@type labels: list of StringType
        """
	
        if not isinstance(ics, list):
	    if isinstance(ics, IonChromatogram):
		ics = [ics]
	    else:
		error("ics argument must be an IonChromatogram\
		or a list of Ion Chromatograms")
	
        if not isinstance(labels, list) and labels != None:
            labels = [labels]
        # TODO: take care of case where one element of ics is
	# not an IonChromatogram
		
	
	intensity_list = []
	time_list = ics[0].get_time_list()
			
	
	for i in range(len(ics)):
	    intensity_list.append(ics[i].get_intensity_array())
		
	
        # Case for labels not present
        if labels == None:
            for i in range(len(ics)):
                self.__tic_ic_plots.append(plt.plot(time_list, \
	            intensity_list[i], self.__col_ic[self.__col_count]))
	        if self.__col_count == 5:
                    self.__col_count = 0
                else:
                    self.__col_count += 1
        
        # Case for labels present
        else:
            for i in range(len(ics)):	
		
	        self.__tic_ic_plots.append(plt.plot(time_list, \
	            intensity_list[i], self.__col_ic[self.__col_count]\
	                , label = labels[i]))
	        if self.__col_count == 5:
                    self.__col_count = 0
                else:
                    self.__col_count += 1
Exemplo n.º 38
0
    def __init__(self, rt=0.0, ms=None, minutes=False):

        """
        @param rt: Retention time
        @type rt: FloatType
        @param ms: A ion mass, or spectra of maximising ions
        @type ms: FloatType, pyms.GCSM.Class.MassSpectrum
        @param minutes: Retention time units flag. If True, retention time
            is in minutes; if False retention time is in seconds
        @type minutes: BooleanType
        """

        if not is_number(rt):
            error("'rt' must be a number")

        if not ms == None and \
            not isinstance(ms, MassSpectrum) and \
            not is_number(ms):
            error("'ms' must be a Float or a MassSpectrum object")

        if minutes:
            rt = rt*60.0

        self.__minutes = minutes

        # basic peak attributes
        self.__rt = float(rt)
        # these two attributes are required for
        # setting the peak mass spectrum
        if not ms == None:
            if isinstance(ms, MassSpectrum):
                # mass spectrum
                self.__mass_spectrum = ms
                self.__ic_mass = None
                self.make_UID()

                # TEST: to test if this speeds things up
                self.mass_spec = ms.mass_spec
                self.ms = ms

            else:
                # single ion chromatogram properties
                self.__ic_mass = ms
                self.__mass_spectrum = None
                self.make_UID()

                # TEST: to test if this speeds things up
                self.mass_spec = None


        self.__pt_bounds = None
        self.__area = None
        self.__ion_areas = {}

        # TEST: to test if this speeds things up
        self.rt = self.__rt
Exemplo n.º 39
0
    def write(self, file_root):

        """
        @summary: Writes the entire raw data to two files, one
            'file_root'.I.csv (intensities) and 'file_root'.mz.csv
            (m/z values).

            This method writes two CSV files, containing intensities
            and corresponding m/z values. In general these are not
            two-dimensional matrices, because different scans may
            have different number of m/z values recorded.

        @param file_root: The root for the output file names
        @type file_root: StringType

        @author: Vladimir Likic
        """

        if not is_str(file_root):
            error("'file_root' must be a string")

        file_name1 = file_root + ".I.csv"
        file_name2 = file_root + ".mz.csv"

        print " -> Writing intensities to '%s'" % ( file_name1 )
        print " -> Writing m/z values to '%s'" % ( file_name2 )

        fp1 = open_for_writing(file_name1)
        fp2 = open_for_writing(file_name2)

        for ii in range(len(self.__scan_list)):

            scan = self.__scan_list[ii]

            intensity_list = scan.get_intensity_list()
            mass_list = scan.get_mass_list()

            for ii in range(len(intensity_list)):
                v = intensity_list[ii]
                if ii == 0:
                    fp1.write("%.4f" % (v))
                else:
                    fp1.write(",%.4f" % (v))
            fp1.write("\n")

            for ii in range(len(mass_list)):
                v = mass_list[ii]
                if ii == 0:
                    fp2.write("%.4f" % (v))
                else:
                    fp2.write(",%.4f" % (v))
            fp2.write("\n")

        close_for_writing(fp1)
        close_for_writing(fp2)
Exemplo n.º 40
0
    def write(self, file_root):

        """
        @summary: Writes the entire raw data to two files, one
            'file_root'.I.csv (intensities) and 'file_root'.mz.csv
            (m/z values).

            This method writes two CSV files, containing intensities
            and corresponding m/z values. In general these are not
            two-dimensional matrices, because different scans may
            have different number of m/z values recorded.

        @param file_root: The root for the output file names
        @type file_root: StringType

        @author: Vladimir Likic
        """

        if not is_str(file_root):
            error("'file_root' must be a string")

        file_name1 = file_root + ".I.csv"
        file_name2 = file_root + ".mz.csv"

        print " -> Writing intensities to '%s'" % ( file_name1 )
        print " -> Writing m/z values to '%s'" % ( file_name2 )

        fp1 = open_for_writing(file_name1)
        fp2 = open_for_writing(file_name2)

        for ii in range(len(self.__scan_list)):

            scan = self.__scan_list[ii]

            intensity_list = scan.get_intensity_list()
            mass_list = scan.get_mass_list()

            for ii in range(len(intensity_list)):
                v = intensity_list[ii]
                if ii == 0:
                    fp1.write("%.4f" % (v))
                else:
                    fp1.write(",%.4f" % (v))
            fp1.write("\n")

            for ii in range(len(mass_list)):
                v = mass_list[ii]
                if ii == 0:
                    fp2.write("%.4f" % (v))
                else:
                    fp2.write(",%.4f" % (v))
            fp2.write("\n")

        close_for_writing(fp1)
        close_for_writing(fp2)
Exemplo n.º 41
0
    def get_ion_areas(self):
        """
        @summary: returns a copy of the ion areas dict

        @return: The dictionary of ion:ion area pairs
        @rtype: dictType
        """
        if len(self.__ion_areas) == 0:
            error("no ion areas set")

        return copy.deepcopy(self.__ion_areas)
Exemplo n.º 42
0
    def crop_mass(self, mass_min, mass_max):

        """
        @summary: Crops mass spectrum

        @param mass_min: Minimum mass value
        @type mass_min: IntType or FloatType
        @param mass_max: Maximum mass value
        @type mass_max: IntType or FloatType

        @return: none
        @rtype: NoneType

        @author: Andrew Isaac
        """

        if not is_number(mass_min) or not is_number(mass_max):
            error("'mass_min' and 'mass_max' must be numbers")
        if mass_min >= mass_max:
            error("'mass_min' must be less than 'mass_max'")

        mass_list = self.__mass_spectrum.mass_list

        if mass_min < min(mass_list):
            error("'mass_min' is less than the smallest mass: %d" \
              % min(mass_list))
        if mass_max > max(mass_list):
            error("'mass_max' is greater than the largest mass: %d" \
              % max(mass_list))

        # pre build mass_list and list of indecies
        new_mass_list = []
        new_mass_spec = []
        mass_spec = self.__mass_spectrum.mass_spec
        for ii in range(len(mass_list)):
             mass = mass_list[ii]
             if mass >= mass_min and mass <= mass_max:
                 new_mass_list.append(mass)
                 new_mass_spec.append(mass_spec[ii])

        self.__mass_spectrum.mass_list = new_mass_list
        self.__mass_spectrum.mass_spec = new_mass_spec

        if len(new_mass_list) == 0:
            error("mass spectrum is now empty")
        elif len(new_mass_list) < 10:
            print " WARNING: peak mass spectrum contains < 10 points"

        # update UID
        self.make_UID()

        # TEST: to test if this speeds things up
        self.mass_spec = self.__mass_spectrum.mass_spec
Exemplo n.º 43
0
    def plot_ics(self, ics, labels=None):
        """
        @summary: Adds an Ion Chromatogram or a 
	list of Ion Chromatograms to plot list
	
	@param ics: List of Ion Chromatograms m/z channels
		for plotting
	@type ics: list of pyms.GCMS.Class.IonChromatogram 
	
	@param labels: Labels for plot legend
	@type labels: list of StringType
        """

        if not isinstance(ics, list):
            if isinstance(ics, IonChromatogram):
                ics = [ics]
            else:
                error("ics argument must be an IonChromatogram\
		or a list of Ion Chromatograms")

        if not isinstance(labels, list) and labels != None:
            labels = [labels]
    # TODO: take care of case where one element of ics is
        # not an IonChromatogram

        intensity_list = []
        time_list = ics[0].get_time_list()

        for i in range(len(ics)):
            intensity_list.append(ics[i].get_intensity_array())

    # Case for labels not present
        if labels == None:
            for i in range(len(ics)):
                self.__tic_ic_plots.append(plt.plot(time_list, \
             intensity_list[i], self.__col_ic[self.__col_count]))
                if self.__col_count == 5:
                    self.__col_count = 0
                else:
                    self.__col_count += 1

    # Case for labels present
        else:
            for i in range(len(ics)):

                self.__tic_ic_plots.append(plt.plot(time_list, \
                    intensity_list[i], self.__col_ic[self.__col_count]\
                        , label = labels[i]))
                if self.__col_count == 5:
                    self.__col_count = 0
                else:
                    self.__col_count += 1
Exemplo n.º 44
0
    def get_mass(self):
        """
        @summary: Returns the m/z channel of the IC
        
        @return: m/z channel of the IC
        @rtype: intType
        
        @author: Sean O'Callaghan
        """
        if self.__mass == None:
            error("TIC has no m/z label")

        return self.__mass
Exemplo n.º 45
0
 def get_mass(self):
     
     """
     @summary: Returns the m/z channel of the IC
     
     @return: m/z channel of the IC
     @rtype: intType
     
     @author: Sean O'Callaghan
     """
     if self.__mass == None:
         error("TIC has no m/z label")
     
     return self.__mass
Exemplo n.º 46
0
    def set_area(self, area):
        """
        @summary: Sets the area under the peak

        @param area: The peak area
        @type area: FloatType

        @author: Andrew Isaac
        """

        if not is_number(area) or area <= 0:
            error("'area' must be a positive number")

        self.__area = area
Exemplo n.º 47
0
    def set_area(self, area):

        """
        @summary: Sets the area under the peak

        @param area: The peak area
        @type area: FloatType

        @author: Andrew Isaac
        """

        if not is_number(area) or area <= 0:
            error("'area' must be a positive number")

        self.__area = area
Exemplo n.º 48
0
    def export_ascii(self, root_name, format='dat'):

        """
        @summary: Exports the intensity matrix, retention time vector, and
        m/z vector to the ascii format

        By default, export_ascii("NAME") will create NAME.im.dat, NAME.rt.dat,
        and NAME.mz.dat where these are the intensity matrix, retention
        time vector, and m/z vector in tab delimited format. If format='csv',
        the files will be in the CSV format, named NAME.im.csv, NAME.rt.csv,
        and NAME.mz.csv.

        @param root_name: Root name for the output files
        @type root_name: StringType

        @return: none
        @rtype: NoneType

        @author: Milica Ng
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_str(root_name):
            error("'root_name' is not a string")

        if format == 'dat':
            separator = " "
            extension = ".dat"
        elif format == 'csv':
            separator = ","
            extension = ".csv"
        else:
            error("unkown format '%s'. Only 'dat' or 'csv' supported" % format)

        # export 2D matrix of intensities
        vals = self.__intensity_matrix
        save_data(root_name+'.im'+extension, vals, sep=separator)

        # export 1D vector of m/z's, corresponding to rows of
        # the intensity matrix
        mass_list = self.__mass_list
        save_data(root_name+'.mz'+extension, mass_list, sep=separator)

        # export 1D vector of retention times, corresponding to
        # columns of the intensity matrix
        time_list = self.__time_list
        save_data(root_name+'.rt'+extension, time_list, sep=separator)
Exemplo n.º 49
0
    def export_ascii(self, root_name, format='dat'):

        """
        @summary: Exports the intensity matrix, retention time vector, and
        m/z vector to the ascii format

        By default, export_ascii("NAME") will create NAME.im.dat, NAME.rt.dat,
        and NAME.mz.dat where these are the intensity matrix, retention
        time vector, and m/z vector in tab delimited format. If format='csv',
        the files will be in the CSV format, named NAME.im.csv, NAME.rt.csv,
        and NAME.mz.csv.

        @param root_name: Root name for the output files
        @type root_name: StringType

        @return: none
        @rtype: NoneType

        @author: Milica Ng
        @author: Andrew Isaac
        @author: Vladimir Likic
        """

        if not is_str(root_name):
            error("'root_name' is not a string")

        if format == 'dat':
            separator = " "
            extension = ".dat"
        elif format == 'csv':
            separator = ","
            extension = ".csv"
        else:
            error("unkown format '%s'. Only 'dat' or 'csv' supported" % format)

        # export 2D matrix of intensities
        vals = self.__intensity_matrix
        save_data(root_name+'.im'+extension, vals, sep=separator)

        # export 1D vector of m/z's, corresponding to rows of
        # the intensity matrix
        mass_list = self.__mass_list
        save_data(root_name+'.mz'+extension, mass_list, sep=separator)

        # export 1D vector of retention times, corresponding to
        # columns of the intensity matrix
        time_list = self.__time_list
        save_data(root_name+'.rt'+extension, time_list, sep=separator)
Exemplo n.º 50
0
def mzML_reader(file_name):

    """
    @summary: A reader for mzML files, returns
        a GC-MS data object

    @param file_name: The name of the mzML file
    @type file_name: StringType

    @author: Sean O'Callaghan
    """

    if not is_str(file_name):
        error("'file_name' must be a string")
    try:
        mzml_file = pymzml.run.Reader(file_name)
    except:
        error("Cannot open file '%s'" % file_name)

    print " -> Reading mzML file '%s'" % (file_name)

    scan_list = []
    time_list = []

    for spectrum in mzml_file:
        mass_list = []
        intensity_list = []

        for mz,i in spectrum.peaks:
            mass_list.append(mz)
            intensity_list.append(i)

        #scan_list.append(Scan(mass_list, intensity_list))
        for element in spectrum.xmlTree:
            # For some reason there are spectra with no time value, 
            # Ignore these????????????
            if element.get('accession') == "MS:1000016": #time value
                # We need time in seconds not minutes
                time_list.append(60*float(element.get('value')))
                scan_list.append(Scan(mass_list, intensity_list))

    print "time:", len(time_list)
    print "scan:", len(scan_list)

    data = GCMS_data(time_list, scan_list)

    return data
Exemplo n.º 51
0
def get_maxima_indices(ion_intensities, points=3):

    """
    @summary: Find local maxima.

    @param ion_intensities: A list of intensities for a single ion
    @type ion_intensities: ListType
    @param points: Peak if maxima over 'points' number of scans
    @type points: IntType

    @return: A list of scan indices
    @rtype: ListType

    @author: Andrew Isaac
    """

    if not is_list(ion_intensities) or not is_number(ion_intensities[0]):
        error("'ion_intensities' must be a List of numbers")

    # find peak inflection points
    # use a 'points' point window
    # for a plateau after a rise, need to check if it is the left edge of
    # a peak
    peak_point = []
    edge = -1
    points = int(points)
    half = int(points/2)
    points = 2*half+1  # ensure odd number of points
    for index in range(len(ion_intensities)-points+1):
        left = ion_intensities[index:index+half]
        mid = ion_intensities[index+half]
        right = ion_intensities[index+half+1:index+points]
        # max in middle
        if mid > max(left) and mid > max(right):
            peak_point.append(index+half)
            edge = -1  # ignore previous rising edge
        # flat from rise (left of peak?)
        if mid > max(left) and mid == max(right):
            edge = index+half  # ignore previous rising edge, update latest
        # fall from flat
        if mid == max(left) and mid > max(right):
            if edge > -1:
                centre = int((edge+index+half)/2)  # mid point
                peak_point.append(centre)
            edge = -1

    return peak_point