---
$schema: "https://json-schema.org/draft/2020-12/schema"

$id: /schemas/config.schema.yaml

description: snakemake configuration file

type: object

# possible entries of the config file

properties:
  # start_version
  version:
    description: Version of MPRAsnakeflow
    type: string
    pattern: ^(\d+(\.\d+)?(\.\d+)?)|(0\.\d+(\.\d+)?)$
  skip_version_check:
    description: Skip version check
    type: boolean
    default: false
  # start_assignments
  assignments:
    description: Assignments to run with configurations
    type: object
    patternProperties:
      description: name of the assignment
      ^([^_\.]+)$:
        type: object
        properties:
          alignment_tool:
            type: object
            properties:
              split_number:
                type: integer
                default: 1
              tool:
                type: string
                enum:
                  - exact
                  - bwa
                  - bbmap
                  - bwa-additional-filtering
                  - pbmm2
                default: bbmap
            allOf:
              - if:
                  properties:
                    tool:
                      const: bwa
                then:
                  properties:
                    configs:
                      type: object
                      properties:
                        min_mapping_quality:
                          type: integer
                          minimum: 0
                          default: 1
                        sequence_length:
                          type: object
                          properties:
                            min:
                              type: integer
                            max:
                              type: integer
                        alignment_start:
                          type: object
                          properties:
                            min:
                              type: integer
                            max:
                              type: integer
                        M:
                          type: boolean
                          description: mark shorter split hits as secondary
                          default: true
                        L:
                          type: array
                          description: penalty for 5'- and 3'-end clipping 80
                          items:
                            type: integer
                          minItems: 1
                          maxItems: 2
                          default: [80]
                        cigar_filter_regex:
                          type: string
                          description: Optional regex for full CIGAR matching (e.g. 200M or 200M|210M)
                      required:
                        - min_mapping_quality
                  required:
                    - configs
              - if:
                  properties:
                    tool:
                      const: bwa-additional-filtering
                then:
                  properties:
                    configs:
                      type: object
                      properties:
                        sequence_length:
                          type: integer
                          minimum: 1
                        min_mapping_quality:
                          type: integer
                          minimum: 0
                          default: 1
                        M:
                          type: boolean
                          description: mark shorter split hits as secondary
                          default: true
                        L:
                          type: array
                          description: penalty for 5'- and 3'-end clipping 80
                          items:
                            type: integer
                          minItems: 1
                          maxItems: 2
                          default: [80]
                        identity_threshold:
                          description: Identity threshold is used to choose which alignments are worth trying to rescue.
                          type: number
                          default: 0.98
                        mismatches_threshold:
                          description: Threshold of mismatches we investigate if we should try to rescue.
                          type: integer
                          default: 3
                        verbose:
                          description: print which alignments were rescured and which could not be rescued
                          type: boolean
                          default: false
                      required:
                        - sequence_length
                        - min_mapping_quality
                  required:
                    - configs
              - if:
                  properties:
                    tool:
                      const: bbmap
                then:
                  properties:
                    configs:
                      type: object
                      properties:
                        min_mapping_quality:
                          type: integer
                          minimum: 0
                          default: 30
                        cigar_filter_regex:
                          type: string
                          description: Optional regex for full CIGAR matching (e.g. 200M or 200M|210M)
                      required:
                        - min_mapping_quality
                  required:
                    - configs
              - if:
                  properties:
                    tool:
                      const: exact
                then:
                  properties:
                    configs:
                      type: object
                      properties:
                        sequence_length:
                          type: integer
                          minimum: 1
                        alignment_start:
                          type: integer
                          minimum: 1
                      required:
                        - sequence_length
                        - alignment_start
                  required:
                    - configs
              - if:
                  properties:
                    tool:
                      const: pbmm2
                then:
                  properties:
                    configs:
                      type: object
                      properties:
                        preset:
                          type: string
                          enum:
                            - SUBREAD
                            - CCS
                            - ISOSEQ
                            - HIFI
                            - UNROLLED
                          default: SUBREAD
                        min_concordance:
                          type: number
                          minimum: 0
                          maximum: 1
                          default: 0.9
                      required:
                        - preset
                        - min_concordance
                  required:
                    - configs
            required:
              - tool
          bc_length:
            type: integer
          BC_rev_comp:
            type: boolean
            default: false
          linker_length:
            type: integer
          linker:
            type: string
            pattern: ^[ATCGNatcgn]+$
          FWD:
            type: array
            items:
              type: string
            minItems: 1
            uniqueItems: true
          BC:
            type: array
            items:
              type: string
            minItems: 1
            uniqueItems: true
          REV:
            type: array
            items:
              type: string
            minItems: 1
            uniqueItems: true
          adapters:
            type: object
            properties:
              BC:
                oneOf:
                  - type: object
                    properties:
                      five_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                      three_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                  - type: array
                    items:
                      type: integer
                    minItems: 1
                    uniqueItems: true
              FWD:
                oneOf:
                  - type: object
                    properties:
                      five_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                      three_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                  - type: array
                    items:
                      type: integer
                    minItems: 1
                    uniqueItems: true
              REV:
                oneOf:
                  - type: object
                    properties:
                      five_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                      three_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                  - type: array
                    items:
                      type: integer
                    minItems: 1
                    uniqueItems: true
          merge_tool:
            type: string
            enum:
              - NGmerge
              - fastq-join
            default: NGmerge
          NGmerge:
            type: object
            properties:
              min_overlap:
                type: integer
                default: 20
              frac_mismatches_allowed:
                type: number
                default: 0.1
              min_dovetailed_overlap:
                type: integer
                default: 50
            required:
              - min_overlap
              - frac_mismatches_allowed
              - min_dovetailed_overlap
            default: {}
          fastq-join:
            type: object
            properties:
              min_overlap:
                type: integer
                default: 6
              max_pct_mismatch:
                type: number
                default: 8
            required:
              - min_overlap
              - max_pct_mismatch
            default: {}
          design_file:
            type: string
          design_check:
            type: object
            properties:
              fast:
                type: boolean
                default: true
              sequence_collisions:
                type: boolean
                default: true
              sequence_start:
                type: integer
                minimum: 1
              sequence_length:
                type: integer
                minimum: 1
            default: {}
            required:
              - fast
              - sequence_collisions
            allOf:
              - if:
                  properties:
                    sequence_collisions:
                      const: true
                  required:
                    - sequence_collisions
                then:
                  required:
                    - sequence_start
                    - sequence_length
          strand_sensitive:
            type: object
            default: {}
            properties:
              enable:
                type: boolean
                default: false
              forward_adapter:
                type: string
                pattern: ^[ATCGN]+$
                default: AGGACCGGATCAACT
              reverse_adapter:
                type: string
                pattern: ^[ATCGN]+$
                default: TCGGTTCACGCAATG
            required:
              - enable
          configs:
            type: object
            patternProperties:
              ^([^_\.]+)$:
                type: object
                properties:
                  min_support:
                    type: integer
                    minimum: 1
                    default: 3
                  fraction:
                    type: number
                    exclusiveMinimum: 0.5
                    maximum: 1
                    default: 0.75
                required:
                  - min_support
                  - fraction
            minProperties: 1
        oneOf:
          - required:
              - FWD
              - linker_length
          - required:
              - FWD
              - linker
          - required:
              - FWD
              - BC
          - required:
              - long_read_input
              - linker
        required:
          - strand_sensitive
          - bc_length
          - design_file
          - configs
          - alignment_tool
    minProperties: 1
  # start_experiments
  experiments:
    description: MPRA experiments to run with configurations
    type: object
    patternProperties:
      description: name of the experiment
      ^([^_\.]+)$:
        type: object
        properties:
          split_number:
            type: integer
            default: 1
          bc_length:
            type: integer
            minimum: 1
          bc_extraction:
            type: string
            enum:
              - start
              - end
            default: start
          umi_length:
            type: integer
            minimum: 1
          umi_extraction:
            type: string
            enum:
              - start
              - end
            default: start
          adapters:
            type: object
            properties:
              UMI:
                oneOf:
                  - type: object
                    properties:
                      five_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                      three_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                  - type: array
                    items:
                      type: integer
                    minItems: 1
                    uniqueItems: true
              FWD:
                oneOf:
                  - type: object
                    properties:
                      five_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                      three_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                  - type: array
                    items:
                      type: integer
                    minItems: 1
                    uniqueItems: true
              REV:
                oneOf:
                  - type: object
                    properties:
                      five_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                      three_prime:
                        type: array
                        items:
                          type: string
                          pattern: ^[ATCGNatcgn]+$
                        minItems: 1
                        uniqueItems: true
                  - type: array
                    items:
                      type: integer
                    minItems: 1
                    uniqueItems: true
          data_folder:
            type: string
          experiment_file:
            type: string
          demultiplex:
            type: boolean
            default: false
          merge_tool:
            type: string
            enum:
              - custom
              - NGmerge
            default: custom
          NGmerge:
            type: object
            properties:
              min_overlap:
                type: integer
                default: 11
              frac_mismatches_allowed:
                type: number
                default: 0.1
            required:
              - min_overlap
              - frac_mismatches_allowed
            default: {}
          label_file:
            type: string
          assignments:
            type: object
            patternProperties:
              ^([^_\.]+)$:
                type: object
                properties:
                  type:
                    type: string
                    enum:
                      - file
                      - config
                  assignment_file:
                    type: string
                  assignment_name:
                    type: string
                  assignment_config:
                    type: string
                  sampling:
                    type: object
                    properties:
                      prop:
                        type: number
                        exclusiveMinimum: 0
                        maximum: 1
                      total:
                        type: integer
                        minimum: 1
                required:
                  - type
                allOf:
                  - if:
                      properties:
                        type:
                          const: config
                      required:
                        - type
                    then:
                      required:
                        - assignment_name
                        - assignment_config
                  - if:
                      properties:
                        type:
                          const: file
                      required:
                        - type
                    then:
                      required:
                        - assignment_file
          configs:
            type: object
            patternProperties:
              ^([^_\.]+)$:
                type: object
                properties:
                  filter:
                    type: object
                    default: {}
                    properties:
                      bc_threshold:
                        type: integer
                        minimum: 1
                        default: 10
                      outlier_detection:
                        type: object
                        properties:
                          method:
                            type: string
                            enum:
                              - rna_counts_zscore
                          times_zscore:
                            type: number
                            exclusiveMinimum: 0
                            default: 3
                        required:
                          - times_zscore
                        default: {}
                      min_dna_counts:
                        type: integer
                        miminum: 0
                        default: 1
                      min_rna_counts:
                        type: integer
                        miminum: 0
                        default: 1
                    required:
                      - bc_threshold
                      - min_rna_counts
                      - min_dna_counts
                  sampling:
                    type: object
                    patternProperties:
                      ^((DNA)|(RNA))$:
                        type: object
                        properties:
                          threshold:
                            type: integer
                            minimum: 1
                          prop:
                            type: number
                            exclusiveMinimum: 0
                            maximum: 1
                          total:
                            type: number
                            minimum: 1
                          seed:
                            type: integer
                required:
                  - filter
          variants:
            type: object
            properties:
              map:
                type: string
              min_barcodes:
                type: array
                items:
                  type: integer
                  minimum: 1
            required:
              - map
              - min_barcodes
        # entries that have to be in the config file for successful validation
        required:
          - split_number
          - bc_length
          - data_folder
          - experiment_file
          - demultiplex
          - assignments
          - configs
# end_experiments
additionalProperties: false
required:
  - version
  - skip_version_check
minProperties: 3
