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
#[cfg(feature = "serialization")]
use serde::Serialize;

#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(PartialEq, PartialOrd, Clone, Default)]
/// Protein-level TMT quantification data, as well as additional

/// metadata about the protein that is output in the Census file

pub struct Protein {
    /// Uniprot accession identifier

    pub accession: String,
    /// Long-form description

    pub description: String,
    /// Number of spectral counts

    pub spectral_count: u16,
    /// Number of unique sequence counts

    pub sequence_count: u16,
    /// Sequence coverage

    pub sequence_coverage: f32,
    /// Molecular weight

    pub molecular_weight: u32,
    /// Raw signal intensity channels

    pub peptides: Vec<Peptide>,

    pub channels: u8,
}

impl Protein {
    /// Return the summed intensities for all peptides

    pub fn total(&self) -> Vec<u32> {
        let mut v = Vec::with_capacity(self.channels as usize);
        for c in 0..self.channels {
            let sum = self.peptides.iter().map(|pep| pep.values[c as usize]).sum();
            v.push(sum);
        }
        v
    }

    /// Return a vector of normalized ratios, where the signal intensity

    /// for each channel is divided by the sum of all channels

    pub fn ratios(&self) -> Vec<f64> {
        let values = self.total();
        let total = values.iter().sum::<u32>() as f64;
        values.iter().map(|v| *v as f64 / total).collect()
    }
}

#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(PartialEq, PartialOrd, Clone, Debug, Default)]
/// Peptide-level TMT quantification data

pub struct Peptide {
    /// Peptide sequence

    pub sequence: String,
    /// Raw isobaric ion intensity values

    pub values: Vec<u32>,
    /// Is this a unique peptide?

    pub unique: bool,

    pub purity: f32,

    pub scan: usize,
}

impl Peptide {
    /// Return a boolean indicating whether the peptide has 2 tryptic sites

    pub fn tryptic(&self) -> bool {
        let kdot = self.sequence.matches("K.").count();
        let rdot = self.sequence.matches("R.").count();
        kdot + rdot == 2
    }

    /// Return a vector of normalized ratios, where the signal intensity

    /// for each channel is divided by the sum of all channels

    pub fn ratios(&self) -> Vec<f64> {
        let total: f64 = self.values.iter().sum::<u32>() as f64;
        self.values.iter().map(|v| *v as f64 / total).collect()
    }

    /// Swap channels A and B, which are 0 indexed into the peptide values

    /// vector.

    ///

    /// # May panic

    ///

    /// May panic if A or B exceed the length of the vector

    pub fn swap_channels(&mut self, a: usize, b: usize) {
        self.values.swap(a, b)
    }
}