예제 #1
0
def run_search(config, IMS_dataset, sum_formulae, adducts, mz_list):
    import time
    from pyImagingMSpec import image_measures
    ### Runs the main pipeline
    # Get sum formula and predicted m/z peaks for molecules in database
    ppm = config['image_generation']['ppm']  # parts per million -  a measure of how accuracte the mass spectrometer is
    nlevels = config['image_generation']['nlevels']  # parameter for measure of chaos
    do_preprocessing = config['image_generation']['do_preprocessing']
    interp = config['image_generation']['smooth']
    measure_value_score = {}
    iso_correlation_score = {}
    iso_ratio_score = {}
    t0 = time.time()
    t_el = 0
    for adduct in adducts:
        print 'searching -> {}'.format(adduct)
        for ii,sum_formula in enumerate(sum_formulae):
            if sum_formula not in mz_list:
                print 'mssing sf: {}'.format(sum_formula)
                continue
            if adduct not in mz_list[sum_formula]:
                # adduct may not be present if it would make an impossible formula, is there a better way to handle this?
                # this hack is also used for fdr calculations
                # print '{} adduct not found for {}'.format(adduct, sum_formula)
                continue
            if time.time() - t_el > 10.:
                t_el = time.time()
                print '{:3.2f} done in {:3.0f} seconds'.format(float(ii)/len(sum_formulae),time.time()-t0)
            # Allocate dicts if required
            if not sum_formula in measure_value_score:
                    measure_value_score[sum_formula] = {}
            if not sum_formula in iso_correlation_score:
                    iso_correlation_score[sum_formula] = {}
            if not sum_formula in iso_ratio_score:
                    iso_ratio_score[sum_formula] = {}
            try:
                # 1. Generate ion images
                mzs = mz_list[sum_formula][adduct][0] #+ 5*mz_list[sum_formula][adduct][0]*1e-6
                ion_datacube = IMS_dataset.get_ion_image(mzs, ppm)  # for each spectrum, sum the intensity of all peaks within tol of mz_list
                if do_preprocessing:
                    apply_image_processing(config,ion_datacube) #currently just supports hot-spot removal
                # 2. Spatial Chaos
                measure_value_score[sum_formula][adduct] = image_measures.measure_of_chaos(
                    ion_datacube.xic_to_image(0), nlevels)
                if measure_value_score[sum_formula][adduct] == 1:
                    measure_value_score[sum_formula][adduct] = 0
                # 3. Score correlation with monoiso
                if len(mz_list[sum_formula][adduct][1]) > 1:
                    iso_correlation_score[sum_formula][adduct] = image_measures.isotope_image_correlation(
                        ion_datacube.xic, weights=mz_list[sum_formula][adduct][1][1:])
                else:  # only one isotope peak, so correlation doesn't make sense
                    iso_correlation_score[sum_formula][adduct] = 1
                # 4. Score isotope ratio
                iso_ratio_score[sum_formula][adduct] = image_measures.isotope_pattern_match(ion_datacube.xic,
                                                                                                   mz_list[sum_formula][
                                                                                                       adduct][1])
            except KeyError as e:
                print str(e)
                print "bad key in: \"{}\" \"{}\" ".format(sum_formula, adduct)
        output_results(config, measure_value_score, iso_correlation_score, iso_ratio_score, sum_formulae, [adduct], mz_list)
    return measure_value_score, iso_correlation_score, iso_ratio_score
예제 #2
0
def plot_images(ion_datacube,iso_spect,iso_max,q_val=99,c_map='hot'):
    import numpy as np
    import matplotlib.pyplot as plt
    from pyImagingMSpec.image_measures import measure_of_chaos, isotope_image_correlation, isotope_pattern_match
    from pyImagingMSpec import smoothing as im_smoothing
    for ii in range(0, iso_max):
        # hot-spot removal
        xic = ion_datacube.xic[ii]
        im_smoothing.hot_spot_removal(xic, q_val)  # updated in place
        im = ion_datacube.xic_to_image(ii)
        #im = im_smoothing.median(im, size=3)
        ion_datacube.xic[ii] = ion_datacube.image_to_xic(im)
    measure_value_score = measure_of_chaos(
            ion_datacube.xic_to_image(0), 30)
    # 3. Score correlation with monoiso
    if len(iso_spect[1]) > 1:
        iso_correlation_score = isotope_image_correlation(
            ion_datacube.xic, weights=iso_spect[1][1:])
    else:  # only one isotope peak, so correlation doesn't make sense
        iso_correlation_score = 1
    iso_ratio_score = isotope_pattern_match(ion_datacube.xic,iso_spect[1])
    msm_score = measure_value_score*iso_correlation_score*iso_ratio_score

    ax = [   plt.subplot2grid((2, 4), (0, 0)),
         plt.subplot2grid((2, 4), (0, 1)),
         plt.subplot2grid((2, 4), (0, 2)),
         plt.subplot2grid((2, 4), (0, 3)),
         plt.subplot2grid((2, 4), (1, 0), colspan=4, rowspan=1)
     ]
    for a in ax:
        a.cla()
    # plot images
    for ii in range(0,iso_max):
        im = ion_datacube.xic_to_image(ii)
        ax[ii].imshow(im,cmap=c_map,interpolation='nearest')
        ax[ii].set_title('m/z: {:3.4f}'.format(iso_spect[0][ii]))
        ax[ii].set_xticks([],[])
        ax[ii].set_yticks([],[])
    # plot spectrum
    notnull=ion_datacube.xic_to_image(0)>0
    data_spect = [np.sum(ion_datacube.xic_to_image(ii)[notnull]) for ii in range(0,iso_max)]
    data_spect = data_spect / np.linalg.norm(data_spect)
    iso_spect[1] = iso_spect[1]/np.linalg.norm(iso_spect[1])

    markerline, stemlines, baseline = ax[4].stem(iso_spect[0][0:iso_max],iso_spect[1][0:iso_max],'g')
    plt.title("moc: {:3.5f} spat: {:3.5f} spec: {:3.5f} msm: {:3.5f}".format(measure_value_score,iso_correlation_score,iso_ratio_score,msm_score))
    plt.setp(stemlines, linewidth=2, color='g')     # set stems  colors
    plt.setp(markerline, 'markerfacecolor', 'g','markeredgecolor','g')    # make points 

    markerline, stemlines, baseline = ax[4].stem(iso_spect[0][0:iso_max],data_spect,'r')
    plt.setp(stemlines, linewidth=2, color='r')     # set stems colors
    plt.setp(markerline, 'markerfacecolor', 'r','markeredgecolor','r')    # make points 

    #plot proxy artist
    proxies=[]
    h, = plt.plot(iso_spect[0][0],[0],'-g')
    proxies.append(h)
    h, = plt.plot(iso_spect[0][0],[0],'-r')
    proxies.append(h)
    ax[4].legend(proxies,('predicted pattern','data pattern'), numpoints=1)
    return ax
