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
use std::collections::HashMap;

use super::{
    DataProcessing, FileDescription, InstrumentConfiguration, MassSpectrometryRun, Software,
};

/// Mass spectrometry data files have several facets of descriptive metadata
pub trait MSDataFileMetadata {
    /// The series of [`DataProcessing`] workflows applied to spectra in
    /// this data file.
    fn data_processings(&self) -> &Vec<DataProcessing>;
    /// A mapping over different [`InstrumentConfiguration`] modes that spectra
    /// were acquired under.
    fn instrument_configurations(&self) -> &HashMap<u32, InstrumentConfiguration>;
    /// A description of the contents and the sources for this mass spectrometry
    /// data file.
    fn file_description(&self) -> &FileDescription;
    /// The series of [`Software`] applied to the data file to apply different
    /// [`DataProcessing`] methods.
    fn softwares(&self) -> &Vec<Software>;

    /// Mutably access the [`DataProcessing`] list for this data file
    fn data_processings_mut(&mut self) -> &mut Vec<DataProcessing>;
    /// Mutably access the [`InstrumentConfiguration`] mapping for this data file
    fn instrument_configurations_mut(&mut self) -> &mut HashMap<u32, InstrumentConfiguration>;
    /// Mutably access the [`FileDescription`] description of the contents and the
    /// sources for this mass spectrometry data file.
    fn file_description_mut(&mut self) -> &mut FileDescription;
    /// Mutably access the list of [`Software`] of this data file.
    fn softwares_mut(&mut self) -> &mut Vec<Software>;

    /// Copy the metadata from another [`MSDataFileMetadata`] implementation into
    /// this one.
    fn copy_metadata_from(&mut self, source: &impl MSDataFileMetadata) {
        *self.data_processings_mut() = source.data_processings().clone();
        *self.instrument_configurations_mut() = source.instrument_configurations().clone();
        *self.file_description_mut() = source.file_description().clone();
        *self.softwares_mut() = source.softwares().clone();

        match source.run_description() {
            Some(run) => {
                let desc = self.run_description_mut();
                if let Some(r) = desc {
                    *r = run.clone();
                };
            }
            None => {
                let mut desc = self.run_description_mut();
                desc.take();
            }
        }
    }

    /// A hint about how many spectra are in this data file
    fn spectrum_count_hint(&self) -> Option<u64> {
        None
    }

    /// Access the [`MassSpectrometryRun`] metadata record if it is available
    fn run_description(&self) -> Option<&MassSpectrometryRun> {
        None
    }

    /// Mutably access the [`MassSpectrometryRun`] metadata record if it is available
    fn run_description_mut(&mut self) -> Option<&mut MassSpectrometryRun> {
        None
    }

    /// Get the name of the primary source file, if available
    fn source_file_name(&self) -> Option<&str> {
        self.file_description().source_files.first().map(|s| s.name.as_str())
    }
}


#[macro_export]
/// Assumes a field for the non-`Option` facets of the [`MSDataFileMetadata`]
/// implementation are present. Passing an extra level `extended` token implements
/// the optional methods.
macro_rules! impl_metadata_trait {
    (extended) => {
        $crate::impl_metadata_trait();

        fn spectrum_count_hint(&self) -> Option<u64> {
            self.num_spectra
        }

        fn run_description(&self) -> Option<&$crate::meta::MassSpectrometryRun> {
            Some(&self.run)
        }

        fn run_description_mut(&mut self) -> Option<&mut $crate::meta::MassSpectrometryRun> {
            Some(&mut self.run)
        }
    };
    () => {
        fn data_processings(&self) -> &Vec<$crate::meta::DataProcessing> {
            &self.data_processings
        }

        fn instrument_configurations(&self) -> &std::collections::HashMap<u32, $crate::meta::InstrumentConfiguration> {
            &self.instrument_configurations
        }
        fn file_description(&self) -> &$crate::meta::FileDescription {
            &self.file_description
        }
        fn softwares(&self) -> &Vec<$crate::meta::Software> {
            &self.softwares
        }

        fn data_processings_mut(&mut self) -> &mut Vec<$crate::meta::DataProcessing> {
            &mut self.data_processings
        }

        fn instrument_configurations_mut(&mut self) -> &mut std::collections::HashMap<u32, $crate::meta::InstrumentConfiguration> {
            &mut self.instrument_configurations
        }

        fn file_description_mut(&mut self) -> &mut $crate::meta::FileDescription {
            &mut self.file_description
        }

        fn softwares_mut(&mut self) -> &mut Vec<$crate::meta::Software> {
            &mut self.softwares
        }
    };
}


#[macro_export]
/// Delegates the implementation of [`MSDataFileMetadata`] to a member. Passing an extra
/// level `extended` token implements the optional methods.
macro_rules! delegate_impl_metadata_trait {
    ($src:tt, extended) => {
        $crate::delegate_impl_metadata_trait($src);

        fn spectrum_count_hint(&self) -> Option<u64> {
            self.$src.spectrum_count_hint()
        }

        fn run_description(&self) -> Option<&$crate::meta::MassSpectrometryRun> {
            self.$src.run_description()
        }

        fn run_description_mut(&mut self) -> Option<&mut $crate::meta::MassSpectrometryRun> {
            self.$src.run_description_mut()
        }
    };
    ($src:tt) => {

        fn data_processings(&self) -> &Vec<$crate::meta::DataProcessing> {
            self.$src.data_processings()
        }

        fn instrument_configurations(&self) -> &std::collections::HashMap<u32, $crate::meta::InstrumentConfiguration> {
            self.$src.instrument_configurations()
        }

        fn file_description(&self) -> &$crate::meta::FileDescription {
            self.$src.file_description()
        }

        fn softwares(&self) -> &Vec<$crate::meta::Software> {
            self.$src.softwares()
        }

        fn data_processings_mut(&mut self) -> &mut Vec<$crate::meta::DataProcessing> {
            self.$src.data_processings_mut()
        }

        fn instrument_configurations_mut(&mut self) -> &mut std::collections::HashMap<u32, $crate::meta::InstrumentConfiguration> {
            self.$src.instrument_configurations_mut()
        }

        fn file_description_mut(&mut self) -> &mut $crate::meta::FileDescription {
            self.$src.file_description_mut()
        }

        fn softwares_mut(&mut self) -> &mut Vec<$crate::meta::Software> {
            self.$src.softwares_mut()
        }

        fn spectrum_count_hint(&self) -> Option<u64> {
            self.$src.spectrum_count_hint()
        }

        fn run_description(&self) -> Option<&$crate::meta::MassSpectrometryRun> {
            self.$src.run_description()
        }

        fn run_description_mut(&mut self) -> Option<&mut $crate::meta::MassSpectrometryRun> {
            self.$src.run_description_mut()
        }

    };
}