rustynetics 0.1.3

A high-performance genomics libary specialized in handling BAM and BigWig files
Documentation
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// Copyright (C) 2024 Philipp Benner
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

/* -------------------------------------------------------------------------- */

use std::fmt;
use std::error::Error;

use list_comprehension_macro::comp;

use crate::range::Range;

/* -------------------------------------------------------------------------- */

#[derive(Debug, Clone)]
pub enum MetaData {
    StringMatrix (Vec<Vec<String>>),
    StringArray  (Vec<String>     ),
    FloatMatrix  (Vec<Vec<f64>>   ),
    FloatArray   (Vec<f64>        ),
    IntMatrix    (Vec<Vec<i64>>   ),
    IntArray     (Vec<i64>        ),
    RangeArray   (Vec<Range>      ),
}

/* -------------------------------------------------------------------------- */

impl MetaData {
    pub fn len(&self) -> usize {
        match self {
            MetaData::FloatArray  (v) => v.len(),
            MetaData::IntArray    (v) => v.len(),
            MetaData::StringArray (v) => v.len(),
            MetaData::StringMatrix(v) => v.len(),
            MetaData::FloatMatrix (v) => v.len(),
            MetaData::IntMatrix   (v) => v.len(),
            MetaData::RangeArray  (v) => v.len(),
        }
    }

    pub fn concat(&self, data : &Self) -> Result<Self, Box<dyn Error>> {
        match (self, data) {
            (MetaData::FloatArray  (v), MetaData::FloatArray  (w)) => Ok(MetaData::FloatArray  ([v.clone(), w.clone()].concat())),
            (MetaData::IntArray    (v), MetaData::IntArray    (w)) => Ok(MetaData::IntArray    ([v.clone(), w.clone()].concat())),
            (MetaData::StringArray (v), MetaData::StringArray (w)) => Ok(MetaData::StringArray ([v.clone(), w.clone()].concat())),
            (MetaData::StringMatrix(v), MetaData::StringMatrix(w)) => Ok(MetaData::StringMatrix([v.clone(), w.clone()].concat())),
            (MetaData::FloatMatrix (v), MetaData::FloatMatrix (w)) => Ok(MetaData::FloatMatrix ([v.clone(), w.clone()].concat())),
            (MetaData::IntMatrix   (v), MetaData::IntMatrix   (w)) => Ok(MetaData::IntMatrix   ([v.clone(), w.clone()].concat())),
            (MetaData::RangeArray  (v), MetaData::RangeArray  (w)) => Ok(MetaData::RangeArray  ([v.clone(), w.clone()].concat())),
            _ => Err(format!("MetaData types do not match").into())
        }
    }

    pub fn slice(&self, ifrom : usize, ito : usize) -> Self {
        match self {
            MetaData::FloatArray  (v) => MetaData::FloatArray  (comp![ v[i] for i in ifrom..ito ]),
            MetaData::IntArray    (v) => MetaData::IntArray    (comp![ v[i] for i in ifrom..ito ]),
            MetaData::StringArray (v) => MetaData::StringArray (comp![ v[i].clone() for i in ifrom..ito ]),
            MetaData::StringMatrix(v) => MetaData::StringMatrix(comp![ v[i].clone() for i in ifrom..ito ]),
            MetaData::FloatMatrix (v) => MetaData::FloatMatrix (comp![ v[i].clone() for i in ifrom..ito ]),
            MetaData::IntMatrix   (v) => MetaData::IntMatrix   (comp![ v[i].clone() for i in ifrom..ito ]),
            MetaData::RangeArray  (v) => MetaData::RangeArray  (comp![ v[i].clone() for i in ifrom..ito ]),
        }
    }

    pub fn subset(&self, indices : &[usize]) -> Self {
        match self {
            MetaData::FloatArray  (v) => MetaData::FloatArray  (comp![ v[*i] for i in indices ]),
            MetaData::IntArray    (v) => MetaData::IntArray    (comp![ v[*i] for i in indices ]),
            MetaData::StringArray (v) => MetaData::StringArray (comp![ v[*i].clone() for i in indices ]),
            MetaData::StringMatrix(v) => MetaData::StringMatrix(comp![ v[*i].clone() for i in indices ]),
            MetaData::FloatMatrix (v) => MetaData::FloatMatrix (comp![ v[*i].clone() for i in indices ]),
            MetaData::IntMatrix   (v) => MetaData::IntMatrix   (comp![ v[*i].clone() for i in indices ]),
            MetaData::RangeArray  (v) => MetaData::RangeArray  (comp![ v[*i].clone() for i in indices ]),
        }
    }

