# Rosalind: ASMQ — Assessing Assembly Quality with N50 and N75
# https://rosalind.info/problems/asmq/
#
# Given: A collection of at most 1000 DNA strings.
# Return: N50 and N75 for the collection.

let contigs = [
    "GATTACA",
    "TACTACTAC",
    "ATTGAT",
    "GAAGA"
]

# NXX is the length of the shortest contig in the set of longest contigs that
# together cover at least XX% of the assembly.
fn n_statistic(lengths, percent) {
    let sorted = lengths |> sort() |> reverse()
    let target = float(sum(sorted)) * percent / 100.0
    let running = 0.0
    let answer = 0
    let i = 0
    while i < len(sorted) and answer == 0 {
        running = running + float(sorted[i])
        if running >= target then answer = sorted[i]
        i = i + 1
    }
    answer
}

let lengths = contigs |> map(|c| len(c))
let n50_value = n_statistic(lengths, 50.0)
let n75 = n_statistic(lengths, 75.0)

println("Result:   " + str(n50_value) + " " + str(n75))
println("Expected: 7 6")

fn test_asmq_n50_and_n75() {
    assert n50_value == 7, "ASMQ: N50 was " + str(n50_value)
    assert n75 == 6, "ASMQ: N75 was " + str(n75)
}