예제 #3
0
def plot_images(ion_datacube, iso_spect, iso_max, q_val=99, c_map='hot'):
    import numpy as np
    import matplotlib.pyplot as plt
    from pyImagingMSpec.image_measures import measure_of_chaos, isotope_image_correlation, isotope_pattern_match
    from pyImagingMSpec import smoothing as im_smoothing
    for ii in range(0, iso_max):
        # hot-spot removal
        xic = ion_datacube.xic[ii]
        im_smoothing.hot_spot_removal(xic, q_val)  # updated in place
        im = ion_datacube.xic_to_image(ii)
        #im = im_smoothing.median(im, size=3)
        ion_datacube.xic[ii] = ion_datacube.image_to_xic(im)
    measure_value_score = measure_of_chaos(ion_datacube.xic_to_image(0), 30)
    # 3. Score correlation with monoiso
    if len(iso_spect[1]) > 1:
        iso_correlation_score = isotope_image_correlation(
            ion_datacube.xic, weights=iso_spect[1][1:])
    else:  # only one isotope peak, so correlation doesn't make sense
        iso_correlation_score = 1
    iso_ratio_score = isotope_pattern_match(ion_datacube.xic, iso_spect[1])
    msm_score = measure_value_score * iso_correlation_score * iso_ratio_score

    ax = [
        plt.subplot2grid((2, 4), (0, 0)),
        plt.subplot2grid((2, 4), (0, 1)),
        plt.subplot2grid((2, 4), (0, 2)),
        plt.subplot2grid((2, 4), (0, 3)),
        plt.subplot2grid((2, 4), (1, 0), colspan=4, rowspan=1)
    ]
    for a in ax:
        a.cla()
    # plot images
    for ii in range(0, iso_max):
        im = ion_datacube.xic_to_image(ii)
        ax[ii].imshow(im, cmap=c_map, interpolation='nearest')
        ax[ii].set_title('m/z: {:3.4f}'.format(iso_spect[0][ii]))
        ax[ii].set_xticks([], [])
        ax[ii].set_yticks([], [])
    # plot spectrum
    notnull = ion_datacube.xic_to_image(0) > 0
    data_spect = [
        np.sum(ion_datacube.xic_to_image(ii)[notnull])
        for ii in range(0, iso_max)
    ]
    data_spect = data_spect / np.linalg.norm(data_spect)
    iso_spect[1] = iso_spect[1] / np.linalg.norm(iso_spect[1])

    markerline, stemlines, baseline = ax[4].stem(iso_spect[0][0:iso_max],
                                                 iso_spect[1][0:iso_max], 'g')
    plt.title("moc: {:3.5f} spat: {:3.5f} spec: {:3.5f} msm: {:3.5f}".format(
        measure_value_score, iso_correlation_score, iso_ratio_score,
        msm_score))
    plt.setp(stemlines, linewidth=2, color='g')  # set stems  colors
    plt.setp(markerline, 'markerfacecolor', 'g', 'markeredgecolor',
             'g')  # make points

    markerline, stemlines, baseline = ax[4].stem(iso_spect[0][0:iso_max],
                                                 data_spect, 'r')
    plt.setp(stemlines, linewidth=2, color='r')  # set stems colors
    plt.setp(markerline, 'markerfacecolor', 'r', 'markeredgecolor',
             'r')  # make points

    #plot proxy artist
    proxies = []
    h, = plt.plot(iso_spect[0][0], [0], '-g')
    proxies.append(h)
    h, = plt.plot(iso_spect[0][0], [0], '-r')
    proxies.append(h)
    ax[4].legend(proxies, ('predicted pattern', 'data pattern'), numpoints=1)
    return ax