#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: Workflow
label: "rRNA annotation workflow with scatter processing"
doc: |
  "This workflow performs rRNA annotation processing for multiple index files using scatter.
  It executes 4 processes: makeblastdb, blastn alignment, filtering, and rRNA removal
  for each rRNA index file.
  related CWL file:
  ./Tools/09_makeblastdb_rRNA.cwl
  ./Tools/10_blastn_rRNA_alignment.cwl
  ./Tools/10_blastn_rRNA_filter1.cwl
  ./Tools/10_blastn_rRNA_filter2.cwl
  ./Tools/10_blastn_rRNA_filter3.cwl"

requirements:
  WorkReuse:
    enableReuse: true

inputs:
############## Parameters for makeblastdb (./Tools/09_makeblastdb_rRNA.cwl) ##############
  - id: BLASTN_rRNA_FASTA_FILE1
    type: File
    label: "SILVA_138.1_LSUParc_tax_silva"
    doc: |
      "rRNA file for SILVA_138.1_LSUParc_tax_silva
      You must obtain the file in advance from the following link.
      https://ftp.arb-silva.de/release_138.1/Exports/SILVA_138.1_LSUParc_tax_silva.fasta.gz"
    default:
      class: File
      location: ../Data/SILVA_138.1_LSUParc_tax_silva.fasta.gz
  
  - id: BLASTN_rRNA_FASTA_FILE2
    type: File
    label: "SILVA_138.1_SSUParc_tax_silva"
    doc: |
      "rRNA file for SILVA_138.1_SSUParc_tax_silva
      You must obtain the file in advance from the following link.
      https://ftp.arb-silva.de/release_138.1/Exports/SILVA_138.1_SSUParc_tax_silva.fasta.gz"
    default:
      class: File
      location: ../Data/SILVA_138.1_SSUParc_tax_silva.fasta.gz
  
  - id: BLASTN_rRNA_INDEX_DIR_NAME1
    type: string
    label: "SILVA_138.1_LSUParc_tax_silva (directory name)"
    doc: |
      "rRNA index directory name for SILVA_138.1_LSUParc_tax_silva"
    default: "SILVA_138.1_LSUParc_tax_silva"

  - id: BLASTN_rRNA_INDEX_DIR_NAME2
    type: string
    label: "SILVA_138.1_SSUParc_tax_silva (directory name)"
    doc: |
      "rRNA index directory name for SILVA_138.1_SSUParc_tax_silva"
    default: "SILVA_138.1_SSUParc_tax_silva"

############## Parameters for blastn alignment (./Tools/10_blastn_rRNA_alignment.cwl) ##############
  - id: THREADS
    type: int
    label: "threads"
    doc: "number of threads to use"
    default: 16
  
  - id: INPUT_FASTA_FILE
    type: File
    label: "input fasta file (nucleotide sequence generated by prodigal process)"
    doc: "predicted protein coding sequences produced by Prodigal process"
    default:
      class: File
      location: ../out/all_contigs_SRR27548858_dna.fasta

  - id: OUTPUT_FILE_NAME1
    type: string
    label: "output file name"
    doc: "text file of annotaion information of contaminating rRNA"
    default: "blastn_rRNA_alignment_silva_138.1_LSUParc_tax_silva.txt"
  
  - id: OUTPUT_FILE_NAME2
    type: string
    label: "output file name"
    doc: "text file of annotaion information of contaminating rRNA"
    default: "blastn_rRNA_alignment_silva_138.1_SSUParc_tax_silva.txt"
  
  - id: MAX_TARGET_SEQS
    type: int
    label: "max target seqs"
    doc: "number of annotaion output for each DNA sequences, sometimes annotaiton output will be more than 1 even though the setting is 1"
    default: 1
  
  - id: EVALUE
    type: float
    label: "evalue"
    doc: "E-value threshod of BLASTN search"
    default: 0.1

############## Parameters for blastn filter (./Tools/10_blastn_rRNA_filter1.cwl) ##############

