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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! Pairs of intervals that match contiguously.
use omics::coordinate;
use omics::coordinate::interbase::Coordinate;
use omics::coordinate::interval::interbase::Interval;
use thiserror::Error;
/// An error related to constructing a contiguous interval pair.
#[derive(Debug, Error)]
pub enum Error {
/// The two intervals don't have the same size. As such, they can't
/// contiguously map to one another.
#[error(
"reference interval entity count ({0}) doesn't match query interval entity count ({1})"
)]
EntityCountsDontMatch(u64, u64),
/// An interval error.
#[error("interval error: {0}")]
Interval(coordinate::interval::Error),
}
/// A utility struct which contains a linearly mapped segment of both the
/// reference and the query sequence.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContiguousIntervalPair(Interval, Interval);
impl ContiguousIntervalPair {
/// Attempts to create a new [`ContiguousIntervalPair`] from a reference
/// interval and a query interval.
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::interval::interbase::Interval;
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:0-1000".parse::<Interval>()?;
/// ContiguousIntervalPair::try_new(reference, query)?;
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn try_new(reference: Interval, query: Interval) -> Result<Self, Error> {
if reference.count_entities() != query.count_entities() {
return Err(Error::EntityCountsDontMatch(
reference.count_entities(),
query.count_entities(),
));
}
Ok(Self(reference, query))
}
/// Gets the reference interval by reference for the interval pair.
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::interval::interbase::Interval;
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:0-1000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference.clone(), query)?;
///
/// assert_eq!(pair.reference(), &reference);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn reference(&self) -> &Interval {
&self.0
}
/// Consumes `self` and gets the reference interval for the interval pair.
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::interval::interbase::Interval;
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:0-1000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference.clone(), query)?;
///
/// assert_eq!(pair.into_reference(), reference);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn into_reference(self) -> Interval {
self.0
}
/// Gets the query interval by reference for the interval pair.
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::interval::interbase::Interval;
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:0-1000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference, query.clone())?;
///
/// assert_eq!(pair.query(), &query);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn query(&self) -> &Interval {
&self.1
}
/// Consumes `self` and returns the query interval for the interval pair.
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::interval::interbase::Interval;
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:0-1000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference, query.clone())?;
///
/// assert_eq!(pair.into_query(), query);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn into_query(self) -> Interval {
self.1
}
/// Consumes `self` and returns the consituent contiguous intervals that
/// make up the [`ContiguousIntervalPair`].
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::interval::interbase::Interval;
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:0-1000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference.clone(), query.clone())?;
///
/// let (a, b) = pair.into_parts();
/// assert_eq!(a, reference);
/// assert_eq!(b, query);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn into_parts(self) -> (Interval, Interval) {
(self.0, self.1)
}
/// Lifts over a coordinate within the reference coordinate system to the
/// query coordinate system.
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::Contig;
/// use omics::coordinate::Strand;
/// use omics::coordinate::interbase::Coordinate;
/// use omics::coordinate::interval::interbase::Interval;
///
/// // Positive-stranded to positive-stranded
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:1000-2000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference, query)?;
///
/// let old = Coordinate::new(Contig::new_unchecked("seq0"), Strand::Positive, 50u64);
/// let new = Coordinate::new(Contig::new_unchecked("seq1"), Strand::Positive, 1050u64);
/// let lifted = pair.liftover(&old).unwrap();
///
/// assert_eq!(new, lifted);
///
/// // Positive-stranded to negative-stranded
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:-:2000-1000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference, query)?;
///
/// let old = Coordinate::new(Contig::new_unchecked("seq0"), Strand::Positive, 50u64);
/// let new = Coordinate::new(Contig::new_unchecked("seq1"), Strand::Negative, 1950u64);
/// let lifted = pair.liftover(&old).unwrap();
///
/// assert_eq!(new, lifted);
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn liftover(&self, coordinate: &Coordinate) -> Option<Coordinate> {
let offset = self.reference().coordinate_offset(coordinate)?;
self.query().coordinate_at_offset(offset)
}
/// Consumes self to clamp an interval pair to the specified `interval` for
/// the reference interval. The query interval is similarly lifted over and
/// clamped.
///
/// # Examples
///
/// ```
/// use chainfile::liftover::stepthrough::interval_pair::ContiguousIntervalPair;
/// use omics::coordinate::Contig;
/// use omics::coordinate::Strand;
/// use omics::coordinate::interbase::Coordinate;
/// use omics::coordinate::interval::interbase::Interval;
///
/// // Positive-stranded to positive-stranded
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:+:1000-2000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference, query)?;
///
/// let interval = "seq0:+:50-51".parse::<Interval>()?;
/// let result = pair.clamp(interval)?;
///
/// assert_eq!(
/// result.reference().start(),
/// &Coordinate::new(Contig::new_unchecked("seq0"), Strand::Positive, 50u64)
/// );
/// assert_eq!(
/// result.reference().end(),
/// &Coordinate::new(Contig::new_unchecked("seq0"), Strand::Positive, 51u64)
/// );
/// assert_eq!(
/// result.query().start(),
/// &Coordinate::new(Contig::new_unchecked("seq1"), Strand::Positive, 1050u64)
/// );
/// assert_eq!(
/// result.query().end(),
/// &Coordinate::new(Contig::new_unchecked("seq1"), Strand::Positive, 1051u64)
/// );
///
/// // Positive-stranded to negative-stranded
///
/// let reference = "seq0:+:0-1000".parse::<Interval>()?;
/// let query = "seq1:-:2000-1000".parse::<Interval>()?;
/// let pair = ContiguousIntervalPair::try_new(reference, query)?;
///
/// let interval = "seq0:+:50-51".parse::<Interval>()?;
/// let result = pair.clamp(interval)?;
///
/// assert_eq!(
/// result.reference().start(),
/// &Coordinate::new(Contig::new_unchecked("seq0"), Strand::Positive, 50u64)
/// );
/// assert_eq!(
/// result.reference().end(),
/// &Coordinate::new(Contig::new_unchecked("seq0"), Strand::Positive, 51u64)
/// );
/// assert_eq!(
/// result.query().start(),
/// &Coordinate::new(Contig::new_unchecked("seq1"), Strand::Negative, 1950u64)
/// );
/// assert_eq!(
/// result.query().end(),
/// &Coordinate::new(Contig::new_unchecked("seq1"), Strand::Negative, 1949u64)
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn clamp(self, interval: Interval) -> Result<ContiguousIntervalPair, Error> {
let reference = self
.reference()
.clone()
.clamp(interval)
.map_err(Error::Interval)?;
let query_start = self.liftover(reference.start()).unwrap();
// Note that the position is moved backward with a bounds check because
// we always expect the _end_ of an interval minus one to fall within
// the interval again. I don't feel that the bounds check is strictly
// required since this assumption is trivially known, but we do it here
// nonetheless.
//
// Adding back the one, however, is a completely different matter. Since
// the ending coordinate is not included in the interval, we expect that
// the end plus one will fall outside of the interval any time the end
// coordinate pointed to the end of a [`ContiguousIntervalPair`]. Thus,
// we _must_ do the unchecked `move_forward` method below after
// liftover.
let query_end = self
.liftover(
&reference
.end()
.clone()
.into_move_backward(1)
.filter(|coord| self.reference().contains_coordinate(coord))
.unwrap(),
)
.unwrap()
.into_move_forward(1)
.unwrap();
let query = Interval::try_new(query_start, query_end).unwrap();
ContiguousIntervalPair::try_new(reference, query)
}
}
impl std::fmt::Display for ContiguousIntervalPair {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} -> {}", self.reference(), self.query())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_interval_pair() {
let reference = "seq0:+:0-1000".parse::<Interval>().unwrap();
let query = "seq1:+:1000-2000".parse::<Interval>().unwrap();
ContiguousIntervalPair::try_new(reference, query).unwrap();
}
#[test]
fn interval_sizes_dont_match() {
let reference = "seq0:+:0-1000".parse::<Interval>().unwrap();
let query = "seq1:+:0-20000".parse::<Interval>().unwrap();
let err = ContiguousIntervalPair::try_new(reference, query).unwrap_err();
assert!(matches!(err, Error::EntityCountsDontMatch(_, _)));
assert_eq!(
err.to_string(),
"reference interval entity count (1000) doesn't match query interval entity count \
(20000)"
);
}
}