Simple Python Scripts for CCPN
To load a macro go to M: Macro: Organise Macros and click [Add Macro] at the bottom right of the table. In the file browser popup that opens, navigate to the location of the Python file, e.g. "myMacro.py" and click on the name of this file in the top table. In the bottom table a list of all the Python functions in the file will be presented in the lower table. Click the name of the function you want to load and click [Load Macro]. When the file browser popup disappears the new macro will be at the bottom of the table, so scroll down until it is visible.
The macro can either be run from the table directly by selecting its row and clicking [Run]. A more convenient alternative is often to put the macro in the main Analysis menu. Do this my double-clicking in the |In main menu?| column of the table. If you close this popup window you will now see the new macro appear in the M:Macro section of Analysis. If you make a change to the Python code for a macro that has been entered into Analysis make sure you select M:Macro:Reload Menu Macros to ensure that the latest version of the code is being used.
Below are two examples of two simple python scripts which should be written into a text file and saved before loading into Analysis:
def addMarksToPeaks(argServer, peaks=None):
"""Descrn: Adds position line markers to the selected peaks.
Inputs: ArgumentServer, List of Nmr.Peaks
Output: None
"""
from ccpnmr.analysis.core.MarkBasic import createPeakMark
if not peaks:
peaks = argServer.getCurrentPeaks()
# no peaks - nothing happens
for peak in peaks:
createPeakMark(peak, remove=False)
def calcAveragePeakListIntensity(argServer, peakList=None, intensityType='height'):
"""Descrn: Find the average height of peaks in a peak list.
Inputs: ArgumentServer, Nmr.PeakList
Output: Float
"""
from ccpnmr.analysis.core.ConstraintBasic import getMeanPeakIntensity
if not peakList:
peakList = argServer.getPeakList()
if not peakList:
argServer.showWarning('No peak list selected')
return
answer = argServer.askYesNo('Use peak volumes? Height will be used otherwise.')
if answer: # is true
intensityType = 'volume'
spec = peakList.dataSource
expt = spec.experiment
intensity = getMeanPeakIntensity(peakList.peaks, intensityType=intensityType)
data = (intensityType,expt.name,spec.name,peakList.serial,intensity)
argServer.showInfo('Mean peak %s for %s %s peak list %d is %e' % data)
return intensity