Exemple #1
0
def has_peaks(exp: ms.MSExperiment) -> bool:
    """Checks if any spectrum in an experiment has peaks."""
    for i in range(exp.getNrSpectra()):
        spec = exp.getSpectrum(i)
        if spec.size() > 0:
            return True
    return False
Exemple #2
0
def read_sample(input_file, expt_='ecoli', mode='pos'):
    SM = Sample(expt_, mode, input_file)
    exp = MSExperiment()
    MzMLFile().load(input_file, exp)
    print(input_file, exp.getNrSpectra())
    scans = []
    ii = 0
    for sp in exp:
        ms_level = sp.getMSLevel()
        if ms_level == 1:  # ONLY dealig with MS1 here
            rt = sp.getRT()
            mz, intensity = sp.get_peaks()
            # use int and tuple to save space when storage is considered
            intensity = [int(x) for x in intensity]
            # Scan defined as 'scan_number', 'retention_time', 'mz_values', 'intensity_values'
            scans.append(Scan(ii, rt, tuple(mz), tuple(intensity)))
            # scans.append(Scan(ii, rt, mz, intensity))
        ii += 1

    SM.scans = scans
    SM.retention_index = tuple([sc.scan_number for sc in scans])
    SM.retention_time = tuple([sc.retention_time for sc in scans])
    return SM
Exemple #3
0
    def store(self, ofname: str, peakMap: pyopenms.MSExperiment):

        #sort peakMap if necessary
        if not peakMap.isSorted():
            peakMap.sortSpectra()
            peakMap.updateRanges()

        outF = open(ofname, 'w')

        firstScan = self._getScan(
            peakMap.getSpectrum(0).getNativeID().decode('utf-8'))
        dataType = peakMap.getSpectrum(0).getType()
        dataType = 'Centroid' if dataType == 1 else 'Profile' if dataType == 2 else 'Unknown'
        lastScan = self._getScan(
            peakMap.getSpectrum(peakMap.getNrSpectra() -
                                1).getNativeID().decode('utf-8'))
        precursorFile = basename(ofname).replace('ms2', 'ms1')

        #print header
        outF.write(
            self._writeValue(
                MS2File._h_tag, 'CreationDate',
                datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p')))
        outF.write(self._writeValue(MS2File._h_tag, 'Extractor', 'msConvert'))
        outF.write(self._writeValue(MS2File._h_tag, 'ExtractorVersion', '0.1'))
        outF.write(
            self._writeValue(MS2File._h_tag, 'Comments',
                             'msConvert was written by Aaron Maurais, 2019'))
        outF.write(self._writeValue(MS2File._h_tag, 'ExtractorOptions', 'MS2'))
        outF.write(
            self._writeValue(MS2File._h_tag, 'AcquisitionMethod',
                             'Data-Dependent'))
        outF.write(
            self._writeValue(MS2File._h_tag, 'InstrumentType', 'Unknown'))
        outF.write(self._writeValue(MS2File._h_tag, 'ScanType', 'MS2'))
        outF.write(self._writeValue(MS2File._h_tag, 'DataType', dataType))
        outF.write(self._writeValue(MS2File._h_tag, 'FirstScan', firstScan))
        outF.write(self._writeValue(MS2File._h_tag, 'LastScan', lastScan))

        #iterate through spectra
        preScan = 'Unknown'
        for i, scan in enumerate(peakMap.getSpectra()):
            if scan.getMSLevel() == 1:
                preScan = self._getScan(scan.getNativeID().decode('utf-8'))

            if scan.getMSLevel() == 2:
                #write header info
                curScan = self._getScan(scan.getNativeID().decode('utf-8'))
                precursors = scan.getPrecursors()
                preCharge = int(precursors[0].getCharge())
                preMZ = precursors[0].getMZ()

                #print scan line
                outF.write('{}\t{}\t{}\t{}\n'.format(MS2File._s_tag,
                                                     curScan.zfill(6),
                                                     curScan.zfill(6), preMZ))
                #print scan info
                outF.write(
                    self._writeValue(MS2File._i_tag, 'RetTime', scan.getRT()))
                outF.write(
                    self._writeValue(MS2File._i_tag, 'PrecursorInt',
                                     precursors[0].getIntensity()))
                outF.write(
                    self._writeValue(MS2File._i_tag, 'IonInjectionTime',
                                     'Unknown'))
                ameth = list(precursors[0].getActivationMethods())
                ameth = ' '.join(
                    [MS2File._activationMethods[x] for x in ameth])
                if not ameth:
                    ameth = 'Unknown'
                outF.write(
                    self._writeValue(MS2File._i_tag, 'ActivationType', ameth))
                outF.write(
                    self._writeValue(MS2File._i_tag, 'PrecursorFile',
                                     precursorFile))
                outF.write(
                    self._writeValue(MS2File._i_tag, 'PrecursorScan', preScan))
                outF.write(
                    self._writeValue(MS2File._i_tag, 'InstrumentType',
                                     'Unknown'))

                #write z line
                #after charge, the M+H m/z for the ion is listed, so calculate that here
                outF.write(
                    self._writeValue(
                        MS2File._z_tag, preCharge, (float(preMZ) * preCharge) -
                        (preCharge * MS2File._H_mass) + MS2File._H_mass))

                #write ions
                for ion in scan:
                    outF.write('{0:.4f} {1:.1f}\n'.format(
                        round(ion.getMZ(), 4), round(ion.getIntensity(), 1)))
Exemple #4
0
def combine_experiments(exp1: ms.MSExperiment, exp2: ms.MSExperiment) -> None:
    """Merges two experiments (putting the second into the first)."""
    for i in range(exp2.getNrSpectra()):
        spec = exp2.getSpectrum(i)
        exp1.addSpectrum(spec)