Subject: Re: "next peak" macro
From: nickfitzkee
Date: Jun 14, 2010

Previous: 743 Next: 745


Hello again,

Since it didnt appear that a good solution existed, heres the solution I came up with. It works great for me; hopefully if others are interested it will work for them, too. Ive provided the source code below as is, but I welcome pointers on how the extension could be better.

Regards,
Nick

#
# title: iterator tools for peak list navigation in sparky
# date: June 14, 2010
# author: Nick Fitzkee (nfitzkee at nih dot gov)
# summary: adds next peak and prior peak commands
#
# description:
# The code below, when placed in $SPARKY_HOME/Sparky/sparky_init.py,
# will enable two sparky accelerators, np and pp, which will
# deselect the current peak and focus the view on the next peak
# in the peak list of the current selected spectrum and
# vice-versa. If no peaks are present, nothing is changed.
# If no peaks are selected, the first peak is centered. If
# multiple peaks are selected, only the first selected
# peak is considered. Any selected peaks in other spectra are
# unselected.
#

def next(session):
active_spectrum = session.selected_spectrum()
all_peaks = active_spectrum.peak_list()
first = None

npk = len(all_peaks)
if npk == 0: return

for idx in xrange(npk):
if first != None:
all_peaks[idx].selected = 0
if all_peaks[idx].selected:
first = idx

session.unselect_all_ornaments()

if first == None:
all_peaks[0].selected = 1
elif first = npk-1:
all_peaks[0].selected = 1
else:
all_peaks[first+1].selected = 1

session.command_characters(vc)


def prev(session):
active_spectrum = session.selected_spectrum()
all_peaks = active_spectrum.peak_list()
first = None

npk = len(all_peaks)
if npk == 0: return

for idx in xrange(npk):
if first != None:
all_peaks[idx].selected = 0
if all_peaks[idx].selected:
first = idx

session.unselect_all_ornaments()

if first == None:
all_peaks[0].selected = 1
elif first == 0:
all_peaks[npk-1].selected = 1
else:
all_peaks[first-1].selected = 1

session.command_characters(vc)

def initialize_session(session):

def prev_cmd(s = session):
prev(s)
def next_cmd(s = session):
next(s)

session.add_command(np, , next_cmd)
session.add_command(pp, , prev_cmd)