"""Make variant calls from aligned contigs."""

import os

import pav3

global shell
global workflow

POLARS_MAX_THREADS = 4

os.environ['POLARS_MAX_THREADS'] = str(POLARS_MAX_THREADS)

# Load config as PAV_CONFIG
PAV_CONFIG_PATH = config.get('config_file', str(
    pav3.pipeline.get_pav_config_path(config.get('config_file', None))
))

configfile: PAV_CONFIG_PATH

PAV_CONFIG = config


# Read sample table
ASM_TABLE_PATH = pav3.pipeline.get_pav_asm_table_path(
    PAV_CONFIG.get('assembly_table', None)
)

ASM_TABLE = pav3.pipeline.read_assembly_table(ASM_TABLE_PATH, PAV_CONFIG)

# Environment source file for shell commands
SHELL_ENV_PATH = (
    pav3.pipeline.get_pav_env_path(config.get('env_source', None))
    if not config.get('ignore_env_file', False) else None
)

if SHELL_ENV_PATH:
    shell.prefix(f'set -euo pipefail; source {SHELL_ENV_PATH}; ')
else:
    shell.prefix('set -euo pipefail; ')


### Wildcard constraints ###

wildcard_constraints:
    asm_name=r'[A-Za-z_\-0-9\.]+',
    hap=r'[A-Za-z_\-0-9\.]+',
    trim=r'none|qry|qryref'

### Default rule ###

# all
#
# Make all files for all samples.

localrules: pav_all

rule pav_all:
    input:
        vcf=lambda wildcards: pav3.pipeline.expand_pattern(
            '{asm_name}.vcf.gz',
            ASM_TABLE, PAV_CONFIG,
        ),
        # pq_integrated=lambda wildcards: pav3.pipeline.expand_pattern(
        #     'results/{asm_name}/call_hap/call_insdel_{hap}.parquet',
        #     ASM_TABLE, PAV_CONFIG,
        # ),
        # pq_merge=lambda wildcards: pav3.pipeline.expand_pattern(
        #     'results/{asm_name}/call/call_{vartype}.parquet',
        #     ASM_TABLE, PAV_CONFIG,
        #     vartype=('insdel', 'inv', 'snv')
        # )


### Includes ###

include: pav3.pipeline.get_rules_path('pipeline.smk')

include: pav3.pipeline.get_rules_path('align.smk')
include: pav3.pipeline.get_rules_path('call.smk')
include: pav3.pipeline.get_rules_path('tracks.smk')
include: pav3.pipeline.get_rules_path('data.smk')
