macpepdb 1.1.0

Large peptide database for mass spectrometry
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
use std::sync::Arc;

use anyhow::Result;
use async_stream::try_stream;
use futures::Stream;
use scylla::statement::batch::Batch;
use scylla::value::CqlValue;

use super::client::Client;
use crate::database::generic_client::GenericClient;
use crate::database::table::Table;
use crate::entities::domain::Domain;
use crate::entities::peptide::Peptide;
use crate::entities::protein::Protein;

const TABLE_NAME: &str = "proteins";

const SELECT_COLS: [&str; 11] = [
    "accession",
    "secondary_accessions",
    "entry_name",
    "name",
    "genes",
    "taxonomy_id",
    "proteome_id",
    "is_reviewed",
    "sequence",
    "updated_at",
    "domains",
];

const INSERT_COLS: [&str; 11] = SELECT_COLS;

lazy_static! {
    /// Select statement without WHERE clause.
    ///
    static ref SELECT_STATEMENT: String = format!(
        "SELECT {} FROM :KEYSPACE:.{}",
        SELECT_COLS.join(", "),
        TABLE_NAME
    );

    /// Insert statement.
    /// Takes INSERT_COLS as columns in same order.
    static ref INSERT_STATEMENT: String = format!(
        "INSERT INTO :KEYSPACE:.{} ({}) VALUES ({})",
        TABLE_NAME,
        INSERT_COLS.join(", "),
        vec!["?"; INSERT_COLS.len()].join(", ")
    );

    /// Delete statement.
    /// Takes only the primary key as parameter.
    ///
    static ref DELETE_STATEMENT: String =
        format!("DELETE FROM :KEYSPACE:.{} WHERE accession = ?", TABLE_NAME);
}

/// Tuoel which represents a row of the protein table.
///
type TypedProteinRow = (
    String,
    Vec<String>,
    String,
    String,
    Vec<String>,
    i64,
    String,
    bool,
    String,
    i64,
    Vec<Domain>,
);

impl From<TypedProteinRow> for Protein {
    fn from(row: TypedProteinRow) -> Self {
        Protein::new(
            row.0, row.1, row.2, row.3, row.4, row.5, row.6, row.7, row.8, row.9, row.10,
        )
    }
}

pub struct ProteinTable {}

impl ProteinTable {
    pub async fn insert(client: &Client, protein: &Protein) -> Result<()> {
        let statement = client.get_prepared_statement(&INSERT_STATEMENT).await?;
        client
            .execute_unpaged(
                &statement,
                (
                    protein.get_accession(),
                    protein.get_secondary_accessions(),
                    protein.get_entry_name(),
                    protein.get_name(),
                    protein.get_genes(),
                    protein.get_taxonomy_id(),
                    protein.get_proteome_id(),
                    &protein.get_is_reviewed(),
                    protein.get_sequence(),
                    &protein.get_updated_at(),
                    protein.get_domains(),
                ),
            )
            .await?;
        Ok(())
    }

    pub async fn update(client: &Client, old_prot: &Protein, updated_prot: &Protein) -> Result<()> {
        let mut batch: Batch = Batch::default();

        let delete_statement = DELETE_STATEMENT.replace(":KEYSPACE:", client.get_database());
        let insert_statement = INSERT_STATEMENT.replace(":KEYSPACE:", client.get_database());

        batch.append_statement(delete_statement.as_str());
        batch.append_statement(insert_statement.as_str());

        client
            .batch(
                &batch,
                (
                    (old_prot.get_accession(),),
                    (
                        updated_prot.get_accession(),
                        updated_prot.get_secondary_accessions(),
                        updated_prot.get_entry_name(),
                        updated_prot.get_name(),
                        updated_prot.get_genes(),
                        updated_prot.get_taxonomy_id(),
                        updated_prot.get_proteome_id(),
                        &updated_prot.get_is_reviewed(),
                        updated_prot.get_sequence(),
                        &updated_prot.get_updated_at(),
                        updated_prot.get_domains(),
                    ),
                ),
            )
            .await?;

        Ok(())
    }

