I was wondering if someone knows what would happen in this case:
rule_a in my snakemake workflow has wildcards in its output ("output_{type}.txt")
rule_b in my snakemake workflow has an output with an explicit word instead of a wildcard, but the name structure is the same as rule_a's ("output_blue.txt")
Originally I only had rule_a, because it handled well all my types (e.g. purple, white, red, etc) but then I had to process "blue" (specifically) files in a significantly different way, so I created rule_b
The thing is, will snakemake know that when I request "output_blue.txt" it must use rule_b? or will it also try to use rule_a?
I just tested it and it should use rule_b for the specific file name with output_blue.txt and then use the other rule (rule_a) for all others that match output_{type}.txt.
Below is the code for the demo Snakefile where init_file_2.txt gets handled differently so that extra text beyond what the user types in as it runs gets added as content inside the result_2.txt file:
import os
import sys
# FILES THAT WILL BE GENERATED--------------------------------------------------
# initiating_files #initiating files to generate in order to to use to trigger
# main rule
# text_files_w_content # file corresponding to trigger file with content
# provided by user via prompt
##----------------HELPER FUNCTIONS----------------------------------------------
def write_string_to_file(s, fn):
'''
Takes a string, `s`, and a name for a file & writes the string to the file.
'''
with open(fn, 'w') as output_file:
output_file.write(s)
##-----------END OF HELPER FUNCTIONS--------------------------------------------
# PREPARATION-------------------------------------------------------------------
number_demo_files_to_make = 4
initiating_files = [f"init_file_{i}.txt" for i in range(
number_demo_files_to_make)]
text_files_w_content = [f"result_{i}.txt" for i in range(
number_demo_files_to_make)]
# MAKE THE initiating_files THAT WILL TRIGGER MAIN RULE FOR EACH FILE-----------
for indx,init_file in enumerate(initiating_files):
if not os.path.isfile(init_file):
write_string_to_file(f"MOCK_CONTENT_for_TRIGGER_FILE{indx}",init_file)
# END OF PREPARATION------------------------------------------------------------
# SNAKEMAKE RULES---------------------------------------------------------------
rule all:
input:
text_files_w_content
rule clean:
shell:
'''
touch init_file_18199xlkleFAKE.txt
touch result_18199xlkleFAKE.txt
rm init_file_*.txt
rm result_*.txt
'''
# Make the result file with the content the user types in when prompted
'''
Main default rule of this demonstration pipeline.
'''
user_input = input
rule make_result_file_with_user_provided_content:
input:
"init_file_{num}.txt"
output:
"result_{num}.txt"
run:
file_to_make = str(list({output})[0])
txt_content = user_input(f"Type a few letters or words to use as file content for {file_to_make} and then hit return: ")
write_string_to_file(txt_content,file_to_make)
# Try to do something different with a file with SPECIFIC name but where that
# name also matches above rule
'''
This is the rule that uses a specific example that overlaps the wildcard pattern.
'''
user_input = input
rule specific_one:
input:
"init_file_2.txt"
output:
"result_2.txt"
run:
file_to_make = str(list({output})[0])
txt_content = user_input(f"Type a few letters or words to use as file content for {file_to_make} and then hit return: ")
write_string_to_file(txt_content+" <===THIS WAS SPECIALLY HANDLED!!!",file_to_make)