def createPpmFreqDataSource(exp,dsName,dsType,numDim): ds = Nmr.DataSource(exp,name = dsName, numDim = numDim, dataType = dsType) for dim in range(0,numDim): expDim = exp.findFirstExpDim(dim = (dim + 1)) expDimRefs = expDim.sortedExpDimRefs() refIsotopeCode = expDimRefs[0].isotopeCodes[0] # User first expDimRef to set number of points # Set numPoints, numPointsOrig to 20 for H, 250 for C,N! if refIsotopeCode == '1H': numPoints = 1024 valuePerPoint = 6.5 else: numPoints = 256 if refIsotopeCode == '13C': valuePerPoint = 100.0 elif refIsotopeCode== '15N': valuePerPoint = 40.0 else: valuePerPoint = 1.0 # Setting up empty freqdatadim and associated datadimref... # Assuming that imaginary data will be deleted! freqDataDim = Nmr.FreqDataDim(ds, expDim = expDim, dim = (dim + 1), isComplex = True,valuePerPoint = valuePerPoint, numPoints = numPoints, numPointsOrig = numPoints) # Tag to mark that this is a 'default' freqDataDim freqDataDim.addApplicationData(Implementation.AppDataBoolean(application = ccpNmr_kw, keyword = 'default', value = True)) # Set sensible dataDimRef info. setDataDimRefs(ds) return ds
def setPeakIntensity(self): # PeakIntensity attributes (only set if they were reported in the file) if self.rawPeak.intensity != None and (self.rawPeak.intensity or not self.rawPeak.volume): peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.intensity, method=self.methods[self.format]['Intensity (data)']) peakInt.intensityType = height_kw if self.rawPeak.intensityFit != None: peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.intensityFit, method=self.methods[self.format]['Intensity (fitted)']) peakInt.intensityType = height_kw if self.rawPeak.volume != None and (self.rawPeak.volume or not self.rawPeak.intensity): methodKey = "Volume (%s)" % self.rawPeak.volumeMethod peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.volume, method=self.methods[self.format][methodKey]) peakInt.intensityType = volume_kw
def createShift(self, resonance, chemShift): keywds = {} # # Also set ambiguity code if listed # if chemShift.ambCode != None: setUniqueAppData('AppDataInt', resonance, self.format, 'ambiguityCode', chemShift.ambCode) # # Shift error could be missing... # if hasattr(chemShift, 'error'): keywds['error'] = chemShift.error # # Create shift linked to resonance # self.convertCount[self.mainCode][1][self.valueCode] += 1 return Nmr.Shift(self.measurementList, value=chemShift.value, resonance=resonance, **keywds)
def setPeak(self): """ Cannot overwrite for Sparky: have to delete all old peaks and create all new ones. """ if self.rawPeak.num != None: details = "Original number %d" % self.rawPeak.num else: details = "No original number" self.peak = Nmr.Peak(self.peakList, details=details) # # Also set original number in applicationdata so can be reused for writing # if self.rawPeak.num != None: self.peak.addApplicationData( Implementation.AppDataInt(application=self.format, keyword=peakNum_kw, value=self.rawPeak.num)) # Sparky specific - has to be reset for every peak self.resLabelCodes = []
def setPeakIntensity(self): # PeakIntensity attributes if self.rawPeak.intensity != 0: peakInt = Nmr.PeakIntensity(self.peak,value = self.rawPeak.intensity, method = self.methods[self.format]['Intensity']) # Assuming always intensity for pronto peakInt.intensityType = height_kw
def setPeakExtras(self): if self.rawPeak.assign[0]: # # Make peakContrib... # self.peakContribs.append(Nmr.PeakContrib(self.peak))
def setPeakIntensity(self): # PeakIntensity attributes # Intensity and volume are always set for an nmrView peak if self.rawPeak.volume != 0: peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.volume, method=self.methods[self.format]['Volume']) peakInt.intensityType = volume_kw if self.rawPeak.intensity != 0: peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.intensity, method=self.methods[self.format]['Intensity']) peakInt.intensityType = height_kw
def setPeakIntensity(self): # PeakIntensity attributes if self.rawPeak.intensity != 0: # Software method not known: could come from anywhere peakInt = Nmr.PeakIntensity(self.peak,value = self.rawPeak.intensity, method = self.methods['unknown']['Intensity']) # Assuming height for autoassign peakInt.intensityType = height_kw
def setPeakIntensity(self): # PeakIntensity attributes if self.rawPeak.volume != 0: peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.volume, method=self.methods[self.format]['Volume']) # Assuming always volume for ansig peakInt.intensityType = volume_kw
def setPeakExtras(self): if self.cyanaFormat and self.rawPeak.ambiguousAssign: # # Make peakContribs for this one... # numContribs = len(self.rawPeak.ambiguousAssign) + 1 for i in range(numContribs): self.peakContribs.append(Nmr.PeakContrib(self.peak))
def setPeakIntensity(self): self.intMethod = self.peakFile.translateIntMethod(self.format,self.rawPeak.intCode) if self.intMethod != 'Not integrated': if self.forceHeight: # PeakIntensity attributes peakInt = Nmr.PeakIntensity(self.peak,value = self.rawPeak.volume, method = self.methods['unknown']['Intensity']) intensityType = height_kw else: # PeakIntensity attributes # Assuming that always volume for xeasy (all integration) peakInt = Nmr.PeakIntensity(self.peak,value = self.rawPeak.volume, method = self.methods[self.format][self.intMethod]) intensityType = volume_kw peakInt.error = self.rawPeak.volumeError peakInt.intensityType = intensityType
def setPeakIntensity(self): # # PeakIntensity attributes # if self.rawPeak.intensity != 0: peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.intensity, error=self.rawPeak.intensityError, method=self.methods[self.format]['Intensity']) peakInt.intensityType = height_kw if self.rawPeak.volume != 0: peakInt = Nmr.PeakIntensity( self.peak, value=self.rawPeak.volume, method=self.methods[self.format]['Volume']) peakInt.intensityType = volume_kw
def createShift(self,resonance,chemShift): # # Also set ambiguity code if listed # if chemShift.ambCode != None: setUniqueAppData('AppDataInt',resonance,self.format,'ambiguityCode',chemShift.ambCode) # # Create shift linked to resonance # return Nmr.Shift(self.measurementList, value = chemShift.value, resonance = resonance)
def newProject(self): if self.project: # Project already present if not self.closeProject(): # If we don't close the current project do nothing return name = askString(title='Project name', prompt='Enter project name:',parent=self) if name: # Make the API Project object project = Implementation.MemopsRoot(name=name) nmrProject = Nmr.NmrProject(project, name = project.name) self.initProject(project)
def createShift(self,resonance,chemShift): # # For xeasy also use chemShift to create atomSerial application data. # # Warning: if there is already an atomSerial available for this resonance # it will be overwritten!! # applData = resonance.findFirstApplicationData(application = self.format, keyword = self.atomSerialKeyword) if applData: resonance.removeApplicationData(applData) resonance.addApplicationData(Implementation.AppDataInt(application = self.format, keyword = self.atomSerialKeyword, value = chemShift.atomSerial)) # # Create shift linked to resonance # return Nmr.Shift(self.measurementList, value = chemShift.value, error = chemShift.valueError, resonance = resonance)
def setPeakExtras(self): if self.peakFile.hasAssignItems: # # Find maximum number (could in principle be different?) # maxNumContribs = 0 for assignIndex in range(len(self.rawPeak.assign)): if self.rawPeak.assign[assignIndex]: numContribs = len(self.rawPeak.assign[assignIndex].split( self.assignTagSep)) else: numContribs = 0 if maxNumContribs < numContribs: maxNumContribs = numContribs for i in range(maxNumContribs): self.peakContribs.append(Nmr.PeakContrib(self.peak))
def setupProject(self, fileName, projectName): self.projectFileName = fileName try: self.ccpnProject = XmlIO.loadProjectFile(fileName) print "Read existing CCPN project..." except: print "Creating new CCPN project..." projectName = returnMemopsWord(projectName) self.ccpnMemopsRoot = Implementation.MemopsRoot(name=projectName) self.nmrProject = Nmr.NmrProject(self.ccpnProject, name=self.ccpnProject.name) # # Set the location... # (path, file) = splitPath(fileName) self.ccpnProject.path = file self.ccpnProject.url.path = path outputPath = os.path.join(path, projectName) try: os.stat(outputPath) except: os.mkdir(outputPath) for url in self.ccpnProject.urls: if url != self.ccpnProject.url: url.path = outputPath # TODO: check name of project: if not same CRASH OUT! return self.ccpnProject
class OpenSpectrumPopup(BasePopup): r""" **Locate Spectrum Data for Use in CCPN Project** This popup window enables the user to locate spectrum data within a file system and associate the files (typically binary) with an experiment and spectrum name so that it may be visualised and accessed within the current CCPN project. Spectra of many different origins and file formats may be loaded, which currently includes Bruker, Varian, Felix, NMRPipe, NmrView, SPARKY/UCSF, Azara and the factorised shape format "USF3". Depending upon the file format of the spectrum, data loaded the user may be required to either select a parameter file which then refers to the actual spectrum intensity data; this is true for Bruker "procs" and AZARA ".par" files, or alternatively a spectrum data file itself that contains referencing information; this is the case for SPARKY/UCSF, NmrView and NMRPipe spectra. The layout of the popup involved two sections; the upper of which is for navigating to and selecting the spectrum or parameter files within the file-system, and the lower is for specifying how each spectrum is loaded into the CCPN project. It should be noted that when spectrum parameters are read the first time, the relevant information is copied into the CCPN project, where it may be adjusted independently of the original file information. No copies of the spectrum intensity data are made, the CCPN project merely refers to the spectrum data on disk, although the data file for a loaded spectrum may subsequently be moved or replaced. In normal operation the user first selects the kind of spectrum file format that will be loaded via the upper "File format" pulldown menu and then checks that the "File type" pulldown (toward the bottom of the file browser) is set to detect the appropriate kinds of filename; if a helpful file name filter is not available the user can add one via the "Manual Select" field, taking care to add any wild-card symbols, like the asterisk in "\*.ft3". Next the spectrum data or parameter files, appropriate to the selected format, are located by navigating within the file-system browser. When the required spectrum files are visible the user selects one *or more* to load. Multiple file selections may be made using left-click with <Ctrl> (toggle selection) or <Shift> (select range). It should be noted that when selecting Bruker files, when using the standard Bruker directory structure, the user only needs to navigate to the numbered spectrum directory; by default the "procs" file two levels down is searched for, e.g. "123/pdata/1/procs" is shown in the directory containing the "123" directory. When spectrum or parameter files are selected in the file table, the lower "Spectra To Open" table is filled to reflect the selection. The user should then be mindful of the settings within this table and may choose to edit various things by double-clicking on the appropriate cell. Typically the user just adjusts the name of the independent "Experiment" and "Spectrum" records. These names are usually concatenated like "expName:specName" in CCPN graphical displays so there is no need to repeat a name in both fields; this only takes up more space. The Experiment, which is a record of *what was done experimentally*, commonly has a short name like "HNCA" or "HSQC_298K" so the user readily knows how to interpret the experimental data. The Spectrum, which is a record of *the data that was collected*, commonly has a short name to identify the spectrum number or file name. An Experiment record may contain several Spectrum records, so the spectrum's name need minimally only identify it amongst others from the same experiment. The Shift List value may be changed if the user knows that the experiment represents a distinct set of conditions, with different spectrum peak/resonance positions, to existing or other experiments being entered. Each shift list will be curated separately, to give separate chemical shift values for assignments made under different conditions (even when relating to the same atoms). The shift list that an experiment uses may also be changed at any time after loading. When all spectra and options are specified the [Open Spectrum] button will load the relevant data into the CCPN project. If the "Skip verification dialogs" option is set it is assumed that all of the spectrum point to frequency referencing information, and any data file references, are correct. Otherwise, the user will be prompted to confirm the file details and referencing information for each spectrum in turn. Finally, after loading the user is asked to set the type of NMR experiment, in terms of general magnetisation transfer pathway, that was performed. **Caveats & Tips** If the name of an Experiment that is *already within the CCPN project* is used, then the loaded spectrum will (assuming it is compatible) be entered under that existing experiment record; no new experiment entity will be defined. The user may legitimately use this feature to load several spectra that relate to the same experiment; typically where spectra are different projections. To facilitate this the "Use shared experiment" option can be selected. Although experiments and spectra may be renamed after loading, a spectrum record may not be placed under a different experiment once created; deletion and re-loading is the only mans of achieving this, and care must be taken in transferring any assignments. """ def __init__(self, parent, *args, **kw): self.experiment = None self.currentObject = None #self.currentObjects = [] # looks obsolete BasePopup.__init__(self, parent=parent, title='Experiment : Open Spectra', **kw) def open(self): self.message() BasePopup.open(self) def body(self, guiFrame): self.fileSelect = None names, objects = self.getShiftLists() self.shiftListPulldown = PulldownList(self, callback=self.setShiftList, texts=names, objects=objects) self.windowPulldown = PulldownList(self, texts=WINDOW_OPTS, callback=self.setWindow) self.experimentEntry = Entry(self, width=16, returnCallback=self.setExperiment) self.spectrumEntry = Entry(self, width=16, returnCallback=self.setSpectrum) guiFrame.grid_columnconfigure(0, weight=1) guiFrame.grid_rowconfigure(0, weight=1) guiFrame.grid_rowconfigure(1, weight=1) leftFrame = LabelFrame(guiFrame, text='File Selection') leftFrame.grid(row=0, column=0, sticky='nsew') leftFrame.grid_columnconfigure(3, weight=1) row = 0 label = Label(leftFrame, text='File format:') label.grid(row=row, column=0, sticky='w') tipText = 'Selects which kind of spectrum file is being loaded; what its data matrix format is' self.formatPulldown = PulldownList(leftFrame, callback=self.chooseFormat, texts=file_formats, tipText=tipText, grid=(row, 1)) self.detailsLabel = Label(leftFrame, text='Show details:') tipText = 'Whether to show an annotation that describes the spectrum in the file selection; currently only uses comment fields from Bruker spectra' self.detailsSelect = CheckButton(leftFrame, selected=False, callback=self.showDetails, tipText=tipText) self.titleRow = row self.detailsSelected = False row = row + 1 leftFrame.grid_rowconfigure(row, weight=1) file_types = [FileType('All', ['*'])] self.fileSelect = FileSelect(leftFrame, multiSelect=True, file_types=file_types, single_callback=self.chooseFiles, extraHeadings=('Details', ), extraJustifies=('left', ), displayExtra=False, getExtraCell=self.getDetails, manualFileFilter=True) self.fileSelect.grid(row=row, column=0, columnspan=6, sticky='nsew') rightFrame = LabelFrame(guiFrame, text='Spectra To Open') rightFrame.grid(row=1, column=0, sticky='nsew') rightFrame.grid_columnconfigure(3, weight=1) row = 0 label = Label(rightFrame, text='Skip verification dialogs:', grid=(row, 0)) tipText = 'Whether to allow the user to check file interpretation and referencing information before the spectrum is loaded' self.verifySelect = CheckButton(rightFrame, selected=False, grid=(row, 1), tipText=tipText) label = Label(rightFrame, text='Use shared experiment:', grid=(row, 2)) tipText = 'When selecting multiple spectrum files, whether the loaded spectra will all belong to (derive from) the same experiment; useful for projection spectra etc.' self.sharedExpSelect = CheckButton(rightFrame, selected=False, tipText=tipText, callback=self.useShared, grid=(row, 3)) row = row + 1 rightFrame.grid_rowconfigure(row, weight=1) tipTexts = [ 'A short textual name for the experiment record that the loaded spectrum will belong to; may be a new experiment or the name of an existing one', 'A short textual name to identify the spectrum within its experiment; typically a few characters or spectrum number, rather than a repeat of the experiment name', 'The location of the file, relative to the current directory, that the spectrum data will be loaded from', 'Sets which window or windows the spectrum will initially appear within once loaded', 'Sets which shift list the experiment (and hence loaded spectrum) will use to curate chemical shift information; can be changed after load time' ] headingList = [ 'Experiment', 'Spectrum', 'File', 'Windows', 'Shift List' ] editWidgets = [ self.experimentEntry, self.spectrumEntry, None, self.windowPulldown, self.shiftListPulldown ] editGetCallbacks = [ self.getExperiment, self.getSpectrum, None, self.getWindow, self.getShiftList ] editSetCallbacks = [ self.setExperiment, self.setSpectrum, None, self.setWindow, self.setShiftList ] self.scrolledMatrix = ScrolledMatrix(rightFrame, headingList=headingList, callback=self.selectCell, editWidgets=editWidgets, multiSelect=True, editGetCallbacks=editGetCallbacks, editSetCallbacks=editSetCallbacks, tipTexts=tipTexts, grid=(row, 0), gridSpan=(1, 4)) row = row + 1 tipTexts = [ 'Load spectrum or spectra into the CCPN project using the selected file(s)', ] texts = ['Open Spectrum'] commands = [self.openSpectra] bottomButtons = UtilityButtonList(guiFrame, texts=texts, tipTexts=tipTexts, doClone=False, commands=commands, helpUrl=self.help_url) bottomButtons.grid(row=row, column=0, columnspan=1, sticky='ew') self.openButton = bottomButtons.buttons[0] self.chooseFormat('Azara') self.message() def message(self): if not self.project or len(self.nmrProject.experiments) < 1: pass #self.parent.ticker.setMessage('Choose spectrum files to open.... ') def showDetails(self, isSelected): self.detailsSelected = isSelected self.fileSelect.updateDisplayExtra(isSelected) # below is so that when Details column is toggled on it will actually # be seen without having to use the scrollbar self.fileSelect.fileList.refreshSize() def useShared(self, isSelected): self.chooseFiles(forceUpdate=True) #if isSelected: #objects = self.scrolledMatrix.objectList #if len(objects) > 1: # self.currentObject = objects[0] # text = objects[0][0] # self.chooseFiles() # for oo in objects[1:]: # oo[0] = text # if self.project: # self.experiment = self.nmrProject.findFirstExperiment(name=text) #self.update() def gridDetails(self, bool): if bool: self.detailsLabel.grid(row=self.titleRow, column=2, sticky='w') self.detailsSelect.grid(row=self.titleRow, column=3, sticky='w') self.fileSelect.updateDisplayExtra(self.detailsSelected) else: self.detailsLabel.grid_forget() self.detailsSelect.grid_forget() self.fileSelect.updateDisplayExtra(False) def openSpectra(self): noVerify = self.verifySelect.getSelected() # tracks if 'add to existing experiment' has already ben OK'ed self.okExpSet = set() directory = self.fileSelect.getDirectory() spectra = [] specIndex = 0 for obj in self.scrolledMatrix.objectList: fileName = uniIo.joinPath(directory, obj.fileName) spectrum = self.openSpectrum(obj.exptName, obj.specName, fileName, obj.window, obj.shiftListName) specIndex += 1 if (spectrum): # check endianness if we are not verifying spectra.append(spectrum) if noVerify: isBigEndian = isSpectrumBigEndian( spectrum) # according to data in file if isBigEndian is not None: isBigEndianCurr = getIsSpectrumBigEndian( spectrum) # according to data model setIsSpectrumBigEndian(spectrum, isBigEndian) if isBigEndian != isBigEndianCurr: if isBigEndian: s = 'big' else: s = 'little' print 'WARNING: swapped endianess of spectrum to %s endian' % s # del self.okExpSet if noVerify and len(spectra) > 1 and self.sharedExpSelect.getSelected( ): # if we are using a shared experiment and not verifying, # set referencing to match first spectrum for all # get reference spectrum and set up data structure # use most recent pre-existing spectrum, otherwise first new one refSpec = spectra[0] for spec in spectra[0].experiment.sortedDataSources(): if spec in spectra: break else: refSpec = spec ddrLists = {} refDdrs = [] for dataDim in refSpec.sortedDataDims(): for ddr in dataDim.dataDimRefs: ddrLists[ddr.expDimRef] = [] refDdrs.append(ddr) # get dataDimRefs, store by ExpDimRef, # checking that all spectra have data dim refs for same set of xdr nTotal = len(ddrLists) for spec in spectra: nFound = 0 for dataDim in spec.sortedDataDims(): for ddr in dataDim.dataDimRefs: xdr = ddr.expDimRef ll = ddrLists.get(xdr) if ll is None: # something did not match - do nothing break else: ll.append(ddr) nFound += 1 else: if nFound == nTotal: # we are OK. Do next spectrum continue # something did not match - do nothing break else: # all spectra matched. Now reset O1 references to match reference if refSpec is spectra[0]: startAt = 1 else: startAt = 0 for refDdr in refDdrs: dataDim = refDdr.dataDim centrePoint = dataDim.numPointsOrig / 2 - dataDim.pointOffset + 1 refValue = refDdr.pointToValue(centrePoint) xdr = refDdr.expDimRef for ddr in ddrLists[xdr][startAt:]: dataDim = ddr.dataDim centrePoint = dataDim.numPointsOrig / 2 - dataDim.pointOffset + 1 ddr.refPoint = centrePoint ddr.refValue = refValue # set refExperiment if there is only one possibility experiments = [] ignoreSet = set() showPopup = False for spectrum in spectra: experiment = spectrum.experiment if experiment not in ignoreSet: ignoreSet.add(experiment) if not experiment.refExperiment: experiments.append(spectrum.experiment) if noVerify: resetCategory = False if not hasattr(experiment, 'category'): if (hasattr(experiment, 'pulProgName') and hasattr(experiment, 'pulProgType')): # this is first time we get here, and we have external name and source # use external source to set fullType experiment.category = 'use external' resetCategory = True refExperiments = getRefExperiments(experiment) if resetCategory and not refExperiments: # no refExperiments match external source. # unset 'use external' category del experiment.category if len(refExperiments) == 1: # only one possibility, just set it setRefExperiment(experiment, refExperiments[0]) # wb104: 20 Oct 2014: do not popup Experiment types dialog if noVerify #else: # showPopup = True # Pop up refExperiment verification if experiments and (showPopup or not noVerify): self.parent.initRefExperiments(experiments) # set up internal Analysis data for spectrum in spectra: self.parent.finishInitSpectrum(spectrum) print 'finished opening spectrum', spectrum.experiment.name, spectrum.name def chooseFiles(self, forceUpdate=False, *file): directory = self.fileSelect.getDirectory() fileNames = self.fileSelect.fileList.currentObjects fullFileNames1 = [uniIo.joinPath(directory, x) for x in fileNames] fullFileNames2 = [x.fileName for x in self.scrolledMatrix.objectList] fullFileNames2 = [uniIo.joinPath(directory, x) for x in fullFileNames2] if fullFileNames1 == fullFileNames2 and not forceUpdate: return objectList = [] textMatrix = [] format = self.formatPulldown.getText() shiftListName = self.getShiftLists()[0][0] windowOpt = WINDOW_OPTS[1] oneUp = os.path.dirname if format == 'Bruker': if self.sharedExpSelect.getSelected(): nameTemplate = 'Bruker_%d' next = self.getNextExpNum(nfiles=len(fileNames), nameTemplate=nameTemplate) exptName = nameTemplate % (next) for i, fileName in enumerate(fileNames): fullFileName = fullFileNames1[i] specName = os.path.basename( oneUp(oneUp(oneUp(fullFileName)))) datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) else: for i, fileName in enumerate(fileNames): fullFileName = fullFileNames1[i] try: # below should not fail ss1 = oneUp(fullFileName) specName = os.path.basename(ss1) ss2 = os.path.basename(oneUp(oneUp(ss1))) exptName = 'Bruker_' + ss2 except: # just put in something ss = os.path.basename(fullFileName) exptName = 'Bruker_' + ss specName = ss datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) else: next = self.getNextExpNum(nfiles=len(fileNames)) if self.sharedExpSelect.getSelected(): exptName = 'Expt_%d' % (next) for i, fileName in enumerate(fileNames): specName = re.sub('\.\w+$', '', fileName) datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) else: for i, fileName in enumerate(fileNames): exptName = 'Expt_%d' % (next + i) specName = re.sub('\.\w+$', '', fileName) datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) if len(fileNames) > 1: self.openButton.config(text='Open Spectra') else: self.openButton.config(text='Open Spectrum') self.scrolledMatrix.update(objectList=objectList, textMatrix=textMatrix) def getNextExpNum(self, nfiles=0, nameTemplate='Expt_%d'): """ get suitable free integer to use for exp names """ next = 1 if self.project: nmrProject = self.nmrProject ii = len(nmrProject.experiments) # find first exp number that is not taken # NBNB TBD could consider expname = specname, specname = proc dir next = ii + 1 if nfiles: while ii < next + nfiles: ii += 1 if nmrProject.findFirstExperiment(name=nameTemplate % ii): next = ii + 1 # return next def getDetails(self, fullfile): details = '' if os.path.isfile(fullfile): format = self.formatPulldown.getText() detailsDir = os.path.dirname(fullfile) detailsFile = uniIo.joinPath(detailsDir, details_file_dict[format]) if os.path.exists(detailsFile): fp = open(detailsFile) details = fp.read().strip().replace('\n', ' ').replace('\r', ' ') fp.close() return (details, ) def update(self): objectList = self.scrolledMatrix.objectList textMatrix = [(obj.exptName, obj.specName, obj.fileName, obj.window, obj.shiftListName) for obj in objectList] self.scrolledMatrix.update(objectList=objectList, textMatrix=textMatrix) def selectCell(self, obj, row, col): self.currentObject = obj if self.project: self.experiment = self.nmrProject.findFirstExperiment( name=obj.exptName) else: self.experiment = None def getWindow(self, obj): if obj: self.windowPulldown.set(obj.window) def setWindow(self, opt): if isinstance(opt, RowObject): self.currentObject.window = opt.window else: self.currentObject.window = opt self.update() def setShiftList(self, obj=None): if self.project: project = self.project shiftList = self.shiftListPulldown.getObject() if shiftList is None: shiftList = newShiftList(project, unit='ppm') if self.experiment and shiftList and ( shiftList is not self.experiment.shiftList): setExperimentShiftList(self.experiment, shiftList) self.currentObject.shiftListName = shiftList.name self.update() def getShiftList(self, object): names, shiftLists = self.getShiftLists() if names: self.shiftListPulldown.setup(names, shiftLists, 0) if self.experiment and self.experiment.shiftList: name = self.experiment.shiftList.name else: name = object.shiftListName if name is not None: self.shiftListPulldown.set(name) def getShiftLists(self): if self.project: names = [] objects = getShiftLists(self.nmrProject) for shiftList in objects: if not shiftList.name: shiftList.name = 'ShiftList %d' % shiftList.serial names.append(shiftList.name) objects.append(None) names.append('<New>') else: objects = [ None, ] names = [ 'ShiftList 1', ] return names, objects def chooseFormat(self, format): if format in ('Bruker', 'Varian'): self.gridDetails(True) else: self.gridDetails(False) file_types = [] file_type = file_type_dict.get(format) if (file_type): file_types.extend([file_type]) file_types.append(FileType('All', ['*'])) file_types.append(self.fileSelect.manualFilter) self.fileSelect.setFileTypes(file_types) def getSpectrum(self, obj): if obj: self.spectrumEntry.set(obj.specName) def setSpectrum(self, *event): if self.currentObject: text = self.spectrumEntry.get() if text and text != ' ': for data in self.scrolledMatrix.objectList: if data is self.currentObject: continue if (data.specName == text) and (data.exptName == self.currentObject.exptName): showWarning( 'Repeated name', 'Spectrum name (%s) already in use for experiment (%s)' % (data.specName, data.exptName), parent=self) return elif (self.experiment) and ( self.experiment.findFirstDataSource(name=text)): showWarning( 'Repeated name', 'Spectrum name (%s) already in use for experiment (%s)' % (data.specName, data.exptName), parent=self) return self.currentObject.specName = text self.update() def getExperiment(self, obj): if obj: self.experimentEntry.set(obj.exptName) def setExperiment(self, *event): if self.currentObject: text = self.experimentEntry.get() if text and text != ' ': if self.sharedExpSelect.getSelected(): # share one experiment for all rows for oo in self.scrolledMatrix.objectList: oo.exptName = text else: #separate experiments self.currentObject.exptName = text if self.project: self.experiment = self.nmrProject.findFirstExperiment( name=text) self.update() def updateShiftLists(self): if self.project: name = self.expt_entry.get() e = self.nmrProject.findFirstExperiment(name=name) else: e = None names, objects = self.getShiftLists() if e and e.shiftList: index = objects.index(e.shiftList) else: index = 0 self.shiftListPulldown.setup(names, objects, index) def openSpectrum(self, exptName, specName, file, windowOpt=WINDOW_OPTS[2], shiftListName='<New>', extraData=None): # input checks if not file: showError('No file', 'Need to enter file', parent=self) return None if not exptName: showError('Experiment', 'Need to enter experiment name', parent=self) return None if not specName: showError('Spectrum', 'Need to enter spectrum name', parent=self) return None # get or set up project project = self.project if not project: self.project = project = defaultProject() self.parent.initProject(project) self.nmrProject = self.parent.nmrProject self.analysisProject = self.parent.analysisProject #Default ShiftList with name 'ShiftList 1' created # set up shift list if shiftListName == '<New>': shiftList = None else: shiftList = self.nmrProject.findFirstMeasurementList( className='ShiftList', name=shiftListName) # read params format = self.formatPulldown.getText() clazz = params_class_dict[format] try: params = clazz(file, extraData=extraData) except Implementation.ApiError, e: showError('Reading params file', 'Fatal error: ' + e.error_msg, parent=self) return None dim = params.pseudoDataDim() if dim is not None: if format == 'NMRPipe': popup = NmrPipePseudoPopup(self, params, dim, file) popup.destroy() elif format == 'Bruker': popup = BrukerPseudoPopup(self, params, dim) popup.destroy() # get or set up experiment experiment = self.nmrProject.findFirstExperiment(name=exptName) if experiment: expIsNew = False if experiment.findFirstDataSource(name=specName): showError('Duplicate name', 'Duplicate spectrum name "%s" in experiment %s' % (specName, experiment.name), parent=self) return None elif (experiment.dataSources and experiment not in self.okExpSet): if showOkCancel('Multiple Spectra Warning', 'Really put multiple ' 'spectra into existing experiment %s?' % experiment.name, parent=self): self.okExpSet.add(experiment) else: return else: expIsNew = True try: # Will also work for shiftList == None experiment = Nmr.Experiment(self.nmrProject, name=exptName, numDim=params.ndim, shiftList=shiftList) except Implementation.ApiError, experiment: showError('Experiment', experiment.error_msg, parent=self) return None
def setDataDimRefs(dsFreq): # TODO: base this on temperature!?! protonFreqRefPpm = 4.773 otherFreqRefPpm = {'13C': 100.0, '15N': 120.0} # Arbitrary! # Get the default center proton frequency protonFreq = 0 otherFreqs = {} protonFreqRefZero = 0.0 for dataDim in dsFreq.dataDims: expDim = dataDim.expDim for expDimRef in expDim.expDimRefs: isotope = expDimRef.isotopeCodes[0] # Set default for first proton dim found # TODO: Should really have a popup here! if isotope == '1H': protonFreq = expDimRef.sf break elif isotope not in otherFreqs.keys(): otherFreqs[isotope] = expDimRef.sf if protonFreq != 0: break if protonFreq == 0: if otherFreqs: isotopeCodes = otherFreqs.keys() isotopeCodes.sort() for isotopeCode in isotopeCodes: if otherFreqRefPpm.has_key(isotopeCode) and INDIRECT_FREQ_CONV.has_key(isotopeCode): print " No 1H expDimRef found, trying to use %s dimension for referencing." % isotopeCode heteroFreqRefPpm = otherFreqRefPpm[isotopeCode] heteroFreq = otherFreqs[isotopeCode] heteroFreqRefZero = heteroFreq / (heteroFreqRefPpm / 1000000 + 1) protonFreqRefZero = heteroFreqRefZero * 100.0 / INDIRECT_FREQ_CONV[isotopeCode] break if not protonFreqRefZero: print " No relevant expDimRef info found for referencing - aborting." return else: protonFreqRefZero = protonFreq / (protonFreqRefPpm / 1000000 + 1) for dataDim in dsFreq.dataDims: expDim = dataDim.expDim for expDimRef in expDim.expDimRefs: isotope = expDimRef.isotopeCodes[0] freqRefZero = protonFreqRefZero * INDIRECT_FREQ_CONV[isotope] / 100 freqRefPpm = ((expDimRef.sf - freqRefZero) / freqRefZero) * 1000000 freqRefPoint = returnInt(dataDim.numPoints / 2) + 1 dataDimRef = Nmr.DataDimRef(dataDim,refPoint = freqRefPoint, refValue = freqRefPpm, expDimRef = expDimRef)
def cleanDihedralConstraintList(self, constraintList): print "Cleaning dihedral constraint list '%s'." % constraintList.name constraints = [] constraintRefs = [] # # Set up some reference data and do initial checking # for constraint in constraintList.constraints: constraints.append([]) constraintRefs.append(constraint) constraintDeleted = 0 resonances = list(constraint.resonances) # # Check if restraint between covalently linked atoms. Assuming resonances # to be listed in order. # TODO: need handling of phi, psi, ... before can do this based on reference data # can't find covalent connections in current case!! # """ for i in range(0,3): resonance = resonances[i] if resonance.resonanceSet and len(resonance.resonanceSet.atomSets) == 1: nextResonance = resonances[i+1] if nextResonance.resonanceSet and len(nextResonance.resonanceSet.atomSets) == 1: nmrAtoms = resonance.resonanceSet.atomSets[0].atoms nextNmrAtoms = nextResonance.resonanceSet.atomSets[0].atoms if len(nmrAtoms) == 1 and len(nextNmrAtoms) == 1: nextChemAtom = nextNmrAtoms[0].chemAtom chemAtom = nmrAtoms[0].chemAtom atomsConnected = 0 for chemBond in chemAtom.chemBonds: if getOtherAtom(chemAtom,chemBond) == nextChemAtom: atomsConnected = 1 if not atomsConnected: print " Deleting dihedral constraint %s (no connection via atoms %s,%s)" % (constraint.serial,chemAtom.name,nextChemAtom.name) constraint.delete() constraintDeleted = 1 """ # # TODO: sorting all resonances now... not entirely correct but should be OK # if not constraintDeleted: resonances.sort() constraints[-1].append(resonances) # # Now check whether there are any duplicates # i = 0 while (i < len(constraints)): refConstraint = constraints[i] j = i + 1 while (j < len(constraints)): if refConstraint == constraints[j]: items = "" for (res1, res2, res3, res4) in constraints[j]: items += "[%s,%s,%s,%s]," % (res1.name, res2.name, res3.name, res4.name) items = items[:-1] print " Constraint %s (items %s) is duplicate of %s - deleted" % ( constraintRefs[j].serial, items, constraintRefs[i].serial) # # Check if items are the same (ONLY checking upper, lower!), copy over if not # for otherDihedralItem in constraintRefs[j].items: itemExists = 0 for dihedralItem in constraintRefs[i].items: if otherDihedralItem.upperLimit == dihedralItem.upperLimit and otherDihedralItem.lowerLimit == dihedralItem.lowerLimit: itemExists = 1 break if not itemExists: keywds = {} for attribute in [ 'upperLimit', 'targetValue', 'lowerLimit', 'error' ]: keywds[attribute] = getattr( otherDihedralItem, attribute) Nmr.DihedralConstraintItem(constraintRefs[i], **keywds) print " Copied item with values %s" % ( str(keywds)) # # Delete after copying information # constraintRefs[j].delete() constraints.pop(j) constraintRefs.pop(j) else: j += 1 i += 1
def createExperiment(proj,expName,refExperiment,unit = 'ppm'): # # Create a default experiment based on a reference experiment... # numDim = len(refExperiment.refExpDims) isReversed = refExperiment.isReversed # Make sure experiment name fits Ccp Line expName = returnMemopsLine(expName) # Create the experiment exp = Nmr.Experiment(proj.currentNmrProject,name = expName, numDim = numDim, refExperiment = refExperiment) # Tag to mark that this is a 'default' experiment exp.addApplicationData(Implementation.AppDataBoolean(application = ccpNmr_kw, keyword = 'default', value = True)) # # Set expDimRefs # atomSiteList = [] acquisitionDim = None for i in range(0,numDim): expDim = exp.findFirstExpDim(dim = i + 1) refExpDim = refExperiment.findFirstRefExpDim(dim = i + 1) expDim.refExpDim = refExpDim atomSiteList.append([]) for refExpDimRef in refExpDim.refExpDimRefs: # TODO Here catch things like ConstantTimePeriod for editing!!!!?!?! expMeasurement = refExpDimRef.expMeasurement isotopeCodes = [] for atomSite in expMeasurement.atomSites: isotopeCodes.append(atomSite.isotopeCode) if isotopeCodes.count('1H'): sf = baseFrequency = 500.0 elif isotopeCodes.count('13C'): sf = baseFrequency = 125.7367377 elif isotopeCodes.count('15N'): sf = baseFrequency = 50.670637 else: sf = baseFrequency = 1.0 # # Setting paramaters correctly goes via GUI interface... these are 'defaults' # expDimRef = Nmr.ExpDimRef(expDim, unit = unit, sf = sf, baseFrequency = baseFrequency, measurementType = expMeasurement.measurementType, isotopeCodes = isotopeCodes, refExpDimRef = refExpDimRef) # Tag to mark that this is a 'default' expDimRef expDimRef.addApplicationData(Implementation.AppDataBoolean(application = ccpNmr_kw, keyword = 'default', value = True)) # # Keep track for expTransfer settings... # atomSiteList[-1].append((expDimRef,expMeasurement.atomSites)) # # Set acquisition dimension. Is the expMeasurement connected to the last # expStep (or first expStep in case isReversed). # expDim = None for expGraph in refExperiment.nmrExpPrototype.expGraphs: expStepNums = [] for expStep in expGraph.expSteps: expStepNums.append(expStep.stepNumber) expStepNums.sort() if not isReversed: expStepNums.reverse() for expStepNum in expStepNums: expStep = expGraph.findFirstExpStep(stepNumber = expStepNum) for refExpDimRef in expStep.expMeasurement.refExpDimRefs: refExpDim = refExpDimRef.refExpDim expDim = exp.findFirstExpDim(refExpDim = refExpDim) if expDim: break if expDim: break if expDim: break if expDim: expDim.isAcquisition = True # # Set expTransfers... # for i in range(0,len(atomSiteList)-1): for (expDimRef,atomSites) in atomSiteList[i]: for otherAtomSiteList in atomSiteList[i+1:]: for (otherExpDimRef,otherAtomSites) in otherAtomSiteList: for expGraph in atomSites[0].nmrExpPrototype.expGraphs: # # Transfers are only set in simple case: direct transfer between single atomSites corresponding to expDimRefs # expDimRefSet = set([expDimRef,otherExpDimRef]) sites = frozenset([atomSites[0],otherAtomSites[0]]) refExpTransfer = expGraph.findFirstExpTransfer(atomSites = sites) if refExpTransfer: expTransfer = exp.findFirstExpTransfer(transferType = refExpTransfer.transferType, expDimRefs = expDimRefSet) if not expTransfer: expTransfer = exp.newExpTransfer(transferType = refExpTransfer.transferType, expDimRefs = expDimRefSet) return exp
# if not os.path.exists(projectDir): os.mkdir(projectDir) projectPath = os.path.join(projectDir, projectName) if os.path.exists(projectPath): shutil.rmtree(projectPath) # # Create a CCPN Data Model Project (this is the root object within the # Data Model) # ccpnProject = Implementation.MemopsRoot(name=projectName) nmrProject = Nmr.NmrProject(ccpnProject, name=ccpnProject.name) # # Make sure it saves the information in the projectDir # To do this on the Python level you have to reset the path # for the urls that indicate the directory locations for the # saved files... # # Alternatively create the project in the right directory to # start with - see convertCns2Pdb # for repository in ccpnProject.repositories: if repository.name in ('userData', 'backup'):
def createChemShifts(peakLists, guiParent=None, messageReporter=None, multiDialog=None, shiftList=None, useAllContribs=1, defaultShiftError=None, shiftListName='None'): # # Set up GUI or non-GuI user interaction if not passed in # if not messageReporter: messageReporter = setupMessageReporter(guiParent) if not multiDialog: multiDialog = setupMultiDialog(guiParent) # # Initial checks # if not peakLists: messageReporter.showError("Error", "No peaklist(s) provided.") return False for peakList in peakLists: if not isinstance(peakList, Nmr.PeakList): messageReporter.showError("Error", "Invalid peaklist provided.") return False # # Initialize some stuff # if not defaultShiftError: defaultShiftError = {} for (nucl, defValue) in (('1H', 0.002), ('13C', 0.1), ('15N', 0.1)): if not defaultShiftError.has_key(nucl): defaultShiftError[nucl] = defValue proj = peakLists[0].root resonanceChemShifts = {} experiments = [] # # Read the chemshifts and link them to the relevant resonances # for peakList in peakLists: if peakList.dataSource.experiment not in experiments: experiments.append(peakList.dataSource.experiment) for peak in peakList.sortedPeaks(): for peakDim in peak.sortedPeakDims(): chemShift = peakDim.value peakDimContribs = tuple(peakDim.peakDimContribs) if useAllContribs == 0 and len(peakDimContribs) > 1: # # First check if resonances for contribs connected through one resonanceSet # If so, then set values anyway. # resonanceSet = peakDimContribs[0].resonance.resonanceSet sameShiftGroup = 1 if resonanceSet: resonanceSetResonances = resonanceSet.sortedResonances( ) for peakDimContrib in peakDimContribs[1:]: if peakDimContrib.resonance not in resonanceSetResonances: sameShiftGroup = 0 break if not sameShiftGroup: # # Ignore this peakDim - many contribs possible and not wanted (?) # print " Warning: ignoring peakDim %d for peak %d... more than one resonance contribution" % ( peakDim.dim, peak.serial) continue for peakDimContrib in peakDimContribs: resonance = peakDimContrib.resonance if not resonanceChemShifts.has_key(resonance): resonanceChemShifts[resonance] = [] resonanceChemShifts[resonance].append(chemShift) # # Make a new chemical shift list if none passed in # if shiftList == None: shiftList = Nmr.ShiftList(proj.currentNmrProject, name=shiftListName, unit='ppm', experiments=experiments) # # Get the average (and standard dev) of the chemical shift for each resonance # Create the chemical shifts # for resonance in resonanceChemShifts.keys(): chemShiftTotal = 0.0 for chemShift in resonanceChemShifts[resonance]: chemShiftTotal += chemShift chemShiftAverage = chemShiftTotal / len(resonanceChemShifts[resonance]) standardDev = getStandardDev(resonanceChemShifts[resonance], chemShiftTotal) if standardDev == None: if defaultShiftError.has_key(resonance.isotopeCode): shiftError = defaultShiftError[resonance.isotopeCode] else: shiftError = 0.1 else: shiftError = standardDev * 2 # # Set the chemical shift value # shift = shiftList.findFirstMeasurement(resonance=resonance) if not shift: Nmr.Shift(shiftList, value=chemShiftAverage, error=shiftError, resonance=resonance) else: allowedErrorMargin = shiftError / 10 if shift.value + allowedErrorMargin < chemShiftAverage or chemShiftAverage < shift.value - allowedErrorMargin: resName = getResNameText(resonance) multiDialog.MeasurementSelect(guiParent, resName, resonance, chemShiftAverage, shiftList, 'Shift') return shiftList
while 1: self.name = askString('Give the name of the experiment', 'Experiment name:', initial_value=self.expName, parent=self.parent) if (self.name in names): showError('Repeated experiment name', 'Name already used - choose another.') else: break self.skip = 0 return True if __name__ == '__main__': root = Tkinter.Tk() from memops.api import Implementation from ccp.api.nmr import Nmr project = Implementation.MemopsRoot(name='test') nmrProject = Nmr.NmrProject(project, name=project.name) popup = ExperimentCreatePopup(root, project, numDim=2) root.mainloop()