import os
from os.path import join
import sys

sys.path.append(".")
from pkg.io import files_match_pattern

"""
This is a full pre-processing workflow for MSI data (provided as .imzML or .d).

input:
    data folder must contain the following directories:
    - "msi": directory with .imzML files or .d directories named group_sampleno.imzML/.d

output:
    sub-directories will be created for each pre-processing step containing the processed data
    along with quality control visualizations.
"""


configfile: 'data/config.yaml'


msi_pattern = r"^[a-zA-z0-9]+_[a-zA-z0-9]+\.(imzML|d)$" # imzML files must be named: condition_sampleno.imzML


SCRIPT_PATH = os.path.join('msi_preprocessing_flow', 'scripts')
SEGMENTATION_SCRIPT_PATH = os.path.join(SCRIPT_PATH, '../..', 'msi_segmentation_flow/scripts')
DATA_PATH = config['data']

# BENCHMARK_PATH = join(DATA_PATH, 'benchmark')
# BM_REPEAT = 5
INPUT_DIR = join(DATA_PATH, 'msi')
OUT_DIR = join(DATA_PATH, 'msi')
FILES = files_match_pattern(INPUT_DIR, msi_pattern)
FILES = [f.split('.')[0] for f in FILES]
EXT = [f.split('.')[1] for f in os.listdir(INPUT_DIR) if f.endswith('.d') or f.endswith('.imzML')][0]
#print('found {} files. \n processing files: {}'.format(len(FILES), FILES))

if config['matrix_removal']['matrix_mzs']:
    ruleorder: get_matrix_img_list > single_sample_segmentation
else:
    ruleorder: single_sample_segmentation > get_matrix_img_list
if config['matrix_removal']['clustering']:
    ruleorder: single_sample_segmentation > get_matrix_img_list


# Custom function to calculate resources based on file size
def file_size_resources(wildcards, input_file):
    # Get the file size in bytes
    file_size = os.path.getsize(input_file)

    # Convert file size to MB
    file_size_mb = file_size / (1024 * 1024)

    # Define the resources based on file size
    mem_mb_needed = file_size_mb * 10

    return {"mem_mb": mem_mb_needed}