    pub async fn delete(client: &Client, protein: &Protein) -> Result<()> {
        let statement = client.get_prepared_statement(&DELETE_STATEMENT).await?;

        client
            .execute_unpaged(&statement, (protein.get_accession(),))
            .await?;

        Ok(())
    }

    /// Searches for a protein by accession or gene name. Gene name needs to be exact,
    /// attribute is wrapped in a like-query.
    ///
    /// # Arguments
    /// * `client` - The database client
    /// * `attribute` - Accession or gene name, will be wrapped in a like-query
    pub async fn search<'a>(
        client: Arc<Client>,
        attribute: String,
    ) -> Result<impl Stream<Item = Result<Protein>> + 'a> {
        Ok(try_stream! {

            for await protein in Self::select(
                client.as_ref(),
                "WHERE accession = ?",
                &[&CqlValue::Text(attribute.clone())],
            ).await? {
                yield protein?;
            }

            for await protein in Self::select(
                client.as_ref(),
                "WHERE genes contains ?",
                &[&CqlValue::Text(attribute.clone())],
            ).await? {
                yield protein?;
            }
        })
    }

    /// get proteins of peptide
    ///
    /// # Arguments
    /// * `client` - The database client
    /// * `peptide` - The peptide
    pub async fn get_proteins_of_peptide<'a>(
        client: &'a Client,
        peptide: &'a Peptide,
    ) -> Result<impl Stream<Item = Result<Protein>> + 'a> {
        Ok(try_stream! {
            // Scylla limiting the tuples per IN query. 100 per default.
            // Chunking by 99 to be on the safe side.
            for proteins in peptide.get_proteins().as_slice().chunks(99) {
                for await protein in Self::select(
                    client,
                    "WHERE accession IN ?",
                    &[&CqlValue::List(
                        proteins.iter().map(|x| CqlValue::Text(x.to_owned())).collect(),
                    )],
                ).await? {
                    yield protein?;
                }
            }
        })
    }

    /// Selects proteins from the database ans streams them back.
    ///
    /// # Arguments
    /// * `client` - The database client
    /// * `additional` - Additional statement to add to the select statement, e.g. WHERE clause
    /// * `params` - Parameters for the additional statement
    ///
    pub async fn select(
        client: &Client,
        additional: &str,
        params: &[&CqlValue],
    ) -> Result<impl Stream<Item = Result<Protein>>> {
        let statement = format!("{} {};", SELECT_STATEMENT.as_str(), additional);
        let prepared_statement = client.get_prepared_statement(&statement).await?;

        let stream = client
            .execute_iter(prepared_statement, params)
            .await?
            .rows_stream::<TypedProteinRow>()?;

        Ok(try_stream! {
            for await protein_result in stream  {
                yield protein_result?.into();
            }
        })
    }
}

impl Table for ProteinTable {
    fn table_name() -> &'static str {
        TABLE_NAME
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::Path;

    use fallible_iterator::FallibleIterator;
    use futures::TryStreamExt;
    use serial_test::serial;
    use tokio::pin;

    use super::*;
    use crate::chemistry::amino_acid::calc_sequence_mass_int;
    use crate::database::generic_client::GenericClient;
    use crate::database::scylla::client::Client;
    use crate::database::scylla::prepare_database_for_tests;
    use crate::database::scylla::tests::get_test_database_url;
    use crate::entities::domain::Domain;
    use crate::io::uniprot_text::reader::Reader;

    const EXPECTED_PROTEINS: i64 = 3;

