qslib 0.14.0-pre1

QSlib QuantStudio qPCR machine library
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
use std::collections::HashMap;

use quick_xml::{de::from_str, se::to_string};
use serde::de::Error;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "Plate", deny_unknown_fields)]
pub struct PlateSetup {
    #[serde(rename = "Name")]
    pub name: Option<String>,
    #[serde(rename = "BarCode")]
    pub barcode: Option<String>,
    #[serde(rename = "Description")]
    pub description: Option<String>,
    #[serde(rename = "Rows")]
    pub rows: u32,
    #[serde(rename = "Columns")]
    pub columns: u32,
    #[serde(rename = "PlateKind")]
    pub plate_kinds: Vec<PlateKind>,
    #[serde(rename = "FeatureMap", default)]
    pub feature_maps: Vec<FeatureMap>,
    #[serde(skip)]
    pub plate_type: PlateType,
    #[serde(rename = "Wells", default)]
    pub wells: Vec<OtherTag>,
    #[serde(rename = "MultiZoneEnabled")]
    pub multi_zone_enabled: Option<String>,
    #[serde(rename = "LogicalZone", default)]
    pub logical_zones: Vec<OtherTag>,
    #[serde(rename = "PassiveReferenceDye", default)]
    pub passive_reference_dye: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct OtherTag {
    #[serde(flatten)]
    pub other: HashMap<String, MapOrString>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PlateKind {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "Type")]
    pub kind_type: PlateType,
    #[serde(rename = "RowCount")]
    pub row_count: u32,
    #[serde(rename = "ColumnCount")]
    pub column_count: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FeatureMap {
    #[serde(rename = "Feature")]
    pub feature: Feature,
    #[serde(rename = "FeatureValue", default)]
    pub feature_values: Vec<FeatureValue>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Feature {
    #[serde(rename = "Id")]
    pub id: String,
    #[serde(rename = "Name")]
    pub name: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FeatureValue {
    #[serde(rename = "Index")]
    pub index: u32,
    #[serde(rename = "FeatureItem")]
    pub feature_item: FeatureItem,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MapOrString {
    Map(HashMap<String, MapOrString>),
    String(String),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FeatureItem {
    #[serde(rename = "Sample", skip_serializing_if = "Option::is_none")]
    pub sample: Option<Sample>,
    #[serde(flatten)]
    pub other: HashMap<String, MapOrString>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Sample {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "Color")]
    pub color: Color,
    #[serde(rename = "CustomProperty")]
    pub custom_properties: Vec<CustomProperty>,
}

impl Sample {
    pub fn new(name: String) -> Self {
        Self {
            name,
            color: Color::rgb(100, 100, 100),
            custom_properties: vec![],
        }
    }

    pub fn with_color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }

    pub fn with_custom_property(mut self, property: CustomProperty) -> Self {
        self.custom_properties.push(property);
        self
    }
}

/// A color in RGBA format, but serialized and deserialized as an i32.
/// This makes little sense, but it is the format used by the QuantStudio
/// files.
#[derive(Debug)]
pub struct Color {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
}

impl Color {
    pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }

    pub fn rgb(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b, a: 255 }
    }

    pub fn with_a(self, a: u8) -> Self {
        Self { a, ..self }
    }

    pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }

    pub fn to_rgba(&self) -> (u8, u8, u8, u8) {
        (self.r, self.g, self.b, self.a)
    }

    pub fn to_hex(&self) -> String {
        format!("#{:02X}{:02X}{:02X}{:02X}", self.r, self.g, self.b, self.a)
    }
}

impl Serialize for Color {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let value = i32::from_le_bytes([self.r, self.g, self.b, self.a]);
        serializer.serialize_i32(value)
    }
}

impl<'de> Deserialize<'de> for Color {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = i32::deserialize(deserializer)?;
        let [r, g, b, a] = value.to_le_bytes();
        Ok(Color { r, g, b, a })
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CustomProperty {
    #[serde(rename = "Property")]
    pub property: Vec<String>,
    #[serde(rename = "Value")]
    pub value: Vec<String>,
}

impl PlateSetup {
    pub fn from_xml(xml: &str) -> Result<Self, quick_xml::DeError> {
        let mut plate: PlateSetup = from_str(xml)?;

        // Determine plate type from PlateKind
        if let Some(kind) = plate.plate_kinds.first() {
            plate.plate_type = kind.kind_type;
        } else {
            return Err(quick_xml::DeError::Custom("Missing plate kind".into()));
        }

        Ok(plate)
    }

    pub fn to_xml(&self) -> Result<String, quick_xml::SeError> {
        to_string(self)
    }

