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
use crate::align::aligners::constants::MIN_SCORE;
use bio::alignment::pairwise::MatchFunc;
use serde::Serialize;
/// Details of scoring are encapsulated in this structure.
///
/// An [affine gap score model](https://en.wikipedia.org/wiki/Gap_penalty#Affine)
/// is used so that the gap score for a length `k` is:
/// `GapScore(k) = gap_open + gap_extend * k
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize)]
pub struct Scoring<F: MatchFunc> {
pub gap_open: i32,
pub gap_extend: i32,
pub jump_score_same_contig_and_strand: i32,
pub jump_score_same_contig_opposite_strand: i32,
pub jump_score_inter_contig: i32,
pub match_fn: F,
pub match_scores: Option<(i32, i32)>,
pub xclip_prefix: i32,
pub xclip_suffix: i32,
pub yclip_prefix: i32,
pub yclip_suffix: i32,
}
impl<F: MatchFunc> Scoring<F> {
/// Create new Scoring instance with given gap open, gap extend penalties
/// and the score function. The clip penalties are set to [`MIN_SCORE`](constant.MIN_SCORE.html) by default
///
/// # Arguments
///
/// * `gap_open` - the score for opening a gap (should not be positive)
/// * `gap_extend` - the score for extending a gap (should not be positive)
/// * `jump_score` - the score for jumping in the query (should not be positive)
/// * `match_fn` - function that returns the score for substitutions
/// (see also [`bio::alignment::pairwise::Scoring`](struct.Scoring.html))
pub fn with_jump_score(gap_open: i32, gap_extend: i32, jump_score: i32, match_fn: F) -> Self {
assert!(gap_open <= 0, "gap_open can't be positive");
assert!(gap_extend <= 0, "gap_extend can't be positive");
assert!(jump_score <= 0, "jump_score can't be positive");
Self::with_jump_scores(
gap_open, gap_extend, jump_score, jump_score, jump_score, match_fn,
)
}
/// Create new Scoring instance with given gap open, gap extend penalties
/// and the score function. The clip penalties are set to [`MIN_SCORE`](constant.MIN_SCORE.html) by default
///
/// # Arguments
///
/// * `gap_open` - the score for opening a gap (should not be positive)
/// * `gap_extend` - the score for extending a gap (should not be positive)
/// * `jump_score_same_contig_and_strand` - the score for jumping to the same contig and strand in the query (should not be positive)
/// * `jump_score_same_contig_opposite_strand` - the score for jumping to the same contig and opposite strand in the query (should not be positive)
/// * `jump_score_inter_contig` - the score for jumping to a different contig in the query (should not be positive)
/// * `match_fn` - function that returns the score for substitutions
/// (see also [`bio::alignment::pairwise::Scoring`](struct.Scoring.html))
pub fn with_jump_scores(
gap_open: i32,
gap_extend: i32,
jump_score_same_contig_and_strand: i32,
jump_score_same_contig_opposite_strand: i32,
jump_score_inter_contig: i32,
match_fn: F,
) -> Self {
assert!(
jump_score_same_contig_and_strand <= 0,
"jump_score_same_contig_and_strand can't be positive"
);
assert!(
jump_score_same_contig_opposite_strand <= 0,
"jump_score_same_contig_opposite_strand can't be positive"
);
assert!(
jump_score_inter_contig <= 0,
"jump_score_inter_contig can't be positive"
);
Self {
gap_open,
gap_extend,
jump_score_same_contig_and_strand,
jump_score_same_contig_opposite_strand,
jump_score_inter_contig,
match_fn,
match_scores: None,
xclip_prefix: MIN_SCORE,
xclip_suffix: MIN_SCORE,
yclip_prefix: MIN_SCORE,
yclip_suffix: MIN_SCORE,
}
}
/// Sets the jump scores to the given value
///
/// # Arguments
///
/// * `jump_score` - Jump score
#[allow(dead_code)]
pub fn set_jump_score(mut self, jump_score: i32) -> Self {
self.jump_score_same_contig_and_strand = jump_score;
self.jump_score_same_contig_opposite_strand = jump_score;
self.jump_score_inter_contig = jump_score;
self
}
/// Sets the jump scores to the given values
#[allow(dead_code)]
pub fn set_jump_scores(
mut self,
jump_score_same_contig_and_strand: i32,
jump_score_same_contig_opposite_strand: i32,
jump_score_inter_contig: i32,
) -> Self {
self.jump_score_same_contig_and_strand = jump_score_same_contig_and_strand;
self.jump_score_same_contig_opposite_strand = jump_score_same_contig_opposite_strand;
self.jump_score_inter_contig = jump_score_inter_contig;
self
}
/// Sets the prefix and suffix clipping penalties for x to the input value
///
/// # Arguments
///
/// * `penalty` - Clipping penalty for x (both prefix and suffix, should not be positive)
///
/// ```rust
/// use bio::alignment::pairwise::{Scoring, MIN_SCORE};
/// let scoring = Scoring::from_scores(0, -2, 1, -2).xclip(-5);
/// assert!(scoring.xclip_prefix == -5);
/// assert!(scoring.yclip_prefix == MIN_SCORE);
/// assert!(scoring.xclip_suffix == -5);
/// assert!(scoring.yclip_suffix == MIN_SCORE);
/// ```
#[allow(dead_code)]
pub fn set_xclip(mut self, penalty: i32) -> Self {
assert!(penalty <= 0, "Clipping penalty can't be positive");
self.xclip_prefix = penalty;
self.xclip_suffix = penalty;
self
}
/// Sets the prefix clipping penalty for x to the input value
///
/// # Arguments
///
/// * `penalty` - Prefix clipping penalty for x (should not be positive)
///
/// # Example
/// ```rust
/// use bio::alignment::pairwise::{Scoring, MIN_SCORE};
/// let scoring = Scoring::from_scores(0, -2, 1, -2).xclip_prefix(-5);
/// assert!(scoring.xclip_prefix == -5);
/// assert!(scoring.yclip_prefix == MIN_SCORE);
/// assert!(scoring.xclip_suffix == MIN_SCORE);
/// assert!(scoring.yclip_suffix == MIN_SCORE);
/// ```
#[allow(dead_code)]
pub fn set_xclip_prefix(mut self, penalty: i32) -> Self {
assert!(penalty <= 0, "Clipping penalty can't be positive");
self.xclip_prefix = penalty;
self
}
/// Sets the suffix clipping penalty for x to the input value
///
/// # Arguments
///
/// * `penalty` - Suffix clipping penalty for x (should not be positive)
///
/// ```rust
/// use bio::alignment::pairwise::{Scoring, MIN_SCORE};
/// let scoring = Scoring::from_scores(0, -2, 1, -2).xclip_suffix(-5);
/// assert!(scoring.xclip_prefix == MIN_SCORE);
/// assert!(scoring.yclip_prefix == MIN_SCORE);
/// assert!(scoring.xclip_suffix == -5);
/// assert!(scoring.yclip_suffix == MIN_SCORE);
/// ```
#[allow(dead_code)]
pub fn set_xclip_suffix(mut self, penalty: i32) -> Self {
assert!(penalty <= 0, "Clipping penalty can't be positive");
self.xclip_suffix = penalty;
self
}
/// Sets the prefix and suffix clipping penalties for y to the input value
///
/// # Arguments
///
/// * `penalty` - Clipping penalty for y (both prefix and suffix, should not be positive)
///
/// ```rust
/// use bio::alignment::pairwise::{Scoring, MIN_SCORE};
/// let scoring = Scoring::from_scores(0, -2, 1, -2).yclip(-5);
/// assert!(scoring.xclip_prefix == MIN_SCORE);
/// assert!(scoring.yclip_prefix == -5);
/// assert!(scoring.xclip_suffix == MIN_SCORE);
/// assert!(scoring.yclip_suffix == -5);
/// ```
#[allow(dead_code)]
pub fn set_yclip(mut self, penalty: i32) -> Self {
assert!(penalty <= 0, "Clipping penalty can't be positive");
self.yclip_prefix = penalty;
self.yclip_suffix = penalty;
self
}
/// Sets the prefix clipping penalty for y to the input value
///
/// # Arguments
///
/// * `penalty` - Prefix clipping penalty for y (should not be positive)
///
/// ```rust
/// use bio::alignment::pairwise::{Scoring, MIN_SCORE};
/// let scoring = Scoring::from_scores(0, -2, 1, -2).yclip_prefix(-5);
/// assert!(scoring.xclip_prefix == MIN_SCORE);
/// assert!(scoring.yclip_prefix == -5);
/// assert!(scoring.xclip_suffix == MIN_SCORE);
/// assert!(scoring.yclip_suffix == MIN_SCORE);
/// ```
#[allow(dead_code)]
pub fn set_yclip_prefix(mut self, penalty: i32) -> Self {
assert!(penalty <= 0, "Clipping penalty can't be positive");
self.yclip_prefix = penalty;
self
}
/// Sets the suffix clipping penalty for y to the input value
///
/// # Arguments
///
/// * `penalty` - Suffix clipping penalty for y (should not be positive)
///
/// ```rust
/// use bio::alignment::pairwise::{Scoring, MIN_SCORE};
/// let scoring = Scoring::from_scores(0, -2, 1, -2).yclip_suffix(-5);
/// assert!(scoring.xclip_prefix == MIN_SCORE);
/// assert!(scoring.yclip_prefix == MIN_SCORE);
/// assert!(scoring.xclip_suffix == MIN_SCORE);
/// assert!(scoring.yclip_suffix == -5);
/// ```
#[allow(dead_code)]
pub fn set_yclip_suffix(mut self, penalty: i32) -> Self {
assert!(penalty <= 0, "Clipping penalty can't be positive");
self.yclip_suffix = penalty;
self
}
}