4

I'm using snakemake for some automation and had a question about determining which rule fails when 'onerror' is called.

I have tried cycling through the rules to determine which outputs don't exist, but the rules aren't ordered correctly.

Here's the code I'm using in onerror:

#find out which rule failed
failed_rule = None

for rulename in dir(rules):
    rule = getattr(rules,rulename)
    if hasattr(rule,'output'):
        output = getattr(rule,'output')

        print ('rule: ',rulename, output)

        #check if output file exists
        if output and (not os.path.exists(str(output))):
            failed_rule = rule
            print ('Failed rule is ', rulename)
            break

Thanks in advance

1 Answers1

0

I was able to solve my issue by searching for 'rule' in the returned log file in onerror. Through testing, I verified the last rule listed in the log file is the error'd log file. The following code works on this assumption.

#find failed rule
with Path(log).open() as f:
    rule_names = [line.strip() for line in f if line.startswith('rule')]

    if rule_names:
        failed_rule_line = rule_names[-1]
        failed_rulename = failed_rule_line.split()[1]
        failed_rulename = failed_rulename.replace(':','')

        #find failed rule
        failed_rule = None

        for rulename in dir(rules):
            if rulename == failed_rulename:
                failed_rule = getattr(rules,rulename)
                print ('Failed rule is ', rulename)
                break