def get_input(out_dir, files, input_list=[]):
    path = (out_dir + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc')
    if config['general']['matrix_removal']:
        path += '/matrix_removal'
    if config['general']['peak_filtering']:
        path += '/{}sc_filtered'.format(config['peak_filtering']['thr'])
    if config['general']['norm']:
        path += '/intranorm_{}'.format(config['intranorm']['method'])
        if len(files) > 1:
            if config['internorm']['method'] == 'mfc':
                input_list.append(path + '/median_spectrum.npy')
            path += '/internorm_{}'.format(config['internorm']['method'])
    if config['general']['outlier_removal']:
        path += '/outlier_removal'
    if config['general']['deisotoping']:
        input_list.append(path + '/deisotoped/deiso_mz.npy')
        path += '/deisotoped'
    input_list.append(expand(path + '/{fl}.imzML', fl=files))
    #print(input_list)
    return input_list


def get_output_path_single_sample_segmentation():
    if not config['matrix_removal']['matrix_mzs']:
        output = (OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_'
                  + str(config['alignment']['pixel_percentage']) + 'pxperc/' + config['matrix_removal']['dim_reduction'])
        if config['matrix_removal']['dim_reduction'] == 'umap':
            output += (str(config['matrix_removal']['umap_params']['n_neighbors']) + 'neighb_'
                       + config['matrix_removal']['cluster_algorithm'])
        if config['matrix_removal']['cluster_algorithm'] == 'hdbscan':
            output += ('_' + str(config['matrix_removal']['hdbscan_params']['min_samples']) + 'ms_'
                       + str(config['matrix_removal']['hdbscan_params']['min_cluster_size']) + 'mcs')
    else:
        output = (OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_'
                  + str(config['alignment']['pixel_percentage']) + 'pxperc')
    return output


def get_io_path_spatial_coherence():
    path = (OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc')
    if config['general']['matrix_removal']:
        path += '/matrix_removal'
    out_path = path + '/' + str(config['peak_filtering']['thr']) + 'sc_filtered'
    return path, out_path


def get_path_intranorm():
    path = (OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc')
    if config['general']['matrix_removal']:
        path += '/matrix_removal'
    if config['general']['peak_filtering']:
        path += '/{}sc_filtered'.format(config['peak_filtering']['thr'])
    return path


def get_path_internorm():
    path = (OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc')
    if config['general']['matrix_removal']:
        path += '/matrix_removal'
    if config['general']['peak_filtering']:
        path += '/{}sc_filtered'.format(config['peak_filtering']['thr'])
    path += '/intranorm_{}/internorm_{}'.format(config['intranorm']['method'], config['internorm']['method'])
    return path


def get_io_path_multi_sample_segmentation():
    path = (OUT_DIR + '/peakpicking_{}snr/alignment_{}pxperc').format(config['peak_picking']['snr'],
        config['alignment']['pixel_percentage'])
    if config['general']['matrix_removal']:
        path += '/matrix_removal'
    if config['general']['peak_filtering']:
        path += '/{}sc_filtered'.format(config['peak_filtering']['thr'])
    if config['general']['norm']:
        path += '/intranorm_{}'.format(config['intranorm']['method'])
        if len(FILES) > 1:
            path += '/internorm_{}'.format(config['internorm']['method'])
    out_path = path + '/umap_{}neighb_hdbscan_{}mcs_{}ms'.format(config['outlier_detection']['umap_params']['n_neighbors'],
        config['outlier_detection']['hdbscan_params']['min_cluster_size'], config['outlier_detection']['hdbscan_params']['min_samples'])
    return path, out_path


def get_io_path_outlier_detection():
    path = (OUT_DIR + '/peakpicking_{}snr/alignment_{}pxperc').format(config['peak_picking']['snr'],
        config['alignment']['pixel_percentage'])
    if config['general']['matrix_removal']:
        path += '/matrix_removal'
    if config['general']['peak_filtering']:
        path += '/{}sc_filtered'.format(config['peak_filtering']['thr'])
    if config['general']['norm']:
        path += '/intranorm_{}'.format(config['intranorm']['method'])
        if len(FILES) > 1:
            path += '/internorm_{}'.format(config['internorm']['method'])
    out_path = path + '/outlier_removal'
    return path, out_path


def get_io_path_deisotoping():
    path = (OUT_DIR + '/peakpicking_{}snr/alignment_{}pxperc').format(config['peak_picking']['snr'],
        config['alignment']['pixel_percentage'])
    if config['general']['matrix_removal']:
        path += '/matrix_removal'
    if config['general']['peak_filtering']:
        path += '/{}sc_filtered'.format(config['peak_filtering']['thr'])
    if config['general']['norm']:
        path += '/intranorm_{}'.format(config['intranorm']['method'])
        if len(FILES) > 1:
            path += '/internorm_{}'.format(config['internorm']['method'])
    if config['general']['outlier_removal']:
        path += '/outlier_removal'
    out_path = path + '/deisotoped'
    return path, out_path


if config['internorm']['method'] == 'mfc':
    ruleorder: internorm_mfc > internorm
else:
    ruleorder: internorm > internorm_mfc


rule all:
    input: get_input(OUT_DIR, FILES)
    #input: expand(OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/{fl}.imzML', fl=FILES)
    #input: expand(OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_' + str(config['alignment']['pixel_percentage']) + 'pxperc/{fl}.imzML', fl=FILES)

rule peak_picking:
    input: INPUT_DIR + '/{fl}.' + EXT
    output: OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.peak_picking.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/peak_picking/peak_picking.py {input:q} -snr {config[peak_picking][snr]} \
            -window_size {config[peak_picking][window]} -order {config[peak_picking][order]} -smooth {config[peak_picking][smooth]} \
            -out_dir \'' + INPUT_DIR + '/peakpicking_{config[peak_picking][snr]}snr\''

rule get_reference_spectrum:
   input: expand(OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/{fl}.imzML', fl=FILES)
   output: OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/alignment_' + str(config['alignment']['pixel_percentage']) + 'pxperc/cmz.npy'
   # benchmark: repeat(BENCHMARK_PATH + "/get_reference_spectrum.benchmark.txt", BM_REPEAT)
   shell: 'python ' + SCRIPT_PATH + '/alignment/get_reference_spectrum.py \'' +  OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr' +
           '\' -result_dir \'' + OUT_DIR + '/peakpicking_{config[peak_picking][snr]}snr/alignment_{config[alignment][pixel_percentage]}pxperc\' \
           -mz_res {config[alignment][mz_resolution]} -px_perc {config[alignment][pixel_percentage]} -num_px_perc {config[alignment][num_pixel_percentage]}'

rule alignment:
    input: file = OUT_DIR + '/peakpicking_' + str(config['peak_picking']['snr']) + 'snr/{fl}.imzML',
           reference = OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr'])
                       + 'snr/alignment_' + str(config['alignment']['pixel_percentage']) + 'pxperc/cmz.npy'
    output: OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.alignment.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/alignment/alignment.py {input.file} {input.reference} -result_dir \'' + OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr'])
           + 'snr/alignment_' + str(config['alignment']['pixel_percentage']) + 'pxperc\''

rule single_sample_segmentation:
    input: OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/{fl}.imzML'
    output: get_output_path_single_sample_segmentation() + '/binary_imgs/{fl}_matrix_cluster.tif'
    resources:
        mem_mb=lambda wildcards: file_size_resources(wildcards,input_file="{}/peakpicking_{}snr/alignment_{}pxperc/{}.ibd".format(OUT_DIR,config['peak_picking']['snr'],config['alignment']['pixel_percentage'],wildcards.fl))["mem_mb"]
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.single_sample_segmentation.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SEGMENTATION_SCRIPT_PATH + '/single_sample_segmentation.py {input:q} \
            -result_dir ' + get_output_path_single_sample_segmentation() + ' -method {config[matrix_removal][dim_reduction]} -n_components {config[matrix_removal][n_components]}'
            ' -metric {config[matrix_removal][umap_params][metric]} -n_neighbors {config[matrix_removal][umap_params][n_neighbors]}'
            ' -min_dist {config[matrix_removal][umap_params][min_dist]} -n_clusters {config[matrix_removal][kmeans_params][n_clusters]}'
            ' -min_cluster_size {config[matrix_removal][hdbscan_params][min_cluster_size]} -min_samples {config[matrix_removal][hdbscan_params][min_samples]}'
            ' -matrix_cluster True -cluster {config[matrix_removal][cluster_algorithm]}'

rule get_matrix_img_list:
    input: OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/{fl}.imzML'
    output: get_output_path_single_sample_segmentation() + '/binary_imgs/{fl}_matrix_cluster.tif'
    shell: 'python ' + SCRIPT_PATH + '/matrix_removal/get_matrix_pixels_from_mz_list.py {input} '
            '{config[matrix_removal][matrix_mzs]} -result_dir ' + get_output_path_single_sample_segmentation() + '/binary_imgs '
            '-threshold {config[matrix_removal][thr_method]}'

rule get_matrix_pixels:
    input: imzML_file = OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/{fl}.imzML',
            matrix_img = get_output_path_single_sample_segmentation() + '/binary_imgs/{fl}_matrix_cluster.tif'
    output: OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/matrix_removal/{fl}_extended_matrix_image.tif'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.get_matrix_pixels.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/matrix_removal/get_matrix_pixels_from_segmentation.py {input.imzML_file} '
            + get_output_path_single_sample_segmentation() + '/binary_imgs/ -result_dir ' + OUT_DIR + '/peakpicking_'
            +  str(config['peak_picking']['snr']) + 'snr/alignment_' + str(config['alignment']['pixel_percentage']) + 'pxperc/matrix_removal/'
            + ' -matrix_corr_thr {config[matrix_removal][matrix_corr_thr]} -pixel_perc_thr {config[matrix_removal][pixel_perc_thr]}'

rule matrix_removal:
    input: imzML_file = OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/{fl}.imzML',
            extended_matrix_img = OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/matrix_removal/{fl}_extended_matrix_image.tif'
    output: OUT_DIR + '/peakpicking_' +  str(config['peak_picking']['snr']) + 'snr/alignment_'
            + str(config['alignment']['pixel_percentage']) + 'pxperc/matrix_removal/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.matrix_removal.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/matrix_removal/matrix_removal.py {input.imzML_file:q} {input.extended_matrix_img:q} '
            '-proc_matrix_img {config[matrix_removal][matrix_postproc]} -pixel_removal {config[matrix_removal][pixel_removal]}'
            ' -matrix_subtraction {config[matrix_removal][matrix_subtraction]}'
            ' -matrix_peaks_removal {config[matrix_removal][matrix_peak_removal]}'
            ' -num_matrix_peaks {config[matrix_removal][num_matrix_peaks]}'

rule calc_spatial_coherence:
    input: get_io_path_spatial_coherence()[0] + '/{fl}.imzML'
    output: get_io_path_spatial_coherence()[0] + '/{fl}_sc.csv'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.calc_spatial_coherence.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/peak_filtering/spatial_coherence.py {input:q} -result_dir \''
            + get_io_path_spatial_coherence()[0] + '\''

rule sum_spatial_coherence:
    input: sc_files = expand(get_io_path_spatial_coherence()[0] + '/{fl}_sc.csv', fl=FILES),
           imzML_files = expand(get_io_path_spatial_coherence()[0] + '/{fl}.imzML', fl=FILES)
    output: get_io_path_spatial_coherence()[0] + '/peaks_above_' + str(config['peak_filtering']['thr']) + 'sc.npy'
    # benchmark: repeat(BENCHMARK_PATH + "/sum_spatial_coherence.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/peak_filtering/get_mzs_above_spatial_coherence_thr.py ' + get_io_path_spatial_coherence()[0]
            + ' ' + get_io_path_spatial_coherence()[0] + ' ' + get_io_path_spatial_coherence()[0]
            + ' -thr ' + str(config['peak_filtering']['thr']) + ' -sum ' + str(config['peak_filtering']['sum'])

rule filter_spatial_coherence:
    input: imzML_file = get_io_path_spatial_coherence()[0] + '/{fl}.imzML',
           sc_thr_file = get_io_path_spatial_coherence()[0] + '/peaks_above_' + str(config['peak_filtering']['thr']) + 'sc.npy'
    output: get_io_path_spatial_coherence()[1] + '/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.filter_spatial_coherence.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/peak_filtering/filter_spatial_coherence.py {input.imzML_file} '
            '{config[peak_filtering][thr]} {input.sc_thr_file} -result_dir \'' +  get_io_path_spatial_coherence()[1] + '\''

rule intranorm:
    input: get_path_intranorm() + '/{fl}.imzML'
    output: get_path_intranorm() + '/intranorm_' + config['intranorm']['method'] + '/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.intranorm.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/intranorm/intranorm.py {input:q} -result_dir \'' \
            + get_path_intranorm() + '/intranorm_' + config['intranorm']['method'] + '\' \
            -method {config[intranorm][method]}'

rule median_spectrum:
    input: expand(get_path_intranorm() + '/intranorm_' + config['intranorm']['method'] + '/{fl}.imzML', fl=FILES)
    output: get_path_intranorm() + '/intranorm_' + config['intranorm']['method'] + '/median_spectrum.npy'
    # benchmark: repeat(BENCHMARK_PATH + "/median_spectrum.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/internorm/get_median_overall_spectrum.py '
            + get_path_intranorm() + '/intranorm_' + config['intranorm']['method']  + ' {output:q}'

rule internorm_mfc:
    input: file = get_path_intranorm() + '/intranorm_' + config['intranorm']['method'] + '/{fl}.imzML',
            reference = rules.median_spectrum.output
    output: get_path_internorm() + '/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.internorm.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/internorm/internorm.py {input.file:q} -result_dir \'' \
           + get_path_internorm() + '\' -method {config[internorm][method]} -reference {input.reference:q}'

rule internorm:
    input: get_path_intranorm() + '/intranorm_' + config['intranorm']['method'] + '/{fl}.imzML'
    output: get_path_internorm() + '/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.internorm.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/internorm/internorm.py {input.file:q} -result_dir \'' \
           + get_path_internorm() + '\' -method {config[internorm][method]}'

rule multi_sample_segmentation:
    input: expand(get_io_path_multi_sample_segmentation()[0] + '/{fl}.imzML', fl=FILES)
    #output: seg_imgs = get_io_path_multi_sample_segmentation()[1]  + '/{fl}.png',
    #        umap_data = get_io_path_multi_sample_segmentation()[1] + '/umap_data.csv'
    output: umap_data = get_io_path_multi_sample_segmentation()[1] + '/umap_data.csv'
    # benchmark: repeat(BENCHMARK_PATH + "/multi_sample_segmentation.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SEGMENTATION_SCRIPT_PATH + '/multi_sample_segmentation.py ' + get_io_path_multi_sample_segmentation()[0]
            + ' -result_dir ' + get_io_path_multi_sample_segmentation()[1]
            + ' -n_neighbors {config[outlier_detection][umap_params][n_neighbors]}'
            ' -min_cluster_size {config[outlier_detection][hdbscan_params][min_cluster_size]}'
            ' -min_samples {config[outlier_detection][hdbscan_params][min_samples]}'

rule outlier_detection:
    input: umap_data = rules.multi_sample_segmentation.output.umap_data,
            imzML_files = expand(get_io_path_outlier_detection()[0] + '/{fl}.imzML', fl=FILES)
    output: expand(get_io_path_outlier_detection()[1] + '/{fl}.imzML', fl=FILES)
    # benchmark: repeat(BENCHMARK_PATH + "/outlier_detection.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/outlier_detection/umap_outlier_detection.py {input.umap_data} '
            + get_io_path_outlier_detection()[0] + ' -result_dir \'' + get_io_path_outlier_detection()[1]
            + '\' -cluster_thr {config[outlier_detection][cluster_thr]} -sample_thr {config[outlier_detection][sample_thr]}'
              ' -remove_ssc {config[outlier_detection][remove_ssc]}'

rule get_deisotoped_mz:
    input: expand(get_io_path_deisotoping()[0] + '/{fl}.imzML', fl=FILES)
    output: get_io_path_deisotoping()[1] + '/deiso_mz.npy'
    # benchmark: repeat(BENCHMARK_PATH + "/get_deisotoped_mz.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/deisotoping/get_deisotoped_mz.py \'' + get_io_path_deisotoping()[0] +
            '\' -result_dir \'' + get_io_path_deisotoping()[1] + '\' \
            -tolerance {config[deisotoping][tolerance]} -min_isotopes {config[deisotoping][min_isotopes]} \
            -max_isotopes {config[deisotoping][max_isotopes]} -openMS {config[deisotoping][openMS]}'

rule deisotoping:
    input: reference = get_io_path_deisotoping()[1] + '/deiso_mz.npy',
            file = get_io_path_deisotoping()[0] + '/{fl}.imzML'
    output: get_io_path_deisotoping()[1] + '/{fl}.imzML'
    # benchmark: repeat(BENCHMARK_PATH + "/{fl}.deisotoping.benchmark.txt", BM_REPEAT)
    shell: 'python ' + SCRIPT_PATH + '/deisotoping/deisotope.py {input.file:q} {input.reference:q} \
            -result_dir \'' + get_io_path_deisotoping()[1] + '\''


# rule get_matrix_pixels_list:
#     input: get_path_for_matrixsub(OUT_DIR) + '/{fl}.imzML'
#     output: coords = get_path_for_matrixsub(OUT_DIR) + '/matrix_subtraction/{fl}_matrix_pixels.csv',
#             img = get_path_for_matrixsub(OUT_DIR) + '/matrix_subtraction/{fl}_matrix_cluster.tif'
#     shell: 'python ' + SCRIPT_PATH + '/get_matrix_pixels_from_mz_list.py {input:q} \
#             {config[matrixsub][mz]} -result_dir \'' + get_path_for_matrixsub(OUT_DIR) + '/matrix_subtraction\''
#
# rule matrix_subtraction:
#     input: imzml = get_path_for_matrixsub(OUT_DIR) + '/{fl}.imzML',
#            matrix_pixel = get_path_for_matrixsub(OUT_DIR) + '/matrix_subtraction/{fl}_matrix_pixels.csv'
#     output: get_path_for_matrixsub(OUT_DIR) + '/matrix_subtraction/{fl}.imzML'
#     shell: 'python ' + SCRIPT_PATH + '/matrix_removal.py {input.imzml:q} {input.matrix_pixel:q} \
#             -result_dir \'' + get_path_for_matrixsub(OUT_DIR) + '/matrix_subtraction\' \
#             -pixel_removal {config[matrixsub][pixel_removal]} -matrix_subtraction {config[matrixsub][matrix_subtraction]}'
#
# rule get_region_imzML:
#     input: get_path_for_region(OUT_DIR) + '/{fl}.imzML'
#     output: get_path_for_region(OUT_DIR) + '/' + config['region']['name'] + '/{fl}.imzML'
#     shell: 'python ' + SCRIPT_PATH + '/get_region_imzML_from_mask.py {input:1} \'{config[region][bin_dir]}/{wildcards.fl}.tif\' \
#              -result_dir \'' + get_path_for_region(OUT_DIR) + '/' + config['region']['name'] + '\''
#
#
#
# rule transformation:
#     input: get_path_for_transform(OUT_DIR, FILES) + '/{fl}.imzML'
#     output: get_path_for_transform(OUT_DIR, FILES) + '/transformation/{fl}.imzML'
#     shell: 'python ' + SCRIPT_PATH + '/transformation.py {input:q} -result_dir \'' + \
#            get_path_for_transform(OUT_DIR,FILES) + '/transformation\''
#
# rule get_spatial_coherence:
#     input: get_path_for_spatial_coherence(OUT_DIR, FILES) + '/{fl}.imzML'
#     output: get_path_for_spatial_coherence(OUT_DIR, FILES) + '/spatial_coherence/{fl}_sc.csv'
#     shell: 'python ' + SCRIPT_PATH + '/spatial_coherence.py {input:q} ' + \
#             get_path_for_spatial_coherence(OUT_DIR, FILES) + '/spatial_coherence'
#
# rule get_mzs_above_spatial_coherence_thr:
#     input: get_input_for_filter_spatial_coherence(OUT_DIR, FILES)[1]
#     output: get_input_for_filter_spatial_coherence(OUT_DIR, FILES)[0] + '/peaks_above_sc.npy'
#     shell: 'python ' + SCRIPT_PATH + '/get_mzs_above_spatial_coherence_thr.py \'' + \
#             get_input_for_filter_spatial_coherence(OUT_DIR, FILES)[0] + '\' \'' + \
#            get_path_for_spatial_coherence(OUT_DIR,FILES) + '\' \'' +
#            get_input_for_filter_spatial_coherence(OUT_DIR, FILES)[0] + '\' -thr {config[spatial_coherence][thr]}'
#
# rule filter_spatial_coherence:
#     input: imzML=get_path_for_spatial_coherence(OUT_DIR, FILES) + '/{fl}.imzML',
#             refmz=get_input_for_filter_spatial_coherence(OUT_DIR, FILES)[0] + '/peaks_above_sc.npy'
#     output: get_path_for_spatial_coherence(OUT_DIR, FILES) + '/spatial_coherence/{fl}.imzML'
#     shell: 'python ' + SCRIPT_PATH + '/filter_spatial_coherence.py {input.imzML:q} {input.refmz:q} \'' + \
#             get_input_for_filter_spatial_coherence(OUT_DIR, FILES)[0] + '\''
#
# rule get_summarized_spectra:
#     input: get_input_for_summarized(OUT_DIR, FILES)[1]
#     output: get_input_for_summarized(OUT_DIR, FILES)[0] + '/summarized/summarized.csv'
#     shell: 'python ' + SCRIPT_PATH + '/get_summarized_spectra.py \'' + get_input_for_summarized(OUT_DIR, FILES)[0] + '\' \
#             -result_dir \'' + get_input_for_summarized(OUT_DIR, FILES)[0] + '/summarized