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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::iter::FromIterator;
use std::str::FromStr;

mod structures;

pub use crate::structures::{
    AnyStructure, CnicStructure, GulyasStructure, NeuromorphoStructure, StructureIdentifier,
    VnedStructure,
};

mod header;
pub use crate::header::Header;

type SampleId = usize;
type Radius = f64;

/// Error in parsing a SWC sample (row).
#[derive(thiserror::Error, Debug)]
pub enum SampleParseError {
    /// A field which should be an integer could not be parsed.
    #[error("Integer field parse error")]
    IntField(#[from] std::num::ParseIntError),
    /// A field which should be a float/ decimal could not be parsed.
    #[error("Float field parse error")]
    FloatField(#[from] std::num::ParseFloatError),
    /// The value of the `structure` field was not recognised.
    /// Not checked for structure enums with a catch-all variant.
    #[error("Reference to unknown structure {0}")]
    UnknownStructure(isize),
    /// Line terminated early; more fields expected.
    #[error("Incorrect number of fields: found {0}")]
    IncorrectNumFields(usize),
}

/// Struct representing a row of a SWC file (a single sample from the neuron),
/// which gives information about a single node in the tree.
///
/// If the `parent_id` is None, this is a root node.
#[derive(Debug, Clone, Copy)]
pub struct SwcSample<S: StructureIdentifier> {
    pub sample_id: SampleId,
    pub structure: S,
    pub x: f64,
    pub y: f64,
    pub z: f64,
    pub radius: Radius,
    pub parent_id: Option<SampleId>,
}

impl<S: StructureIdentifier> FromStr for SwcSample<S> {
    type Err = SampleParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let trimmed = s.trim();
        let mut items = trimmed.split_whitespace();

        let sample_id = items
            .next()
            .ok_or(SampleParseError::IncorrectNumFields(0))?
            .parse()?;

        let struct_int = items
            .next()
            .ok_or(SampleParseError::IncorrectNumFields(1))?
            .parse::<isize>()?;
        let structure =
            S::try_from(struct_int).map_err(|_e| SampleParseError::UnknownStructure(struct_int))?;
        let x = items
            .next()
            .ok_or(SampleParseError::IncorrectNumFields(2))?
            .parse::<f64>()?;
        let y = items
            .next()
            .ok_or(SampleParseError::IncorrectNumFields(3))?
            .parse::<f64>()?;
        let z = items
            .next()
            .ok_or(SampleParseError::IncorrectNumFields(4))?
            .parse::<f64>()?;
        let radius = items
            .next()
            .ok_or(SampleParseError::IncorrectNumFields(5))?
            .parse::<f64>()?;
        let parent_id = match items.next() {
            Some("-1") => None,
            Some(p_str) => Some(p_str.parse()?),
            None => None,
        };

        let count: usize = items.fold(0, |x, _| x + 1);
        if count > 0 {
            return Err(SampleParseError::IncorrectNumFields(7 + count));
        }

        Ok(Self {
            sample_id,
            structure,
            x,
            y,
            z,
            radius,
            parent_id,
        })
    }
}

impl<S: StructureIdentifier> ToString for SwcSample<S> {
    fn to_string(&self) -> String {
        let structure: isize = self.structure.into();
        format!(
            "{} {} {} {} {} {} {}",
            self.sample_id,
            structure,
            self.x,
            self.y,
            self.z,
            self.radius,
            self.parent_id.map_or("-1".to_string(), |p| p.to_string()),
        )
    }
}

impl<S: StructureIdentifier> SwcSample<S> {
    /// Create a new [SwcSample] with  different sample and parent IDs.
    ///
    /// The other features are the same.
    fn with_ids(self, sample: SampleId, parent: Option<SampleId>) -> Self {
        SwcSample {
            sample_id: sample,
            structure: self.structure,
            x: self.x,
            y: self.y,
            z: self.z,
            radius: self.radius,
            parent_id: parent,
        }
    }
}

/// Struct representing a neuron skeleton as a tree of [SwcSample]s.
#[derive(Debug, Clone)]
pub struct SwcNeuron<S: StructureIdentifier, H: Header> {
    pub samples: Vec<SwcSample<S>>,
    pub header: Option<H>,
}

impl<S: StructureIdentifier, H: Header> FromIterator<SwcSample<S>> for SwcNeuron<S, H> {
    fn from_iter<I: IntoIterator<Item = SwcSample<S>>>(iter: I) -> Self {
        SwcNeuron {
            samples: iter.into_iter().collect(),
            header: None,
        }
    }
}

/// Error for a SWC file which cannot be parsed
#[derive(thiserror::Error, Debug)]
pub enum SwcParseError {
    /// A line could not be read from the file.
    #[error("Line read error")]
    Read(#[from] std::io::Error),
    /// The line could not be parsed as a SwcSample.
    #[error("Sample parse error")]
    SampleParse(#[from] SampleParseError),
    /// The SWC header could not be parsed.
    #[error("Header parse error")]
    HeaderParse(String),
}

/// Error where a sample refers to an unknown parent sample.
#[derive(thiserror::Error, Debug)]
#[error("Parent sample {parent_sample} does not exist")]
pub struct MissingSampleError {
    parent_sample: usize,
}

/// Error where a neuron represented by a SWC file is inconsistent.
#[derive(thiserror::Error, Debug)]
pub enum InconsistentNeuronError {
    /// Neuron is not a tree as it has multiple roots (parentless nodes).
    #[error("Neuron has >1 root")]
    MultipleRoots,
    /// Neuron is not a tree as it has several disconnected parts.
    #[error("Neuron has >1 connected component")]
    Disconnected,
    /// Neuron has no root: it could be empty or be cyclic.
    #[error("Neuron has no root")]
    NoRootError,
    /// Samples refer to unknown parent samples.
    #[error("Neuron has missing sample(s)")]
    MissingSample(#[from] MissingSampleError),
    /// Samples have clashing IDs.
    #[error("Neuron has multiple samples numbered {0}")]
    DuplicateSample(SampleId),
}

impl<S: StructureIdentifier, H: Header> SwcNeuron<S, H> {
    /// Sort the neuron's samples by their index.
    pub fn sort_index(mut self) -> Self {
        self.samples
            .sort_unstable_by(|a, b| a.sample_id.cmp(&b.sample_id));
        self
    }

    /// Re-index the neuron's samples in order of occurrence, starting at 1.
    /// Returns error if the SWC is missing a parent sample
    pub fn reindex(self) -> Result<Self, MissingSampleError> {
        let old_to_new: HashMap<SampleId, SampleId> = self
            .samples
            .iter()
            .enumerate()
            .map(|(idx, row)| (row.sample_id, idx + 1))
            .collect();

        let mut samples = Vec::with_capacity(self.samples.len());
        for (idx, row) in self.samples.into_iter().enumerate() {
            let parent;
            if let Some(old) = row.parent_id {
                parent = Some(
                    *old_to_new
                        .get(&old)
                        .ok_or(MissingSampleError { parent_sample: old })?,
                );
            } else {
                parent = None;
            }
            samples.push(row.with_ids(idx + 1, parent));
        }

        Ok(Self {
            samples,
            header: self.header,
        })
    }

    /// Re-order its samples in pre-order depth first search.
    /// Children are visited in the order of their original sample number.
    ///
    /// Returns an error if the neuron is not a self-consistent tree.
    pub fn sort_topo(self, reindex: bool) -> Result<Self, InconsistentNeuronError> {
        let mut parent_to_children: HashMap<SampleId, Vec<SampleId>> =
            HashMap::with_capacity(self.samples.len());
        let mut id_to_sample: HashMap<SampleId, SwcSample<S>> =
            HashMap::with_capacity(self.samples.len());
        let mut root = None;

        for row in self.samples {
            if let Some(p) = row.parent_id {
                let entry = parent_to_children.entry(p).or_default();
                entry.push(row.sample_id);
                if id_to_sample.insert(row.sample_id, row).is_some() {
                    return Err(InconsistentNeuronError::DuplicateSample(row.sample_id));
                }
            } else if root.is_some() {
                return Err(InconsistentNeuronError::MultipleRoots);
            } else {
                root = Some(row);
            }
        }

        let mut samples = Vec::with_capacity(id_to_sample.len() + 1);

        let mut to_visit = vec![];
        let mut next_id: SampleId = 1;

        if let Some(r) = root {
            let children = parent_to_children.remove(&r.sample_id).map_or_else(
                || Vec::with_capacity(0),
                |mut v| {
                    v.sort_unstable_by_key(|&x| Reverse(x));
                    v
                },
            );
            if reindex {
                samples.push(r.with_ids(next_id, None));
                to_visit.extend(children.into_iter().map(|c| (next_id, c)));
                next_id += 1;
            } else {
                samples.push(r);
                to_visit.extend(children.into_iter().map(|c| (r.sample_id, c)));
            }
        } else {
            return Err(InconsistentNeuronError::NoRootError);
        }

        while let Some((parent_id, row_id)) = to_visit.pop() {
            let row = id_to_sample
                .remove(&row_id)
                .expect("Just constructed this vec");
            let children = parent_to_children.remove(&row.sample_id).map_or_else(
                || Vec::with_capacity(0),
                |mut v| {
                    v.sort_unstable_by_key(|&id| Reverse(id));
                    v
                },
            );
            if reindex {
                samples.push(row.with_ids(next_id, Some(parent_id)));
                to_visit.extend(children.into_iter().map(|c| (next_id, c)));
                next_id += 1;
            } else {
                samples.push(row);
                to_visit.extend(children.into_iter().map(|c| (row.sample_id, c)));
            }
        }

        if id_to_sample.is_empty() {
            Ok(SwcNeuron {
                samples,
                header: self.header,
            })
        } else {
            Err(InconsistentNeuronError::Disconnected)
        }
        // todo: possible MissingSampleError
    }

    /// Ensure that [SwcNeuron] is a self-consistent tree.
    ///
    /// If `ignore_order` is `true`, samples' parents must be defined before they are.
    pub fn validate(&self, ignore_order: bool) -> Result<(), InconsistentNeuronError> {
        use InconsistentNeuronError::*;

        // use sample IDs
        let mut parent_to_children: HashMap<SampleId, Vec<SampleId>> =
            HashMap::with_capacity(self.samples.len());
        let mut sample_ids: HashSet<usize> = HashSet::with_capacity(self.samples.len());
        let mut root = None;

        for row in self.samples.iter() {
            if !sample_ids.insert(row.sample_id) {
                return Err(DuplicateSample(row.sample_id));
            }
            if let Some(p) = row.parent_id {
                if !ignore_order && !sample_ids.contains(&p) {
                    return Err(MissingSample(MissingSampleError { parent_sample: p }));
                }
                let entry = parent_to_children.entry(p).or_default();
                entry.push(row.sample_id);
            } else if root.is_some() {
                return Err(MultipleRoots);
            } else {
                root = Some(row.sample_id);
            }
        }

        let Some(rt) = root else {
            return Err(NoRootError);
        };

        let mut to_visit = Vec::default();
        to_visit.push(rt);

        while let Some(sample_id) = to_visit.pop() {
            let children = parent_to_children
                .remove(&sample_id)
                .unwrap_or_else(|| Vec::with_capacity(0));
            to_visit.extend(children.into_iter());
        }

        if parent_to_children.is_empty() {
            Ok(())
        } else if let Some((unknown_p, _)) = parent_to_children
            .iter()
            .take_while(|(p, _)| !sample_ids.contains(p))
            .next()
        {
            Err(MissingSample(MissingSampleError {
                parent_sample: *unknown_p,
            }))
        } else {
            Err(Disconnected)
        }
    }

    /// Parse a [SwcNeuron] from a [Read]er.
    ///
    /// Does not check the neuron for consistency, but does check for valid structures.
    pub fn from_reader<R: Read>(reader: R) -> Result<Self, SwcParseError> {
        let buf = BufReader::new(reader);

        let mut samples = Vec::default();
        let mut is_header = true;
        let mut header_lines = Vec::default();
        for result in buf.lines() {
            let raw_line = result.map_err(SwcParseError::Read)?;
            let line = raw_line.trim();
            if line.starts_with('#') {
                if is_header {
                    header_lines.push(line.trim_start_matches('#').trim_start().to_string());
                }
                continue;
            }
            is_header = false;
            if line.is_empty() {
                continue;
            }
            samples.push(SwcSample::from_str(line)?);
        }

        let header: Option<H> = if header_lines.is_empty() {
            None
        } else {
            let header_str = header_lines.join("\n");
            Some(
                header_str
                    .parse()
                    .map_err(|_e| SwcParseError::HeaderParse(header_str))?,
            )
        };

        Ok(Self { samples, header })
    }

    /// Dump this [SwcNeuron] to a [Write]r.
    pub fn to_writer<W: Write>(&self, writer: &mut W) -> Result<(), std::io::Error> {
        let mut w = BufWriter::new(writer);
        if let Some(h) = self.header.clone() {
            for line in h.to_string().lines() {
                if !line.starts_with('#') {
                    writeln!(w, "# {}", line)?;
                } else {
                    writeln!(w, "{}", line)?;
                }
            }
        }
        for s in self.samples.iter() {
            writeln!(w, "{}", s.to_string())?;
        }
        Ok(())
    }

    /// Replace the existing header with a new one, returning the existing.
    pub fn replace_header(&mut self, header: Option<H>) -> Option<H> {
        std::mem::replace(&mut self.header, header)
    }
}