def _parseSpectrum(self): """Parse spectrum data.""" # get acquisition parames params = self._parseParams() if not params: return False # open spectrum try: document = file(self.path, 'rb') rawData = document.read() document.close() except IOError: return False # convert to list try: count = len(rawData) / struct.calcsize('i') start, end = 0, len(rawData) points = struct.unpack('i' * count, rawData[start:end]) except: return False # check data length if len(points) != int(params['TD']): return False # shift X-axis if params['SPTYPE'] == 'psd': shift = float(params['ML1']) interval = float(params['DW']) else: shift = float(params['DELAY']) interval = float(params['DW']) buff = [] for x in points: buff.append([shift, x]) shift += interval points = buff # convert X-axis to m/z if params['SPTYPE'] == 'tof': points = self._convertTimeToMZ(points, float(params['ML1']), float(params['ML2']), float(params['ML3'])) # convert to scan object spectrum = objects.scan(points) # set additional parameters spectrum.msLevel = 1 if params['SPTYPE'] == 'psd': spectrum.msLevel = 2 if 'Parent' in params: spectrum.precursorMZ = params['Parent'] return spectrum
def _parseSpectrum(self): """Parse spectrum data.""" # get acquisition parames params = self._parseParams() if not params: return False # open spectrum try: document = file(self.path, 'rb') rawData = document.read() document.close() except IOError: return False # convert to list try: count = len(rawData)/struct.calcsize('i') start, end = 0, len(rawData) points = struct.unpack('i' * count, rawData[start:end]) except: return False # check data length if len(points) != int(params['TD']): return False # shift X-axis if params['SPTYPE'] == 'psd': shift = float(params['ML1']) interval = float(params['DW']) else: shift = float(params['DELAY']) interval = float(params['DW']) buff = [] for x in points: buff.append([shift, x]) shift += interval points = buff # convert X-axis to m/z if params['SPTYPE'] == 'tof': points = self._convertTimeToMZ(points, float(params['ML1']), float(params['ML2']), float(params['ML3'])) # convert to scan object spectrum = objects.scan(points) # set additional parameters spectrum.msLevel = 1 if params['SPTYPE'] == 'psd': spectrum.msLevel = 2 if 'Parent' in params: spectrum.precursorMZ = params['Parent'] return spectrum
def getSpectrum(self, discrete=False): """Get spectrum from document.""" # parse data data = self._parseData() if not data: return False # parse data as peaklist (discrete points) if discrete: for x, p in enumerate(data): data[x] = objects.peak(p[0], p[1]) spectrum = objects.scan(peaks=data) # parse data as spectrum (continuous line) else: spectrum = objects.scan(points=data) return spectrum
def getSpectrum(self, scanNumber=None): """Get spectrum from document.""" # init parser handler = scanHandler(scanNumber) parser = xml.sax.make_parser() parser.setContentHandler(handler) # parse document try: document = file(self.path) parser.parse(document) document.close() scan = handler.scan except stopParsing: scan = handler.scan except xml.sax.SAXException: return False # check data if not scan: return False # parse peaks try: points = self._parsePoints(scan) except: return False # parse data as peaklist (discrete points) if scan['spectrumType'] == 'discrete': for x, p in enumerate(points): points[x] = objects.peak(p[0], p[1]) spectrum = objects.scan(peaks=points) # parse data as spectrum (continuous line) else: spectrum = objects.scan(points=points) # set metadata if scan['scanNumber']: spectrum.scanNumber = int(scan['scanNumber']) if scan['parentScanNumber']: spectrum.parentScanNumber = int(scan['parentScanNumber']) if scan['msLevel']: spectrum.msLevel = int(scan['msLevel']) if scan['polarity']: spectrum.polarity = scan['polarity'] if scan['retentionTime']: spectrum.retentionTime = scan['retentionTime'] if scan['totIonCurrent']: spectrum.totIonCurrent = float(scan['totIonCurrent']) if scan['basePeakMz']: spectrum.basePeakMZ = float(scan['basePeakMz']) if scan['basePeakIntensity']: spectrum.basePeakIntensity = float(scan['basePeakIntensity']) if scan['precursorMz']: spectrum.precursorMZ = float(scan['precursorMz']) if scan['precursorIntensity']: spectrum.precursorIntensity = float(scan['precursorIntensity']) return spectrum