#flow #fcs #cytometry

fcs_rs

Basic tool for manipulating and analyzing Flow Cytometry Standard (FCS) files

1 unstable release

0.1.0 May 19, 2024

#148 in Database implementations

MIT license

250KB
694 lines

Rust CI

Module Overview: fcs_rs

The fcs_rs module provides a basic 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 using arcshinh transformation, all while handling various potential errors gracefully.

Supports FCS versions FCS3.0 and FCS3.1.

Key Features

  • 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 data frames.
  • Error Handling: Comprehensive error handling to deal with various issues that may arise during file operations.

Modules

The fcs_rs module is divided into several sub-modules, each focusing on specific aspects of FCS file handling:

  • 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.

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.

Usage Examples

Opening an FCS File

To open an FCS file, use the FcsFile::open method:

use fcs_rs::FcsFile;

let fcs_file = FcsFile::open("path/to/file.fcs")?;

Reading an FCS File

To read an FCS file and extract metadata and data segments:

use fcs_rs::{FcsFile, FcsError};

let fcs_file = FcsFile::open("path/to/file.fcs")?;
// Read the FCS file and return metadata and parameter data in a `FlowSample` struct
let fcs_data = fcs_file.read()?;
println!("{:?}", fcs_data.data); // Prints FCS data in a dataframe
shape: (5_555, 10)
┌───────────┬──────────┬──────────┬───────────┬───┬──────────┬──────────┬──────────┐
│ SSC-SSC-H ┆ FSC-H    ┆ FSC-A    ┆ SSC-SSC-W ┆ … ┆ APC-A    ┆ Time     ┆ FITC-A   │
│ ------------       ┆   ┆ ---------      │
│ f64f64f64f64       ┆   ┆ f64f64f64      │
╞═══════════╪══════════╪══════════╪═══════════╪═══╪══════════╪══════════╪══════════╡
│ 545719.0423142.0390864.094.0      ┆ … ┆ 200.05.0-50.0    │
│ 126.0212991.0317041.015310.0   ┆ … ┆ 626392.0388875.0-16.0    │
│ 121.094.099.0688422.0  ┆ … ┆ 496565.0314649.0251.0    │
│ 11877.093.05129.0465353.0  ┆ … ┆ 125.0-58.0617030.0 │
│ …         ┆ …        ┆ …        ┆ …         ┆ … ┆ …        ┆ …        ┆ …        │
│ 115.022.0147.0599985.0  ┆ … ┆ 208766.0321130.06696.0   │
│ 4672.050664.0135.0543467.0  ┆ … ┆ 36.087.0397669.0 │
│ 437883.0750.03382.0136.0     ┆ … ┆ 10998.089.0204834.0 │
│ 357261.0628195.0427706.0124.0     ┆ … ┆ 11286.01770.0123.0    │
└───────────┴──────────┴──────────┴───────────┴───┴──────────┴──────────┴──────────┘

View information relating to the sample.

use fcs_rs::{FcsFile, FcsError};

let fcs_file = FcsFile::open("path/to/file.fcs")?;
let fcs_data = fcs_file.read()?;
println!("{}", fcs_data); // Prints sample information
FlowSample:
    Machine: 1234567 Attune NxT Acoustic Focusing Cytometer (Lasers: BRVY)
    Begin Time:
    End Time:
    Date:
    File: file.fcs
    Volume run: 250000
    Labels:
        Time (Time)
        FSC-A (FSC-A)
        SSC-A (SSC-A)
        BL1-A (CMV-GFP-FITC-A)
        BL3-A (CD11c-PerCP-Cy5.5-A)
        YL4-A (--PE-Cy7-A)
        YL2-A (TNF alpha ICS-PE-Texas Red-A)
        YL1-A (CD123-R-PE-A)
        RL1-A (IFN alpha ICS-APC-A)
        RL2-A (CD45RA-Alexa Fluor 700-A)
        RL3-A (CD45-APC-Cy7-A)
        VL4-A (--Qdot 700-A)
        VL3-A (--Qdot 605-A)
        VL2-A (Live-Dead-Fixable Aqua-A)
        VL1-A (CD69-Pacific Blue-A)
        FSC-H (FSC-H)
        SSC-H (SSC-H)
        FSC-W (FSC-W)
        SSC-W (SSC-W)

Perform Arcsinh Transformation on FCS data

To read an FCS file and extract metadata and data segments:

use fcs_rs::{FcsFile, FcsError};

let fcs_file = FcsFile::open("path/to/file.fcs")?;
let fcs_data = fcs_file.read()?;

// Get the column names
let column_names = flow_sample.get_dataframe_columns();
    
// Perform arcsinh transformation of data with scaling factor of 5.0
flow_sample.arcsinh_transform(5.0, &column_names)?;
println!("{:?}", fcs_data.data); // Prints transformed FCS data
shape: (5_555, 10)
┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┐
│ FSC-A     ┆ FSC-W     ┆ SSC-SSC-A ┆ Time      ┆ … ┆ FSC-H     ┆ APC-A     │
│ ------------       ┆   ┆ ------       │
│ f64f64f64f64       ┆   ┆ f64f64       │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╡
│ 22.5333546.10911421.7056290.346574  ┆ … ┆ 22.692057.378071  │
│ 22.1146975.8477666.45447522.523151 ┆ … ┆ 21.31913523.476587 │
│ 5.97263815.80852214.13020222.09955  ┆ … ┆ 5.86912623.012064 │
│ 13.86645723.92726815.367728 ┆ NaN       ┆ … ┆ 5.8477666.438551  │
│ …         ┆ …         ┆ …         ┆ …         ┆ … ┆ …         ┆ …         │
│ 6.76256711.1676.76256722.140327 ┆ … ┆ 2.98839121.279063 │
│ 6.59235922.74084713.8242925.714589  ┆ … ┆ 18.4470663.957715  │
│ 13.0335721.45610723.6434955.759973  ┆ … ┆ 10.02129315.392062 │
│ 22.7135075.84776623.07145911.738598 ┆ … ┆ 23.48233615.443761 │
└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┘

Creating a DataFrame

To create a DataFrame from column titles and corresponding data vectors:

use fcs_rs::data::create_dataframe;
use polars::prelude::*;

let column_titles = vec!["APC-A".to_string(), "FSC-W".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);

Conclusion

The fcs_rs module provides a robust framework for handling FCS files. Users can easily and efficiently work with flow cytometry data.

Dependencies

~20–30MB
~498K SLoC