    // Helper method to get well names based on plate type
    pub fn well_names(&self) -> Vec<String> {
        let (rows, cols) = match self.plate_type {
            PlateType::Well96 => ("ABCDEFGH", 12),
            PlateType::Well384 => ("ABCDEFGHIJKLMNOP", 24),
        };

        rows.chars()
            .flat_map(|r| (1..=cols).map(move |c| format!("{}{}", r, c)))
            .collect()
    }

    // Get samples by well position
    pub fn get_sample_wells(&self) -> HashMap<String, Vec<String>> {
        let well_names = self.well_names();
        let mut sample_wells: HashMap<String, Vec<String>> = HashMap::new();

        for feature_map in &self.feature_maps {
            if feature_map.feature.id == "sample" {
                for value in &feature_map.feature_values {
                    if let Some(well_name) = well_names.get(value.index as usize) {
                        if let Some(sample) = &value.feature_item.sample {
                            sample_wells
                                .entry(sample.name.clone())
                                .or_default()
                                .push(well_name.clone());
                        }
                    }
                }
            }
        }
        sample_wells
    }

    /// Convert plate setup to InfluxDB line protocol format
    ///
    /// # Arguments
    /// * `timestamp` - Unix timestamp in nanoseconds
    /// * `run_name` - Optional run name to include in the tags
    pub fn to_lineprotocol(
        &self,
        timestamp: i64,
        run_name: Option<&str>,
        machine_name: Option<&str>,
    ) -> Vec<String> {
        let well_sample = self
            .get_sample_wells()
            .into_iter()
            .flat_map(|(sample, wells)| wells.into_iter().map(move |well| (well, sample.clone())))
            .collect::<HashMap<_, _>>();

        let (rows, cols) = match self.plate_type {
            PlateType::Well96 => ("ABCDEFGH", 12),
            PlateType::Well384 => ("ABCDEFGHIJKLMNOP", 24),
        };

        let run_tag = run_name.map_or(String::new(), |name| format!(",run_name=\"{}\"", name));
        let machine_tag =
            machine_name.map_or(String::new(), |name| format!(",machine_name=\"{}\"", name));
        let well_sample_ref = &well_sample;

        let run_tag_ref = &run_tag;
        let machine_tag_ref = &machine_tag;

        rows.chars()
            .flat_map(|row| {
                (1..=cols).map(move |col| {
                    let well = format!("{}{}", row, col);
                    let sample = well_sample_ref
                        .get(&well)
                        .map_or("", |s| s.as_str())
                        .to_string();
                    format!(
                        "platesetup,row={},col={}{}{} sample=\"{}\" {}",
                        row, col, run_tag_ref, machine_tag_ref, sample, timestamp
                    )
                })
            })
            .collect()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PlateType {
    #[default]
    Well96,
    Well384,
}

impl Serialize for PlateType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(match self {
            PlateType::Well96 => "TYPE_8X12",
            PlateType::Well384 => "TYPE_16X24",
        })
    }
}

impl<'de> Deserialize<'de> for PlateType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "TYPE_8X12" => Ok(PlateType::Well96),
            "TYPE_16X24" => Ok(PlateType::Well384),
            _ => Err(D::Error::custom("Invalid plate type")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::path::{Path, PathBuf};
    use zip::ZipArchive;

    #[test]
    fn test_deserialize_plate_setup() {
        let xml = r#"
    <Plate>
        <Name>Test Plate</Name>
        <BarCode>BC123</BarCode>
        <Description>Test Description</Description>
        <Rows>8</Rows>
        <Columns>12</Columns>
        <FeatureMap>
            <Feature>
                <Id>sample</Id>
                <Name>Sample</Name>
            </Feature>
            <FeatureValue>
                <Index>0</Index>
                <FeatureItem>
                    <Sample>
                        <Name>Test Sample</Name>
                        <Color>-16776961</Color>
                        <CustomProperty>
                            <Property>SP_UUID</Property>
                            <Value>f29793389d7511efbfaeb88584b13f7c</Value>
                        </CustomProperty>
                    </Sample>
                </FeatureItem>
            </FeatureValue>
        </FeatureMap>
        <PlateKind>
            <Name>96-Well Plate (8x12)</Name>
            <Type>TYPE_8X12</Type>
            <RowCount>8</RowCount>
            <ColumnCount>12</ColumnCount>
        </PlateKind>
    </Plate>
    "#;

        let result = PlateSetup::from_xml(xml);
        assert!(result.is_ok());
        let plate = result.unwrap();
        assert_eq!(plate.plate_type, PlateType::Well96);

        // Test sample data
        let sample_wells = plate.get_sample_wells();
        assert!(sample_wells.contains_key("Test Sample"));
        assert_eq!(sample_wells["Test Sample"], vec!["A1"]);
    }

    #[test]
    fn test_plate_type_detection() {
        let xml_96 = r#"
        <Plate>
            <Name>Test Plate</Name>
            <BarCode>BC123</BarCode>
            <Description>Test Description</Description>
            <Rows>8</Rows>
            <Columns>12</Columns>
            <PlateKind>
                <Name>96-Well Plate (8x12)</Name>
                <Type>TYPE_8X12</Type>
                <RowCount>8</RowCount>
                <ColumnCount>12</ColumnCount>
            </PlateKind>
        </Plate>
        "#;

        let plate_96 = PlateSetup::from_xml(xml_96).unwrap();
        assert_eq!(plate_96.plate_type, PlateType::Well96);
        let xml_384 = r#"
        <Plate>
            <Name>Test Plate</Name>
            <BarCode>BC123</BarCode>
            <Description>Test Description</Description>
            <Rows>16</Rows>
            <Columns>24</Columns>
            <PlateKind>
                <Name>384-Well Plate (16x24)</Name>
                <Type>TYPE_16X24</Type>
                <RowCount>16</RowCount>
                <ColumnCount>24</ColumnCount>
            </PlateKind>
        </Plate>
        "#;

        let plate_384 = PlateSetup::from_xml(xml_384).unwrap();
        assert_eq!(plate_384.plate_type, PlateType::Well384);
    }

    #[test]
    fn test_well_names() {
        let xml = r#"
        <Plate>
            <Name />
            <BarCode />
            <Description />
            <Rows>8</Rows>
            <Columns>12</Columns>
            <PlateKind>
                <Name>96-Well Plate (8x12)</Name>
                <Type>TYPE_8X12</Type>
                <RowCount>8</RowCount>
                <ColumnCount>12</ColumnCount>
            </PlateKind>
            <FeatureMap>
                <Feature>
                    <Id>sample</Id>
                    <Name>Sample</Name>
                </Feature>
                <FeatureValue>
                    <Index>0</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Test Sample</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>f29793389d7511efbfaeb88584b13f7c</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
            </FeatureMap>
        </Plate>
        "#;

        let plate = PlateSetup::from_xml(xml).unwrap();
        let well_names = plate.well_names();
        assert_eq!(well_names[0], "A1");
        assert_eq!(well_names[95], "H12");
        assert_eq!(well_names.len(), 96);
    }

    #[test]
    fn test_sample_serialization() {
        let sample = Sample::new("Test Sample".to_string())
            .with_color(Color::rgb(255, 128, 64))
            .with_custom_property(CustomProperty {
                property: vec!["SP_UUID".to_string()],
                value: vec!["f29793389d7511efbfaeb88584b13f7c".to_string()],
            });
        let serialized = to_string(&sample).unwrap();
        let deserialized: Sample = from_str(&serialized).unwrap();
        assert_eq!(sample.color.to_rgba(), deserialized.color.to_rgba());
        assert_eq!(sample.name, deserialized.name);
        assert_eq!(
            sample.custom_properties[0].value[0],
            deserialized.custom_properties[0].value[0]
        );
    }

    #[test]
    fn test_get_sample_wells() {
        let xml = r#"
        <Plate>
            <Name>Test Plate</Name>
            <BarCode>BC123</BarCode>
            <Description>Test Description</Description>
            <Rows>8</Rows>
            <Columns>12</Columns>
            <PlateKind>
                <Name>96-Well Plate (8x12)</Name>
                <Type>TYPE_8X12</Type>
                <RowCount>8</RowCount>
                <ColumnCount>12</ColumnCount>
            </PlateKind>
            <FeatureMap>
                <Feature>
                    <Id>sample</Id>
                    <Name>Sample</Name>
                </Feature>
                <FeatureValue>
                    <Index>0</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample1</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid1</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
                <FeatureValue>
                    <Index>1</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample1</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid1</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
                <FeatureValue>
                    <Index>12</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample2</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid2</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
            </FeatureMap>
        </Plate>
        "#;

        let plate = PlateSetup::from_xml(xml).unwrap();
        let sample_wells = plate.get_sample_wells();

        // Test that we have the expected number of samples
        assert_eq!(sample_wells.len(), 2);

        // Test Sample1 wells
        assert!(sample_wells.contains_key("Sample1"));
        let sample1_wells = &sample_wells["Sample1"];
        assert_eq!(sample1_wells.len(), 2);
        assert!(sample1_wells.contains(&"A1".to_string()));
        assert!(sample1_wells.contains(&"A2".to_string()));

        // Test Sample2 wells
        assert!(sample_wells.contains_key("Sample2"));
        let sample2_wells = &sample_wells["Sample2"];
        assert_eq!(sample2_wells.len(), 1);
        assert!(sample2_wells.contains(&"B1".to_string()));
    }

    #[test]
    fn test_get_sample_wells_384() {
        let xml = r#"
        <Plate>
            <Name>Test Plate 384</Name>
            <BarCode>BC123</BarCode>
            <Description>Test Description</Description>
            <Rows>16</Rows>
            <Columns>24</Columns>
            <PlateKind>
                <Name>384-Well Plate (16x24)</Name>
                <Type>TYPE_16X24</Type>
                <RowCount>16</RowCount>
                <ColumnCount>24</ColumnCount>
            </PlateKind>
            <FeatureMap>
                <Feature>
                    <Id>sample</Id>
                    <Name>Sample</Name>
                </Feature>
                <FeatureValue>
                    <Index>0</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample1</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid1</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
                <FeatureValue>
                    <Index>23</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample1</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid1</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
                <FeatureValue>
                    <Index>383</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample2</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid2</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
            </FeatureMap>
        </Plate>
        "#;

        let plate = PlateSetup::from_xml(xml).unwrap();
        let sample_wells = plate.get_sample_wells();

        // Test that we have the expected number of samples
        assert_eq!(sample_wells.len(), 2);

        // Test Sample1 wells
        assert!(sample_wells.contains_key("Sample1"));
        let sample1_wells = &sample_wells["Sample1"];
        assert_eq!(sample1_wells.len(), 2);
        assert!(sample1_wells.contains(&"A1".to_string()));
        assert!(sample1_wells.contains(&"A24".to_string()));

        assert!(sample_wells.contains_key("Sample2"));
        let sample2_wells = &sample_wells["Sample2"];
        assert_eq!(sample2_wells.len(), 1);
    }

    #[test]
    fn test_get_sample_wells_empty() {
        let xml = r#"
        <Plate>
            <Name>Empty Plate</Name>
            <BarCode>BC123</BarCode>
            <Description>Test Description</Description>
            <Rows>8</Rows>
            <Columns>12</Columns>
            <PlateKind>
                <Name>96-Well Plate (8x12)</Name>
                <Type>TYPE_8X12</Type>
                <RowCount>8</RowCount>
                <ColumnCount>12</ColumnCount>
            </PlateKind>
            <FeatureMap>
                <Feature>
                    <Id>sample</Id>
                    <Name>Sample</Name>
                </Feature>
            </FeatureMap>
        </Plate>
        "#;

        let plate = PlateSetup::from_xml(xml).unwrap();
        let sample_wells = plate.get_sample_wells();

        // Test that we have no samples
        assert_eq!(sample_wells.len(), 0);
    }

    #[test]
    fn test_to_lineprotocol() {
        let xml = r#"
        <Plate>
            <Name>Test Plate</Name>
            <BarCode>BC123</BarCode>
            <Description>Test Description</Description>
            <Rows>8</Rows>
            <Columns>12</Columns>
            <PlateKind>
                <Name>96-Well Plate (8x12)</Name>
                <Type>TYPE_8X12</Type>
                <RowCount>8</RowCount>
                <ColumnCount>12</ColumnCount>
            </PlateKind>
            <FeatureMap>
                <Feature>
                    <Id>sample</Id>
                    <Name>Sample</Name>
                </Feature>
                <FeatureValue>
                    <Index>0</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample1</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid1</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
            </FeatureMap>
        </Plate>
        "#;

        let plate = PlateSetup::from_xml(xml).unwrap();
        let lines = plate.to_lineprotocol(1234567890, None, None);

        // Check total number of lines (96-well plate)
        assert_eq!(lines.len(), 96);

        // Check first line (A1 with Sample1)
        assert_eq!(
            lines[0],
            "platesetup,row=A,col=1 sample=\"Sample1\" 1234567890"
        );

        // Check a random empty well (H12)
        assert_eq!(lines[95], "platesetup,row=H,col=12 sample=\"\" 1234567890");

        // Test with run name
        let lines_with_run = plate.to_lineprotocol(1234567890, Some("Test Run"), None);
        assert_eq!(
            lines_with_run[0],
            "platesetup,row=A,col=1,run_name=\"Test Run\" sample=\"Sample1\" 1234567890"
        );
    }

    #[test]
    fn test_to_lineprotocol_384() {
        let xml = r#"
        <Plate>
            <Name>Test Plate 384</Name>
            <BarCode>BC123</BarCode>
            <Description>Test Description</Description>
            <Rows>16</Rows>
            <Columns>24</Columns>
            <PlateKind>
                <Name>384-Well Plate (16x24)</Name>
                <Type>TYPE_16X24</Type>
                <RowCount>16</RowCount>
                <ColumnCount>24</ColumnCount>
            </PlateKind>
            <FeatureMap>
                <Feature>
                    <Id>sample</Id>
                    <Name>Sample</Name>
                </Feature>
                <FeatureValue>
                    <Index>0</Index>
                    <FeatureItem>
                        <Sample>
                            <Name>Sample1</Name>
                            <Color>-16776961</Color>
                            <CustomProperty>
                                <Property>SP_UUID</Property>
                                <Value>uuid1</Value>
                            </CustomProperty>
                        </Sample>
                    </FeatureItem>
                </FeatureValue>
            </FeatureMap>
        </Plate>
        "#;

        let plate = PlateSetup::from_xml(xml).unwrap();
        let lines = plate.to_lineprotocol(1234567890, None, None);

        // Check total number of lines (384-well plate)
        assert_eq!(lines.len(), 384);

        // Check first line (A1 with Sample1)
        assert_eq!(
            lines[0],
            "platesetup,row=A,col=1 sample=\"Sample1\" 1234567890"
        );

        // Check last well (P24)
        assert_eq!(lines[383], "platesetup,row=P,col=24 sample=\"\" 1234567890");
    }

    #[test]
    fn test_parse_example_eds_files() {
        let example_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("example-eds");

        // Get all .eds files in the directory
        let eds_files: Vec<PathBuf> = std::fs::read_dir(example_dir)
            .expect("Failed to read example-eds directory")
            .filter_map(|entry| {
                let entry = entry.ok()?;
                let path = entry.path();
                if path.extension()? == "eds" {
                    Some(path)
                } else {
                    None
                }
            })
            .collect();

        if eds_files.is_empty() {
            return;
        }

        for eds_path in eds_files {
            let file = File::open(&eds_path)
                .unwrap_or_else(|e| panic!("Failed to open {}: {}", eds_path.display(), e));

            let mut archive = ZipArchive::new(file)
                .unwrap_or_else(|e| panic!("Failed to read {} as zip: {}", eds_path.display(), e));

            // Try to find and read the plate setup XML file
            let mut plate_setup_xml = archive
                .by_name("apldbio/sds/plate_setup.xml")
                .unwrap_or_else(|e| {
                    panic!(
                        "Failed to find plate_setup.xml in {}: {}",
                        eds_path.display(),
                        e
                    )
                });

            let mut xml_content = String::new();
            std::io::Read::read_to_string(&mut plate_setup_xml, &mut xml_content).unwrap_or_else(
                |e| {
                    panic!(
                        "Failed to read plate_setup.xml content from {}: {}",
                        eds_path.display(),
                        e
                    )
                },
            );

            // Try to parse the XML
            let result = PlateSetup::from_xml(&xml_content);
            assert!(
                result.is_ok(),
                "Failed to parse plate_setup.xml from {}: {:?}",
                eds_path.display(),
                result.err()
            );

            // Additional validation of the parsed plate setup
            let plate = result.unwrap();

            // Verify plate type matches dimensions
            match plate.plate_type {
                PlateType::Well96 => {
                    assert_eq!(
                        plate.rows,
                        8,
                        "96-well plate should have 8 rows in {}",
                        eds_path.display()
                    );
                    assert_eq!(
                        plate.columns,
                        12,
                        "96-well plate should have 12 columns in {}",
                        eds_path.display()
                    );
                }
                PlateType::Well384 => {
                    assert_eq!(
                        plate.rows,
                        16,
                        "384-well plate should have 16 rows in {}",
                        eds_path.display()
                    );
                    assert_eq!(
                        plate.columns,
                        24,
                        "384-well plate should have 24 columns in {}",
                        eds_path.display()
                    );
                }
            }

            // Test round-trip serialization
            let serialized = plate.to_xml().unwrap_or_else(|e| {
                panic!(
                    "Failed to serialize plate setup from {}: {}",
                    eds_path.display(),
                    e
                )
            });

            let reparse_result = PlateSetup::from_xml(&serialized);
            assert!(
                reparse_result.is_ok(),
                "Failed to reparse serialized plate setup from {}: {:?}",
                eds_path.display(),
                reparse_result.err()
            );
        }
    }
}