Skip to content

Commit

Permalink
Better shimadzu detector selection
Browse files Browse the repository at this point in the history
  • Loading branch information
PlethoraChutney committed Mar 24, 2023
1 parent a20fcee commit b1cdb01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/appia/parsers/process_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ def main(args):
default = ['A', 'Trp', 'B', 'GFP'],
help = 'Channel mappings for old Shimadzu instruments. Default: A Trp B GFP'
)
process_args.add_argument(
'--prefer-detector',
help = 'New Shimadzu: if two detectors have the same channel, prefer the one with this name.'
)
process_args.add_argument(
'--agilent-channel-name',
help = 'Channel name for Agilent channels. This will override the channel specified by filenames if you have both.'
Expand Down
40 changes: 30 additions & 10 deletions src/appia/processors/hplc.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ def process_file(self) -> None:

class NewShimProcessor(HplcProcessor):
def __init__(self, filename, **kwargs):
if not hasattr(NewShimProcessor, 'prefer_detector'):
# this attribute is used to pick detectors for channels that have
# multiple detectors. We want the samples to be consistent across
# channels, and don't want to have to ask the user multiple times.
NewShimProcessor.prefer_detector = kwargs.get('prefer_detector')
super().__init__(
filename,
manufacturer = 'Shimadzu',
Expand Down Expand Up @@ -372,8 +377,6 @@ def prepare_sample(self) -> None:
# - and have the user resolve duplicates if they exist

if len(set(detector_channel_pairs.values())) != len(detector_channel_pairs.values()):
logging.warning('Duplicate channels detected: ')

counter = {}
for channel in detector_channel_pairs.values():
try:
Expand All @@ -390,14 +393,31 @@ def prepare_sample(self) -> None:
if channel == dup:
duped_detectors.append(detector)

print(f'Select a detector for {dup}. Any non-numeric choice will default to 0.')
for i in range(len(duped_detectors)):
print(f'{i}: {duped_detectors[i]}')

try:
selected_detector = duped_detectors[int(input())]
except (ValueError, IndexError):
selected_detector = duped_detectors[0]
# check if there is exactly one duplicated detector which matches the user's
# preferred detector. Otherwise, prompt them for input.
selected_detector = None
if NewShimProcessor.prefer_detector is not None:
# split on space to avoid "Detector" and add -Ch to avoid "C" matching supriously
is_preferred = [x for x in duped_detectors if NewShimProcessor.prefer_detector + '-Ch' in x.split(' ')[1]]
if len(is_preferred) == 1:
selected_detector = is_preferred[0]
elif len(is_preferred) > 1:
logging.warning(f'Preferred detector matched more than once: {", ".join(is_preferred)}')
else:
logging.info(f'Preferred detector {NewShimProcessor.prefer_detector} does not match any duplicated channel.')

if selected_detector is None:
logging.warning('Duplicate channels detected. You can use --prefer-detector to select one in the future.')
print(f'Select a detector for {dup}. Any non-numeric choice will default to 0.')
for i in range(len(duped_detectors)):
print(f'{i}: {re.sub("-Ch[0-9]*", "", duped_detectors[i])}')

try:
selected_detector = duped_detectors[int(input())]
# update the class preferred detector so that the user isn't prompted for each file.
NewShimProcessor.prefer_detector = re.search(r'Detector (.*?)-Ch[0-9]*', selected_detector).group(1)
except (ValueError, IndexError):
selected_detector = duped_detectors[0]

# drop other detectors for this channel from the paired dict
to_drop = []
Expand Down

0 comments on commit b1cdb01

Please sign in to comment.