#!/usr/bin/env -S awk -f
# Filter FASTA file
# to keep or remove only the records
# with ID in records.list
#
# Usage:
# awk -f (-v direction=keep|remove) filter_record_fasta.awk \
# records.list records.fasta

BEGIN {
    on_good_record = 0
}

NR == FNR {
    sequence_id = $1
    kept[sequence_id] = 1
    next
}

/^>/ {
    on_good_record = 0
    sequence_id = $1
    gsub(">", "", sequence_id)
    if (sequence_id in kept) {
        if (direction == "keep") {
            print $0
            on_good_record = 1
        } 
    } else {
        if (direction == "remove") {
            print $0
            on_good_record = 1
        }
    }
}

/^[^>]/ && on_good_record {
    print $0
}
