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
use super::*;

use std::path::Path;
use std::fs::File;
use std::io::{Write, BufWriter};
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub struct Cgats {
    pub vendor: Option<Vendor>,
    pub meta: DataVec,
    pub fields: DataFormat,
    pub data_map: DataMap,
}

impl Cgats {
    pub fn new() -> Cgats {
    //! Create a new empty CGATS object
        Cgats::default()
    }

    pub fn sample_count(&self) -> usize {
    //! Returns the number of samples in the data
        self.data_map.len()
    }

    pub fn new_with_vendor(vendor: Vendor) -> Cgats {
    //! Create a new empty CGATS object with a Vendor
        Cgats {
            vendor: Some(vendor),
            ..Cgats::default()
        }
    }

    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Cgats> {
    //! Create a CGATS object from an existing CGATS file
        let raw = DataVec::from_file(path)?;

        let vendor = raw.get_vendor();
        let meta = raw.extract_meta_data();
        let fields = raw.extract_data_format()?;
        let data_map = raw.to_data_map()?;

        Ok(Cgats {
            vendor,
            meta,
            fields,
            data_map,
        })
    }

    pub fn write_to_file<P: AsRef<Path>>(&self, file: P) -> Result<()> {
    //! Write a CGATS object to a properly formatted CGATS file
        let mut buf = BufWriter::new(File::create(file)?);
        write!(buf, "{}", self.format())?;
        Ok(())
    }

    pub fn format(&self) -> String {
    //! Format the entire CGATS object to a string
        format!("{}{}{}",
            &self.format_meta(),
            &self.format_fields(),
            &self.format_data_map(),
        )
    }

    fn format_meta(&self) -> String {
    //! Format the CGATS metadata section to a string
        format!("{}", self.meta)
    }

    fn format_fields(&self) -> String {
    //! Format the DATA_FORMAT section to a string
        let mut s = String::new();

        // ColorBurst does not include DATA_FORMAT information in LineFiles
        if self.vendor == Some(Vendor::ColorBurst) {
            return s;
        }

        s.push_str("BEGIN_DATA_FORMAT\n");

        for field in &self.fields {
            s.push_str(&format!("{}\t", field));
        }

        // Pop off the last tab ('\t')
        s.pop();
        s.push_str("\nEND_DATA_FORMAT\n");

        format!("{}", s)
    }

    fn format_data_map(&self) -> String {
    //! Format the DATA section to a string
        format!("{}{}{}",
            "BEGIN_DATA\n",

            &self.data_map.iter()
                .map(|(_index, sample)| format!("{}\n", sample))
                .collect::<String>(),

            "END_DATA\n"
        )
    }

}

impl Default for Cgats {
    fn default() -> Cgats {
        Cgats {
            vendor: None,
            fields: DataFormat::new(),
            data_map: DataMap::new(),
            meta: DataVec::new(),
        }
    }
}

impl fmt::Display for Cgats {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let vendor = match &self.vendor {
            Some(v) => v.to_string(),
            None => "None".to_string(),
        };

        let format = format!("{}({}):{:?}", vendor, &self.sample_count(), self.fields);

        write!(f, "{}", format)
    }
}

#[test]
fn cgats_display() -> Result<()> {
    let cgats = Cgats::from_file("test_files/cgats1.tsv")?;
    let colorburst = Cgats::from_file("test_files/colorburst0.txt")?;
    let curve = Cgats::from_file("test_files/curve0.txt")?;

    assert_eq!(cgats.to_string(),       "Cgats(11):[SAMPLE_ID, SAMPLE_NAME, CMYK_C, CMYK_M, CMYK_Y, CMYK_K]");
    assert_eq!(colorburst.to_string(),  "ColorBurst(84):[D_RED, D_GREEN, D_BLUE, D_VIS, LAB_L, LAB_A, LAB_B]");
    assert_eq!(curve.to_string(),       "Curve(21):[SAMPLE_ID, SAMPLE_NAME, CMYK_C, CMYK_M, CMYK_Y, CMYK_K]");

    Ok(())
}

#[test]
fn write_meta() -> Result<()> {
    let cgats = Cgats::from_file("test_files/cgats1.tsv")?;
    assert_eq!(
        cgats.format_meta(),
        "CGATS.17\n"
    );

    let colorburst = Cgats::from_file("test_files/colorburst0.txt")?;
    assert_eq!(
        colorburst.format_meta(),
        "ColorBurst\n"
    );

    Ok(())
}

#[test]
fn write_fields() -> Result<()> {
    let cgats = Cgats::from_file("test_files/cgats1.tsv")?;
    assert_eq!(
        cgats.format_fields(),
        "BEGIN_DATA_FORMAT\nSAMPLE_ID\tSAMPLE_NAME\tCMYK_C\tCMYK_M\tCMYK_Y\tCMYK_K\nEND_DATA_FORMAT\n"
    );

    let curve = Cgats::from_file("test_files/curve0.txt")?;
    assert_eq!(
        curve.format_fields(),
        "BEGIN_DATA_FORMAT\nSAMPLE_ID\tSAMPLE_NAME\tCMYK_C\tCMYK_M\tCMYK_Y\tCMYK_K\nEND_DATA_FORMAT\n"  
    );

    let colorburst = Cgats::from_file("test_files/colorburst0.txt")?;
    assert_eq!(colorburst.format_fields(), "");

    Ok(())
}

#[test]
fn write_data_map() -> Result<()> {
    let cgats = Cgats::from_file("test_files/cgats1.tsv")?;
    println!("{}", cgats.format_data_map());
    Ok(())
}