Skip to content

Commit 84b1931

Browse files
committed
ran cargo fmt
1 parent 27d420f commit 84b1931

21 files changed

Lines changed: 354 additions & 261 deletions

File tree

crates/termal-alignment/src/alignment.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
mod permutation;
55

6-
use std::{
7-
fmt,
8-
collections::HashMap
9-
};
6+
use std::{collections::HashMap, fmt};
107

118
use itertools::Itertools;
129

@@ -44,8 +41,7 @@ impl fmt::Display for RefSpecError {
4441
let err_msg = match self {
4542
RefSpecError::MalformedInt(mfi) => format!("Malformed integer {}", mfi),
4643
RefSpecError::ZeroRef => "Ref # must be > 0".to_string(),
47-
RefSpecError::RefTooLarge(max) => format!("Ref # too large (max {})",
48-
max),
44+
RefSpecError::RefTooLarge(max) => format!("Ref # too large (max {})", max),
4945
};
5046
write!(f, "{}", err_msg)
5147
}
@@ -71,7 +67,7 @@ pub struct Alignment {
7167
* example, does not depend on anything but the sequence itself, and could be a field in a
7268
* struct that also contains the sequence and its header. */
7369
pub id_wrt_reference: Vec<f64>, // reference is usually the consensus, but CAN be an aln seq.
74-
// Recompute if ref changes.
70+
// Recompute if ref changes.
7571
// Of course the sequence length is an integer, but using an integer type like u32 would make
7672
// it hard (for me, at least...) to write a function that accepts a Vec of either lengths or
7773
// %IDs. Tried Box, and generics, but the extra work doesn't seem warranted.
@@ -191,7 +187,8 @@ impl Alignment {
191187
}
192188
// Probable change of ref -> Recompute the identities WRT ref
193189
let reference = self.reference();
194-
self.id_wrt_reference = self.sequences
190+
self.id_wrt_reference = self
191+
.sequences
195192
.iter()
196193
.map(|seq| percent_identity(seq, &reference))
197194
.collect();
@@ -351,9 +348,8 @@ fn seq_type(sequence: &str) -> SeqType {
351348
mod tests {
352349
use crate::alignment::{
353350
best_residue, consensus, densities, entropies, entropy, percent_identity, res_count,
354-
seq_len_nogaps, seq_type, to_freq_distrib, Alignment, BestResidue, ResidueCounts,
351+
seq_len_nogaps, seq_type, to_freq_distrib, Alignment, BestResidue, RefSpec, ResidueCounts,
355352
ResidueDistribution, SeqType,
356-
RefSpec,
357353
SeqType::{Nucleic, Protein},
358354
};
359355
use crate::seq::fasta::read_fasta_file;

crates/termal-alignment/src/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ impl fmt::Display for AlignmentError {
2323
impl std::error::Error for AlignmentError {}
2424

2525
impl From<std::io::Error> for AlignmentError {
26-
fn from(e: std::io::Error) -> Self { AlignmentError::Io(e) }
26+
fn from(e: std::io::Error) -> Self {
27+
AlignmentError::Io(e)
28+
}
2729
}

crates/termal-alignment/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Copyright (c) 2025-2026 Thomas Junier
33

44
pub mod alignment;
5-
pub mod seq;
65
pub mod error;
76
pub mod rgb;
7+
pub mod seq;
88

99
pub use alignment::Alignment;

crates/termal-alignment/src/rgb.rs

Lines changed: 113 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ pub struct Rgb {
1111
}
1212

1313
impl Rgb {
14-
1514
// The simplest case is when R, G, and B are known; if the colour is passed as a hex integer,
1615
// then the following constructor can be used instead. This function is marked 'const' so it
1716
// can be used to initialize maps at compile time.
1817
pub const fn from_u32(h: u32) -> Self {
1918
// Ignore bits > 16
2019
Self {
2120
r: ((h >> 16) & 0xFF) as u8,
22-
g: ((h >> 8) & 0xFF) as u8,
23-
b: ( h & 0xFF) as u8,
21+
g: ((h >> 8) & 0xFF) as u8,
22+
b: (h & 0xFF) as u8,
2423
}
2524
}
2625

@@ -30,31 +29,30 @@ impl Rgb {
3029
let s = s.trim();
3130

3231
// Strip common prefixes
33-
let s = s.strip_prefix("0x")
32+
let s = s
33+
.strip_prefix("0x")
3434
.or_else(|| s.strip_prefix("#"))
3535
.unwrap_or(s);
3636

3737
match s.len() {
3838
6 => {
3939
// RRGGBB
40-
let value = u32::from_str_radix(s, 16)
41-
.map_err(|_| "invalid hex color")?;
40+
let value = u32::from_str_radix(s, 16).map_err(|_| "invalid hex color")?;
4241

4342
Ok(Self {
4443
r: ((value >> 16) & 0xFF) as u8,
45-
g: ((value >> 8) & 0xFF) as u8,
46-
b: ( value & 0xFF) as u8,
44+
g: ((value >> 8) & 0xFF) as u8,
45+
b: (value & 0xFF) as u8,
4746
})
4847
}
4948
8 => {
5049
// Assume AARRGGBB, ignore alpha
51-
let value = u32::from_str_radix(s, 16)
52-
.map_err(|_| "invalid hex color")?;
50+
let value = u32::from_str_radix(s, 16).map_err(|_| "invalid hex color")?;
5351

5452
Ok(Self {
5553
r: ((value >> 16) & 0xFF) as u8,
56-
g: ((value >> 8) & 0xFF) as u8,
57-
b: ( value & 0xFF) as u8,
54+
g: ((value >> 8) & 0xFF) as u8,
55+
b: (value & 0xFF) as u8,
5856
})
5957
}
6058
_ => Err("hex color must have 6 or 8 digits"),
@@ -67,22 +65,46 @@ impl Rgb {
6765
}
6866
}
6967

70-
pub const RGB_RED: Rgb = Rgb{r: 255, g: 0, b: 0};
71-
pub const RGB_GRAY: Rgb = Rgb{r: 127, g: 127, b: 127};
72-
pub const RGB_LIGHT_GRAY: Rgb = Rgb{r: 224, g: 224, b: 224};
73-
pub const RGB_WHITE: Rgb = Rgb{r: 255, g: 255, b: 255};
68+
pub const RGB_RED: Rgb = Rgb { r: 255, g: 0, b: 0 };
69+
pub const RGB_GRAY: Rgb = Rgb {
70+
r: 127,
71+
g: 127,
72+
b: 127,
73+
};
74+
pub const RGB_LIGHT_GRAY: Rgb = Rgb {
75+
r: 224,
76+
g: 224,
77+
b: 224,
78+
};
79+
pub const RGB_WHITE: Rgb = Rgb {
80+
r: 255,
81+
g: 255,
82+
b: 255,
83+
};
7484

7585
pub const GAP_COLOR: Rgb = RGB_LIGHT_GRAY;
7686

7787
// In-house colors
78-
pub const TERMAL_ORANGE: Rgb = Rgb {r: 255, g: 165, b: 0};
79-
pub const TERMAL_SALMON: Rgb = Rgb {r: 250, g: 128, b: 114};
88+
pub const TERMAL_ORANGE: Rgb = Rgb {
89+
r: 255,
90+
g: 165,
91+
b: 0,
92+
};
93+
pub const TERMAL_SALMON: Rgb = Rgb {
94+
r: 250,
95+
g: 128,
96+
b: 114,
97+
};
8098

8199
// ASCII 8-Color palette colors
82-
pub const ASCII_8_COLOR_GREEN: Rgb = Rgb { r: 0, g: 128, b: 0 };
83-
pub const ASCII_8_COLOR_MAGENTA: Rgb = Rgb { r: 128, g: 0, b: 128 };
84-
pub const ASCII_8_COLOR_RED: Rgb = Rgb { r: 128, g: 0, b: 0 };
85-
pub const ASCII_8_COLOR_BLUE: Rgb = Rgb { r: 0, g: 0, b: 128 };
100+
pub const ASCII_8_COLOR_GREEN: Rgb = Rgb { r: 0, g: 128, b: 0 };
101+
pub const ASCII_8_COLOR_MAGENTA: Rgb = Rgb {
102+
r: 128,
103+
g: 0,
104+
b: 128,
105+
};
106+
pub const ASCII_8_COLOR_RED: Rgb = Rgb { r: 128, g: 0, b: 0 };
107+
pub const ASCII_8_COLOR_BLUE: Rgb = Rgb { r: 0, g: 0, b: 128 };
86108

87109
// Lesk aa colors (source?)
88110
pub const LESK_ORANGE: Rgb = TERMAL_ORANGE;
@@ -93,14 +115,46 @@ pub const LESK_RED: Rgb = ASCII_8_COLOR_RED;
93115

94116
// ClustalX aa colors (source:
95117
// https://www.cgl.ucsf.edu/chimera/1.2065/docs/ContributedSoftware/multalignviewer/colprot.par)
96-
pub const CLUSTALX_RED: Rgb = Rgb{r: 229, g: 51, b: 25};
97-
pub const CLUSTALX_BLUE: Rgb = Rgb{r: 25, g: 127, b: 229};
98-
pub const CLUSTALX_GREEN: Rgb = Rgb{r: 25, g: 204, b: 25};
99-
pub const CLUSTALX_CYAN: Rgb = Rgb{r: 25, g: 178, b: 178};
100-
pub const CLUSTALX_PINK: Rgb = Rgb{r: 229, g: 127, b: 127};
101-
pub const CLUSTALX_MAGENTA: Rgb = Rgb{r: 204, g: 76, b: 204};
102-
pub const CLUSTALX_YELLOW: Rgb = Rgb{r: 204, g: 204, b: 0};
103-
pub const CLUSTALX_ORANGE: Rgb = Rgb{r: 229, g: 153, b: 76};
118+
pub const CLUSTALX_RED: Rgb = Rgb {
119+
r: 229,
120+
g: 51,
121+
b: 25,
122+
};
123+
pub const CLUSTALX_BLUE: Rgb = Rgb {
124+
r: 25,
125+
g: 127,
126+
b: 229,
127+
};
128+
pub const CLUSTALX_GREEN: Rgb = Rgb {
129+
r: 25,
130+
g: 204,
131+
b: 25,
132+
};
133+
pub const CLUSTALX_CYAN: Rgb = Rgb {
134+
r: 25,
135+
g: 178,
136+
b: 178,
137+
};
138+
pub const CLUSTALX_PINK: Rgb = Rgb {
139+
r: 229,
140+
g: 127,
141+
b: 127,
142+
};
143+
pub const CLUSTALX_MAGENTA: Rgb = Rgb {
144+
r: 204,
145+
g: 76,
146+
b: 204,
147+
};
148+
pub const CLUSTALX_YELLOW: Rgb = Rgb {
149+
r: 204,
150+
g: 204,
151+
b: 0,
152+
};
153+
pub const CLUSTALX_ORANGE: Rgb = Rgb {
154+
r: 229,
155+
g: 153,
156+
b: 76,
157+
};
104158

105159
// JalView Nucleotide Colors
106160

@@ -138,7 +192,6 @@ pub struct ResidueColorMap {
138192
}
139193

140194
impl ResidueColorMap {
141-
142195
pub fn with_default(gap_color: Rgb, default: Rgb) -> Self {
143196
let mut tbl = [default; 256];
144197
tbl[b'-' as usize] = gap_color;
@@ -148,7 +201,7 @@ impl ResidueColorMap {
148201

149202
pub fn by_name(name: ColorMapName) -> Self {
150203
match name {
151-
ColorMapName::AALesk => Self::aa_lesk(),
204+
ColorMapName::AALesk => Self::aa_lesk(),
152205
ColorMapName::AAClustalX => Self::aa_clustalx(),
153206
ColorMapName::DNAJalView => Self::dna_jalview(),
154207
ColorMapName::Monochrome => Self::monochrome(),
@@ -169,16 +222,29 @@ impl ResidueColorMap {
169222
self.set(b.to_ascii_uppercase(), color);
170223
}
171224

172-
173225
pub fn monochrome() -> Self {
174226
Self::with_default(GAP_COLOR, RGB_WHITE)
175227
}
176228

177229
pub fn dna_basic() -> Self {
178-
let mut map = Self::with_default(GAP_COLOR, Rgb { r: 160, g: 160, b: 160 });
230+
let mut map = Self::with_default(
231+
GAP_COLOR,
232+
Rgb {
233+
r: 160,
234+
g: 160,
235+
b: 160,
236+
},
237+
);
179238
map.set_pair(b'A', Rgb { r: 0, g: 200, b: 0 });
180239
map.set_pair(b'T', Rgb { r: 200, g: 0, b: 0 });
181-
map.set_pair(b'G', Rgb { r: 255, g: 165, b: 0 });
240+
map.set_pair(
241+
b'G',
242+
Rgb {
243+
r: 255,
244+
g: 165,
245+
b: 0,
246+
},
247+
);
182248
map.set_pair(b'C', Rgb { r: 0, g: 0, b: 200 });
183249
map
184250
}
@@ -256,14 +322,13 @@ impl ResidueColorMap {
256322
map.set_pair(b'X', RGB_WHITE);
257323
map
258324
}
259-
260325
}
261326

262327
impl Display for ResidueColorMap {
263-
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
328+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
264329
write!(f, "{{")?;
265330
let residues = "ABCDEFGHIKLMNPQRSTUVWXY";
266-
for c in residues.as_bytes().iter() {
331+
for c in residues.as_bytes().iter() {
267332
write!(f, "{}: {}, ", *c as char, self.rgb(*c).to_hex())?;
268333
}
269334
write!(f, "}}")?;
@@ -274,14 +339,7 @@ impl Display for ResidueColorMap {
274339
#[cfg(test)]
275340
mod test {
276341

277-
use super::{
278-
CLUSTALX_MAGENTA,
279-
ColorMapName,
280-
GAP_COLOR,
281-
RGB_RED,
282-
ResidueColorMap,
283-
Rgb,
284-
};
342+
use super::{ColorMapName, ResidueColorMap, Rgb, CLUSTALX_MAGENTA, GAP_COLOR, RGB_RED};
285343

286344
#[test]
287345
fn test_default_colormap() {
@@ -292,8 +350,15 @@ mod test {
292350
#[test]
293351
fn test_simple_colormap() {
294352
let cmap = ResidueColorMap::dna_basic();
295-
assert_eq!(Rgb{r:160, g:160, b:160}, cmap.rgb(b'%'));
296-
assert_eq!(Rgb{r:0, g:200, b:0}, cmap.rgb(b'A'));
353+
assert_eq!(
354+
Rgb {
355+
r: 160,
356+
g: 160,
357+
b: 160
358+
},
359+
cmap.rgb(b'%')
360+
);
361+
assert_eq!(Rgb { r: 0, g: 200, b: 0 }, cmap.rgb(b'A'));
297362
}
298363

299364
#[test]
@@ -321,5 +386,4 @@ mod test {
321386
assert_eq!(GAP_COLOR, cmap.rgb(b'-'));
322387
assert_eq!(GAP_COLOR, cmap.rgb(b'.'));
323388
}
324-
325389
}

crates/termal-alignment/src/seq/stockholm.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ pub fn read_stockholm_file<P: AsRef<Path>>(path: P) -> Result<SeqFile, Alignment
3333
};
3434
result.push(record);
3535
}
36-
_ => return Err(AlignmentError::InvalidFormat{
37-
msg: String::from("Expected two fields")
38-
}),
36+
_ => {
37+
return Err(AlignmentError::InvalidFormat {
38+
msg: String::from("Expected two fields"),
39+
})
40+
}
3941
}
4042
}
4143
}

crates/termal-export/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ mod svg;
55

66
//use termal_alignment::{Alignment, Region};
77
// NOTE: Regions will be implemented later
8-
use termal_alignment::{
9-
Alignment,
10-
rgb::ResidueColorMap,
11-
};
8+
use termal_alignment::{rgb::ResidueColorMap, Alignment};
129

1310
pub use svg::export_svg;
1411

@@ -38,7 +35,7 @@ pub fn compute_layout(aln: &Alignment, opts: &ExportOpts) -> Layout {
3835
let max_hdr_len = aln.headers.iter().map(|h| h.len()).max().unwrap_or(0);
3936
// + 1: add 1 char's width of space between headers and sequences.
4037
let hdr_txt_width = max_hdr_len as f32 * opts.char_width + GUTTER_WIDTH;
41-
38+
4239
Layout {
4340
grid_width: hdr_txt_width + aln.aln_len() as f32 * opts.cell_width,
4441
grid_height: aln.num_seq() as f32 * opts.cell_height,

0 commit comments

Comments
 (0)