Compare commits
No commits in common. "5e33a2bccfab3d36c86677a727f9f99d2d9bc9d7" and "031dbf06c95a3a0f6f273f05085d8aeac82e6a63" have entirely different histories.
5e33a2bccf
...
031dbf06c9
7 changed files with 152 additions and 270 deletions
|
|
@ -4,11 +4,13 @@ version = "0.0.1"
|
|||
authors = ["Thomas A. Christensen II <25492070+MillironX@users.noreply.github.com> and contributors"]
|
||||
|
||||
[deps]
|
||||
OptParse = "1244ff17-1aa7-4121-9347-f4ead108e58f"
|
||||
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
|
||||
FilePathsBase = "48062228-2e41-5def-b9a4-89aafe57970f"
|
||||
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
|
||||
|
||||
[compat]
|
||||
OptParse = "0.4.3"
|
||||
ArgParse = "1.2.0"
|
||||
FilePathsBase = "0.9.24"
|
||||
UUIDs = "1.11.0"
|
||||
julia = "1.12"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
# CualerID
|
||||
|
||||
[](https://millironx.com/CualerID.jl/stable)
|
||||
[](https://millironx.com/CualerID.jl/dev)
|
||||
[](https://woodpecker.millironx.com/repos/31)
|
||||
[](https://github.com/invenia/BlueStyle)
|
||||
[](https://github.com/JuliaTesting/Aqua.jl)
|
||||
|
|
|
|||
|
|
@ -27,12 +27,11 @@ const BASE30_ALPHABET = UInt8.([
|
|||
])
|
||||
|
||||
"""
|
||||
base30encode(v::Integer; pad::Integer=1)
|
||||
base30encode(v::UUID; pad::Integer=1)
|
||||
base30encode(v::Integer)
|
||||
base30encode(v::UUID)
|
||||
|
||||
Creates a base30 representation of `v` padded out to `pad` digits. If `v` is a
|
||||
`UUID`, then the `UInt128` value of the `UUID` is passed directly into the
|
||||
method.
|
||||
Creates a base30 representation of `v`. If `v` is a `UUID`, then the `UInt128`
|
||||
value of the `UUID` is passed directly into the method.
|
||||
|
||||
# Examples
|
||||
|
||||
|
|
@ -46,12 +45,10 @@ julia> using UUIDs; base30encode(uuid1())
|
|||
"7ZK00JECBDF2H7GNBXN59C6S9S"
|
||||
```
|
||||
"""
|
||||
function base30encode(v::Integer; pad::Integer = 1)
|
||||
return String([
|
||||
BASE30_ALPHABET[i] for i in reverse(digits(v; base = 30, pad = pad) .+ 1)
|
||||
])
|
||||
function base30encode(v::Integer)
|
||||
return String([BASE30_ALPHABET[i] for i in reverse(digits(v; base = 30) .+ 1)])
|
||||
end #function
|
||||
|
||||
function base30encode(v::UUID; pad::Integer = 1)
|
||||
return base30encode(v.value; pad = pad)
|
||||
function base30encode(v::UUID)
|
||||
return base30encode(v.value)
|
||||
end #function
|
||||
|
|
|
|||
251
src/CualerID.jl
251
src/CualerID.jl
|
|
@ -1,143 +1,174 @@
|
|||
module CualerID
|
||||
|
||||
using OptParse:
|
||||
@parser,
|
||||
OptParse,
|
||||
arg,
|
||||
command,
|
||||
default,
|
||||
flag,
|
||||
flt,
|
||||
lift,
|
||||
option,
|
||||
or,
|
||||
path,
|
||||
runparse,
|
||||
str,
|
||||
u64
|
||||
using UUIDs: UUID, uuid4
|
||||
using ArgParse: ArgParse, ArgParseSettings, parse_args, parse_item, @add_arg_table!
|
||||
using FilePathsBase: AbstractPath, Path, exists, isfile
|
||||
using UUIDs: UUID
|
||||
|
||||
include("Base30.jl")
|
||||
include("ResultCodeOption.jl")
|
||||
include("ResultCodeOptionValueParser.jl")
|
||||
include("Minting.jl")
|
||||
|
||||
export ResultCode
|
||||
export ResultCodeOption
|
||||
export base30encode
|
||||
export cualer_id
|
||||
export has_duplicate
|
||||
export has_fixed
|
||||
export has_not_fixable
|
||||
export has_valid
|
||||
export mint_ids!
|
||||
|
||||
@parser struct CreateIDs
|
||||
@description "Create a set of new CualerIDs"
|
||||
function _create_arg_table()
|
||||
s = ArgParseSettings(autofix_names = true)
|
||||
|
||||
"The length of the printed sample ids"
|
||||
length = option("-l", "--length", u64("LENGTH")) |> default(0x05)
|
||||
#! format: off
|
||||
@add_arg_table! s begin
|
||||
"create"
|
||||
help = "Create CualerID sample ids or barcode labels"
|
||||
action = :command
|
||||
"fix"
|
||||
help = "Compare a set of possibly invalid IDs against a correct set of IDs to identify errors in the IDs"
|
||||
action = :command
|
||||
"convert"
|
||||
help = "Convert CualID sample ids to CualerID sample ids or vice-versa"
|
||||
action = :command
|
||||
end #add_arg_table
|
||||
|
||||
"The fraction of ids which must be dissimilar"
|
||||
fail_threshold =
|
||||
option("-f", "--fail-threshold", flt("THRESHOLD"; min = 0.0, max = 1.0)) |>
|
||||
default(0.99)
|
||||
@add_arg_table! s["create"] begin
|
||||
"ids"
|
||||
help = "Generate sample ids"
|
||||
action = :command
|
||||
"labels"
|
||||
help = "Generate a pdf of barcodes (NOT YET IMPLEMENTED)"
|
||||
action = :command
|
||||
end #add_arg_table
|
||||
|
||||
"The number of sample ids to print"
|
||||
number_of_ids = arg(u64("NUMBER"; min = 0x01))
|
||||
end #struct
|
||||
@add_arg_table! s["create"]["ids"] begin
|
||||
"--length", "-l"
|
||||
help = "The length of the printed sample ids"
|
||||
arg_type = UInt64
|
||||
default = UInt64(5)
|
||||
"--fail-threshold", "-f"
|
||||
help = "NOT IMPLEMENTED: The rate at which similar ids will fail the process"
|
||||
arg_type = Float64
|
||||
range_tester = x -> x >= 0 && x <= 1.0
|
||||
default = 0.99
|
||||
"number_of_ids"
|
||||
help = "The number of sample ids to print"
|
||||
arg_type = UInt64
|
||||
range_tester = x -> x >= 1
|
||||
required = true
|
||||
end #add_arg_table
|
||||
|
||||
@parser struct CreateLabels
|
||||
@description "Create printable labels from CualerIDs"
|
||||
@add_arg_table! s["fix"] begin
|
||||
"--correct-ids"
|
||||
help = "Path to the list of correct ids"
|
||||
arg_type = AbstractPath
|
||||
range_tester = x -> exists(x) && isfile(x)
|
||||
required = true
|
||||
"--show", "-s"
|
||||
help = """
|
||||
Which types of fixes should be printed to output. Can be a
|
||||
combination of D for duplicate, F for fixed, N for not fixable,
|
||||
and V for valid.
|
||||
"""
|
||||
arg_type = ResultCodeOption
|
||||
default = ResultCodeOption("DFN")
|
||||
"--all", "-a"
|
||||
help = "Show all ids regardless of fixed status"
|
||||
action = :store_true
|
||||
"ids_to_be_corrected"
|
||||
help = "File containing CualerIDs that need correction"
|
||||
arg_type = AbstractPath
|
||||
range_tester = x -> exists(x) && isfile(x)
|
||||
required = true
|
||||
end #add_arg_table
|
||||
|
||||
"""
|
||||
The input file containing sample ids. The input sample ids can be in either CualID or
|
||||
CualerID format, but the output labels will always be in CualerID format.
|
||||
"""
|
||||
input = arg(path("SAMPLE_IDS"))
|
||||
@add_arg_table! s["convert"] begin
|
||||
"--length", "-l"
|
||||
help = "The length of the printed sample ids"
|
||||
arg_type = UInt64
|
||||
default = UInt64(5)
|
||||
"--fail-threshold", "-f"
|
||||
help = "NOT IMPLEMENTED: The rate at which similar ids will fail the process"
|
||||
arg_type = Float64
|
||||
range_tester = x -> x >= 0 && x <= 1.0
|
||||
default = 0.99
|
||||
"--to-cual-id"
|
||||
help = "Convert from CualerID to CualID"
|
||||
action = :store_true
|
||||
"ids_to_be_converted"
|
||||
help = "TSV file containing CualIDs or CualerIDs to be converted"
|
||||
arg_type = AbstractPath
|
||||
range_tester = x -> exists(x) && isfile(x)
|
||||
required = true
|
||||
end #@add_arg_table!
|
||||
|
||||
"Path to output file"
|
||||
output_pdf = option("-o", "--output-pdf", path("OUTPUT_FILE"))
|
||||
#! format: on
|
||||
|
||||
"Do not print sample ids on the labels (i.e. barcodes only)"
|
||||
suppress_ids = flag("--suppress-ids")
|
||||
return s
|
||||
|
||||
"""
|
||||
Barcode type selector. This option is present to maintain cli compatibility with
|
||||
cual-id, however it was never implemented in the original program, and as
|
||||
such is not implemented, but instead silently ignored by cualer-id.
|
||||
"""
|
||||
barcode = option("--barcode", str("DUMMY")) |> default("DUMMY")
|
||||
end #struct
|
||||
|
||||
@parser struct Fix
|
||||
@description "Fix typos in lists of CualerIDs"
|
||||
|
||||
"Path to the list of correct ids"
|
||||
correct_ids = option("--correct-ids", path("CORRECT_IDS"))
|
||||
|
||||
"""
|
||||
Which types of fixes should be printed to output. Can be a combination of D
|
||||
for duplicate, F for fixed, N for not fixable, and V for valid.
|
||||
"""
|
||||
show =
|
||||
option("-s", "--show", result_code_option("FIXES")) |>
|
||||
default(ResultCodeOption("DFN"))
|
||||
|
||||
# TODO: Implement the --all switch as an exclusive that returns
|
||||
# a ResultCodeOption("DFNV")
|
||||
|
||||
"File containing CualerIDs that need correction"
|
||||
ids_to_be_corrected = arg(path("FILE"))
|
||||
end #struct
|
||||
|
||||
@parser struct Convert
|
||||
@description "Convert existing CualIDs to CualerIDs (and vice-versa)"
|
||||
|
||||
"The length of the printed sample ids"
|
||||
length = option("-l", "--length", u64("LENGTH")) |> default(0x05)
|
||||
|
||||
"The fraction of ids which must be dissimilar"
|
||||
fail_threshold =
|
||||
option("-f", "--fail-threshold", flt("THRESHOLD"; min = 0.0, max = 1.0)) |>
|
||||
default(0.99)
|
||||
|
||||
"Convert from CualerID to CualID"
|
||||
to_cual_id = flag("--to-cual-id")
|
||||
|
||||
"TSV file containing CualIDs or CualerIDs to be converted"
|
||||
ids_to_be_converted = arg(path("SAMPLE_IDS"))
|
||||
end #struct
|
||||
|
||||
const CLI = or(
|
||||
command(
|
||||
"create",
|
||||
or(command("ids", lift(CreateIDs)), command("labels", lift(CreateLabels))),
|
||||
),
|
||||
command("fix", lift(Fix)),
|
||||
command("convert", lift(Convert)),
|
||||
)
|
||||
|
||||
function (@main)(args)
|
||||
opts = runparse(CLI, args; progname="cualer-id")
|
||||
|
||||
# Exit early in case of a help command
|
||||
isnothing(opts) && return 0
|
||||
|
||||
return _main(opts)
|
||||
end #function
|
||||
|
||||
function _main(opts::CreateIDs)
|
||||
ids = mint_ids!(opts.number_of_ids, opts.length, opts.fail_threshold)
|
||||
const ARG_PARSE_TABLE = _create_arg_table()
|
||||
|
||||
foreach(i -> println(i, '\t', cualer_id(i; length = opts.length)), ids)
|
||||
function _create(parsed_args::Dict{Symbol,Any})
|
||||
if parsed_args[:_COMMAND_] == :ids
|
||||
_create_ids(parsed_args[:ids])
|
||||
elseif parsed_args[:_COMMAND_] == :labels
|
||||
_create_labels(parsed_args[:labels])
|
||||
end #if
|
||||
|
||||
return 0
|
||||
end #function
|
||||
|
||||
function _main(opts)
|
||||
@show opts
|
||||
@assert false
|
||||
function _create_ids(parsed_args::Dict{Symbol,Any})
|
||||
println("COMMAND:\tcreate ids")
|
||||
for (key, val) in parsed_args
|
||||
println("\t$key:\t$val")
|
||||
end #function
|
||||
|
||||
return 0
|
||||
end #function
|
||||
|
||||
function _create_labels(parsed_args::Dict{Symbol,Any})
|
||||
println("COMMAND:\tcreate labels")
|
||||
for (key, val) in parsed_args
|
||||
println("\t$key:\t$val")
|
||||
end #function
|
||||
|
||||
return 0
|
||||
end #function
|
||||
|
||||
function _fix(parsed_args::Dict{Symbol,Any})
|
||||
println("COMMAND:\tfix")
|
||||
for (key, val) in parsed_args
|
||||
println("\t$key:\t$val")
|
||||
end #function
|
||||
|
||||
return 0
|
||||
end #function
|
||||
|
||||
function _convert(parsed_args::Dict{Symbol,Any})
|
||||
println("COMMAND:\tconvert")
|
||||
for (key, val) in parsed_args
|
||||
println("\t$key:\t$val")
|
||||
end #function
|
||||
|
||||
return 0
|
||||
end #function
|
||||
|
||||
|
||||
function (@main)()
|
||||
parsed_args = parse_args(ARG_PARSE_TABLE; as_symbols = true)
|
||||
|
||||
if parsed_args[:_COMMAND_] == :create
|
||||
_create(parsed_args[:create])
|
||||
elseif parsed_args[:_COMMAND_] == :fix
|
||||
_fix(parsed_args[:fix])
|
||||
elseif parsed_args[:_COMMAND_] == :convert
|
||||
_convert(parsed_args[:convert])
|
||||
end #if
|
||||
|
||||
return 0
|
||||
end #function
|
||||
|
||||
end #module
|
||||
|
|
|
|||
|
|
@ -9,102 +9,3 @@ function _hamming(str1::AbstractString, str2::AbstractString)
|
|||
throw(DimensionMismatch("strings must be of equal length"))
|
||||
return sum(c -> first(c) != last(c), zip(str1, str2))
|
||||
end #function
|
||||
|
||||
"""
|
||||
cualer_id(u::UUID; length::Integer = 29)
|
||||
|
||||
Convert `u` to a base30 CualerID, truncating all but the last `length` digits as
|
||||
the human-readable representation of `u`
|
||||
"""
|
||||
function cualer_id(u::UUID; length::Integer = 29)
|
||||
length > 29 &&
|
||||
throw(BoundsError("CualerIDs cannot be printed at lengths longer than 29"))
|
||||
return base30encode(u; pad = length)[(end-(length-1)):end]
|
||||
end #function
|
||||
|
||||
"""
|
||||
mint_ids!(
|
||||
[existing_ids::Vector{UUID}],
|
||||
desired_total::Integer,
|
||||
desired_length::Integer,
|
||||
failure_threshold::AbstractFloat,
|
||||
)
|
||||
|
||||
Creates `desired_total` number of `UUID`s that are valid CualerIDs at a `desired_length`
|
||||
|
||||
# Arguments
|
||||
- `existing_ids::Vector{UUID}`: A set of existing CualerIDs that have already been
|
||||
validated. The default is an empty array, i.e. construct an entirely new set of CualerIDs.
|
||||
- `desired_total::Integer`: The length to grow `existing_ids` to
|
||||
- `desired_length::Integer`: The length of the CualerID string that is compared to check for
|
||||
conflicts
|
||||
- `failure_threshold::AbstractFloat`: The fraction of non-conflicting values (between 0 and
|
||||
1) that is required
|
||||
"""
|
||||
function mint_ids!(
|
||||
existing_ids::Vector{UUID},
|
||||
desired_total::Integer,
|
||||
desired_length::Integer,
|
||||
failure_threshold::AbstractFloat,
|
||||
)
|
||||
# Exception to my recent thirst for "parse, don't validate"
|
||||
# We could technically make failure_threshold a `Normed{T,f}` value from
|
||||
# FixedPointNumbers.jl, but that would
|
||||
# a) Require taking a dependency on FixedPointNumbers.jl
|
||||
# b) Require all dependents to also depend on FixedPointNumbers.jl in order to
|
||||
# properly call this function
|
||||
if failure_threshold < 0 || failure_threshold > 1
|
||||
throw(
|
||||
DomainError(
|
||||
failure_threshold,
|
||||
"`failure_threshold` must be between 0.0 and 1.0",
|
||||
),
|
||||
)
|
||||
end #if
|
||||
|
||||
# Strategy to grow the vector:
|
||||
# a) Check how many elements are needed
|
||||
# b) Add unvalidated elements
|
||||
# c) Validate new elements against all elements up to itself
|
||||
# d) Delete invalid elements
|
||||
# e) Repeat
|
||||
existing_length = length(existing_ids)
|
||||
needed_to_fill = desired_total - existing_length
|
||||
failed_ids = Int[]
|
||||
|
||||
push!(existing_ids, [uuid4() for i = 1:needed_to_fill]...)
|
||||
|
||||
for i = (existing_length+1):desired_total
|
||||
for j = 1:(i-1)
|
||||
if _hamming(
|
||||
cualer_id(existing_ids[i]; length = desired_length),
|
||||
cualer_id(existing_ids[j], length = desired_length),
|
||||
) < 3
|
||||
push!(failed_ids, i)
|
||||
@debug """
|
||||
Conflict found between
|
||||
$(existing_ids[i]) ($(cualer_id(existing_ids[i])))
|
||||
and
|
||||
$(existing_ids[j]) ($(cualer_id(existing_ids[i])))
|
||||
"""
|
||||
break
|
||||
end #if
|
||||
end #for
|
||||
end #for
|
||||
|
||||
if 1 - length(failed_ids) / desired_total > failure_threshold
|
||||
return existing_ids
|
||||
else
|
||||
return mint_ids!(existing_ids, desired_total, desired_length, failure_threshold)
|
||||
end #if
|
||||
end #function
|
||||
|
||||
|
||||
function mint_ids!(
|
||||
desired_total::Integer,
|
||||
desired_length::Integer,
|
||||
failure_threshold::AbstractFloat,
|
||||
)
|
||||
return mint_ids!(UUID[], desired_total, desired_length, failure_threshold)
|
||||
|
||||
end #function
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ function Base.show(io::IO, r::ResultCodeOption)
|
|||
c -> r.val & UInt8(c) != 0 && print(io, string(Symbol(c))),
|
||||
instances(ResultCode),
|
||||
)
|
||||
return print(io, ")")
|
||||
print(io, ")")
|
||||
end #function
|
||||
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
@enum ResultCodeOptionErrCode::UInt8 begin
|
||||
RESULTCODEOPTION_InvalidOption
|
||||
RESULTCODEOPTION_NoOptions
|
||||
end #@enum
|
||||
|
||||
struct ResultCodeOptionError <: OptParse.AbstractParseError
|
||||
code::ResultCodeOptionErrCode
|
||||
token::String
|
||||
end #struct
|
||||
|
||||
function OptParse.render_error(io::IO, err::ResultCodeOptionError)
|
||||
return if err.code == RESULTCODEOPTION_InvalidOption
|
||||
print(io, "Expected valid ResultCode (D, F, N, or V), got $(err.token)")
|
||||
elseif err.code == RESULTCODEOPTION_NoOptions
|
||||
print(io, "Must include one ResultCode")
|
||||
else
|
||||
@assert false "unreachable"
|
||||
end #if
|
||||
end #function
|
||||
|
||||
@kwdef struct ResultCodeOptionVal{T} <:
|
||||
OptParse.AbstractValueParser{T,ResultCodeOptionError}
|
||||
metavar::String = "RESULT_CODES"
|
||||
end #struct
|
||||
|
||||
(rco::ResultCodeOptionVal{T})(input::AbstractString) where {T} =
|
||||
let
|
||||
val = uppercase(input)
|
||||
occursin(r"[^DFNV]", val) && return OptParse.Result{T,ResultCodeOptionError}'.Err(
|
||||
ResultCodeOptionError(RESULTCODEOPTION_InvalidOption, input),
|
||||
)
|
||||
|
||||
occursin(r"[DFNV]", val) || return OptParse.Result{T,ResultCodeOptionError}'.Err(
|
||||
ResultCodeOptionError(RESULTCODEOPTION_NoOptions, input),
|
||||
)
|
||||
|
||||
return OptParse.Result{T,ResultCodeOptionError}'.Ok(parse(ResultCodeOption, input))
|
||||
end #let
|
||||
|
||||
result_code_option(::Type{T}; kw...) where {T<:ResultCodeOption} =
|
||||
ResultCodeOptionVal{T}(; kw...)
|
||||
result_code_option(metavar::AbstractString, ::Type{T}; kw...) where {T<:ResultCodeOption} =
|
||||
ResultCodeOptionVal{T}(; metavar = String(metavar), kw...)
|
||||
result_code_option(; kw...) = ResultCodeOptionVal{ResultCodeOption}(; kw...)
|
||||
result_code_option(metavar::AbstractString; kw...) =
|
||||
ResultCodeOptionVal{ResultCodeOption}(; metavar = String(metavar), kw...)
|
||||
Loading…
Add table
Add a link
Reference in a new issue