Exemplo n.º 1
0
def mzImages(filename_in_list,save_dir):
    import sys
    sys.path.append('C:\\Users\\Luca Rappez\\Desktop\\python_codebase\\')
    from pyMS.centroid_detection import gradient
    from pyIMS.hdf5.inMemoryIMS_hdf5 import inMemoryIMS_hdf5
    from pyIMS.image_measures import level_sets_measure
    import matplotlib.pyplot as plt
    import numpy as np
    #%matplotlib inline
    import bokeh as bk
    from bokeh.plotting import output_notebook
    output_notebook()
    ppm = 0.75
    if len(list)>2:
        raise ValueError('list should only have two entries')
    c_thresh=0.05
    mz_list_all=[]
    for filename_in in filename_in_list:
     # Parse data
        IMS_dataset = inMemoryIMS_hdf5(filename_in)
        hist_axis,freq_spec = IMS_dataset.generate_summary_spectrum(summary_type='freq',ppm=ppm/2)
        # Centroid detection of frequency spectrum
        mz_list,count_list,idx_list = gradient(np.asarray(hist_axis),np.asarray(freq_spec),weighted_bins=2)
        mz_list=[m for m,c in zip(mz_list,count_list) if c>c_thresh]
        mz_list_all.append(mz_list)

    im_list = [m for m in mz_list_all[0] if any([1e6*(m-mm)/m<ppm for mm in mz_list_all[1]]) ]

    """# Calcualte MoC for images of all peaks
    nlevels=30
    im_list={}
    for ii, c in enumerate(count_list):
        ion_image = IMS_dataset.get_ion_image(np.asarray([mz_list[ii],]),ppm)
        im = ion_image.xic_to_image(0)
        m,im_moc,levels,nobjs = level_sets_measure.measure_of_chaos(im,nlevels,interp='median') #just output measure value
        m=1-m
        im_list[mz_list[ii]]={'image':im,'moc':m,'freq':c}
"""
    from pySpatialMetabolomics.tools import colourmaps
    c_map = colourmaps.get_colormap('grey')#if black images: open->save->rerun
    c_pal=[[int(255*cc) for cc in c_map(c)] for c in range(0,254)]

    # Export all images
    import png as pypng
    for filename_in in filename_in_list:
     # Parse data
        IMS_dataset = inMemoryIMS_hdf5(filename_in)
        for mz in im_list:
            with open('{}/{}_{}.png'.format(save_dir,mz,filename_in),'wb') as f_out:
                im_out = IMS_dataset.get_ion_image([mz,],[ppm,]).xic_to_image(0)
                im_out = 254*im_out/np.max(im_out)
                w,h = np.shape(im_out)
                w = pypng.Writer(h, w, palette=c_pal, bitdepth=8)
                w.write(f_out,im_out)
Exemplo n.º 2
0
    def load_data(self):
        from pyIMS.hdf5.inMemoryIMS_hdf5 import inMemoryIMS_hdf5

        self.IMS_dataset = inMemoryIMS_hdf5(self.data_file)
        self.coords = self.IMS_dataset.coords - 1
        self._calculate_dimensions()
        self.pixel_indices = self.IMS_dataset.cube_pixel_indices
def run_search(config):
    ### Runs the main pipeline
    # Get sum formula and predicted m/z peaks for molecules in database
    sum_formulae,adducts,mz_list = generate_isotope_patterns(config)
    # Parse dataset
    from pyIMS.hdf5.inMemoryIMS_hdf5 import inMemoryIMS_hdf5
    from pyIMS.image_measures import level_sets_measure,isotope_image_correlation,isotope_pattern_match
    IMS_dataset=inMemoryIMS_hdf5(config['file_inputs']['data_file'])
        
    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
    q=config['image_generation']['q'] 
    measure_value_score={}
    iso_correlation_score = {}
    iso_ratio_score = {}
    
    for sum_formula in sum_formulae:
        for adduct in adducts:
            # 1. Geneate ion images
            ion_datacube = IMS_dataset.get_ion_image(mz_list[sum_formula][adduct][0],ppm) #for each spectrum, sum the intensity of all peaks within tol of mz_list
            ion_datacube.xic=hot_spot_removal(ion_datacube.xic,q)

            # 2. Spatial Chaos 
            if not sum_formula in measure_value_score:
                measure_value_score[sum_formula] = {}
            measure_value_score[sum_formula][adduct] = 1-level_sets_measure.measure_of_chaos(ion_datacube.xic_to_image(0),nlevels,interp=False)[0]
            if measure_value_score[sum_formula][adduct] == 1:
                measure_value_score[sum_formula][adduct] = 0
            # only compare pixels with values in the monoisotopic (otherwise high correlation for large empty areas)
            notnull_monoiso = ion_datacube.xic[0] > 0 
            #for xic in ion_datacube.xic:
            #    xic = xic[notnull_monoiso]
            # 3. Score correlation with monoiso
            if not sum_formula in iso_correlation_score:
                iso_correlation_score[sum_formula] = {}
            if len(mz_list[sum_formula][adduct][1]) > 1:
                iso_correlation_score[sum_formula][adduct] = isotope_image_correlation.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
            if not sum_formula in iso_ratio_score:
                iso_ratio_score[sum_formula] = {}
            iso_ratio_score[sum_formula][adduct]  = isotope_pattern_match.isotope_pattern_match(ion_datacube.xic,mz_list[sum_formula][adduct][1])
    return measure_value_score,iso_correlation_score,iso_ratio_score
