allfractionbgratios = {} for isotopelabel_id, data in allsumionratiodata.iteritems(): allfractionbgratios[isotopelabel_id] = np.median(data) # second normalization so that the bg-ratios all add to 1 for isotopelabel_id, data in allfractionbgratios.iteritems(): allfractionbgratios[isotopelabel_id] = data / sum(allfractionbgratios.values()) logger.log.debug(('allfractionbgratios are %s' % str(allfractionbgratios))) for corrects2iquantob in corrects2iquantoblist: # perform correction for each of the analyzed .hdf5 files. s2icorrecteddata = corrects2iquantob.performS2Icorrection(allfractionbgratios) corrects2iquantob.hdf5corrects2iquant.updates2ivalues(s2icorrecteddata) hdf5corrects2iquant.close() except ExHa.czException as czEx: ExHa.reformatException(czEx) ExHa.addContext(czEx, 'Error during corrects2iquant run') ExHa.exportError2File(czEx, cfg.parameters['runtime']['datadir'] / Path('errors.error')) if logger: logger.log.warning(ExHa.oneLineRepr(czEx)) else: print ExHa.multiLineRepr(czEx) except Exception as genEx: ExHa.reformatException(genEx) ExHa.addContext(genEx, 'Error during corrects2iquant run') ExHa.exportError2File(genEx, cfg.parameters['runtime']['datadir'] / 'errors.error') if logger: logger.log.warning(ExHa.oneLineRepr(genEx)) else:
class mgftools: def __init__(self, hdf5file): """ @brief initialises the mgftools class @param hdf5file <string/path>: path for the hdf5 file to analyse """ self.cfgFilters = cfg.parameters['msmsfilters'] self.maxint = 0 self.proton = cfg.parameters['general']['proton'] self.neutron = cfg.parameters['general']['neutron'] self.hdf5 = hdf5Base(hdf5file) self.hdf5.readOpen() self.filters = dict(ten=self.tenpercent, zone=self.zonefilter, repion=self.repionfilter, deconv=self.deconvolute, mascot=self.mascot, neutralloss=self.neutrallossfilter, multi=self.multichargefilt, immonium=self.immoniumfilter, none='') self.isos = self.hdf5.readTable('/rawdata/isotopes') frags = self.getActivationTypes() if len(frags) == 1: if frags == ['HCD']: # hcd olny method usefilts = self.cfgFilters['filt_hcd'] self.remove = self.cfgFilters['rem_hcd'] else: # CID/PQD only method usefilts = self.cfgFilters['filt_other'] self.remove = self.cfgFilters['rem_other'] else: # mixed method usefilts = self.cfgFilters['filt_mixed_hcd_other'] self.remove = self.cfgFilters['rem_mixed_hcd_other'] if len(self.isos) == 0: repionIdx = -1 for idx in range(len(usefilts)): if usefilts[idx] == 'repion': repionIdx = idx break if repionIdx != -1: usefilts.pop(repionIdx) self.usefilts = usefilts self.hdf5.close() def close(self): self.hdf5.close() def getActivationTypes(self): data = self.hdf5.getDataEqual('/rawdata/parameters', 'parameter', 'Activation Type') if len(data) == 0: data = self.hdf5.getDataEqual('/rawdata/parameters', 'parameter', 'activation') types = {} for act in data.flat: activation = act['value'].upper() types[activation] = types.get(activation, 0) + 1 return types.keys() def export(self, hcdonly=0): """ @brief creates an mgf file for the MS/MS spectra in the hdf5 file @param hcdonly <integer>: flag to switch output to only HCD spectra bypassing the normal export filters """ if hcdonly: remove = ['CID'] filters = ['none'] else: remove = self.remove filters = self.usefilts hdf = self.hdf5 mgfFile = hdf.filePath.parent.joinpath(hdf.filePath.stem + '.mgf') # extra = path(hdf.filepath.splitext()[0] + '.txt') # self.fextra = extra.open('w') # self.fextra.write('spec_id\tmz\tinten\trel_inten\tion\texp_mz\n') mgfOut = open(str(mgfFile), 'w') mgfOut.write('#Removed spectra = %s, filtering = %s\n' % (remove, filters)) spec = 0 # read parameters from hdf5 file try: hdf.appendOpen() headers = hdf.readTable('/rawdata/msmsheader') runTimeEntry = hdf.getDataEqual('/rawdata/parameters', 'parameter', 'MS Run Time (min)') if len(runTimeEntry) == 0: raise ExHa.MGFprocessingError( 'MGF Error: Could not find "MS Run Time (min)" parameter in HDF5 file.' ) runtime = runTimeEntry[0]['value'] units = self.readUnitsOK() # add new table for the deconvoluted spectrum data hdf.removeTable('/rawdata/deconvions') hdf.createTable('rawdata', 'deconvions', 'DeconvIons') ident = [] for frag in units[1]: # find all the frag methods to be used in identification if 'I' in frag['use']: ident.append(frag['order']) logger.log.info('Reading %d spectra from %s' % (len(headers), hdf.filePath.name)) if 'deconv' in filters: deconv = 1 else: deconv = 0 pBar = progBar.ProgressBar(widgets=progBar.name_widgets, maxval=len(headers), name='Create .mgf').start() for idx, h in enumerate(headers): if hcdonly: if h['fragmeth'] != 'HCD': continue elif not h['order'] in ident: continue pBar.update(idx) # get spectrum data spec = h['spec_id'] spectrum = hdf.getDataEqual('/rawdata/ions', 'spec_id', spec) if deconv: # need extra column for charge information spectrum = self.addChargeColumn(spectrum) data = hdf.getDataGeneral( '/rawdata/specparams', '(spec_id == %i) & (parameter == "%s")' % (spec, 'setmass1')) setmass = data[0]['value'] data = hdf.getDataGeneral( '/rawdata/specparams', '(spec_id == %i) & (parameter == "%s")' % (spec, 'frag1')) frag = data[0]['value'] try: self.maxint = max(spectrum['inten']) except: self.maxint = 0 # construct title values list rt = '%.3f' % h['rt'] use = units[1][h['order'] - 1]['use'] pretitle = '' if use == 'IQ': # spec is both ID and Quan so us normal msms ID titles = ['msmsid:F%06d' % h['spec_id']] elif use == 'I': if h['quan_spec'] == 0: # no quant data so use spec_id titles = ['msmsid:F%06d' % h['spec_id']] else: # spec is only for ident find the quan spec titles = ['msmsid:F%06d' % h['quan_spec']] pretitle = '#CID=F%06d\n' % h['id_spec'] elif use == 'Q': titles = ['msmsid:F%06d' % h['quan_spec']] pretitle = '#CID=F%06d\n' % h['id_spec'] titles.append('rt:' + rt) titles.append('survey:S%06d' % h['survey_spec']) titles.append('parent:' + setmass) titles.append('AnalTime:' + runtime) titles.append('Activation:' + frag.upper()) titleline = 'TITLE=%s\n' % ','.join(titles) if h['precmz'] > 0: pepmass = h['precmz'] elif h['precmz_surv'] > 0: pepmass = h['precmz_surv'] else: pepmass = h['monomz'] if pepmass == 0: continue for filt in filters: if len(spectrum) > 5 and self.filters[filt]: spectrum = self.filters[filt](h, spectrum) # filter for mascot interference ionList = [] if len(spectrum) > 2: mgfOut.write(pretitle) mgfOut.write('BEGIN IONS\n') mgfOut.write(titleline) mgfOut.write('PEPMASS=%f\n' % pepmass) mgfOut.write('CHARGE=%d+\n' % h['charge']) if deconv: for pt in spectrum: if pt['inten'] == 0: continue mgfOut.write('%f %f %s\n' % (pt['mz'], pt['inten'], pt['charge'])) ionList.append( dict(spec_id=pt['spec_id'], mz=pt['mz'], inten=pt['inten'], charge=pt['charge'])) else: for pt in spectrum: if pt['inten'] == 0: continue mgfOut.write('%f %f\n' % (pt['mz'], pt['inten'])) ionList.append( dict(spec_id=pt['spec_id'], mz=pt['mz'], inten=pt['inten'])) mgfOut.write('END IONS\n\n') if len(ionList) > 0: hdf.appendRows('/rawdata/deconvions', ionList) pBar.finish() except ExHa.MGFprocessingError, czEx: if spec: ExHa.addContext(czEx, 'Raised whist processing spectrum %i' % spec) raise except Exception as genEx: ExHa.reformatException(genEx) if spec: ExHa.addContext(genEx, 'Raised whist processing spectrum %i' % spec) raise
ret = cfg.evaluateCommandLineArgs(sys.argv) try: cfg.scalePpmMda() dataDir = cfg.parameters['runtime']['datadir'] logParam = cfg.parameters['logging'] logPath = Path(dataDir.joinpath(logParam['logdir'])) if not logPath.exists(): logPath.mkdir(parents=True) logFile = logPath.joinpath(logParam['logfile']) logger = Logger(logFile, logParam['loglevel'], logParam['screenlevel'], False) logger.setMascotParserLogs() jobcontrol(cfg, logger) except ExHa.UsageError as useEx: ExHa.reformatException(useEx) print useEx.context except Exception as genEx: ExHa.reformatException(genEx) errorFile = Path(cfg.parameters['runtime']['hdf5file']).stem + '.error' ExHa.exportError2File( genEx, cfg.parameters['runtime']['datadir'].joinpath(errorFile)) if logs: logs.datlog.warning(ExHa.oneLineRepr(genEx)) else: print ExHa.multiLineRepr(genEx)
from pathlib import Path # cellzome CommonUtils sys.path.insert(0, '..') from CommonUtils.tools import * from CommonUtils.ConfigManager import pyMSsafeConfigManager from CommonUtils.LoggingManager import Logger from CommonUtils.hdf5Base import hdf5Base from CommonUtils.QuantMethodHandler import QuantMethods import CommonUtils.ExceptionHandler as ExHa # pyMSsafe modules try: from xRawFile import XRawFile except ImportError, ieEx: ExHa.reformatException(ieEx) ExHa.addContext(ieEx, 'Xcalibur not set up properly') configPath = './pymssafe.cfg' cfg = pyMSsafeConfigManager(configPath) ret = cfg.evaluateCommandLineArgs(sys.argv) dataDir = cfg.parameters['runtime']['datadir'] ExHa.exportError2File(ieEx, dataDir.joinpath('errors.error')) from datastore import Datamanager class pymssafe: def __init__(self, config): """ @brief initiaise pyMSsafe controller @param mode <string>: operational mode of the application