    pub fn get_float(&self) -> Option<&Vec<f64>> {
        match self {
            MetaData::FloatArray(v) => Some(v),
            _ => None
        }
    }

    pub fn get_int(&self) -> Option<&Vec<i64>> {
        match self {
            MetaData::IntArray(v) => Some(v),
            _ => None
        }
    }

    pub fn get_str(&self) -> Option<&Vec<String>> {
        match self {
            MetaData::StringArray(v) => Some(v),
            _ => None
        }
    }

    pub fn get_range(&self) -> Option<&Vec<Range>> {
        match self {
            MetaData::RangeArray(v) => Some(v),
            _ => None
        }
    }

    pub fn get_float_mut(&mut self) -> Option<&mut Vec<f64>> {
        match self {
            MetaData::FloatArray(v) => Some(v),
            _ => None
        }
    }

    pub fn get_int_mut(&mut self) -> Option<&mut Vec<i64>> {
        match self {
            MetaData::IntArray(v) => Some(v),
            _ => None
        }
    }

    pub fn get_str_mut(&mut self) -> Option<&mut Vec<String>> {
        match self {
            MetaData::StringArray(v) => Some(v),
            _ => None
        }
    }

    pub fn get_range_mut(&mut self) -> Option<&mut Vec<Range>> {
        match self {
            MetaData::RangeArray(v) => Some(v),
            _ => None
        }
    }

}

/* -------------------------------------------------------------------------- */

impl PartialEq for MetaData {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (MetaData::FloatArray  (v), MetaData::FloatArray  (w)) => v == w,
            (MetaData::IntArray    (v), MetaData::IntArray    (w)) => v == w,
            (MetaData::StringArray (v), MetaData::StringArray (w)) => v == w,
            (MetaData::StringMatrix(v), MetaData::StringMatrix(w)) => v == w,
            (MetaData::FloatMatrix (v), MetaData::FloatMatrix (w)) => v == w,
            (MetaData::IntMatrix   (v), MetaData::IntMatrix   (w)) => v == w,
            (MetaData::RangeArray  (v), MetaData::RangeArray  (w)) => v == w,
            _ => false
        }
    }
}

/* -------------------------------------------------------------------------- */

/// A structure for storing metadata associated with genomic ranges in a
/// flexible, column-based format. The `Meta` struct allows storing columns of
/// various data types (integers, floats, strings, and ranges), facilitating
/// metadata storage and manipulation for genomic datasets.
///
/// Each `Meta` instance comprises named metadata columns, where each column can
/// have a different type (handled by `MetaData`). Columns are organized by name
/// in `meta_name` and data in `meta_data`, with rows representing entries.
///
/// # Fields
/// - `meta_name`: A vector of column names, each corresponding to an entry in `meta_data`.
/// - `meta_data`: A vector of `MetaData`, each holding values for a column.
/// - `rows`: The number of rows (entries) in each column.
///
/// # Examples
/// ```
/// use rustynetics::meta::{Meta, MetaData};
///
/// // Creating a new Meta instance with column names and data
/// let meta = Meta::new(
///     vec!["Gene", "Expression"],
///     vec![MetaData::StringArray(vec!["GeneA".to_string(), "GeneB".to_string()]),
///          MetaData::FloatArray(vec![0.85, 1.23])]
/// ).unwrap();
/// ```
///
/// # Usage
/// The `Meta` struct provides methods for adding, appending, retrieving, and
/// managing metadata columns. Metadata can be accessed and modified by column name,
/// and transformations such as subsetting, sorting, or slicing rows are also supported.
///
/// # Note
/// This struct is commonly used in bioinformatics to attach structured annotations
/// to genomic ranges, enabling efficient storage and retrieval of associated metadata.
#[derive(Clone, Debug)]
pub struct Meta {
    pub meta_name: Vec<String>,
    pub meta_data: Vec<MetaData>,
    rows: usize,
}

