use std::ffi::CStr;
use std::mem::MaybeUninit;
use std::os::raw::{c_char, c_int, c_void};
use crate::sequence::{
is_atg, is_gc, is_gtg, is_start, is_stop, is_ttg, mer_ndx, shine_dalgarno_exact,
shine_dalgarno_mm,
};
use crate::types::{
ATG, EDGE_BONUS, EDGE_UPS, GTG, MAX_SAM_OVLP, META_PEN, MIN_EDGE_GENE, MIN_GENE, Mask, Motif,
Node, OPER_DIST, STOP, TTG, Training,
};
use crate::sequence::{calc_mer_bg, max_fr, mer_text};
use crate::sequence::{shine_dalgarno_exact_mask, shine_dalgarno_mm_mask};
#[inline]
unsafe fn fprint(fp: c_int, s: &str) {
let _ = crate::output::write_to_handle(fp, s);
}
#[inline]
unsafe fn cstr(p: *const c_char) -> &'static str {
if p.is_null() {
""
} else {
CStr::from_ptr(p).to_str().unwrap_or("")
}
}
#[inline]
unsafe fn clear_next_node(nodes: *mut Node, nn: c_int) -> *mut Node {
let node = nodes.offset(nn as isize);
std::ptr::write(node, std::mem::zeroed());
node
}
pub fn node_room(nodes: &mut Vec<MaybeUninit<Node>>, slen: c_int) -> *mut Node {
nodes.reserve_exact(slen.max(0) as usize / 8);
nodes.as_mut_ptr() as *mut Node
}
mod build;
mod coding;
mod output;
mod rbs;
mod score;
mod train_nonsd;
mod train_sd;
mod upstream;
pub use build::*;
pub use coding::*;
pub(crate) use coding::{mer_from, mer_table, raw_coding_score_with};
pub use output::*;
pub use rbs::*;
pub(crate) use score::score_starts;
pub use score::*;
pub use train_nonsd::*;
pub use train_sd::*;
pub use upstream::*;
pub(crate) use upstream::{find_best_upstream_motif_with, score_upstream_composition_with};
#[inline(always)]
pub unsafe fn intergenic_mod(n1: *mut Node, n2: *mut Node, tinf: *const Training) -> f64 {
let mut rval: f64 = 0.0;
let mut ovlp: f64 = 0.0;
if ((*n1).strand == 1
&& (*n2).strand == 1
&& ((*n1).ndx + 2 == (*n2).ndx || (*n1).ndx - 1 == (*n2).ndx))
|| ((*n1).strand == -1
&& (*n2).strand == -1
&& ((*n1).ndx + 2 == (*n2).ndx || (*n1).ndx - 1 == (*n2).ndx))
{
if (*n1).strand == 1 && (*n2).rscore < 0.0 {
rval -= (*n2).rscore;
}
if (*n1).strand == -1 && (*n1).rscore < 0.0 {
rval -= (*n1).rscore;
}
if (*n1).strand == 1 && (*n2).uscore < 0.0 {
rval -= (*n2).uscore;
}
if (*n1).strand == -1 && (*n1).uscore < 0.0 {
rval -= (*n1).uscore;
}
}
let dist = ((*n1).ndx - (*n2).ndx).abs();
if (*n1).strand == 1 && (*n2).strand == 1 && (*n1).ndx + 2 >= (*n2).ndx {
ovlp = 1.0;
} else if (*n1).strand == -1 && (*n2).strand == -1 && (*n1).ndx >= (*n2).ndx + 2 {
ovlp = 1.0;
}
if dist > 3 * OPER_DIST || (*n1).strand != (*n2).strand {
rval -= 0.15 * (*tinf).st_wt;
} else if (dist <= OPER_DIST && ovlp == 0.0) || (dist as f64) < 0.25 * OPER_DIST as f64 {
rval += (2.0 - dist as f64 / OPER_DIST as f64) * 0.15 * (*tinf).st_wt;
}
rval
}
pub unsafe fn cross_mask(x: c_int, y: c_int, mlist: *mut Mask, nm: c_int) -> c_int {
for i in 0..nm {
if y < (*mlist.offset(i as isize)).begin || x > (*mlist.offset(i as isize)).end {
continue;
}
return 1;
}
0
}
pub unsafe fn dmin(x: f64, y: f64) -> f64 {
if x < y { x } else { y }
}
pub unsafe fn dmax(x: f64, y: f64) -> f64 {
if x > y { x } else { y }
}
pub unsafe fn compare_nodes(v1: *const c_void, v2: *const c_void) -> c_int {
let n1 = v1 as *const Node;
let n2 = v2 as *const Node;
if (*n1).ndx < (*n2).ndx {
return -1;
}
if (*n1).ndx > (*n2).ndx {
return 1;
}
if (*n1).strand > (*n2).strand {
return -1;
}
if (*n1).strand < (*n2).strand {
return 1;
}
0
}
pub unsafe fn stopcmp_nodes(v1: *const c_void, v2: *const c_void) -> c_int {
let n1 = v1 as *const Node;
let n2 = v2 as *const Node;
if (*n1).stop_val < (*n2).stop_val {
return -1;
}
if (*n1).stop_val > (*n2).stop_val {
return 1;
}
if (*n1).strand > (*n2).strand {
return -1;
}
if (*n1).strand < (*n2).strand {
return 1;
}
if (*n1).ndx < (*n2).ndx {
return -1;
}
if (*n1).ndx > (*n2).ndx {
return 1;
}
0
}