1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::io::{Result, Write};

use crate::{
    property::{Named, Parsable, RegionCore, Scored, Serializable, Stranded},
    ChrRef,
};

use super::{Bed3, ToSelfContained};

#[derive(Clone)]
pub enum RcCowString<'a> {
    Borrowed(&'a str),
    RcOwned(Rc<String>),
}

impl <'a> Deref for RcCowString<'a> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        match self {
            RcCowString::Borrowed(val) => val,
            RcCowString::RcOwned(rc_val) => rc_val.as_str(),
        }
    }
}

impl <'a> PartialEq for RcCowString<'a> {
    fn eq(&self, other: &Self) -> bool {
        str::eq(self.deref(), other.deref())
    }
}

impl <'a> PartialOrd for RcCowString<'a> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        str::partial_cmp(self.as_ref(), other.as_ref())
    }
}

impl <'a> Eq for RcCowString<'a> {}

impl <'a> Ord for RcCowString<'a> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.deref().cmp(other)
    }
}

#[derive(Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct Bed4<'a> {
    inner: Bed3,
    pub name: RcCowString<'a>,
}

impl<'a> Deref for Bed4<'a> {
    type Target = Bed3;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<'a> DerefMut for Bed4<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<'a> Serializable for Bed4<'a> {
    fn dump<W: Write>(&self, mut fp: W) -> Result<()> {
        self.inner.dump(&mut fp)?;
        write!(fp, "\t{}", self.name.deref())?;
        Ok(())
    }
}

impl<'a> Serializable for Option<Bed4<'a>> {
    fn dump<W: Write>(&self, mut fp: W) -> Result<()> {
        if let Some(inner) = self {
            inner.dump(fp)
        } else {
            fp.write_all(b".\t.\t.\t.")
        }
    }
}

impl<'a> Parsable<'a> for Bed4<'a> {
    fn parse(s: &'a str) -> Option<(Self, usize)> {
        let (inner, mut start) = Bed3::parse(s)?;
        if s[start..].starts_with('\t') {
            start += 1;
        }
        let s = &s[start..];
        let brk = memchr::memchr(b'\t', s.as_bytes()).unwrap_or(s.len());
        let name = RcCowString::Borrowed(&s[..brk]);
        Some((Self { inner, name }, start + brk))
    }
}

impl<'a> Bed4<'a> {
    pub fn new<T: RegionCore + Named<'a>>(region: &T) -> Self {
        let name = region.to_cow();
        let inner = Bed3::new(&region);
        Self { inner, name }
    }

    #[inline(always)]
    pub fn set_name(&mut self, name: &'a str) {
        self.name = RcCowString::Borrowed(name);
    }

    pub fn get_self_contained_name(&self) -> RcCowString<'static> {
        match &self.name {
            RcCowString::Borrowed(name) => RcCowString::RcOwned(Rc::new(name.to_string())),
            RcCowString::RcOwned(rc_name) => RcCowString::RcOwned(rc_name.clone())
        }
    }
}

impl<'a> RegionCore for Bed4<'a> {
    #[inline(always)]
    fn start(&self) -> u32 {
        self.inner.start()
    }
    #[inline(always)]
    fn end(&self) -> u32 {
        self.inner.end()
    }
    #[inline(always)]
    fn chrom(&self) -> ChrRef<'static> {
        self.inner.chrom()
    }
}

impl<'a> Scored<f64> for Bed4<'a> {
    #[inline(always)]
    fn score(&self) -> Option<f64> {
        Default::default()
    }
}

impl<'a> Stranded for Bed4<'a> {}

impl<'a> Named<'a> for Bed4<'a> {
    fn name(&self) -> &str {
        self.name.as_ref()
    }
    fn to_cow(&self) -> RcCowString<'a> {
        match &self.name {
            RcCowString::Borrowed(name) => RcCowString::Borrowed(name),
            RcCowString::RcOwned(name) => RcCowString::RcOwned(name.clone()),
        }
    }
}

impl<'a> AsRef<Bed3> for Bed4<'a> {
    fn as_ref(&self) -> &Bed3 {
        &self.inner
    }
}

impl <'a> ToSelfContained for Bed4<'a> {
    type SelfContained = Bed4<'static>;
    fn to_self_contained(&self) -> Self::SelfContained {
        Bed4 {
            inner: self.inner,
            name: self.get_self_contained_name(),
        }
    }
}