/* -------------------------------------------------------------------------- */

impl Default for Meta {
    fn default() -> Meta {
        Self {
            meta_name: Vec::new(),
            meta_data: Vec::new(),
            rows: 0,
        }
    }
}

/* -------------------------------------------------------------------------- */

impl Meta {

    /// Creates a new `Meta` instance with the given column names and data.
    ///
    /// # Arguments
    /// - `names`: A vector of names for each column.
    /// - `data`: A vector of `MetaData`, one for each column.
    ///
    /// # Errors
    /// Returns an error if `names` and `data` have mismatched lengths.
    pub fn new(names: Vec<&str>, data: Vec<MetaData>) -> Result<Self, Box<dyn Error>> {
        if names.len() != data.len() {
            return Err(format!("Invalid parameters!").into());
        }
        let mut meta = Meta {
            meta_name: Vec::new(),
            meta_data: Vec::new(),
            rows: 0,
        };
        for i in 0..names.len() {
            meta.add(names[i], data[i].clone())?;
        }
        Ok(meta)
    }

    /// Returns the number of rows in the `Meta` instance.
    pub fn num_rows(&self) -> usize {
        self.rows
    }

    /// Returns the number of columns in the `Meta` instance.
    pub fn num_cols(&self) -> usize {
        self.meta_name.len()
    }

    /// Appends rows from another `Meta` instance to the current instance, matching columns by name.
    ///
    /// This function combines two `Meta` instances by appending rows from `meta` to the existing
    /// rows in `self`. The column names in both `Meta` instances must match exactly.
    ///
    /// # Arguments
    /// - `meta`: A reference to the `Meta` instance to append to `self`.
    ///
    /// # Returns
    /// A new `Meta` instance containing rows from both `self` and `meta`.
    ///
    /// # Errors
    /// Returns an error if:
    /// - The number of rows in `meta` columns does not match the columns in `self`.
    /// - A column name in `meta` is not found in `self`.
    ///
    /// # Example
    /// ```
    /// use rustynetics::meta::{Meta, MetaData};
    ///
    /// let meta1 = Meta::new(
    ///     vec!["Gene", "Expression"],
    ///     vec![MetaData::StringArray(vec!["GeneA".to_string()]),
    ///          MetaData::FloatArray(vec![0.85])]
    /// ).unwrap();
    ///
    /// let meta2 = Meta::new(
    ///     vec!["Gene", "Expression"],
    ///     vec![MetaData::StringArray(vec!["GeneB".to_string()]),
    ///          MetaData::FloatArray(vec![1.23])]
    /// ).unwrap();
    ///
    /// let combined = meta1.append(&meta2).unwrap();
    /// assert_eq!(combined.num_rows(), 2);
    /// ```
    pub fn append(&self, meta : &Meta) -> Result<Self, Box<dyn Error>> {
        let n1 = self.num_rows();
        let n2 = meta.num_rows();

        let mut meta_name = Vec::new();
        let mut meta_data = Vec::new();

        for j in 0..self.num_cols() {
            let meta_col1 = self.meta_data[j].clone();
            let meta_col2 = meta.get_column(&meta.meta_name[j]);

            if meta_col2.is_none() {
                return Err(format!("Column {} not found in meta object", &meta.meta_name[j]).into())
            }
            let meta_col3 = meta_col1.concat(meta_col2.unwrap())?;

            meta_name.push(self.meta_name[j].clone());
            meta_data.push(meta_col3);
        }
        let meta = Meta {
            meta_name: meta_name,
            meta_data: meta_data,
            rows: n1+n2,
        };
        Ok(meta)
    }

    /// Adds a new column to the `Meta` instance.
    ///
    /// # Arguments
    /// - `name`: The name of the new column.
    /// - `data`: The `MetaData` for the new column.
    ///
    /// # Errors
    /// Returns an error if the length of `data` does not match the existing row count.
    pub fn add(&mut self, name: &str, data: MetaData) -> Result<(), Box<dyn Error>> {
        let n = data.len();
        if self.meta_name.len() > 0 {
            if n != self.rows {
                return Err(format!("Column '{}' has invalid length: expected length of '{}' but column has length '{}'", name, self.rows, n).into());
            }
        }
        if self.meta_name.len() == 0 {
            self.rows = n;
        }
        self.meta_name.push(String::from(name));
        self.meta_data.push(data);
        Ok(())
    }