############## Parameters for blastn filter (./Tools/10_blastn_rRNA_filter2.cwl) ##############
  - id: PRODIGAL_RESULT_PROTEIN_FASTA_FILE
    type: File
    label: "prodigal result protein fasta file"
    doc: "predicted protein sequences of Prodigal output"
    default:
      class: File
      location: ../out/all_contigs_SRR27548858_protein.fasta


############## Workflow steps ##############
steps:
  - id: MAKEBLASTDB_SILVA_138.1_LSUParc_tax_silva
    run: ../Tools/09_makeblastdb_rRNA.cwl
    label: "makeblastdb for SILVA_138.1_LSUParc_tax_silva"
    in:
      index_dir_name: BLASTN_rRNA_INDEX_DIR_NAME1
      input_fasta_file: BLASTN_rRNA_FASTA_FILE1

    out: [index_dir, index_file]
  
  - id: MAKEBLASTDB_SILVA_138.1_SSUParc_tax_silva
    run: ../Tools/09_makeblastdb_rRNA.cwl
    label: "makeblastdb for SILVA_138.1_SSUParc_tax_silva"
    in:
      index_dir_name: BLASTN_rRNA_INDEX_DIR_NAME2
      input_fasta_file: BLASTN_rRNA_FASTA_FILE2

    out: [index_dir, index_file]

  - id: BLASTN_rRNA_alignment_silva_138.1_LSUParc_tax_silva
    run: ../Tools/10_blastn_rRNA_alignment.cwl
    label: "blastn alignment for SILVA_138.1_LSUParc_tax_silva"
    in:
      threads: THREADS
      index_dir: MAKEBLASTDB_SILVA_138.1_LSUParc_tax_silva/index_file
      input_fasta_file: INPUT_FASTA_FILE
      output_file_name: OUTPUT_FILE_NAME1
      max_target_seqs: MAX_TARGET_SEQS
      evalue: EVALUE

    out: [output_file]

  - id: BLASTN_rRNA_alignment_silva_138.1_SSUParc_tax_silva
    run: ../Tools/10_blastn_rRNA_alignment.cwl
    label: "blastn alignment for SILVA_138.1_SSUParc_tax_silva"
    in:
      threads: THREADS
      index_dir: MAKEBLASTDB_SILVA_138.1_SSUParc_tax_silva/index_file
      input_fasta_file: INPUT_FASTA_FILE
      output_file_name: OUTPUT_FILE_NAME2
      max_target_seqs: MAX_TARGET_SEQS
      evalue: EVALUE

    out: [output_file]

  - id: BLASTN_rRNA_FILTER1
    run: ../Tools/10_blastn_rRNA_filter1.cwl
    label: "blastn result file filter for SILVA_138.1_LSUParc_tax_silva"
    in:
      blastn_result_file1: BLASTN_rRNA_alignment_silva_138.1_LSUParc_tax_silva/output_file
      blastn_result_file2: BLASTN_rRNA_alignment_silva_138.1_SSUParc_tax_silva/output_file

    out: [rRNAlist_file, rRNA_toplist_file]

  - id: BLASTN_rRNA_FILTER2
    run: ../Tools/10_blastn_rRNA_filter2.cwl
    label: "blastn result file filter for SILVA_138.1_SSUParc_tax_silva"
    in:
      rRNA_toplist_file: BLASTN_rRNA_FILTER1/rRNA_toplist_file
      prodigal_result_protein_fasta_file: PRODIGAL_RESULT_PROTEIN_FASTA_FILE

    out: [rRNA_fasta_file]


############## Workflow outputs ##############
outputs:
  - id: BLASTN_rRNA_concat_file
    type: File
    label: "blastn result file"
    doc: "blastn result file"
    outputSource: BLASTN_rRNA_FILTER1/rRNAlist_file

  - id: FILTERED_rRNA_PROTEIN_FASTA_FILE
    type: File
    label: "filtered rRNA protein fasta file"
    doc: "filtered rRNA protein fasta file"
    outputSource: BLASTN_rRNA_FILTER2/rRNA_fasta_file

$namespaces:
  s: https://schema.org/
  edam: http://edamontology.org/