Exemplo n.º 4
0
def mzImages(filename_in,save_dir):
    import sys
    sys.path.append('C:\\Users\\Luca Rappez\\Desktop\\python_codebase\\')
    from pyMS.centroid_detection import gradient
    from pyIMS.hdf5.inMemoryIMS_hdf5 import inMemoryIMS_hdf5
    from pyIMS.image_measures import level_sets_measure
    import matplotlib.pyplot as plt
    import numpy as np
    #%matplotlib inline
    import bokeh as bk
    from bokeh.plotting import output_notebook
    output_notebook()

    print 'step1'
    #filename_in =  '//psi.embl.de/alexandr/shared/Luca/20150825_CoCulture_Candida_Ecoli/20150821_ADP_LR_colony250K12_DHBsub_260x280_50um.hdf5' #using a temporary hdf5 based format
    #save_dir= '//psi.embl.de/alexandr/shared/Luca/20150825_CoCulture_Candida_Ecoli/20150821_ADP_LR_colony250K12_DHBsub_260x280_50um_figures'

    # Parse data
    IMS_dataset = inMemoryIMS_hdf5(filename_in)
    print 'In memory'
    ppm = 1.5

    # Generate mean spectrum
    #hist_axis,mean_spec =IMS_dataset.generate_summary_spectrum(summary_type='mean')
    hist_axis,freq_spec = IMS_dataset.generate_summary_spectrum(summary_type='freq',ppm=ppm/2)

    #p1 = bk.plotting.figure()
    #p1.line(hist_axis,mean_spec/np.max(mean_spec),color='red')
    #p1.line(hist_axis,freq_spec/np.max(freq_spec),color='orange')
    #bk.plotting.show(p1)
    print len(hist_axis)
    #plt.figure(figsize=(20,10))
    #plt.plot(hist_axis,freq_spec)
    #plt.show()

    # Centroid detection of frequency spectrum
    mz_list,count_list,idx_list = gradient(np.asarray(hist_axis),np.asarray(freq_spec),weighted_bins=2)

    c_thresh=0.05
    moc_thresh=0.99
    print np.sum(count_list>c_thresh)

    # Calcualte MoC for images of all peaks
    nlevels=30
    im_list={}
    for ii, c in enumerate(count_list):
        #print ii
        #print c
        if c>c_thresh:
            ion_image = IMS_dataset.get_ion_image(np.asarray([mz_list[ii],]),ppm)
            im = ion_image.xic_to_image(0)
            m,im_moc,levels,nobjs = level_sets_measure.measure_of_chaos(im,nlevels,interp='median') #just output measure value
            m=1-m
            im_list[mz_list[ii]]={'image':im,'moc':m,'freq':c}

    from pySpatialMetabolomics.tools import colourmaps
    c_map = colourmaps.get_colormap('grey')#if black images: open->save->rerun
    c_pal=[[int(255*cc) for cc in c_map(c)] for c in range(0,254)]

    # Export all images
    import png as pypng
    for mz in im_list:
        if im_list[mz]['moc']>moc_thresh:
             with open('{}/{}_{}.png'.format(save_dir,mz,im_list[mz]['moc']),'wb') as f_out:
                im_out = im_list[mz]['image']
                im_out = 254*im_out/np.max(im_out)
                w,h = np.shape(im_out)
                w = pypng.Writer(h, w, palette=c_pal, bitdepth=8)
                w.write(f_out,im_out)

    #im_out = im_list[mz]['image']
    mz=333.334188269
    ion_image = IMS_dataset.get_ion_image(np.asarray([mz,]),ppm)
    im_out=ion_image.xic_to_image(0)
    m,im_moc,levels,nobjs = level_sets_measure.measure_of_chaos(im,nlevels,interp='') #just output measure value
    print 1-m
    im_out = 254.*im_out/np.max(im_out)
    print mz
    #print im_list[mz]['moc']
    #plt.figure()
    #plt.imshow(im_moc)
    #plt.show()
 def load_data(self):
     self.IMS_dataset = inMemoryIMS_hdf5(self.data_file)
     self.coords = self.IMS_dataset.coords
     self._calculate_dimensions()