    /// Deletes a column by name, if it exists.
    ///
    /// # Arguments
    /// - `name`: The name of the column to delete.
    pub fn delete_meta(&mut self, name: &str) {
        if let Some(index) = self.meta_name.iter().position(|x| x == name) {
            self.meta_name.remove(index);
            self.meta_data.remove(index);
        }
    }

    /// Renames a column in `Meta` by name.
    ///
    /// # Arguments
    /// - `name_old`: The existing name of the column.
    /// - `name_new`: The new name for the column.
    pub fn rename_meta(&mut self, name_old: &str, name_new: &str) {
        if name_old == name_new {
            return;
        }
        if let Some(index) = self.meta_name.iter().position(|x| x == name_old) {
            self.meta_name[index] = name_new.to_string();
        }
    }

    /// Retrieves a reference to a column in the `Meta` instance by its name.
    ///
    /// This function searches for a column with the specified name and returns a reference to the
    /// `MetaData` object associated with that column if it exists.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a reference to the `MetaData` of the specified column. 
    /// Returns `None` if no column with the given name is found.
    ///
    /// # Example
    /// ```
    /// use rustynetics::meta::{Meta, MetaData};
    ///
    /// let meta = Meta::new(
    ///     vec!["Gene", "Expression"],
    ///     vec![MetaData::StringArray(vec!["GeneA".to_string(), "GeneB".to_string()]),
    ///          MetaData::FloatArray(vec![1.0, 2.0])]
    /// ).unwrap();
    ///
    /// if let Some(column) = meta.get_column("Expression") {
    ///     // Process column data here
    /// }
    /// ```
    ///
    /// # Errors
    /// This function does not return an error but rather returns `None` if the specified
    /// column name is not found.
    pub fn get_column(&self, name: &str) -> Option<&MetaData> {
        self.meta_name.iter().position(|x| x == name).map(|index| &self.meta_data[index])
    }

    /// Retrieves a reference to a column containing integer data by its name.
    ///
    /// This function returns a reference to the `Vec<i64>` of integers for the specified column
    /// if the column contains integer data. If the column exists but does not contain integers,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a reference to the integer `Vec<i64>`. Returns `None` if
    /// the column is not found or if it contains non-integer data.
    pub fn get_column_int(&self, name: &str) -> Option<&Vec<i64>> {
        let r = self.get_column(name)?;
        r.get_int()
    }

    /// Retrieves a reference to a column containing floating-point data by its name.
    ///
    /// This function returns a reference to the `Vec<f64>` of floats for the specified column
    /// if the column contains float data. If the column exists but does not contain floats,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a reference to the float `Vec<f64>`. Returns `None` if
    /// the column is not found or if it contains non-float data.
    pub fn get_column_float(&self, name: &str) -> Option<&Vec<f64>> {
        let r = self.get_column(name)?;
        r.get_float()
    }

    /// Retrieves a reference to a column containing string data by its name.
    ///
    /// This function returns a reference to the `Vec<String>` of strings for the specified column
    /// if the column contains string data. If the column exists but does not contain strings,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a reference to the string `Vec<String>`. Returns `None` if
    /// the column is not found or if it contains non-string data.
    pub fn get_column_str(&self, name: &str) -> Option<&Vec<String>> {
        let r = self.get_column(name)?;
        r.get_str()
    }

    /// Retrieves a reference to a column containing range data by its name.
    ///
    /// This function returns a reference to the `Vec<Range>` of ranges for the specified column
    /// if the column contains range data. If the column exists but does not contain ranges,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a reference to the range `Vec<Range>`. Returns `None` if
    /// the column is not found or if it contains non-range data.
    pub fn get_column_range(&self, name: &str) -> Option<&Vec<Range>> {
        let r = self.get_column(name)?;
        r.get_range()
    }