    lazy_static! {
        static ref TRYPSIN: Protein = Protein::new(
            "P07477".to_string(),
            [
                "A1A509",
                "A6NJ71",
                "B2R5I5",
                "Q5NV57",
                "Q7M4N3",
                "Q7M4N4",
                "Q92955",
                "Q9HAN4",
                "Q9HAN5",
                "Q9HAN6",
                "Q9HAN7"
            ].iter().map(|s| s.to_string()).collect(),
            "TRY1_HUMAN".to_string(),
            "Serine protease 1".to_string(),
            [
                "PRSS1",
                "TRP1",
                "TRY1",
                "TRYP1"
            ].iter().map(|s| s.to_string()).collect(),
            9606,
            "UP000005640".to_string(),
            true,
            "MNPLLILTFVAAALAAPFDDDDKIVGGYNCEENSVPYQVSLNSGYHFCGGSLINEQWVVSAGHCYKSRIQVRLGEHNIEVLEGNEQFINAAKIIRHPQYDRKTLNNDIMLIKLSSRAVINARVSTISLPTAPPATGTKCLISGWGNTASSGADYPDELQCLDAPVLSQAKCEASYPGKITSNMFCVGFLEGGKDSCQGDSGGPVVCNGQLQGVVSWGDGCAQKNKPGVYTKVYNYVKWIKNTIAANS".to_string(),
            1677024000,
            vec![]

        );

        static ref UPDATED_TRYPSIN: Protein = Protein::new(
            "UPDTRY".to_string(),
            [
                "P07477",
                "A1A509",
                "A6NJ71",
                "B2R5I5",
                "Q5NV57",
                "Q7M4N3",
                "Q7M4N4",
                "Q92955",
                "Q9HAN4",
                "Q9HAN5",
                "Q9HAN6",
                "Q9HAN7"
            ].iter().map(|s| s.to_string()).collect(),
            "UPD_TRY1_HUMAN".to_string(),
            "Some strange stuff which destroys other stuff".to_string(),
            [
                "SOME NEW UNDISCOVERED GENE",
                "PRSS1",
                "TRP1",
                "TRY1",
                "TRYP1"
            ].iter().map(|s| s.to_string()).collect(),
            0,
            "UP999999999".to_string(),
            true,
            "RUSTISAWESOME".to_string(),
            0,
            vec![Domain::new( 23, 243,  "Peptidase S1".to_string(),  "ECO:0000255|PROSITE-ProRule:PRU00274".to_string(), None, None, None, None)]
        );
    }

    #[tokio::test]
    #[serial]
    /// Prepares database for testing and inserts proteins from test file.
    ///
    async fn test_insert() {
        let client = Client::new(&get_test_database_url()).await.unwrap();
        prepare_database_for_tests(&client).await;

        let mut reader = Reader::new(Path::new("test_files/uniprot.txt"), 1024).unwrap();
        while let Some(protein) = reader.next().unwrap() {
            ProteinTable::insert(&client, &protein).await.unwrap();
        }
        let count_statement = format!(
            "SELECT count(*) FROM {}.{}",
            client.get_database(),
            TABLE_NAME
        );
        let count = client
            .query_unpaged(count_statement, &[])
            .await
            .unwrap()
            .into_rows_result()
            .unwrap()
            .first_row::<(i64,)>()
            .unwrap()
            .0;

        assert_eq!(count, EXPECTED_PROTEINS);
    }

    #[tokio::test]
    #[serial]
    /// Tests selects from database.
    ///
    async fn test_select() {
        let client = Client::new(&get_test_database_url()).await.unwrap();
        prepare_database_for_tests(&client).await;

        let mut reader = Reader::new(Path::new("test_files/uniprot.txt"), 1024).unwrap();
        while let Some(protein) = reader.next().unwrap() {
            ProteinTable::insert(&client, &protein).await.unwrap();
        }

        let protein = ProteinTable::select(
            &client,
            "WHERE accession = ?",
            &[&CqlValue::Text(TRYPSIN.get_accession().to_owned())],
        )
        .await
        .unwrap()
        .try_collect::<Vec<Protein>>()
        .await
        .unwrap()
        .pop()
        .unwrap();

        assert_eq!(protein, TRYPSIN.to_owned());
    }

