Skip to main content

lib_tsalign/a_star_aligner/template_switch_distance/
display.rs

1use std::fmt::{Display, Formatter, Result};
2
3use super::{
4    AlignmentType, GapType, Identifier, TemplateSwitchAncestor, TemplateSwitchDescendant,
5    alignment_type::tsm_uncertainty_range::TSMUncertaintyRange,
6    identifier::TemplateSwitchDirection,
7};
8
9impl Display for AlignmentType {
10    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
11        match self {
12            Self::PrimaryInsertion | Self::PrimaryFlankInsertion | Self::SecondaryInsertion => {
13                write!(f, "I")
14            }
15            Self::PrimaryDeletion | Self::PrimaryFlankDeletion | Self::SecondaryDeletion => {
16                write!(f, "D")
17            }
18            Self::PrimarySubstitution
19            | Self::PrimaryFlankSubstitution
20            | Self::SecondarySubstitution => write!(f, "X"),
21            Self::PrimaryMatch | Self::PrimaryFlankMatch | Self::SecondaryMatch => write!(f, "="),
22            Self::TemplateSwitchEntrance {
23                descendant,
24                ancestor,
25                direction,
26                uncertainty_range,
27                first_offset,
28            } => {
29                write!(f, "[TS{descendant}{ancestor}{direction}:")?;
30                if let Some(uncertainty_range) = uncertainty_range {
31                    write!(f, "{uncertainty_range}")?;
32                } else {
33                    write!(f, "[-]:[-]")?;
34                }
35                write!(f, ":{first_offset}:")
36            }
37            Self::TemplateSwitchExit {
38                anti_descendant_gap,
39            } => write!(f, ":{anti_descendant_gap}]"),
40            Self::Root => Ok(()),
41            Self::AlternativeStart { .. } => Ok(()),
42            Self::SecondaryRoot => Ok(()),
43            Self::PrimaryReentry { .. } => Ok(()),
44            Self::PrimaryShortcut {
45                delta_reference,
46                delta_query,
47            } => write!(f, "[PS:R{delta_reference}Q{delta_query}]"),
48        }
49    }
50}
51
52impl Display for GapType {
53    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
54        match self {
55            Self::Insertion => write!(f, "I"),
56            Self::Deletion => write!(f, "D"),
57            Self::None => write!(f, "=/X"),
58        }
59    }
60}
61
62impl Display for TemplateSwitchDescendant {
63    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
64        match self {
65            Self::Reference => write!(f, "R"),
66            Self::Query => write!(f, "Q"),
67        }
68    }
69}
70
71impl Display for TemplateSwitchAncestor {
72    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
73        match self {
74            Self::Reference => write!(f, "R"),
75            Self::Query => write!(f, "Q"),
76        }
77    }
78}
79
80impl Display for TemplateSwitchDirection {
81    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
82        match self {
83            Self::Forward => write!(f, "F"),
84            Self::Reverse => write!(f, "R"),
85        }
86    }
87}
88
89impl Display for TSMUncertaintyRange {
90    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
91        let Self {
92            start_left_shift,
93            start_right_shift,
94            end_left_shift,
95            end_right_shift,
96        } = self;
97        let start_left_shift = -i32::from(*start_left_shift);
98        let end_left_shift = -i32::from(*end_left_shift);
99        write!(
100            f,
101            "[{start_left_shift},{start_right_shift}]:[{end_left_shift},{end_right_shift}]"
102        )
103    }
104}
105
106impl<PrimaryExtraData> Display for Identifier<PrimaryExtraData> {
107    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
108        match self {
109            Self::Root => write!(f, "Root"),
110            Self::AlternativeStart {
111                reference_index,
112                query_index,
113            } => write!(f, "AlternativeStart({reference_index}R, {query_index}Q)"),
114            Self::Primary {
115                reference_index,
116                query_index,
117                flank_index,
118                gap_type,
119                ..
120            } => write!(
121                f,
122                "Primary({reference_index}R, {query_index}Q, {flank_index}F, {gap_type})",
123            ),
124
125            Self::PrimaryReentry {
126                reference_index,
127                query_index,
128                flank_index,
129                gap_type,
130                ..
131            } => write!(
132                f,
133                "PrimaryReentry({reference_index}R, {query_index}Q, {flank_index}F, {gap_type})",
134            ),
135
136            Self::TemplateSwitchEntrance {
137                entrance_reference_index,
138                entrance_query_index,
139                template_switch_descendant,
140                template_switch_ancestor,
141                template_switch_direction,
142                template_switch_first_offset,
143            } => {
144                write!(
145                    f,
146                    "TemplateSwitchEntrance(Ref={entrance_reference_index}, Query={entrance_query_index}, Desc={template_switch_descendant}, Anc={template_switch_ancestor}, Dir={template_switch_direction}, Offset={template_switch_first_offset})",
147                )
148            }
149
150            #[allow(clippy::uninlined_format_args)]
151            Self::Secondary {
152                entrance_reference_index,
153                entrance_query_index,
154                template_switch_descendant,
155                template_switch_ancestor,
156                template_switch_direction,
157                length,
158                descendant_index,
159                ancestor_index,
160                gap_type,
161            } => write!(
162                f,
163                "Secondary({}R, {}Q, {}L, {}D, {}S, {}, {}, {}, {})",
164                entrance_reference_index,
165                entrance_query_index,
166                length,
167                descendant_index,
168                ancestor_index,
169                template_switch_descendant,
170                template_switch_ancestor,
171                template_switch_direction,
172                gap_type,
173            ),
174
175            #[allow(clippy::uninlined_format_args)]
176            Self::TemplateSwitchExit {
177                entrance_reference_index,
178                entrance_query_index,
179                template_switch_descendant,
180                template_switch_ancestor,
181                template_switch_direction,
182                descendant_index,
183                anti_descendant_gap,
184            } => write!(
185                f,
186                "TemplateSwitchExit({}R, {}Q, {}D, {}G, {}, {}, {})",
187                entrance_reference_index,
188                entrance_query_index,
189                descendant_index,
190                anti_descendant_gap,
191                template_switch_descendant,
192                template_switch_ancestor,
193                template_switch_direction,
194            ),
195        }
196    }
197}