    /// Retrieves a mutable reference to a column in the `Meta` instance by its name.
    ///
    /// This function returns a mutable reference to the `MetaData` object of the specified column,
    /// allowing modifications to the column data if it exists. If the column name is not found,
    /// it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a mutable reference to the `MetaData` of the specified column.
    /// Returns `None` if no column with the given name is found.
    ///
    /// # Example
    /// ```
    /// use rustynetics::meta::{Meta, MetaData};
    ///
    /// let mut meta = Meta::new(
    ///     vec!["Count"],
    ///     vec![MetaData::IntArray(vec![10, 20, 30])],
    /// ).unwrap();
    ///
    /// if let Some(column) = meta.get_column_mut("Count") {
    ///     if let Some(r) = column.get_int_mut() {
    ///         r[1] = 40;
    ///     }
    /// }
    /// ```
    pub fn get_column_mut(&mut self, name: &str) -> Option<&mut MetaData> {
        self.meta_name.iter().position(|x| x == name).map(move |index| &mut self.meta_data[index])
    }

    /// Retrieves a mutable reference to a column containing integer data by its name.
    ///
    /// This function returns a mutable reference to the `Vec<i64>` of integers for the specified column
    /// if the column exists and contains integer data. If the column exists but does not contain integers,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a mutable reference to the integer `Vec<i64>`. Returns `None` if
    /// the column is not found or if it contains non-integer data.
    pub fn get_column_int_mut(&mut self, name: &str) -> Option<&mut Vec<i64>> {
        let r = self.get_column_mut(name)?;
        r.get_int_mut()
    }

    /// Retrieves a mutable reference to a column containing floating-point data by its name.
    ///
    /// This function returns a mutable reference to the `Vec<f64>` of floats for the specified column
    /// if the column exists and contains float data. If the column exists but does not contain floats,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a mutable reference to the float `Vec<f64>`. Returns `None` if
    /// the column is not found or if it contains non-float data.
    pub fn get_column_float_mut(&mut self, name: &str) -> Option<&mut Vec<f64>> {
        let r = self.get_column_mut(name)?;
        r.get_float_mut()
    }

    /// Retrieves a mutable reference to a column containing string data by its name.
    ///
    /// This function returns a mutable reference to the `Vec<String>` of strings for the specified column
    /// if the column exists and contains string data. If the column exists but does not contain strings,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a mutable reference to the string `Vec<String>`. Returns `None` if
    /// the column is not found or if it contains non-string data.
    pub fn get_column_str_mut(&mut self, name: &str) -> Option<&mut Vec<String>> {
        let r = self.get_column_mut(name)?;
        r.get_str_mut()
    }

    /// Retrieves a mutable reference to a column containing range data by its name.
    ///
    /// This function returns a mutable reference to the `Vec<Range>` of ranges for the specified column
    /// if the column exists and contains range data. If the column exists but does not contain ranges,
    /// or if the column name is not found, it returns `None`.
    ///
    /// # Arguments
    /// - `name`: The name of the column to retrieve.
    ///
    /// # Returns
    /// An `Option` containing a mutable reference to the range `Vec<Range>`. Returns `None` if
    /// the column is not found or if it contains non-range data.
    pub fn get_column_range_mut(&mut self, name: &str) -> Option<&mut Vec<Range>> {
        let r = self.get_column_mut(name)?;
        r.get_range_mut()
    }

    /// Creates a subset of the `Meta` struct by slicing rows between specified indices.
    ///
    /// This function creates a new `Meta` instance containing only the rows from `ifrom` to `ito`.
    /// It clones the specified range of rows for each column without modifying the original `Meta` instance.
    ///
    /// # Arguments
    /// - `ifrom`: The starting row index (inclusive).
    /// - `ito`: The ending row index (exclusive).
    ///
    /// # Returns
    /// A new `Meta` instance containing only the specified slice of rows for each column.
    pub fn slice(&self, ifrom : usize, ito : usize) -> Meta {

        let n = ito-ifrom;
        let m = self.meta_name.len();
        let mut data = Vec::new();

        for j in 0..m {
            data.push(self.meta_data[j].slice(ifrom, ito));
        }

        Meta {
            meta_name: self.meta_name.clone(),
            meta_data: data,
            rows: n,
        }
    }

