22 MotifSequenceGenerator
33This module generates random sequences of motifs, under the constrain that the
44sequence has some total length ℓ so that `q - δq ≤ ℓ ≤ q + δq`.
5-
6- The motifs are contained in a vector, and they can be *anything*.
7- The notion of "length" is defined
8- based on the following two functions, which must be provided for the motifs:
9-
10- * `limits(motif)` : Some function that given the `motif` it returns the
11- `(start, fine)` of the the motif in the same units as `q`.
12- Notice that this function establishes a measure of
13- length, which simply is `fine - start`.
14- * `translate(motif, t)` : Some function that given the `motif` it returns a *new*
15- motif which is translated by `t` (either negative or positive), with
16- respect to the same units as `q`.
17-
185All main functionality is given by the function [`random_sequence`](@ref).
196"""
207module MotifSequenceGenerator
@@ -32,22 +19,7 @@ Base.showerror(io::IO, e::DeadEndMotifs) = print(io,
3219" DeadEndMotifs: Couldn't find a proper sequence with $(e. tries) random tries, " *
3320" each with summands up to $(e. summands) (total tailcuts: $(e. tailcut) )." )
3421
35-
36-
37- """
38- random_sequence(motifs::Vector{M}, q, limits, translate, δq = 0; kwargs...)
39- Create a random sequence of motifs of type `M`, under the constraint that the
40- sequence has "length" ℓ **exactly** within `q - δq ≤ ℓ ≤ q + δq`.
41- Return the sequence itself as well as the
42- sequence of indices of `motifs` used to create it.
43-
44- "length" here means an abstracted length defined by the struct `M`,
45- based on the `limits` and `translate` functions.
46- It does **not** refer to the amount of elements!
47- Please see [`MotifSequenceGenerator`](@ref) for defining `limits`
48- and `translate`.
49-
50- ## Description & Keywords
22+ #= Algorithm description
5123The algorithm works as follows: First a random sequence of motifs is created,
5224so that it has length of `q - δq ≤ ℓ ≤ q - δq + maximum(motiflengths)`.
5325The possible tries of random sequences is set by the `tries` keyword (default `5`).
@@ -71,6 +43,34 @@ skyrockets for large `summands`!).
7143
7244If after going though all these combinations of possible sequences we do not find
7345a proper one, an error is thrown.
46+ =#
47+
48+ """
49+ random_sequence(motifs::Vector{M}, q, limits, translate, δq = 0; kwargs...)
50+ Create a random sequence of motifs of type `M`, under the constraint that the
51+ sequence has "length" `ℓ` **exactly** within `q - δq ≤ ℓ ≤ q + δq`.
52+ Return the sequence itself as well as the
53+ sequence of indices of `motifs` used to create it.
54+
55+ "length" here means an abstracted length defined by the struct `M`,
56+ based on the `limits` and `translate` functions.
57+ It does **not** refer to the amount of elements!
58+
59+ `M` can be anything, given the two functions
60+ * `limits(motif)` : Some function that given the `motif` it returns the
61+ `(start, fine)` of the the motif in the same units as `q`.
62+ This function establishes a measure of length, which simply is `fine - start`.
63+ * `translate(motif, t)` : Some function that given the `motif` it returns a *new*
64+ motif which is translated by `t` (either negative or positive), with
65+ respect to the same units as `q`.
66+
67+ ## Keywords
68+ Please see the source code (use `@which`) for a full description of the algorithm.
69+
70+ * `tries = 5` : Up to how many initial random sequences are accepted.
71+ * `taulcut = 2` : Up to how times an element is dropped from the initial guess.
72+ * `summands = 3` : Up to how many motifs may be combined as a sum to
73+ complete a sequence.
7474"""
7575function random_sequence (motifs:: Vector{M} , q,
7676 limits, translate, δq = 0 ;
@@ -84,6 +84,12 @@ function random_sequence(motifs::Vector{M}, q,
8484 " Impossible to make a sequence."
8585 ))
8686
87+ eltype (motiflens) <: AbstractFloat && δq == 0 && throw (ArgumentError (
88+ " Due to finite precision of floating values, it is almost always impossible " *
89+ " to create a sequence of *exact* amount of floating length. Please provide " *
90+ " a δq > 0."
91+ ))
92+
8793 worked = false ; count = 0 ; seq = Int[]
8894 while worked == false
8995 count > tries && throw (DeadEndMotifs (tries, summands, tailcut))
0 commit comments