- """
- Version 3.0.3
-
- Use this macro if you have assigned peaks for multiple spectra in a series and you want extract the peak heights.
-
- This macro will update first the peak height, then will group all in a Pandas dataFrame for easy exporting to file.
-
- The dataFrame has the following structure:
-
-
- | NR_ID | SP1 | SP2 | SP3
- H N | | | |
- -------------+-------- +-----------+-----------+---------
- 7.5 104.3 | A.1.ARG | 10 | 100 | 1000
-
- Index: multiIndex => axisCodes as levels;
- Columns => NR_ID: ID for the nmrResidue(s) assigned to the peak if available
- Spectrum series values sorted by ascending values, if series values are not set, then the
- spectrum name is used instead.
- Cell values: heights (for this macro)
-
- This dataframe is available from the SpectrumGroup class property `seriesPeakHeightForPosition`.
- N.B. this feature is still under development and can change or be deprecated in future releases.
-
- Change the user input as needed.
-
- """
-
-
- import pandas as pd
- from ccpn.core.lib.ContextManagers import undoBlock, undoBlockWithoutSideBar, notificationEchoBlocking
- from ccpn.core.lib.peakUtils import estimateVolumes, updateHeight
- from ccpn.ui.gui.widgets import MessageDialog
- from ccpn.ui.gui.popups.SpectrumGroupEditor import SpectrumGroupEditor
-
- ##################################### User input ####################################
-
- exportPath = `~/Desktop/`
- exportFileName = `myAnalysis.xlsx`
-
- recalculateHeight = True
- recalculateVolume = False
-
- spectrumGroupName = `MySpectrumGroup`
-
- sortByDataByNR_ID = False # Sort table by NmrResidue ID. See below for more sorting options. (see Pandas docs for advanced settings)
- export = True # If you want export to other formats, change the file name and export command below.
-
-
- #################################### Start of the code ####################################
-
- spectrumGroup = get(`SG:`+spectrumGroupName)
-
- def _queryForNewSpectrumGroup(spectrumGroup=None):
-
- msg = `This macro requires a SpectrumGroup containing spectra and defined series`
- if not spectrumGroup:
- needCreateNewSG = MessageDialog.showYesNo(`SpectrumGroup not found`,`%s. Create new?`% msg)
- if needCreateNewSG:
- sge = SpectrumGroupEditor(parent=mainWindow, mainWindow=mainWindow, editMode=False)
- sge.exec_()
- spectrumGroup = sge.obj
- if spectrumGroup.name != spectrumGroupName:
- warning(`The spectrumGroupName variable in this macro is different from the new spectrumGroup name.`
- `If you run this macro again it will prompt another Popup.`)
- else:
- raise RuntimeError(`SpectrumGroup not found. %s`% msg)
-
- return spectrumGroup
-
- while not spectrumGroup:
- spectrumGroup = _queryForNewSpectrumGroup(spectrumGroup)
-
-
- ######## Get the spectra from the spectrumGroup and their peaks from the last added peakList
- spectra = [sp for sp in spectrumGroup.spectra]
- peaks = [pk for sp in spectra for pk in sp.peakLists[-1].peaks]
- info(`Using peaks contained in the last peakList for spectra %s` % `, `.join(map(str, spectra)))
-
- ######## Recalculate peak properties
- info(`Recalculating peak properties...`)
- with undoBlockWithoutSideBar():
- with notificationEchoBlocking():
- if recalculateHeight:
- list(map(lambda x: updateHeight(x), peaks))
- if recalculateVolume:
- list(map(lambda x: estimateVolumes(x), peaks))
- info(`Recalculating peak properties completed.`)
-
- ######## Get dataframes for the spectrumGroup
- df = spectrumGroup.seriesPeakHeightForPosition
-
- def naturalSort(df, col):
- df[`_int`] = df[col].str.extract(`(\d+)`)
- _nonIntDF = df[df[`_int`].isnull()]
- df = df[~df[`_int`].isnull()]
- df[`_int`] = df[`_int`].astype(int)
- df = df.sort_values(by=[`_int`])
- df = pd.concat([df, _nonIntDF])
- df.drop([`_int`], axis=1, inplace = True)
- return df
-
- if sortByDataByNR_ID:
- df = naturalSort(df, `NR_ID`)
-
- ## to sort by axis: e.g. H. The table will be sorted ascending for H ppm position
- # df = df.sort_index(level=`H`)
-
- if export:
- df.to_excel(exportPath+exportFileName) # or use df.to_csv() for "file.csv" fileNames. etc
- info(`DataFrame exported in: %s` %exportPath+exportFileName)
-
- print(df)
Hi, can you help to to run this macro? I hace copy-pasted this in my user macros but when I try to run it it continues asking me where is the Spectral group
Hi,
yes you need to have a spectrumGroup, spectra and peaks.
- On sidebar: create a new spectrumGroup by double click on <New SpectrumGroup>,
- add a name
- select the spectra of interest and add to the group with drag&drop accross the boxes,
- on the series tab, add the units
- save changes
- Open the macro editor from the menus.
- open your macro/or copy paste the above code to a new empty macro,
- modify line 44 spectrumGroupName = `MySpectrumGroup` to your spectrumGroup name defined in the popup
- modify the other user input as needed : such as
exportPath = `~/Desktop/`
exportFileName = `myAnalysis.xlsx`
- press the play button to start.
I hope it helps
Hi Luca,
I am just getting to grips with the command line, and not really getting very far with turning my scripts into macros. This is very similar to something I need. I can’t see where this macro gets the spectrum group name from. Do you have to define it in the macro? Is it possible to write a macro that queries (with a pop-up?) for the spectrum group (drag and drop?) and then ask for an output filename? Otherwise I can’t see the benefit of having macros over textfiles that I can paste into the command line.
Thanks
Matt
Hi, if you copy and paste all the macro in the MacroEditor and run, it should raise a popup asking to create aspectrumGroup if is not there yet.
Alternatively, you can create first the spectrumGroup from sidebar, with the series values (last tab), give it a name, and copy it in line 44 on the macroEditor.
The settings are from line 38 to 47…
OK, thanks, that helps. Do you have to name your new spectrum group MySpectrumGroup?
No, you can call it whatever you like and rename it at any time. Simply double-click on it in the sidebar to rename in the pop-up.
Vicky