Example #1
0
    def commit(self):

        posDisp = self.numDims * [0]

        for i in range(self.numDims):
            posDisp[i] = float(self.dimensionEntries[i].get())
            if self.unit != 'point':
                dataDim = self.dataDims[i]
                if dataDim.className == 'FreqDataDim':
                    self.posn[i] = unit_converter[(self.unit, 'point')](
                        posDisp[i], getPrimaryDataDimRef(dataDim))

            else:
                self.posn[i] = posDisp[i]

        if self.peak:
            movePeak(self.peak, self.posn)
        else:
            self.peak = pickPeak(self.peakList, self.posn)

        height = self.heightEntry.get()
        volume = self.volumeEntry.get()
        setManualPeakIntensity(self.peak, height, intensityType='height')
        setManualPeakIntensity(self.peak, volume, intensityType='volume')

        details = self.detailEntry.get() or None

        self.peak.setDetails(details)

        self.close()
Example #2
0
def pickPeaksFromCing(cingPeakList, peakList, resonanceDict):


  for cingPeak in cingPeakList:
    peakList.root.override = True
    peak = pickPeak(peakList, cingPeak.positions, unit='ppm', doFit=False,
                    serial=cingPeak.xeasyIndex)
    peakList.root.override = False
    setManualPeakIntensity(peak, value=cingPeak.height.value, intensityType="height")
    if len(peak.peakDims) == len(cingPeak.resonances):
      for j, peakDim in enumerate(peak.sortedPeakDims()):
        cingResonance = cingPeak.resonances[j]

        resonance = resonanceDict.get(cingResonance)
        if resonance is not None:
          assignResToDim(peakDim, resonance)

    else:
      # original, fixed
      for j, peakDim in enumerate(peak.sortedPeakDims()):
        numAssignments = len(cingPeak.resonances) / len(peak.peakDims)
        ccpnResonances = []
        indx = j
        for k in range(numAssignments):
          cingResonance = cingPeak.resonances[indx]
          resonance = resonanceDict.get(cingResonance)
          ccpnResonances.append(resonance)
          indx += len(peak.peakDims)
        for res in ccpnResonances:
          if res is not None:
            assignResToDim(peakDim, res)

  print "Peaks Picked"
Example #3
0
def setupPeaks(peakList, data):

    # below assumes that all positions found are in ppm and in fundamental region
    spectrum = peakList.parent
    numDim = spectrum.numDim
    dataDims = spectrum.sortedDataDims()
    dataDimRefs = [getPrimaryDataDimRef(dataDim) for dataDim in dataDims]
    for (position, height, volume, quality) in data:
        peak = peakList.newPeak()
        peakDims = peak.sortedPeakDims()
        for i in range(numDim):
            peakDim = peakDims[i]
            dataDimRef = dataDimRefs[i]
            peakDim.dataDimRef = dataDimRef
            #peakDim.position = dataDimRef.valueToPoint(position[i])
            peakDim.value = position[i]
        setManualPeakIntensity(peak, height, 'height')
        setManualPeakIntensity(peak, volume, 'volume')
        if quality >= 0:  # = -1 if not calculated
            peak.figOfMerit = quality  # assumes that quality is between and 0 and 1
def makeDecoupledPeakList(argServer):

    peakList = argServer.getPeakList()
    spectrum = peakList.dataSource

    peakAssignDict = {}

    for peak in peakList.peaks:

        assignKey = []
        for peakDim in peak.sortedPeakDims():
            resonances = [c.resonance for c in peakDim.peakDimContribs]
            resonances = frozenset(resonances)
            assignKey.append(resonances)

        assignKey = tuple(assignKey)

        if assignKey not in peakAssignDict:
            peakAssignDict[assignKey] = []

        peakAssignDict[assignKey].append(peak)

    if not peakAssignDict:
        argServer.showWarning('No assignments detected')
        return

    newPeakList = spectrum.newPeakList()
    nDim = spectrum.numDim

    for assignKey in peakAssignDict:
        peaks = peakAssignDict[assignKey]
        position = [0.0] * nDim
        height = 0.0
        volume = 0.0

        for peak in peaks:
            peakDims = peak.sortedPeakDims()

            for i, peakDim in enumerate(peakDims):
                position[i] += peakDim.position

            heightIntensity = peak.findFirstPeakIntensity(
                intensityType='height')
            volumeIntensity = peak.findFirstPeakIntensity(
                intensityType='volume')

            if heightIntensity:
                height += heightIntensity.value

            if volumeIntensity:
                volume += volumeIntensity.value

        n = float(len(peaks))
        for i in range(nDim):
            position[i] /= n

        newPeak = pickPeak(newPeakList, position, doFit=False)
        newPeakDims = newPeak.sortedPeakDims()

        setManualPeakIntensity(newPeak, height, 'height')
        setManualPeakIntensity(newPeak, volume, 'volume')

        for i, peakDim in enumerate(newPeakDims):
            resonances = assignKey[i]

            for resonance in resonances:
                assignResToDim(peakDim,
                               resonance,
                               tolerance=1.0,
                               doWarning=False)

    argServer.showWarning('Made new peak list with %d peaks' %
                          len(newPeakList.peaks))
