Source code for ccpn.AnalysisScreen.pipes.AnnotatePeaks

#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2021"
__credits__ = ("Ed Brooksbank, Joanna Fox, Victoria A Higman, Luca Mureddu, Eliza Płoskoń",
               "Timothy J Ragan, Brian O Smith, Gary S Thompson & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
                 "CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
                 "J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Ed Brooksbank $"
__dateModified__ = "$dateModified: 2021-06-04 19:38:32 +0100 (Fri, June 04, 2021) $"
__version__ = "$Revision: 3.0.4 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: Luca Mureddu $"
__date__ = "$Date: 2017-05-28 10:28:42 +0000 (Sun, May 28, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================


#### GUI IMPORTS
from ccpn.AnalysisScreen.gui.widgets import HitFinderWidgets as hw
from ccpn.ui.gui.widgets.PipelineWidgets import GuiPipe
from ccpn.ui.gui.widgets.Label import Label
from ccpn.ui.gui.widgets.RadioButtons import RadioButtons

#### NON GUI IMPORTS
from ccpn.framework.lib.pipeline.PipeBase import SpectraPipe, PIPE_SCREEN
from ccpn.core.lib.peakUtils import snap1DPeaksToExtrema
from ccpn.AnalysisScreen.lib.experimentAnalysis.Common import _getReferencesFromSample
from ccpn.util.Logging import getLogger
from ccpn.core.lib.ContextManagers import undoBlock, notificationEchoBlocking, \
    undoBlockWithoutSideBar, undoStackBlocking

########################################################################################################################
###   Attributes:
###   Used in setting the dictionary keys on _kwargs either in GuiPipe and Pipe
########################################################################################################################

PipeName = 'Annotate Peaks'
AnnotationLabel = 'Annotation'
FromSubstanceName = 'Use_linked_Substance_Name'
FromSpectrumName = 'Use_Spectrum_Name'

AnnotationOptions = [FromSubstanceName, FromSpectrumName]
DefaultPeakListIndice = -1


########################################################################################################################
##########################################      ALGORITHM       ########################################################
########################################################################################################################
def _getAnnotation(spectrum, _kwargs):

    useSpectrumName = _kwargs.get(AnnotationLabel) == FromSpectrumName
    useSubstanceName = _kwargs.get(AnnotationLabel) == FromSubstanceName
    annotation = ''
    if useSpectrumName:
        annotation = spectrum.name
    if useSubstanceName:
        substances = spectrum.referenceSubstances
        if len(substances) > 1:
            getLogger().warning('Multiple substances for Spectrum: %s. Skipped.' % spectrum.name)
            annotation = spectrum.name
        if len(substances) == 1:
            annotation = substances[0].name
    return annotation

########################################################################################################################
##########################################     GUI PIPE    #############################################################
########################################################################################################################


[docs]class SetPeakAnnotationsFromSpectrumGuiPipe(GuiPipe): pipeName = PipeName def __init__(self, name=pipeName, parent=None, project=None, **kw): super(SetPeakAnnotationsFromSpectrumGuiPipe, self) GuiPipe.__init__(self, parent=parent, name=name, project=project, **kw) self.parent = parent row = 0 self.snapPeaksLabel = Label(self.pipeFrame, AnnotationLabel, grid=(row, 0)) setattr(self, AnnotationLabel, RadioButtons(self.pipeFrame, texts=AnnotationOptions, selectedInd=0, direction='v', callback=None, grid=(row, 1)))
######################################################################################################################## ########################################## PIPE ############################################################# ########################################################################################################################
[docs]class SetPeakAnnotationsFromSpectrumPipe(SpectraPipe): """ Annotate peaks in the input spectra using available options: - parent spectrum Name - reference Substance name linked to the parent spectrum - others to be implemented ( such as a free entry Regex text ) """ guiPipe = SetPeakAnnotationsFromSpectrumGuiPipe pipeName = PipeName pipeCategory = PIPE_SCREEN _kwargs = { AnnotationLabel: FromSubstanceName, }
[docs] def runPipe(self, spectra): ''' :param spectra: inputData :return: spectra ''' if not spectra: getLogger().warning('Spectra not present. Add spectra first') return spectra with undoBlockWithoutSideBar(): if self.project is not None: # pks = [pk for sp in spectra for pk in sp.peakLists[DefaultPeakListIndice].peaks] # list(map(lambda pk: setattr(pk, 'annotation', _getAnnotation(pk.spectrum, self._kwargs)), pks)) for sp in spectra: for pk in sp.peakLists[DefaultPeakListIndice].peaks: pk.annotation = _getAnnotation(sp, self._kwargs) return spectra
SetPeakAnnotationsFromSpectrumPipe.register() # Registers the pipe in the pipeline