def compute_alignment(self, other, mz_tolerance): assert isinstance(other, Spectrum), "need spectrum as argument" aligner = pyopenms.SpectrumAlignment() alignment = [] conf = aligner.getDefaults() conf_d = conf.asDict() conf_d[ "is_relative_tolerance"] = "false" # "true" not implemented yet ! conf_d["tolerance"] = mz_tolerance conf.update(conf_d) aligner.setParameters(conf) s0 = self.toMSSpectrum() s1 = other.toMSSpectrum() alignment = [] aligner.getSpectrumAlignment(alignment, s0, s1) return alignment
def compute_alignments(spectra, mz_tolerance): """takes a list of spectra and groups peaks given mz_tolerance. it returns a list of lists. every inner list specifies the alignment of one input spectrum to its follower in the list. One assignment is a list of tuples, where the first entry is a peak index from the first list, the second entry is the index of a peak from the second spectrum. For example: if you run this method with a list or tuple of three spectra (s0, s1, s2) the return values will be [align_0_to_1, align_1_to_2] an alignment is a list [(i0, j0), (i1, j1), ...] so that s0.peaks[i0, :] is assigned to s1.peaks[j0, :] and so on. """ aligner = pyopenms.SpectrumAlignment() alignment = [] conf = aligner.getDefaults() conf_d = conf.asDict() conf_d[ "is_relative_tolerance"] = "false" # "true" not implemented yet ! conf_d["tolerance"] = mz_tolerance conf.update(conf_d) aligner.setParameters(conf) openms_spectra = [s.toMSSpectrum() for s in spectra] # create pairwise alignments alignments = [] for s0, s1 in zip(openms_spectra, openms_spectra[1:]): alignment = [] aligner.getSpectrumAlignment(alignment, s0, s1) alignments.append(alignment) return alignments
mz.append(float(i)) spectrums = [] based_intensity = np.transpose(source['mz_exp']) for i in range(based_intensity.shape[0]): peak_intensity = based_intensity[i] spectrum = pyopenms.MSSpectrum() spectrum.set_peaks([mz, peak_intensity]) spectrum.sortByPosition() spectrums.append(spectrum) super_spectrum_mz = np.array(super_spectrum_mz) print(super_spectrum_mz.shape) aligned_intensities = [] aligner = pyopenms.SpectrumAlignment() target_spectrum = pyopenms.MSSpectrum() target_spectrum.set_peaks( [super_spectrum_mz, np.zeros_like(super_spectrum_mz)]) target_spectrum.sortByPosition() for spectrum in spectrums: alignment = [] aligner.getSpectrumAlignment(alignment, spectrum, target_spectrum) _, to_align_intensity = spectrum.get_peaks() aligned_intensity = np.zeros_like(super_spectrum_mz) for t in alignment: aligned_intensity[t[1]] = to_align_intensity[t[0]] aligned_intensities.append(aligned_intensity) aligned_intensities = np.array(aligned_intensities)