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
//! FCS File Manipulation Operations
//!
//! This module provides a comprehensive set of tools for manipulating and analyzing Flow Cytometry Standard (FCS) files.
//!
//! It includes methods for reading FCS files, extracting metadata, and processing data segments.
//!
//! # Overview
//!
//! The Flow Cytometry Standard (FCS) is a file format used to store data generated by flow cytometry experiments. This library allows users to easily read and manipulate FCS files in Rust. Key features include:
//!
//! - **File Handling**: Open and read FCS files in a structured and efficient manner.
//! - **Metadata Extraction**: Extract and validate metadata from FCS files, ensuring all required information is available.
//! - **Data Processing**: Parse data segments from FCS files and convert them into usable formats such as dataframes.
//! - **Data Transformation**: Can transform raw data using arcsinh.
//! - **Error Handling**: Comprehensive error handling to deal with various issues that may arise during file operations.
//!
//! # Getting Started
//!
//! To get started with this library, you need to add it as a dependency in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! fcs_rs = "0.1.0"
//! ```
//!
//! Then, include the library in your project:
//!
//! ```rust
//! use fcs_rs::{
//! FcsFile,
//! FlowSample,
//! FcsError
//! };
//! ```
//!
//! # Examples
//!
//! ## Opening an FCS File
//!
//! ```rust
//! use fcs_rs::FcsFile;
//!
//! let fcs_file = FcsFile::open("path/to/file.fcs")?;
//! ```
//!
//! ## Reading an FCS File
//!
//! ```rust
//! use fcs_rs::{FcsFile, FcsError};
//!
//! let fcs_file = FcsFile::open("path/to/file.fcs")?;
//! let flow_sample = fcs_file.read()?;
//! println!("{:?}", flow_sample.data);
//! println!("{:?}", flow_sample.parameters);
//! ```
//!
//! ## Extracting Column Names
//!
//! ```rust
//! use fcs_rs::FcsFile;
//!
//! let fcs_file = FcsFile::open("path/to/file.fcs")?;
//! let flow_sample = fcs_file.read()?;
//! let column_names = flow_sample.get_dataframe_columns();
//! println!("{:?}", column_names);
//! ```
//!
//! ## Applying Arcsinh Transformation
//!
//! ```rust
//! use fcs_rs::FcsFile;
//!
//! let fcs_file = FcsFile::open("path/to/file.fcs")?;
//! let mut flow_sample = fcs_file.read()?;
//! let column_names = flow_sample.get_dataframe_columns();
//! flow_sample.arcsinh_transform(5.0, &column_names)?;
//! println!("{:?}", flow_sample.data);
//! ```
//!
//! ## Creating a DataFrame
//!
//! ```rust
//! use fcs_rs::data::create_dataframe;
//! use polars::prelude::*;
//!
//! let column_titles = vec!["FSC-W".to_string(), "FITC-A".to_string()];
//! let data = vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]];
//! let df = create_dataframe(&column_titles, &data)?;
//! println!("{:?}", df);
//! ```
//!
//! # Modules
//!
//! - **data**: Contains structures and functions for handling the data segments of FCS files, including parsing and transformation operations.
//! - **header**: Includes methods for reading and validating the header segments of FCS files.
//! - **text**: Provides functions for reading and validating the text segments of FCS files.
//!
//! # Constants
//!
//! - **VALID_FCS_VERSIONS**: A list of supported FCS versions.
//! - **REQUIRED_KEYWORDS**: A list of required non-parameter indexed keywords for the FCS text segment.
//!
//! # Error Handling
//!
//! The `FcsError` enum defines various errors that can occur while processing FCS files, including I/O errors,
//! invalid headers, unsupported versions, metadata issues, missing required keywords, and invalid data segments.
use HashMap;
use ;
use ;
use File;
use str;
use ReadBytesExt;
use Error;
pub use crateread_header;
pub use crate;
pub use crate;
pub const VALID_FCS_VERSIONS: = ;
/// Required non-parameter indexed keywords for FCS text segment
pub const REQUIRED_KEYWORDS: = ;
/// Represents errors that can occur while processing FCS (Flow Cytometry Standard) files.
///
/// This enum covers various error types that might be encountered, including I/O errors,
/// invalid headers, unsupported FCS versions, metadata issues, missing required keywords,
/// and invalid data segments.
///
/// # Variants
///
/// - `IoError`: Represents an I/O error that occurs during file operations.
/// - `InvalidHeader`: Indicates that the FCS header is invalid, possibly due to file corruption
/// or the file not being a valid FCS file.
/// - `InvalidVersion`: Indicates that the FCS version is not supported. The supported versions are
/// FCS3.0 and FCS3.1.
/// - `InvalidMetadata`: Indicates that the FCS metadata is invalid.
/// - `InvalidText`: Indicates that the FCS file is missing a required keyword in its TEXT section.
/// - `InvalidData`: Indicates that the FCS data segment is invalid, with an associated error message.
///
/// # Examples
///
/// ```
/// use thiserror::Error;
/// use std::io;
///
/// #[derive(Debug, Error)]
/// pub enum FcsError {
/// #[error("IO Error: {0}")]
/// IoError(#[from] io::Error),
/// #[error("Invalid FCS Header. File may be corrupted or not a FCS file.")]
/// InvalidHeader,
/// #[error("FCS version `{0}` not supported. Must be either FCS3.0 or FCS3.1")]
/// InvalidVersion(String),
/// #[error("Invalid FCS Metadata")]
/// InvalidMetadata,
/// #[error("FCS file is corrupted. It is missing required keyword {0} in its TEXT section")]
/// InvalidText(String),
/// #[error("Invalid FCS Data: {0}")]
/// InvalidData(String),
/// }
/// ```
/// An object providing access to an FCS file.
///
/// This struct wraps a file handle and provides methods to open the file and read
/// metadata and parameter data from it.