    #[tokio::test]
    #[serial]
    async fn test_update() {
        let client = Client::new(&get_test_database_url()).await.unwrap();

        let mut reader = Reader::new(Path::new("test_files/uniprot.txt"), 1024).unwrap();
        while let Some(protein) = reader.next().unwrap() {
            ProteinTable::insert(&client, &protein).await.unwrap();
        }

        ProteinTable::update(&client, &TRYPSIN, &UPDATED_TRYPSIN)
            .await
            .unwrap();

        let actual = ProteinTable::select(
            &client,
            "WHERE accession = ? ",
            &[&CqlValue::Text(UPDATED_TRYPSIN.get_accession().to_owned())],
        )
        .await
        .unwrap()
        .try_collect::<Vec<Protein>>()
        .await
        .unwrap()
        .pop()
        .unwrap();

        assert_eq!(actual, UPDATED_TRYPSIN.to_owned());
    }

    #[tokio::test]
    #[serial]
    async fn test_delete() {
        let client = Client::new(&get_test_database_url()).await.unwrap();

        let mut reader = Reader::new(Path::new("test_files/uniprot.txt"), 1024).unwrap();
        while let Some(protein) = reader.next().unwrap() {
            ProteinTable::insert(&client, &protein).await.unwrap();
        }

        ProteinTable::delete(&client, &TRYPSIN).await.unwrap();

        let stream = ProteinTable::select(
            &client,
            "WHERE accession = ? ",
            &[&CqlValue::Text(TRYPSIN.get_accession().to_owned())],
        )
        .await
        .unwrap();

        pin!(stream);

        let row_opt = stream.try_next().await.unwrap();

        assert!(row_opt.is_none());
    }

    #[tokio::test]
    #[serial]
    async fn test_get_proteins_of_peptides() {
        let client = Client::new(&get_test_database_url()).await.unwrap();
        prepare_database_for_tests(&client).await;

        let mut reader = Reader::new(Path::new("test_files/mouse.txt"), 1024).unwrap();
        while let Some(protein) = reader.next().unwrap() {
            ProteinTable::insert(&client, &protein).await.unwrap();
        }

        // LDDTERKIK from test_files/mouse.txt
        let peptide = Peptide::new(
            0,
            calc_sequence_mass_int("LDDTERKIK").unwrap(),
            "LDDTERKIK".to_string(),
            2,
            vec![
                "A0A087WRS6".to_string(),
                "G5E886".to_string(),
                "G5E887".to_string(),
            ],
            true,
            true,
            vec![10090],
            vec![],
            vec!["UP000000589".to_string()],
            vec![],
        )
        .unwrap();

        // Fetch all proteins of the peptide
        let protein_stream = ProteinTable::get_proteins_of_peptide(&client, &peptide)
            .await
            .unwrap();
        pin!(protein_stream);
        let proteins = protein_stream.try_collect::<Vec<Protein>>().await.unwrap();

        // Check if all proteins of the peptide are in the fetched proteins
        assert_eq!(proteins.len(), peptide.get_proteins().len());

        let proteins_map: HashMap<String, Protein> = proteins
            .iter()
            .map(|protein| (protein.get_accession().to_owned(), protein.to_owned()))
            .collect();

        for expected_protein in peptide.get_proteins().iter() {
            assert!(proteins_map.contains_key(expected_protein))
        }
    }

    #[tokio::test]
    #[serial]
    async fn test_tokenawareness() {
        let client = Client::new(&get_test_database_url()).await.unwrap();
        prepare_database_for_tests(&client).await;
        for statement in &[INSERT_STATEMENT.as_str(), DELETE_STATEMENT.as_str()] {
            let prepared_statement = client.get_prepared_statement(statement).await.unwrap();
            assert!(
                prepared_statement.is_token_aware(),
                "Statement `{}` is not token aware",
                statement
            );
        }
    }
}