pub struct GcFragModel { /* private fields */ }Expand description
Implementations§
Source§impl GcFragModel
impl GcFragModel
Sourcepub fn new(cond_bins: usize, gc_bins: usize) -> Self
pub fn new(cond_bins: usize, gc_bins: usize) -> Self
Examples found in repository?
examples/conv_bench.rs (line 59)
33fn main() {
34 let args: Vec<String> = std::env::args().collect();
35 let n: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(100_000);
36 let passes: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(3);
37
38 let mut rng = Lcg(0x9E3779B97F4A7C15);
39
40 // Build a realistic transcript-length distribution: log-normal-ish around
41 // ~1.5 kb with a long tail, clamped to [80, 15000] — close to GRCh38 cDNA.
42 let bases = [b'A', b'C', b'G', b'T'];
43 let mut seqs: Vec<Vec<u8>> = Vec::with_capacity(n);
44 let mut prefixes: Vec<Vec<u32>> = Vec::with_capacity(n);
45 for _ in 0..n {
46 let u = rng.next_f64();
47 // exp of a normal-ish variate -> heavy right tail
48 let len = (7.3 + 0.9 * (u - 0.5) * 4.0).exp() as usize;
49 let len = len.clamp(80, 15_000);
50 let seq: Vec<u8> = (0..len)
51 .map(|_| bases[(rng.next_u32() & 3) as usize])
52 .collect();
53 prefixes.push(gc_prefix(&seq));
54 seqs.push(seq);
55 }
56
57 // A normalized 3×25 GC ratio model (the shape doesn't matter for timing; we
58 // exercise the same binning + lookup the real run does).
59 let mut obs = GcFragModel::new(DEFAULT_COND_BINS, DEFAULT_GC_BINS);
60 let mut exp = GcFragModel::new(DEFAULT_COND_BINS, DEFAULT_GC_BINS);
61 for ctx in 0..=100 {
62 for gc in 0..=100 {
63 obs.inc(gc, ctx, 1.0 + 0.3 * ((gc + ctx) as f64).sin());
64 exp.inc(gc, ctx, 1.0);
65 }
66 }
67 let gc_model = gc_ratio(&mut obs, &mut exp, 1000.0);
68
69 // Fragment-length CDF: gaussian-ish pmf around mean 250, sd 40, over 0..1000.
70 let fld_max = 1000usize;
71 let mean = 250.0f64;
72 let sd = 40.0f64;
73 let pmf: Vec<f64> = (0..=fld_max)
74 .map(|l| {
75 let z = (l as f64 - mean) / sd;
76 (-0.5 * z * z).exp()
77 })
78 .collect();
79 let (cdf, fld_low, fld_high) = salmon_model::seqbias::fld_cdf_and_bounds(&pmf);
80
81 let mut acc = 0.0f64;
82 let mut best = f64::INFINITY;
83 for p in 0..passes {
84 let t = Instant::now();
85 for (seq, prefix) in seqs.iter().zip(&prefixes) {
86 let ref_len = seq.len() as f64;
87 let elen = (ref_len - 200.0).max(1.0); // ensure unprocessed > 0
88 let bias = BiasInputs {
89 seq: None,
90 gc: Some((&gc_model, salmon_model::GcView::Dense(prefix.as_slice()))),
91 pos: None,
92 };
93 acc += corrected_effective_length_full(
94 seq,
95 &cdf,
96 fld_low,
97 fld_high,
98 &bias,
99 elen,
100 GC_SAMP_STRIDE,
101 false,
102 );
103 }
104 let dt = t.elapsed().as_secs_f64();
105 best = best.min(dt);
106 eprintln!(
107 "pass {p}: {:.3}s ({:.2} µs/transcript) acc={}",
108 dt,
109 dt * 1e6 / n as f64,
110 black_box(acc)
111 );
112 }
113 eprintln!(
114 "BEST: {:.3}s over {} transcripts ({:.2} µs/transcript)",
115 best,
116 n,
117 best * 1e6 / n as f64
118 );
119}Sourcepub fn default_model() -> Self
pub fn default_model() -> Self
salmon’s default 3 × 101 model.
Sourcepub fn dump(&self) -> &[f64]
pub fn dump(&self) -> &[f64]
The flattened cond_bins × gc_bins table (counts[ctx * gc_bins + gc],
row-major), for dumping to the aux bias files.
Sourcepub fn inc(&mut self, gc_frac: i32, ctx_frac: i32, weight: f64)
pub fn inc(&mut self, gc_frac: i32, ctx_frac: i32, weight: f64)
Accumulate weight for a fragment with the given GC and context fractions.
Examples found in repository?
examples/conv_bench.rs (line 63)
33fn main() {
34 let args: Vec<String> = std::env::args().collect();
35 let n: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(100_000);
36 let passes: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(3);
37
38 let mut rng = Lcg(0x9E3779B97F4A7C15);
39
40 // Build a realistic transcript-length distribution: log-normal-ish around
41 // ~1.5 kb with a long tail, clamped to [80, 15000] — close to GRCh38 cDNA.
42 let bases = [b'A', b'C', b'G', b'T'];
43 let mut seqs: Vec<Vec<u8>> = Vec::with_capacity(n);
44 let mut prefixes: Vec<Vec<u32>> = Vec::with_capacity(n);
45 for _ in 0..n {
46 let u = rng.next_f64();
47 // exp of a normal-ish variate -> heavy right tail
48 let len = (7.3 + 0.9 * (u - 0.5) * 4.0).exp() as usize;
49 let len = len.clamp(80, 15_000);
50 let seq: Vec<u8> = (0..len)
51 .map(|_| bases[(rng.next_u32() & 3) as usize])
52 .collect();
53 prefixes.push(gc_prefix(&seq));
54 seqs.push(seq);
55 }
56
57 // A normalized 3×25 GC ratio model (the shape doesn't matter for timing; we
58 // exercise the same binning + lookup the real run does).
59 let mut obs = GcFragModel::new(DEFAULT_COND_BINS, DEFAULT_GC_BINS);
60 let mut exp = GcFragModel::new(DEFAULT_COND_BINS, DEFAULT_GC_BINS);
61 for ctx in 0..=100 {
62 for gc in 0..=100 {
63 obs.inc(gc, ctx, 1.0 + 0.3 * ((gc + ctx) as f64).sin());
64 exp.inc(gc, ctx, 1.0);
65 }
66 }
67 let gc_model = gc_ratio(&mut obs, &mut exp, 1000.0);
68
69 // Fragment-length CDF: gaussian-ish pmf around mean 250, sd 40, over 0..1000.
70 let fld_max = 1000usize;
71 let mean = 250.0f64;
72 let sd = 40.0f64;
73 let pmf: Vec<f64> = (0..=fld_max)
74 .map(|l| {
75 let z = (l as f64 - mean) / sd;
76 (-0.5 * z * z).exp()
77 })
78 .collect();
79 let (cdf, fld_low, fld_high) = salmon_model::seqbias::fld_cdf_and_bounds(&pmf);
80
81 let mut acc = 0.0f64;
82 let mut best = f64::INFINITY;
83 for p in 0..passes {
84 let t = Instant::now();
85 for (seq, prefix) in seqs.iter().zip(&prefixes) {
86 let ref_len = seq.len() as f64;
87 let elen = (ref_len - 200.0).max(1.0); // ensure unprocessed > 0
88 let bias = BiasInputs {
89 seq: None,
90 gc: Some((&gc_model, salmon_model::GcView::Dense(prefix.as_slice()))),
91 pos: None,
92 };
93 acc += corrected_effective_length_full(
94 seq,
95 &cdf,
96 fld_low,
97 fld_high,
98 &bias,
99 elen,
100 GC_SAMP_STRIDE,
101 false,
102 );
103 }
104 let dt = t.elapsed().as_secs_f64();
105 best = best.min(dt);
106 eprintln!(
107 "pass {p}: {:.3}s ({:.2} µs/transcript) acc={}",
108 dt,
109 dt * 1e6 / n as f64,
110 black_box(acc)
111 );
112 }
113 eprintln!(
114 "BEST: {:.3}s over {} transcripts ({:.2} µs/transcript)",
115 best,
116 n,
117 best * 1e6 / n as f64
118 );
119}Sourcepub fn get(&self, gc_frac: i32, ctx_frac: i32) -> f64
pub fn get(&self, gc_frac: i32, ctx_frac: i32) -> f64
Value at a (context, GC) cell (a normalized density after normalize,
or a clamped ratio for a model produced by gc_ratio).
Sourcepub fn combine_counts(&mut self, other: &GcFragModel)
pub fn combine_counts(&mut self, other: &GcFragModel)
Merge another (compatible) model’s counts. Both must be pre-normalization.
Trait Implementations§
Source§impl Clone for GcFragModel
impl Clone for GcFragModel
Source§fn clone(&self) -> GcFragModel
fn clone(&self) -> GcFragModel
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for GcFragModel
impl RefUnwindSafe for GcFragModel
impl Send for GcFragModel
impl Sync for GcFragModel
impl Unpin for GcFragModel
impl UnsafeUnpin for GcFragModel
impl UnwindSafe for GcFragModel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.