from snakemake.utils import min_version
min_version("7.17.0")
configfile: "workflow/internal.yaml"
import pretty_errors

configfile: "config/config.yaml"
#report: "report/workflow.rst"

scattergather:
    calling=16,

StarRefdict = {
    'mouse': '/share/data/reference/mouse/mm10/10Xrefdata-gex-mm10-2020-A/mm10_10X_index100',
    'human': '/share/data/reference/human/hg38/10Xrefdata-gex-GRCh38-2020-A/hg38_10X_index100',
    'rat': '/share/data/reference/rat/rat_ensembl110_index100',
}
mtGeneLstdict = {
    #'mouse': '/share/data/reference/mouse/GRCm39/mitochondria.list',
    'human': '/dev/null',
    'mouse': '/dev/null',
    'rat': '/dev/null',
}
usedModels = {
    'segment_anything': '/share/src/third_party/segment-anything/sam_vit_h_4b8939.pth'
}
LOGOfile = 'workflow/template/SalusD.png'
#LOGOfile = 'workflow/template/CapitalBioW.png'

config["STARref"] = config["STARref"].lower()
if config["STARref"] == 'custom':
    if 'ref' in config and 'custom' in config['ref'] and 'star' in config['ref']['custom']:
        StarRefdict['custom'] = config['ref']['custom'].get('star',StarRefdict['mouse'])
        mtGeneLstdict['custom'] = config['ref']['custom'].get('mtgenes',mtGeneLstdict['mouse'])
        if mtGeneLstdict['custom'] is None:
            mtGeneLstdict['custom'] = '/dev/null'
    else:
        config["STARref"] = 'mouse'

FOVStepX = 429.6
FOVStepY = 709.8
FOVResolution = 3.45/14000 # 0.24642
DualZoneSides = 8.4  # 34087 px
SoloZoneSides = 11.2 # 45450 px
HEResolution = 0.87/1000 # 0.87 um/px, 3.53x

import os
tmpdir=os.path.join(os.getcwd(),'tmp')
os.makedirs(tmpdir, exist_ok=True)
#os.environ['TMPDIR'] = tmpdir
os.environ['LC_ALL'] = 'C'
os.environ['OPENBLAS_NUM_THREADS'] = '1'
salus_libexec_PATH = 'workflow/bin'
if 'CONDA_PREFIX' in os.environ:
    newLD_LIBRARY_PATH = os.path.join(os.environ['CONDA_PREFIX'], "lib")
    salus_libexec_PATH = os.path.join(os.environ['CONDA_PREFIX'], "libexec/salusts")
    if 'LD_LIBRARY_PATH' in os.environ:
        newLD_LIBRARY_PATH = ':'.join([os.environ['LD_LIBRARY_PATH'], '/lib', newLD_LIBRARY_PATH])
    os.environ['LD_LIBRARY_PATH'] = newLD_LIBRARY_PATH

# https://github.com/joblib/threadpoolctl/blob/master/multiple_openmp.md
# export MKL_THREADING_LAYER=TBB
os.environ['MKL_THREADING_LAYER'] = 'TBB'

include: "rules/version.py"
include: "rules/functions.smk"
include: "rules/config.smk"
include: "rules/trimming.smk"
include: "rules/alignment.smk"
include: "rules/transform.smk"
include: "rules/pe.smk"
include: "rules/manual.smk"
include: "rules/paired.smk"
include: "rules/intissue.smk"

batches = "all"

#pp.pprint(config)
#pp.pprint(samples)
#pp.pprint(samples.columns)
#pp.pprint(samples[['seqType','chipZone','sndBarcodePrimer','ReadStructures','sampleID','sampleName']])
#pp.pprint(config['defaultNFO'].keys())
eprint(f'[!]SalusGEX Version: {SalustsVer}')
eprint(f"[!]Ref:[{StarRefdict[config['STARref']]}], MTgenes:[{mtGeneLstdict[config['STARref']]}] as [{config['STARref']}].")

localrules: all, help

rule all:
    input:
        done = "results.done",
        #real = get_final_output,