Example #5
0
def SeparatePeakRoutine(params, peakList, routine='bayesys'):
    """ Run Peak Separate Code and then add resultant peaks to CCPN PeakList (peakList) """

    # check that all C library parameters have been set up before proceeding
    fail = False
    for key in params.ClibKeys:
        if params.__dict__[key] == None:
            print '&&& C Library parameter %s not set, aborting.' % (key)
            fail = True

    if fail: return
    del (fail)

    # As the Lorentz/Gaussian model not in use
    (params.maxQ, params.minQ) = (1., 1.)

    # params.ClibKeys.sort()
    # for key in params.ClibKeys:
    #   print key, params.__dict__[key]

    if params.maxHeight <= params.minHeight:
        print '&&& Peak Separator height mismatch - please report to CCPN'
        return

    if routine == 'bayesys':

        # Maybe a quick parameter check so that no one is doing anything silly ?
        # Run the BayesPeakSeparator function in C
        results = BayesPeakSeparator.run_bayes(   \
                                        params.dataFile, params.Ndim, params.isBigEndian, params.nPoints,           #  4
                                        params.blockSize,                                                           #  1
                                        params.sampleStart, params.sampleEnd,                                       #  2
                                        params.maxHeight, params.minHeight,                                         #  2
                                        params.maxSigma, params.minSigma,                                           #  2
                                        params.maxQ, params.minQ,                                                   #  2
                                        params.dimWrapped,                                                          #  1
                                        params.peakShape, params.positivePeaks, params.minAtoms, params.maxAtoms,   #  2
                                        params.rate)                                                                #  1
        # 17

    elif routine == 'pymc':

        try:
            from PeakSeparatorPyMC import PeakSeparatorPyMC
        except ImportError:
            print 'Error, cannot import PeakSeparatorPyMC - PyMC peak separation will not work.'
            return

        if params.maxHeight <= params.minHeight:
            print '&&& Peak Separator height mismatch - please report to CCPN'
            return

        if params.Ndim != 2:
            print '&&& Peak Separator PyMC only in two dims currently'
            return

        results = PeakSeparatorPyMC(params)

    if (results == None) or (len(results) == 0):
        print '&&& SeparatePeakRoutine: failed.', results
        return 1

    shapeDict = {3: 'Gaussian', 4: 'Lorentzian', 5: 'Other'}

    peak_count = 0

    # tempNdim as nDim not correct for pseudo nD spectra
    sampledData = True if False in set([bool(t)
                                        for t in params.isFreqDim]) else False
    tempNdim = (params.Ndim - 1) if sampledData else params.Ndim

    # for each peak in the returned sample set
    for sample in getPeaksFromResults(results):

        height = float(sample[2])
        sigma = [float(sample[2 * i + 2 + 2]) for i in range(tempNdim)]
        position = [float(sample[2 * i + 1 + 2]) for i in range(tempNdim)]

        peak_count += 1

        cluster_peak_position_point = [0] * params.Ndim

        if params.peakShape == 3:  # Gaussian (nd)
            cluster_peak_volume = height
        if params.peakShape == 4:  # Lorentzian (nd)
            cluster_peak_volume = height
        else:  # Other (nd)
            cluster_peak_volume = height

        for i in range(tempNdim):

            # No longer a cluester peak! cluster_peak_position_point
            cluster_peak_position_point[i] = float(
                params.sampleStart[i]) + position[i] + 1.0

            # Gauss Volume
            if params.peakShape == 3:
                cluster_peak_volume *= sigma[i] * math.sqrt(2.0 * math.pi)

            # Lorentz Volume
            elif params.peakShape == 4:
                cluster_peak_volume *= sigma[i] * math.pi

            else:
                print '&&& Write code to integrate this shape: %d' % (
                    params.peakShape)

        # plane dim for pseudo 3d Analysis data (titrations / time lapse etc)
        if sampledData:
            for i in range(params.Ndim):
                if not params.isFreqDim[i]:
                    cluster_peak_position_point[i] = params.sampleEnd[i]

        # pickPeak from ccpnmr.analysis.core.PeakBasic
        peakListPeak = pickPeak(peakList,
                                cluster_peak_position_point,
                                unit='point',
                                doFit=False)

        for i, peakDim in enumerate(peakListPeak.sortedPeakDims()):

            if (sampledData) and (i == (params.Ndim - 1)): break

            cluster_peak_sigma = sigma[i]

            if peakDim.dataDimRef:
                a = pnt2hz(peakDim.position + cluster_peak_sigma,
                           peakDim.dataDimRef)
                b = pnt2hz(peakDim.position, peakDim.dataDimRef)

            peakDim.setLineWidth(2. * abs(a - b))

        # Add note to peak in CCPN peak list (so we know it's a BayeSys peak)
        peakListPeak.setDetails('PeakSeparator %s' %
                                shapeDict[params.peakShape])
        if sample[0] < 0.:
            merit = 0.
        else:
            merit = 1. / (1. + math.exp(-sample[0]))
        peakListPeak.setFigOfMerit(merit)

        # Add peak intensities (which have just been calculated)
        setManualPeakIntensity(peakListPeak, height, intensityType='height')
        setManualPeakIntensity(peakListPeak,
                               cluster_peak_volume,
                               intensityType='volume')