    /// Creates a subset of the `Meta` struct based on a list of row indices.
    ///
    /// This function returns a new `Meta` instance containing only the rows specified by `indices`.
    /// Each index in `indices` is cloned into the new `Meta` instance. This method allows for non-sequential
    /// row selections, providing more flexibility for data manipulation.
    ///
    /// # Arguments
    /// - `indices`: A slice of row indices to include in the new `Meta` instance.
    ///
    /// # Returns
    /// A new `Meta` instance containing only the rows specified by `indices`.
    pub fn subset(&self, indices: &[usize]) -> Meta {
        let n = indices.len();
        let m = self.meta_name.len();
        let mut data = Vec::new();

        for j in 0..m {
            data.push(self.meta_data[j].subset(indices));
        }

        Meta {
            meta_name: self.meta_name.clone(),
            meta_data: data,
            rows: n,
        }
    }

    /// Sorts the `Meta` struct by a specified column, either in ascending or descending order.
    ///
    /// This function sorts the rows of `Meta` based on the values in the specified column.
    /// The column data type must support ordering (e.g., `MetaData::IntArray`, `MetaData::FloatArray`, `MetaData::StringArray`).
    /// If the `reverse` flag is `true`, the sort is performed in descending order; otherwise, it’s ascending.
    ///
    /// # Arguments
    /// - `name`: The name of the column by which to sort the `Meta` rows.
    /// - `reverse`: A boolean flag indicating whether to sort in descending order (`true`) or ascending (`false`).
    ///
    /// # Returns
    /// A `Result` containing a sorted `Meta` instance if successful, or an error if the specified column name
    /// is not found or has an unsupported data type.
    pub fn sort(&self, name: &str, reverse: bool) -> Result<Self, Box<dyn Error>> {
        let mut indices: Vec<usize> = (0..self.rows).collect();

        if reverse {
            match self.get_column(name).unwrap() {
                MetaData::StringArray(v) => indices.sort_by(|&i, &j| v[j].cmp(&v[i])),
                MetaData::FloatArray (v) => indices.sort_by(|&i, &j| v[j].partial_cmp(&v[i]).unwrap()),
                MetaData::IntArray   (v) => indices.sort_by(|&i, &j| v[j].cmp(&v[i])),
                _ => ()
            }
        }
        else {
            match self.get_column(name).unwrap() {
                MetaData::StringArray(v) => indices.sort_by(|&i, &j| v[i].cmp(&v[j])),
                MetaData::FloatArray (v) => indices.sort_by(|&i, &j| v[i].partial_cmp(&v[j]).unwrap()),
                MetaData::IntArray   (v) => indices.sort_by(|&i, &j| v[i].cmp(&v[j])),
                _ => ()
            }
        }
        Ok(self.subset(&indices))
    }

    /// Returns an iterator over the columns in the `Meta` struct.
    ///
    /// This iterator yields pairs of column names and their associated `MetaData` values,
    /// allowing for iteration over each column in the `Meta` instance.
    ///
    /// # Returns
    /// An iterator yielding tuples of (`&String`, `&MetaData`), representing each column's name
    /// and its data, respectively.
    pub fn iter(&self) -> impl Iterator<Item = (&String, &MetaData)> {
        self.meta_name.iter().zip(self.meta_data.iter())
    }
}

/* -------------------------------------------------------------------------- */

impl PartialEq for Meta {
    fn eq(&self, other: &Self) -> bool {
        if self.num_cols() != other.num_cols() {
            return false;
        }
        if self.num_rows() != other.num_rows() {
            return false;
        }
        for j in 0..self.num_cols() {
            let name = &self.meta_name[j];

            let meta_col1 = self.meta_data[j].clone();
            let meta_col2 = other.get_column(name);

            if meta_col2.is_none() {
                return false;
            }
            if meta_col1 != *meta_col2.unwrap() {
                return false;
            }
        }
        true
    }
}

/* -------------------------------------------------------------------------- */

impl fmt::Display for Meta {

    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.pad(&format!("{}", self.format_pretty(10, false).unwrap()))
    }
}