/** 1. Filter out proteome sequence belonging to 'supercontig', Mt, or Pt */
/** 2. Filter proteome to keep only the longest isoform */

process REMOVE_SUPERCONTIGS_CHLOROPLASTS_AND_MITOCHONDRIA {
    input:
    path proteome
    output:
    path 'filtered_proteome.fasta'
    script:
    """
    awk -f "${baseDir}/scripts/remove_supercontigs_chloroplasts_mitochondria_fasta.awk" "${proteome}" > 'filtered_proteome.fasta'
    """
}

process SEQUENCE_LENGTH {
    input:
    path proteome

    output:
    path 'protein_lengths.tsv'

    script:
    """
    awk -f "${baseDir}/scripts/protein_lengths.awk" "${proteome}" > 'protein_lengths.tsv'
    """
}

process PROTEOME_LONGEST {
    input:
    path proteome
    path protein_lengths

    output:
    path 'proteome_filtered.fa'

    script:
    """
    awk -f "${baseDir}/scripts/filter_longest.awk" "${protein_lengths}" "${proteome}" > 'records_filtered.list'
    awk -f "${baseDir}/scripts/filter_records_fasta.awk" 'records_filtered.list' "${proteome}" > 'proteome_filtered.fa'
    """
}



workflow FILTER_FASTA {
    take:
    proteome

    main:
    REMOVE_SUPERCONTIGS_CHLOROPLASTS_AND_MITOCHONDRIA(proteome)
    SEQUENCE_LENGTH(REMOVE_SUPERCONTIGS_CHLOROPLASTS_AND_MITOCHONDRIA.out)
    PROTEOME_LONGEST(REMOVE_SUPERCONTIGS_CHLOROPLASTS_AND_MITOCHONDRIA.out, SEQUENCE_LENGTH.out)

    emit:
    lengths = SEQUENCE_LENGTH.out
    proteome = PROTEOME_LONGEST.out
}
