def get_spectrum(self, index): this_spectrum = mass_spectrum() tmp_str = '/spectral_data/%d/' % (index) this_spectrum.add_centroids( self.hdf[tmp_str + '/centroid_mzs/'], self.hdf[tmp_str + '/centroid_intensities/']) return this_spectrum
def test_get_profile_spectrum(self): mzs = [0,1,2,3] vals = [1,2,1,0] this_spectrum = mass_spectrum() this_spectrum.add_spectrum(mzs,vals) [mzs_,vals_] = this_spectrum.get_spectrum(source='profile') self.assertItemsEqual(mzs,mzs_) self.assertItemsEqual(vals,vals_)
def test_get_centroid_spectrum(self): mzs = [0, 1, 2, 3] vals = [1, 2, 1, 0] this_spectrum = mass_spectrum() this_spectrum.add_centroids(mzs, vals) [mzs_, vals_] = this_spectrum.get_spectrum(source='centroids') self.assertItemsEqual(mzs, mzs_) self.assertItemsEqual(vals, vals_)
def test_get_profile_spectrum(self): mzs = [0, 1, 2, 3] vals = [1, 2, 1, 0] this_spectrum = mass_spectrum() this_spectrum.add_spectrum(mzs, vals) [mzs_, vals_] = this_spectrum.get_spectrum(source='profile') self.assertItemsEqual(mzs, mzs_) self.assertItemsEqual(vals, vals_)
def test_get_centroid_spectrum(self): mzs = [0,1,2,3] vals = [1,2,1,0] this_spectrum = mass_spectrum() this_spectrum.add_centroids(mzs,vals) [mzs_,vals_] = this_spectrum.get_spectrum(source='centroids') self.assertItemsEqual(mzs,mzs_) self.assertItemsEqual(vals,vals_)
def add_spectrum(self, profile_mzs=[], profile_intensities=[], centroids_mz=[], centroid_intensity=[], index=[]): if profile_mzs == [] & centroids_mz == []: raise ValueError("one of profile or centroids mzs should be non-empty") new_spectrum = mass_spectrum() new_spectrum.add_spectrum(profile_mzs, profile_intensities) new_spectrum.add_centroids(centroids_mz, centroid_intensity) if index == []: self.index_list.append(max(self.index_list) + 1) self.spectra.append(new_spectrum)
def get_spectrum(self,index,datatype='profile'): this_spectrum = mass_spectrum() tmp_str='/spectral_data/%d/' %(index) if datatype == 'profile': this_spectrum.add_spectrum(self.hdf[tmp_str+'mzs/'],self.hdf[tmp_str+'/intensities/']) elif datatype=='centroid': this_spectrum.add_centroids(self.hdf[tmp_str+'centroid_mzs/'],self.hdf[tmp_str+'/centroid_intensities/']) elif datatype == 'all': if self.hdf[tmp_str+'mzs/']: this_spectrum.add_spectrum(self.hdf[tmp_str+'mzs/'],self.hdf[tmp_str+'/intensities/']) if self.hdf[tmp_str+'centroid_mzs/']: this_spectrum.add_centroids(self.hdf[tmp_str+'centroid_mzs/'],self.hdf[tmp_str+'/centroid_intensities/']) return this_spectrum
def add_spectrum(self, profile_mzs=[], profile_intensities=[], centroids_mz=[], centroid_intensity=[], index=[]): if profile_mzs == [] & centroids_mz == []: raise ValueError( 'one of profile or centroids mzs should be non-empty') new_spectrum = mass_spectrum() new_spectrum.add_spectrum(profile_mzs, profile_intensities) new_spectrum.add_centroids(centroids_mz, centroid_intensity) if index == []: self.index_list.append(max(self.index_list) + 1) self.spectra.append(new_spectrum)
def get_spectrum(self,index): this_spectrum = mass_spectrum() tmp_str='/spectral_data/%d' %(index) try: this_spectrum.add_spectrum(self.hdf[tmp_str+'/mzs/'],self.hdf[tmp_str+'/intensities/']) got_spectrum=True except KeyError: got_spectrum=False try: this_spectrum.add_centroids(self.hdf[tmp_str+'/centroid_mzs/'],self.hdf[tmp_str+'/centroid_intensities/']) got_centroids=True except KeyError: got_centroids=False if not any([got_spectrum,got_centroids]): raise ValueError('No spectral data found in index {}'.format(index)) return this_spectrum
def test_add_centroids(self): mzs_counts_list = [ [[0,1,2],[2,2,2]], [[0,0,0],[0,0,0]], [[0,1,2], [2,]] ] test_vals_should_add= [ True, True, False, ] for mzs_counts,should_add in zip(mzs_counts_list,test_vals_should_add): this_spectrum = mass_spectrum() if should_add: this_spectrum.add_centroids(mzs_counts[0],mzs_counts[1]) self.assertItemsEqual(this_spectrum.centroids,mzs_counts[0]) self.assertItemsEqual(this_spectrum.centroids_intensity,mzs_counts[1]) else: with self.assertRaises(IOError): this_spectrum.add_centroids(mzs_counts[0],mzs_counts[1])
def get_spectrum(self, index, datatype='profile'): this_spectrum = mass_spectrum() tmp_str = '/spectral_data/%d/' % (index) if datatype == 'profile': this_spectrum.add_spectrum(self.hdf[tmp_str + 'mzs/'], self.hdf[tmp_str + '/intensities/']) elif datatype == 'centroid': this_spectrum.add_centroids( self.hdf[tmp_str + 'centroid_mzs/'], self.hdf[tmp_str + '/centroid_intensities/']) elif datatype == 'all': if self.hdf[tmp_str + 'mzs/']: this_spectrum.add_spectrum(self.hdf[tmp_str + 'mzs/'], self.hdf[tmp_str + '/intensities/']) if self.hdf[tmp_str + 'centroid_mzs/']: this_spectrum.add_centroids( self.hdf[tmp_str + 'centroid_mzs/'], self.hdf[tmp_str + '/centroid_intensities/']) return this_spectrum
def get_spectrum(self, index): this_spectrum = mass_spectrum() tmp_str = '/spectral_data/%d' % (index) try: this_spectrum.add_spectrum(self.hdf[tmp_str + '/mzs/'], self.hdf[tmp_str + '/intensities/']) got_spectrum = True except KeyError: got_spectrum = False try: this_spectrum.add_centroids( self.hdf[tmp_str + '/centroid_mzs/'], self.hdf[tmp_str + '/centroid_intensities/']) got_centroids = True except KeyError: got_centroids = False if not any([got_spectrum, got_centroids]): raise ValueError( 'No spectral data found in index {}'.format(index)) return this_spectrum
def test_add_spectrum(self): mzs_counts_list = [[[0, 1, 2], [2, 2, 2]], [[0, 0, 0], [0, 0, 0]], [[0, 1, 2], [ 2, ]]] test_vals_should_add = [ True, True, False, ] for mzs_counts, should_add in zip(mzs_counts_list, test_vals_should_add): this_spectrum = mass_spectrum() if should_add: this_spectrum.add_spectrum(mzs_counts[0], mzs_counts[1]) self.assertItemsEqual(this_spectrum.mzs, mzs_counts[0]) self.assertItemsEqual(this_spectrum.intensities, mzs_counts[1]) else: with self.assertRaises(IOError): this_spectrum.add_spectrum(mzs_counts[0], mzs_counts[1])
def isodist(molecules, charges=0, output='', plot=False, sigma=0.35, resolution=250, cutoff=0.0001, do_centroid=True, verbose=False): exit = checkhelpcall(molecules) save = checkoutput(output) if exit == True: sys.exit(0) molecules = molecules.split(',') for element in molecules: element = formulaExpander(element) if verbose: print ( 'The mass of %(substance)s is %(Mass)f and the calculated charge is %(Charge)d with m/z of %(Mz)f.' % { 'substance': \ element, 'Mass': molmass(element), 'Charge': molcharge(element), 'Mz': mz(molmass(element), molcharge(element), charges)}) if charges == 0: charges = molcharge(element) if charges == 0: charges = 1 else: if verbose: print "Using user-supplied charge of %d for mass spectrum" % charges isomasses = isotopemasses(element) isoratios = isotoperatios(element) if len(isomasses) != 1: ratios, masses = isotopes(isoratios, isomasses, cutoff) # slow final = genDict(masses, ratios, charges, cutoff) # very slow else: final = genDict(isomasses[0], isoratios[0], charges, cutoff) # for i in sorted(final.keys()): #fast # if final[i]>cutoff: # print i,final[i] ms_output = mass_spectrum() if do_centroid: from pyMS.centroid_detection import gradient pts = resolution2pts(min(final.keys()), max(final.keys()), resolution) xvector, yvector = genGaussian(final, sigma, pts) # slow ms_output.add_spectrum(xvector, yvector) mz_list, intensity_list, centroid_list = gradient(ms_output.get_spectrum()[0], ms_output.get_spectrum()[1], max_output=-1, weighted_bins=5) ms_output.add_centroids(mz_list, intensity_list) else: mz_idx = sorted(final.keys()) ms_output.add_centroids(mz_idx, [final[f] for f in mz_idx]) if plot == True: import matplotlib.pyplot as plt # for plotting plt.plot(xvector, yvector) plt.plot(mz_list, intensity_list, 'rx') plt.show() if save == True: g = open(savefile, 'w') xs = xvector.tolist() ys = yvector.tolist() for i in range(0, len(xs)): g.write(str(xs[i]) + "\t" + str(ys[i]) + "\n") g.close return ms_output
def make_spectrum(self,mzs,vals,centroid_mzs,centroid_vals): this_spectrum = mass_spectrum() this_spectrum.add_spectrum(mzs,vals) this_spectrum.add_centroids(centroid_mzs,centroid_vals) return this_spectrum
def get_spectrum(self,index): this_spectrum = mass_spectrum() tmp_str='/spectral_data/%d/' %(index) this_spectrum.add_centroids(self.hdf[tmp_str+'/centroid_mzs/'],self.hdf[tmp_str+'/centroid_intensities/']) return this_spectrum
def make_spectrum(self, mzs, vals, centroid_mzs, centroid_vals): this_spectrum = mass_spectrum() this_spectrum.add_spectrum(mzs, vals) this_spectrum.add_centroids(centroid_mzs, centroid_vals) return this_spectrum
def get_spectrum_imzml(self,index): mzs, intensities = self.imzml.getspectrum(index) ## temp hack -> assume centroided this_spectrum = mass_spectrum() this_spectrum.add_centroids(mzs,intensities) return this_spectrum